2017-04-17 20:57:07 +02:00
|
|
|
//
|
|
|
|
// Created by Grishka on 29.03.17.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdexcept>
|
2017-06-06 03:44:16 +02:00
|
|
|
#include <algorithm>
|
2017-04-17 20:57:07 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#if defined(_WIN32)
|
2017-07-03 03:42:49 +02:00
|
|
|
#include <winsock2.h>
|
2019-04-15 01:43:10 +02:00
|
|
|
#include "os/windows/NetworkSocketWinsock.h"
|
2017-04-17 20:57:07 +02:00
|
|
|
#else
|
|
|
|
#include "os/posix/NetworkSocketPosix.h"
|
|
|
|
#endif
|
|
|
|
#include "logging.h"
|
|
|
|
#include "VoIPServerConfig.h"
|
2017-06-06 03:44:16 +02:00
|
|
|
#include "VoIPController.h"
|
2018-06-04 21:37:43 +02:00
|
|
|
#include "Buffers.h"
|
2019-04-15 01:43:10 +02:00
|
|
|
#include "NetworkSocket.h"
|
2017-04-17 20:57:07 +02:00
|
|
|
|
|
|
|
#define MIN_UDP_PORT 16384
|
|
|
|
#define MAX_UDP_PORT 32768
|
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
using namespace tgvoip;
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-07-03 03:42:49 +02:00
|
|
|
NetworkSocket::NetworkSocket(NetworkProtocol protocol) : protocol(protocol){
|
2017-06-06 03:44:16 +02:00
|
|
|
ipv6Timeout=ServerConfig::GetSharedInstance()->GetDouble("nat64_fallback_timeout", 3);
|
|
|
|
failed=false;
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
NetworkSocket::~NetworkSocket(){
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
std::string NetworkSocket::GetLocalInterfaceInfo(NetworkAddress *inet4addr, NetworkAddress *inet6addr){
|
2017-06-06 03:44:16 +02:00
|
|
|
std::string r="not implemented";
|
|
|
|
return r;
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
uint16_t NetworkSocket::GenerateLocalPort(){
|
2018-11-09 16:44:01 +01:00
|
|
|
uint16_t rnd;
|
|
|
|
VoIPController::crypto.rand_bytes(reinterpret_cast<uint8_t*>(&rnd), 2);
|
|
|
|
return (uint16_t) ((rnd%(MAX_UDP_PORT-MIN_UDP_PORT))+MIN_UDP_PORT);
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
void NetworkSocket::SetMaxPriority(){
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
bool NetworkSocket::IsFailed(){
|
|
|
|
return failed;
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-07-03 03:42:49 +02:00
|
|
|
NetworkSocket *NetworkSocket::Create(NetworkProtocol protocol){
|
|
|
|
#ifndef _WIN32
|
|
|
|
return new NetworkSocketPosix(protocol);
|
|
|
|
#else
|
|
|
|
return new NetworkSocketWinsock(protocol);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkAddress NetworkSocket::ResolveDomainName(std::string name){
|
2017-04-17 20:57:07 +02:00
|
|
|
#ifndef _WIN32
|
2017-07-03 03:42:49 +02:00
|
|
|
return NetworkSocketPosix::ResolveDomainName(name);
|
2017-04-17 20:57:07 +02:00
|
|
|
#else
|
2017-07-03 03:42:49 +02:00
|
|
|
return NetworkSocketWinsock::ResolveDomainName(name);
|
2017-04-17 20:57:07 +02:00
|
|
|
#endif
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
void NetworkSocket::GenerateTCPO2States(unsigned char* buffer, TCPO2State* recvState, TCPO2State* sendState){
|
|
|
|
memset(recvState, 0, sizeof(TCPO2State));
|
|
|
|
memset(sendState, 0, sizeof(TCPO2State));
|
|
|
|
unsigned char nonce[64];
|
|
|
|
uint32_t *first = reinterpret_cast<uint32_t*>(nonce), *second = first + 1;
|
|
|
|
uint32_t first1 = 0x44414548U, first2 = 0x54534f50U, first3 = 0x20544547U, first4 = 0x20544547U, first5 = 0xeeeeeeeeU;
|
|
|
|
uint32_t second1 = 0;
|
|
|
|
do {
|
|
|
|
VoIPController::crypto.rand_bytes(nonce, sizeof(nonce));
|
|
|
|
} while (*first == first1 || *first == first2 || *first == first3 || *first == first4 || *first == first5 || *second == second1 || *reinterpret_cast<unsigned char*>(nonce) == 0xef);
|
|
|
|
|
|
|
|
// prepare encryption key/iv
|
|
|
|
memcpy(sendState->key, nonce + 8, 32);
|
|
|
|
memcpy(sendState->iv, nonce + 8 + 32, 16);
|
|
|
|
|
|
|
|
// prepare decryption key/iv
|
|
|
|
char reversed[48];
|
|
|
|
memcpy(reversed, nonce + 8, sizeof(reversed));
|
|
|
|
std::reverse(reversed, reversed + sizeof(reversed));
|
|
|
|
memcpy(recvState->key, reversed, 32);
|
|
|
|
memcpy(recvState->iv, reversed + 32, 16);
|
|
|
|
|
|
|
|
// write protocol identifier
|
|
|
|
*reinterpret_cast<uint32_t*>(nonce + 56) = 0xefefefefU;
|
|
|
|
memcpy(buffer, nonce, 56);
|
|
|
|
EncryptForTCPO2(nonce, sizeof(nonce), sendState);
|
|
|
|
memcpy(buffer+56, nonce+56, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkSocket::EncryptForTCPO2(unsigned char *buffer, size_t len, TCPO2State *state){
|
|
|
|
VoIPController::crypto.aes_ctr_encrypt(buffer, len, state->key, state->iv, state->ecount, &state->num);
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-07-03 03:42:49 +02:00
|
|
|
size_t NetworkSocket::Receive(unsigned char *buffer, size_t len){
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkPacket pkt=Receive(len);
|
|
|
|
if(pkt.IsEmpty())
|
|
|
|
return 0;
|
|
|
|
size_t actualLen=std::min(len, pkt.data.Length());
|
|
|
|
memcpy(buffer, *pkt.data, actualLen);
|
|
|
|
return actualLen;
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 16:44:01 +01:00
|
|
|
bool NetworkAddress::operator==(const NetworkAddress &other) const{
|
2019-04-15 01:43:10 +02:00
|
|
|
if(isIPv6!=other.isIPv6)
|
|
|
|
return false;
|
|
|
|
if(!isIPv6){
|
|
|
|
return addr.ipv4==other.addr.ipv4;
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2019-04-15 01:43:10 +02:00
|
|
|
return memcmp(addr.ipv6, other.addr.ipv6, 16)==0;
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2018-11-09 16:44:01 +01:00
|
|
|
bool NetworkAddress::operator!=(const NetworkAddress &other) const{
|
2017-06-06 03:44:16 +02:00
|
|
|
return !(*this == other);
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
std::string NetworkAddress::ToString() const{
|
|
|
|
if(isIPv6){
|
2017-04-17 20:57:07 +02:00
|
|
|
#ifndef _WIN32
|
2019-04-15 01:43:10 +02:00
|
|
|
return NetworkSocketPosix::V6AddressToString(addr.ipv6);
|
2017-04-17 20:57:07 +02:00
|
|
|
#else
|
2019-04-15 01:43:10 +02:00
|
|
|
return NetworkSocketWinsock::V6AddressToString(addr.ipv6);
|
2017-04-17 20:57:07 +02:00
|
|
|
#endif
|
2019-04-15 01:43:10 +02:00
|
|
|
}else{
|
2017-04-17 20:57:07 +02:00
|
|
|
#ifndef _WIN32
|
2019-04-15 01:43:10 +02:00
|
|
|
return NetworkSocketPosix::V4AddressToString(addr.ipv4);
|
2017-04-17 20:57:07 +02:00
|
|
|
#else
|
2019-04-15 01:43:10 +02:00
|
|
|
return NetworkSocketWinsock::V4AddressToString(addr.ipv4);
|
2017-04-17 20:57:07 +02:00
|
|
|
#endif
|
2019-04-15 01:43:10 +02:00
|
|
|
}
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
bool NetworkAddress::IsEmpty() const{
|
|
|
|
if(isIPv6){
|
|
|
|
const uint64_t* a=reinterpret_cast<const uint64_t*>(addr.ipv6);
|
|
|
|
return a[0]==0LL && a[1]==0LL;
|
2019-02-10 13:28:22 +01:00
|
|
|
}
|
2019-04-15 01:43:10 +02:00
|
|
|
return addr.ipv4==0;
|
2019-02-10 13:28:22 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
bool NetworkAddress::PrefixMatches(const unsigned int prefix, const NetworkAddress &other) const{
|
|
|
|
if(isIPv6!=other.isIPv6)
|
|
|
|
return false;
|
|
|
|
if(!isIPv6){
|
|
|
|
uint32_t mask=0xFFFFFFFF << (32-prefix);
|
|
|
|
return (addr.ipv4 & mask) == (other.addr.ipv4 & mask);
|
|
|
|
}
|
|
|
|
return false;
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkAddress NetworkAddress::Empty(){
|
|
|
|
NetworkAddress addr;
|
|
|
|
addr.isIPv6=false;
|
|
|
|
addr.addr.ipv4=0;
|
|
|
|
return addr;
|
2018-06-04 21:37:43 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkAddress NetworkAddress::IPv4(std::string str){
|
|
|
|
NetworkAddress addr;
|
|
|
|
addr.isIPv6=false;
|
2017-04-17 20:57:07 +02:00
|
|
|
#ifndef _WIN32
|
2019-04-15 01:43:10 +02:00
|
|
|
addr.addr.ipv4=NetworkSocketPosix::StringToV4Address(str);
|
2017-04-17 20:57:07 +02:00
|
|
|
#else
|
2019-04-15 01:43:10 +02:00
|
|
|
addr.addr.ipv4=NetworkSocketWinsock::StringToV4Address(str);
|
2017-04-17 20:57:07 +02:00
|
|
|
#endif
|
2019-04-15 01:43:10 +02:00
|
|
|
return addr;
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkAddress NetworkAddress::IPv4(uint32_t addr){
|
|
|
|
NetworkAddress a;
|
|
|
|
a.isIPv6=false;
|
|
|
|
a.addr.ipv4=addr;
|
|
|
|
return a;
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkAddress NetworkAddress::IPv6(std::string str){
|
|
|
|
NetworkAddress addr;
|
|
|
|
addr.isIPv6=false;
|
2018-06-04 21:37:43 +02:00
|
|
|
#ifndef _WIN32
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkSocketPosix::StringToV6Address(str, addr.addr.ipv6);
|
2018-06-04 21:37:43 +02:00
|
|
|
#else
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkSocketWinsock::StringToV6Address(str, addr.addr.ipv6);
|
2018-06-04 21:37:43 +02:00
|
|
|
#endif
|
2019-04-15 01:43:10 +02:00
|
|
|
return addr;
|
2018-06-04 21:37:43 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkAddress NetworkAddress::IPv6(const uint8_t addr[16]){
|
|
|
|
NetworkAddress a;
|
|
|
|
a.isIPv6=true;
|
|
|
|
memcpy(a.addr.ipv6, addr, 16);
|
|
|
|
return a;
|
2017-04-17 20:57:07 +02:00
|
|
|
}
|
2017-07-03 03:42:49 +02:00
|
|
|
|
2018-11-09 16:44:01 +01:00
|
|
|
bool NetworkSocket::Select(std::vector<NetworkSocket *> &readFds, std::vector<NetworkSocket*> &writeFds, std::vector<NetworkSocket *> &errorFds, SocketSelectCanceller *canceller){
|
2017-07-03 03:42:49 +02:00
|
|
|
#ifndef _WIN32
|
2018-11-09 16:44:01 +01:00
|
|
|
return NetworkSocketPosix::Select(readFds, writeFds, errorFds, canceller);
|
2017-07-03 03:42:49 +02:00
|
|
|
#else
|
2018-11-09 16:44:01 +01:00
|
|
|
return NetworkSocketWinsock::Select(readFds, writeFds, errorFds, canceller);
|
2017-07-03 03:42:49 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketSelectCanceller::~SocketSelectCanceller(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketSelectCanceller *SocketSelectCanceller::Create(){
|
|
|
|
#ifndef _WIN32
|
|
|
|
return new SocketSelectCancellerPosix();
|
|
|
|
#else
|
|
|
|
return new SocketSelectCancellerWin32();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkSocketTCPObfuscated::NetworkSocketTCPObfuscated(NetworkSocket *wrapped) : NetworkSocketWrapper(NetworkProtocol::TCP){
|
2017-07-03 03:42:49 +02:00
|
|
|
this->wrapped=wrapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkSocketTCPObfuscated::~NetworkSocketTCPObfuscated(){
|
|
|
|
if(wrapped)
|
|
|
|
delete wrapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkSocket *NetworkSocketTCPObfuscated::GetWrapped(){
|
|
|
|
return wrapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkSocketTCPObfuscated::InitConnection(){
|
2019-04-15 01:43:10 +02:00
|
|
|
Buffer buf(64);
|
|
|
|
GenerateTCPO2States(*buf, &recvState, &sendState);
|
|
|
|
wrapped->Send(NetworkPacket{
|
|
|
|
std::move(buf),
|
|
|
|
NetworkAddress::Empty(),
|
|
|
|
0,
|
|
|
|
NetworkProtocol::TCP
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkSocketTCPObfuscated::Send(NetworkPacket packet){
|
|
|
|
BufferOutputStream os(packet.data.Length()+4);
|
|
|
|
size_t len=packet.data.Length()/4;
|
2017-07-03 03:42:49 +02:00
|
|
|
if(len<0x7F){
|
|
|
|
os.WriteByte((unsigned char)len);
|
|
|
|
}else{
|
|
|
|
os.WriteByte(0x7F);
|
|
|
|
os.WriteByte((unsigned char)(len & 0xFF));
|
|
|
|
os.WriteByte((unsigned char)((len >> 8) & 0xFF));
|
|
|
|
os.WriteByte((unsigned char)((len >> 16) & 0xFF));
|
|
|
|
}
|
2019-04-15 01:43:10 +02:00
|
|
|
os.WriteBytes(packet.data);
|
2017-07-03 03:42:49 +02:00
|
|
|
EncryptForTCPO2(os.GetBuffer(), os.GetLength(), &sendState);
|
2019-04-15 01:43:10 +02:00
|
|
|
wrapped->Send(NetworkPacket{
|
|
|
|
Buffer(std::move(os)),
|
|
|
|
NetworkAddress::Empty(),
|
|
|
|
0,
|
|
|
|
NetworkProtocol::TCP
|
|
|
|
});
|
2017-07-03 03:42:49 +02:00
|
|
|
//LOGD("Sent %u bytes", os.GetLength());
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:44:01 +01:00
|
|
|
bool NetworkSocketTCPObfuscated::OnReadyToSend(){
|
2019-04-15 01:43:10 +02:00
|
|
|
LOGV("TCPO socket ready to send");
|
2018-11-09 16:44:01 +01:00
|
|
|
if(!initialized){
|
2019-04-15 01:43:10 +02:00
|
|
|
LOGV("Initializing TCPO2 connection");
|
2018-11-09 16:44:01 +01:00
|
|
|
initialized=true;
|
|
|
|
InitConnection();
|
|
|
|
readyToSend=true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return wrapped->OnReadyToSend();
|
|
|
|
}
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkPacket NetworkSocketTCPObfuscated::Receive(size_t maxLen){
|
2017-07-03 03:42:49 +02:00
|
|
|
unsigned char len1;
|
|
|
|
size_t packetLen=0;
|
|
|
|
size_t offset=0;
|
|
|
|
size_t len;
|
2018-08-03 16:26:00 +02:00
|
|
|
len=wrapped->Receive(&len1, 1);
|
|
|
|
if(len<=0){
|
2019-04-15 01:43:10 +02:00
|
|
|
return NetworkPacket::Empty();
|
2018-08-03 16:26:00 +02:00
|
|
|
}
|
2017-07-03 03:42:49 +02:00
|
|
|
EncryptForTCPO2(&len1, 1, &recvState);
|
|
|
|
|
|
|
|
if(len1<0x7F){
|
|
|
|
packetLen=(size_t)len1*4;
|
|
|
|
}else{
|
|
|
|
unsigned char len2[3];
|
|
|
|
len=wrapped->Receive(len2, 3);
|
2018-08-03 16:26:00 +02:00
|
|
|
if(len<=0){
|
2019-04-15 01:43:10 +02:00
|
|
|
return NetworkPacket::Empty();
|
2018-08-03 16:26:00 +02:00
|
|
|
}
|
2017-07-03 03:42:49 +02:00
|
|
|
EncryptForTCPO2(len2, 3, &recvState);
|
|
|
|
packetLen=((size_t)len2[0] | ((size_t)len2[1] << 8) | ((size_t)len2[2] << 16))*4;
|
|
|
|
}
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
if(packetLen>1500){
|
|
|
|
LOGW("packet too big to fit into buffer (%u vs %u)", (unsigned int)packetLen, (unsigned int)1500);
|
|
|
|
return NetworkPacket::Empty();
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
2019-04-15 01:43:10 +02:00
|
|
|
Buffer buf(packetLen);
|
2017-07-03 03:42:49 +02:00
|
|
|
|
|
|
|
while(offset<packetLen){
|
2019-04-15 01:43:10 +02:00
|
|
|
len=wrapped->Receive(*buf, packetLen-offset);
|
2018-08-03 16:26:00 +02:00
|
|
|
if(len<=0){
|
2019-04-15 01:43:10 +02:00
|
|
|
return NetworkPacket::Empty();
|
2018-08-03 16:26:00 +02:00
|
|
|
}
|
2017-07-03 03:42:49 +02:00
|
|
|
offset+=len;
|
|
|
|
}
|
2019-04-15 01:43:10 +02:00
|
|
|
EncryptForTCPO2(*buf, packetLen, &recvState);
|
|
|
|
return NetworkPacket{
|
|
|
|
std::move(buf),
|
|
|
|
wrapped->GetConnectedAddress(),
|
|
|
|
wrapped->GetConnectedPort(),
|
|
|
|
NetworkProtocol::TCP
|
|
|
|
};
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkSocketTCPObfuscated::Open(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkSocketTCPObfuscated::Close(){
|
|
|
|
wrapped->Close();
|
|
|
|
}
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
void NetworkSocketTCPObfuscated::Connect(const NetworkAddress address, uint16_t port){
|
2018-11-09 16:44:01 +01:00
|
|
|
wrapped->Connect(address, port);
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NetworkSocketTCPObfuscated::IsFailed(){
|
|
|
|
return wrapped->IsFailed();
|
|
|
|
}
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkSocketSOCKS5Proxy::NetworkSocketSOCKS5Proxy(NetworkSocket *tcp, NetworkSocket *udp, std::string username, std::string password) : NetworkSocketWrapper(udp ? NetworkProtocol::UDP : NetworkProtocol::TCP){
|
2017-07-03 03:42:49 +02:00
|
|
|
this->tcp=tcp;
|
|
|
|
this->udp=udp;
|
|
|
|
this->username=username;
|
|
|
|
this->password=password;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkSocketSOCKS5Proxy::~NetworkSocketSOCKS5Proxy(){
|
|
|
|
delete tcp;
|
|
|
|
}
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
void NetworkSocketSOCKS5Proxy::Send(NetworkPacket packet){
|
|
|
|
if(protocol==NetworkProtocol::TCP){
|
|
|
|
tcp->Send(std::move(packet));
|
|
|
|
}else if(protocol==NetworkProtocol::UDP){
|
|
|
|
BufferOutputStream out(1500);
|
2017-07-03 03:42:49 +02:00
|
|
|
out.WriteInt16(0); // RSV
|
|
|
|
out.WriteByte(0); // FRAG
|
2019-04-15 01:43:10 +02:00
|
|
|
if(!packet.address.isIPv6){
|
2017-07-03 03:42:49 +02:00
|
|
|
out.WriteByte(1); // ATYP (IPv4)
|
2019-04-15 01:43:10 +02:00
|
|
|
out.WriteInt32(packet.address.addr.ipv4);
|
2017-07-03 03:42:49 +02:00
|
|
|
}else{
|
|
|
|
out.WriteByte(4); // ATYP (IPv6)
|
2019-04-15 01:43:10 +02:00
|
|
|
out.WriteBytes(packet.address.addr.ipv6, 16);
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
2019-04-15 01:43:10 +02:00
|
|
|
out.WriteInt16(htons(packet.port));
|
|
|
|
out.WriteBytes(packet.data);
|
|
|
|
udp->Send(NetworkPacket{
|
|
|
|
Buffer(std::move(out)),
|
|
|
|
connectedAddress,
|
|
|
|
connectedPort,
|
|
|
|
NetworkProtocol::UDP
|
|
|
|
});
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkPacket NetworkSocketSOCKS5Proxy::Receive(size_t maxLen){
|
|
|
|
if(protocol==NetworkProtocol::TCP){
|
|
|
|
NetworkPacket packet=tcp->Receive();
|
|
|
|
packet.address=connectedAddress;
|
|
|
|
packet.port=connectedPort;
|
|
|
|
return packet;
|
|
|
|
}else{
|
|
|
|
NetworkPacket p=udp->Receive();
|
|
|
|
if(!p.IsEmpty() && p.address==connectedAddress && p.port==connectedPort){
|
|
|
|
BufferInputStream in(p.data);
|
2017-07-03 03:42:49 +02:00
|
|
|
in.ReadInt16(); // RSV
|
|
|
|
in.ReadByte(); // FRAG
|
|
|
|
unsigned char atyp=in.ReadByte();
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkAddress address=NetworkAddress::Empty();
|
2017-07-03 03:42:49 +02:00
|
|
|
if(atyp==1){ // IPv4
|
2019-04-15 01:43:10 +02:00
|
|
|
address=NetworkAddress::IPv4((uint32_t) in.ReadInt32());
|
2017-07-03 03:42:49 +02:00
|
|
|
}else if(atyp==4){ // IPv6
|
|
|
|
unsigned char addr[16];
|
|
|
|
in.ReadBytes(addr, 16);
|
2019-04-15 01:43:10 +02:00
|
|
|
address=NetworkAddress::IPv6(addr);
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
2019-04-15 01:43:10 +02:00
|
|
|
return NetworkPacket{
|
|
|
|
Buffer::CopyOf(p.data, in.GetOffset(), in.Remaining()),
|
|
|
|
address,
|
|
|
|
htons(in.ReadInt16()),
|
|
|
|
protocol
|
|
|
|
};
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-15 01:43:10 +02:00
|
|
|
return NetworkPacket::Empty();
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkSocketSOCKS5Proxy::Open(){
|
2018-11-29 01:03:15 +01:00
|
|
|
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkSocketSOCKS5Proxy::Close(){
|
|
|
|
tcp->Close();
|
|
|
|
}
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
void NetworkSocketSOCKS5Proxy::Connect(const NetworkAddress address, uint16_t port){
|
|
|
|
connectedAddress=address;
|
2018-11-09 16:44:01 +01:00
|
|
|
connectedPort=port;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkSocket *NetworkSocketSOCKS5Proxy::GetWrapped(){
|
2019-04-15 01:43:10 +02:00
|
|
|
return protocol==NetworkProtocol::TCP ? tcp : udp;
|
2018-11-09 16:44:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkSocketSOCKS5Proxy::InitConnection(){
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NetworkSocketSOCKS5Proxy::IsFailed(){
|
|
|
|
return NetworkSocket::IsFailed() || tcp->IsFailed();
|
|
|
|
}
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkAddress NetworkSocketSOCKS5Proxy::GetConnectedAddress(){
|
2018-11-09 16:44:01 +01:00
|
|
|
return connectedAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t NetworkSocketSOCKS5Proxy::GetConnectedPort(){
|
|
|
|
return connectedPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NetworkSocketSOCKS5Proxy::OnReadyToSend(){
|
|
|
|
//LOGV("on ready to send, state=%d", state);
|
|
|
|
if(state==ConnectionState::Initial){
|
2019-04-15 01:43:10 +02:00
|
|
|
BufferOutputStream p(16);
|
2018-11-09 16:44:01 +01:00
|
|
|
p.WriteByte(5); // VER
|
|
|
|
if(!username.empty()){
|
|
|
|
p.WriteByte(2); // NMETHODS
|
|
|
|
p.WriteByte(0); // no auth
|
|
|
|
p.WriteByte(2); // user/pass
|
2017-07-03 03:42:49 +02:00
|
|
|
}else{
|
2018-11-09 16:44:01 +01:00
|
|
|
p.WriteByte(1); // NMETHODS
|
|
|
|
p.WriteByte(0); // no auth
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
2019-04-15 01:43:10 +02:00
|
|
|
tcp->Send(NetworkPacket{
|
|
|
|
Buffer(std::move(p)),
|
|
|
|
NetworkAddress::Empty(),
|
|
|
|
0,
|
|
|
|
NetworkProtocol::TCP
|
|
|
|
});
|
2018-11-09 16:44:01 +01:00
|
|
|
state=ConnectionState::WaitingForAuthMethod;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return udp ? udp->OnReadyToSend() : tcp->OnReadyToSend();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NetworkSocketSOCKS5Proxy::OnReadyToReceive(){
|
|
|
|
//LOGV("on ready to receive state=%d", state);
|
|
|
|
unsigned char buf[1024];
|
|
|
|
if(state==ConnectionState::WaitingForAuthMethod){
|
2017-07-03 03:42:49 +02:00
|
|
|
size_t l=tcp->Receive(buf, sizeof(buf));
|
|
|
|
if(l<2 || tcp->IsFailed()){
|
|
|
|
failed=true;
|
2018-11-09 16:44:01 +01:00
|
|
|
return false;
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
|
|
|
BufferInputStream in(buf, l);
|
|
|
|
unsigned char ver=in.ReadByte();
|
2018-11-09 16:44:01 +01:00
|
|
|
unsigned char chosenMethod=in.ReadByte();
|
|
|
|
LOGV("socks5: VER=%02X, METHOD=%02X", ver, chosenMethod);
|
2017-07-03 03:42:49 +02:00
|
|
|
if(ver!=5){
|
2018-11-09 16:44:01 +01:00
|
|
|
LOGW("socks5: incorrect VER in response");
|
2017-07-03 03:42:49 +02:00
|
|
|
failed=true;
|
2018-11-09 16:44:01 +01:00
|
|
|
return false;
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
2018-11-09 16:44:01 +01:00
|
|
|
if(chosenMethod==0){
|
|
|
|
// connected, no further auth needed
|
|
|
|
SendConnectionCommand();
|
|
|
|
}else if(chosenMethod==2 && !username.empty()){
|
2019-04-15 01:43:10 +02:00
|
|
|
BufferOutputStream p(512);
|
2018-11-09 16:44:01 +01:00
|
|
|
p.WriteByte(1); // VER
|
|
|
|
p.WriteByte((unsigned char)(username.length()>255 ? 255 : username.length())); // ULEN
|
|
|
|
p.WriteBytes((unsigned char*)username.c_str(), username.length()>255 ? 255 : username.length()); // UNAME
|
|
|
|
p.WriteByte((unsigned char)(password.length()>255 ? 255 : password.length())); // PLEN
|
|
|
|
p.WriteBytes((unsigned char*)password.c_str(), password.length()>255 ? 255 : password.length()); // PASSWD
|
2019-04-15 01:43:10 +02:00
|
|
|
tcp->Send(NetworkPacket{
|
|
|
|
Buffer(std::move(p)),
|
|
|
|
NetworkAddress::Empty(),
|
|
|
|
0,
|
|
|
|
NetworkProtocol::TCP
|
|
|
|
});
|
2018-11-09 16:44:01 +01:00
|
|
|
state=ConnectionState::WaitingForAuthResult;
|
|
|
|
}else{
|
|
|
|
LOGW("socks5: unsupported auth method");
|
2017-07-03 03:42:49 +02:00
|
|
|
failed=true;
|
2018-11-09 16:44:01 +01:00
|
|
|
return false;
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
2018-11-09 16:44:01 +01:00
|
|
|
return false;
|
|
|
|
}else if(state==ConnectionState::WaitingForAuthResult){
|
|
|
|
size_t l=tcp->Receive(buf, sizeof(buf));
|
2017-07-03 03:42:49 +02:00
|
|
|
if(l<2 || tcp->IsFailed()){
|
|
|
|
failed=true;
|
2018-11-09 16:44:01 +01:00
|
|
|
return false;
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
2018-11-09 16:44:01 +01:00
|
|
|
BufferInputStream in(buf, l);
|
|
|
|
uint8_t ver=in.ReadByte();
|
2017-07-03 03:42:49 +02:00
|
|
|
unsigned char status=in.ReadByte();
|
|
|
|
LOGV("socks5: auth response VER=%02X, STATUS=%02X", ver, status);
|
|
|
|
if(ver!=1){
|
|
|
|
LOGW("socks5: auth response VER is incorrect");
|
|
|
|
failed=true;
|
2018-11-09 16:44:01 +01:00
|
|
|
return false;
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
|
|
|
if(status!=0){
|
|
|
|
LOGW("socks5: username/password auth failed");
|
|
|
|
failed=true;
|
2018-11-09 16:44:01 +01:00
|
|
|
return false;
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
2018-05-15 20:23:46 +02:00
|
|
|
LOGV("socks5: authentication succeeded");
|
2018-11-09 16:44:01 +01:00
|
|
|
SendConnectionCommand();
|
|
|
|
return false;
|
|
|
|
}else if(state==ConnectionState::WaitingForCommandResult){
|
|
|
|
size_t l=tcp->Receive(buf, sizeof(buf));
|
2019-04-15 01:43:10 +02:00
|
|
|
if(protocol==NetworkProtocol::TCP){
|
2018-11-09 16:44:01 +01:00
|
|
|
if(l<2 || tcp->IsFailed()){
|
|
|
|
LOGW("socks5: connect failed")
|
|
|
|
failed=true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
BufferInputStream in(buf, l);
|
|
|
|
unsigned char ver=in.ReadByte();
|
|
|
|
if(ver!=5){
|
|
|
|
LOGW("socks5: connect: wrong ver in response");
|
|
|
|
failed=true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unsigned char rep=in.ReadByte();
|
|
|
|
if(rep!=0){
|
|
|
|
LOGW("socks5: connect: failed with error %02X", rep);
|
|
|
|
failed=true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOGV("socks5: connect succeeded");
|
|
|
|
state=ConnectionState::Connected;
|
|
|
|
tcp=new NetworkSocketTCPObfuscated(tcp);
|
|
|
|
readyToSend=true;
|
|
|
|
return tcp->OnReadyToSend();
|
2019-04-15 01:43:10 +02:00
|
|
|
}else if(protocol==NetworkProtocol::UDP){
|
2018-11-09 16:44:01 +01:00
|
|
|
if(l<2 || tcp->IsFailed()){
|
|
|
|
LOGW("socks5: udp associate failed");
|
|
|
|
failed=true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try{
|
|
|
|
BufferInputStream in(buf, l);
|
|
|
|
unsigned char ver=in.ReadByte();
|
|
|
|
unsigned char rep=in.ReadByte();
|
|
|
|
if(ver!=5){
|
|
|
|
LOGW("socks5: udp associate: wrong ver in response");
|
|
|
|
failed=true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(rep!=0){
|
|
|
|
LOGW("socks5: udp associate failed with error %02X", rep);
|
|
|
|
failed=true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
in.ReadByte(); // RSV
|
|
|
|
unsigned char atyp=in.ReadByte();
|
|
|
|
if(atyp==1){
|
|
|
|
uint32_t addr=(uint32_t) in.ReadInt32();
|
2019-04-15 01:43:10 +02:00
|
|
|
connectedAddress=NetworkAddress::IPv4(addr);
|
2018-11-09 16:44:01 +01:00
|
|
|
}else if(atyp==3){
|
|
|
|
unsigned char len=in.ReadByte();
|
|
|
|
char domain[256];
|
|
|
|
memset(domain, 0, sizeof(domain));
|
|
|
|
in.ReadBytes((unsigned char*)domain, len);
|
|
|
|
LOGD("address type is domain, address=%s", domain);
|
|
|
|
connectedAddress=ResolveDomainName(std::string(domain));
|
2019-04-15 01:43:10 +02:00
|
|
|
if(connectedAddress.IsEmpty()){
|
2018-11-09 16:44:01 +01:00
|
|
|
LOGW("socks5: failed to resolve domain name '%s'", domain);
|
|
|
|
failed=true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}else if(atyp==4){
|
|
|
|
unsigned char addr[16];
|
|
|
|
in.ReadBytes(addr, 16);
|
2019-04-15 01:43:10 +02:00
|
|
|
connectedAddress=NetworkAddress::IPv6(addr);
|
2018-11-09 16:44:01 +01:00
|
|
|
}else{
|
|
|
|
LOGW("socks5: unknown address type %d", atyp);
|
|
|
|
failed=true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
connectedPort=(uint16_t)ntohs(in.ReadInt16());
|
|
|
|
state=ConnectionState::Connected;
|
|
|
|
readyToSend=true;
|
2019-04-15 01:43:10 +02:00
|
|
|
LOGV("socks5: udp associate successful, given endpoint %s:%d", connectedAddress.ToString().c_str(), connectedPort);
|
2018-11-09 16:44:01 +01:00
|
|
|
}catch(std::out_of_range& x){
|
|
|
|
LOGW("socks5: udp associate response parse failed");
|
|
|
|
failed=true;
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
2018-11-09 16:44:01 +01:00
|
|
|
return udp ? udp->OnReadyToReceive() : tcp->OnReadyToReceive();
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 16:44:01 +01:00
|
|
|
void NetworkSocketSOCKS5Proxy::SendConnectionCommand(){
|
2019-04-15 01:43:10 +02:00
|
|
|
BufferOutputStream out(1024);
|
|
|
|
if(protocol==NetworkProtocol::TCP){
|
2018-11-09 16:44:01 +01:00
|
|
|
out.WriteByte(5); // VER
|
|
|
|
out.WriteByte(1); // CMD (CONNECT)
|
|
|
|
out.WriteByte(0); // RSV
|
2019-04-15 01:43:10 +02:00
|
|
|
if(!connectedAddress.isIPv6){
|
2018-11-09 16:44:01 +01:00
|
|
|
out.WriteByte(1); // ATYP (IPv4)
|
2019-04-15 01:43:10 +02:00
|
|
|
out.WriteInt32(connectedAddress.addr.ipv4);
|
2018-11-09 16:44:01 +01:00
|
|
|
}else{
|
2019-04-15 01:43:10 +02:00
|
|
|
out.WriteByte(4); // ATYP (IPv6)
|
|
|
|
out.WriteBytes((unsigned char*)connectedAddress.addr.ipv6, 16);
|
2018-11-09 16:44:01 +01:00
|
|
|
}
|
|
|
|
out.WriteInt16(htons(connectedPort)); // DST.PORT
|
2019-04-15 01:43:10 +02:00
|
|
|
}else if(protocol==NetworkProtocol::UDP){
|
2018-11-09 16:44:01 +01:00
|
|
|
LOGV("Sending udp associate");
|
|
|
|
out.WriteByte(5); // VER
|
|
|
|
out.WriteByte(3); // CMD (UDP ASSOCIATE)
|
|
|
|
out.WriteByte(0); // RSV
|
|
|
|
out.WriteByte(1); // ATYP (IPv4)
|
|
|
|
out.WriteInt32(0); // DST.ADDR
|
|
|
|
out.WriteInt16(0); // DST.PORT
|
|
|
|
}
|
2019-04-15 01:43:10 +02:00
|
|
|
tcp->Send(NetworkPacket{
|
|
|
|
Buffer(std::move(out)),
|
|
|
|
NetworkAddress::Empty(),
|
|
|
|
0,
|
|
|
|
NetworkProtocol::TCP
|
|
|
|
});
|
|
|
|
state=ConnectionState::WaitingForCommandResult;
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 16:44:01 +01:00
|
|
|
bool NetworkSocketSOCKS5Proxy::NeedSelectForSending(){
|
|
|
|
return state==ConnectionState::Initial || state==ConnectionState::Connected;
|
2017-07-03 03:42:49 +02:00
|
|
|
}
|