// File: renzibei[P]TSProxy/proxyclient.cpp
// Function: socksHandShake(int, int, ClientContext*, std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<int const, int>>>&)
int socksHandShake(int localFd, int serverFd, ClientContext *ctx, LocalFdMap &localFdMap) {
    LogHelper::log(Debug, "client start to socks handshake for localFd: %d", localFd);
    static uint8_t socksBuffer[sizeof(SocksTcpReply)];
    memset(socksBuffer, 0, sizeof(socksBuffer));
    int recvCode = readNBytes(localFd, socksBuffer, 2, constants::clientNonBlocking);
    if (recvCode < 0) {
        LogHelper::log(Warn, "Fail to socks handshake in reading header in the first step");
        return recvCode;
    }
    uint8_t socksVersion = socksBuffer[0];
    uint8_t methodsNum = socksBuffer[1];
    if (socksVersion != constants::SocksVersion) {
        LogHelper::log(Warn, "socks version mismatch, version: %d", socksVersion);
        return -1;
    }
    memset(socksBuffer, 0, 2);
    recvCode = readNBytes(localFd, socksBuffer, methodsNum, constants::clientNonBlocking);
    if (recvCode <= 0 || methodsNum != recvCode) {
        LogHelper::log(Warn, "Fail to socks handshake in reading methods");
        return recvCode;
    }
    uint8_t supportMethod = constants::SocksNoSupportMethod;
    for (int i = 0; i < methodsNum; ++i) {
        if (socksBuffer[i] == constants::SocksNoAuthMethod) {
            supportMethod = socksBuffer[i];
            break;
        }
    }
    if (supportMethod == constants::SocksNoSupportMethod) {
        LogHelper::log(Warn, "No supportMethod in socks");
        // return -2;
    }
    memset(socksBuffer, 0, methodsNum);
    socksBuffer[0] = constants::SocksVersion;
    socksBuffer[1] = supportMethod;
    int sendCode = writeNBytes(localFd, socksBuffer, 2, constants::clientNonBlocking);
    if (sendCode < 0) {
        LogHelper::log(Warn, "Failed to send second packet in socks handshake");
        return sendCode;
    }

    memset(socksBuffer, 0, sizeof(socksBuffer));
    recvCode = readNBytes(localFd, socksBuffer, 4, constants::clientNonBlocking);
    if (recvCode < 0) {
        LogHelper::log(Warn, "Fail to recv thrid socks handshake header");
        return recvCode;
    }
    socksVersion = socksBuffer[0];
    uint8_t requestCmd = socksBuffer[1];
    uint8_t reserved = socksBuffer[2];
    uint8_t dstAddrType = socksBuffer[3];
    
    memset(socksBuffer, 0, 4);
    size_t totalLen = 1;
    socksBuffer[0] = dstAddrType;
    if (dstAddrType == constants::SocksAddrDomainType) {
        recvCode = readNBytes(localFd, socksBuffer + 1, 1, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv domain addr length in the third handshake recv");
            return recvCode;
        }
        uint8_t domainLen = socksBuffer[1];
        totalLen += 1;
        // socksBuffer[0] = 0;
        recvCode = readNBytes(localFd, socksBuffer + 2, domainLen, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv full domain addr in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
        recvCode = readNBytes(localFd, socksBuffer + 2 + recvCode, 2, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv dst port in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
    }
    else if (dstAddrType == constants::SocksAddrIpv4Type) {
        recvCode = readNBytes(localFd, socksBuffer + 1, 4, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv ipv4 addr in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
        recvCode = readNBytes(localFd, socksBuffer + 1 + recvCode, 2, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv dst port in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
    }
    else if (dstAddrType == constants::SocksAddrIpv6Type) {
        recvCode = readNBytes(localFd, socksBuffer + 1, 16, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv ipv6 addr in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
        recvCode = readNBytes(localFd, socksBuffer + 1 + recvCode, 2, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv dst port in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
    }
    else {
        LogHelper::log(Warn, "Not valid addr type in the third handshake recv, type: %d", dstAddrType);
        return recvCode;
    }

    static uint8_t addrBuffer[constants::SocksDomainMaxLength + 2];
    memset(addrBuffer, 0, sizeof(addrBuffer));
    memcpy(addrBuffer, socksBuffer, totalLen);
    memset(socksBuffer, 0, totalLen);
    socksBuffer[0] = requestCmd;
    memcpy(socksBuffer + 1, addrBuffer, totalLen);
    hton_copy4bytes(socksBuffer + 1 + totalLen, &localFd);
    // memcpy(addrBuffer, socksBuffer, recvCode);
    // memset(socksBuffer, 0, recvCode);
    sendCode = sendMessage(serverFd, socksBuffer, totalLen + 1 + sizeof(localFd), ctx, 6);
    if (sendCode < 0) {
        LogHelper::log(Error, "Fail to send socks handshake third request to server");
        return sendCode;
    }
    localFdMap[localFd] = LocalFdWaiting;
    
    return 0;
}

// Assembly:
subq $0x48, %rsp
movl %edi, 0x40(%rsp)
movl %esi, 0x3c(%rsp)
movq %rdx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movl 0x40(%rsp), %edx
movl $0x3, %edi
leaq 0x8022(%rip), %rsi      # 0xb438
movb $0x0, %al
callq 0x2a50
leaq 0x6de0c(%rip), %rdi     # 0x71230
xorl %esi, %esi
movl $0x106, %edx            # imm = 0x106
callq 0x2150
movl 0x40(%rsp), %edi
leaq 0x6ddf5(%rip), %rsi     # 0x71230
movl $0x2, %edx
movl $0x1, %ecx
callq 0x7e80
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jge 0x3475
movl $0x1, %edi
leaq 0x8007(%rip), %rsi      # 0xb468
movb $0x0, %al
callq 0x2a50
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
movb 0x6ddb5(%rip), %al      # 0x71230
movb %al, 0x23(%rsp)
movb 0x6ddac(%rip), %al      # 0x71231
movb %al, 0x22(%rsp)
movzbl 0x23(%rsp), %eax
cmpl $0x5, %eax
je 0x34b8
movzbl 0x23(%rsp), %edx
movl $0x1, %edi
leaq 0x8000(%rip), %rsi      # 0xb4a4
movb $0x0, %al
callq 0x2a50
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x3a73
leaq 0x6dd71(%rip), %rdi     # 0x71230
xorl %esi, %esi
movl $0x2, %edx
callq 0x2150
movl 0x40(%rsp), %edi
movzbl 0x22(%rsp), %eax
movl %eax, %edx
leaq 0x6dd53(%rip), %rsi     # 0x71230
movl $0x1, %ecx
callq 0x7e80
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jle 0x34fd
movzbl 0x22(%rsp), %eax
cmpl 0x24(%rsp), %eax
je 0x351d
movl $0x1, %edi
leaq 0x7fbf(%rip), %rsi      # 0xb4c8
movb $0x0, %al
callq 0x2a50
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
movb $-0x1, 0x21(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movzbl 0x22(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3570
movslq 0x1c(%rsp), %rcx
leaq 0x6dced(%rip), %rax     # 0x71230
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x3561
movslq 0x1c(%rsp), %rcx
leaq 0x6dcd8(%rip), %rax     # 0x71230
movb (%rax,%rcx), %al
movb %al, 0x21(%rsp)
jmp 0x3570
jmp 0x3563
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x352a
movzbl 0x21(%rsp), %eax
cmpl $0xff, %eax
jne 0x358f
movl $0x1, %edi
leaq 0x7f6b(%rip), %rsi      # 0xb4f3
movb $0x0, %al
callq 0x2a50
movzbl 0x22(%rsp), %eax
movl %eax, %edx
leaq 0x6dc93(%rip), %rdi     # 0x71230
xorl %esi, %esi
callq 0x2150
movb $0x5, 0x6dc85(%rip)     # 0x71230
movb 0x21(%rsp), %al
movb %al, 0x6dc7c(%rip)      # 0x71231
movl 0x40(%rsp), %edi
leaq 0x6dc70(%rip), %rsi     # 0x71230
movl $0x2, %edx
movl $0x1, %ecx
callq 0x7f90
movl %eax, 0x18(%rsp)
cmpl $0x0, 0x18(%rsp)
jge 0x35fa
movl $0x1, %edi
leaq 0x7f27(%rip), %rsi      # 0xb50d
movb $0x0, %al
callq 0x2a50
movl 0x18(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
leaq 0x6dc2f(%rip), %rdi     # 0x71230
xorl %esi, %esi
movl $0x106, %edx            # imm = 0x106
callq 0x2150
movl 0x40(%rsp), %edi
leaq 0x6dc18(%rip), %rsi     # 0x71230
movl $0x4, %edx
movl $0x1, %ecx
callq 0x7e80
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jge 0x3652
movl $0x1, %edi
leaq 0x7eff(%rip), %rsi      # 0xb53d
movb $0x0, %al
callq 0x2a50
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
movb 0x6dbd8(%rip), %al      # 0x71230
movb %al, 0x23(%rsp)
movb 0x6dbcf(%rip), %al      # 0x71231
movb %al, 0x17(%rsp)
movb 0x6dbc6(%rip), %al      # 0x71232
movb %al, 0x16(%rsp)
movb 0x6dbbd(%rip), %al      # 0x71233
movb %al, 0x15(%rsp)
leaq 0x6dbaf(%rip), %rdi     # 0x71230
xorl %esi, %esi
movl $0x4, %edx
callq 0x2150
movq $0x1, 0x8(%rsp)
movb 0x15(%rsp), %al
movb %al, 0x6db90(%rip)      # 0x71230
movzbl 0x15(%rsp), %eax
cmpl $0x3, %eax
jne 0x37ce
movl 0x40(%rsp), %edi
leaq 0x6db77(%rip), %rsi     # 0x71230
addq $0x1, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x7e80
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jge 0x36f7
movl $0x1, %edi
leaq 0x7e84(%rip), %rsi      # 0xb567
movb $0x0, %al
callq 0x2a50
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
movb 0x6db34(%rip), %al      # 0x71231
movb %al, 0x7(%rsp)
movq 0x8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x8(%rsp)
movl 0x40(%rsp), %edi
movzbl 0x7(%rsp), %eax
movl %eax, %edx
leaq 0x6db0f(%rip), %rsi     # 0x71230
addq $0x2, %rsi
movl $0x1, %ecx
callq 0x7e80
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jge 0x375a
movl $0x1, %edi
leaq 0x7e5d(%rip), %rsi      # 0xb5a3
movb $0x0, %al
callq 0x2a50
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
movslq 0x24(%rsp), %rax
addq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x40(%rsp), %edi
movslq 0x24(%rsp), %rax
leaq 0x6dab7(%rip), %rsi     # 0x71230
addq $0x2, %rsi
addq %rax, %rsi
movl $0x2, %edx
movl $0x1, %ecx
callq 0x7e80
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jge 0x37ba
movl $0x1, %edi
leaq 0x7e37(%rip), %rsi      # 0xb5dd
movb $0x0, %al
callq 0x2a50
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
movslq 0x24(%rsp), %rax
addq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x398a
movzbl 0x15(%rsp), %eax
cmpl $0x1, %eax
jne 0x3899
movl 0x40(%rsp), %edi
leaq 0x6da49(%rip), %rsi     # 0x71230
addq $0x1, %rsi
movl $0x4, %edx
movl $0x1, %ecx
callq 0x7e80
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jge 0x3825
movl $0x1, %edi
leaq 0x7dfe(%rip), %rsi      # 0xb60f
movb $0x0, %al
callq 0x2a50
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
movslq 0x24(%rsp), %rax
addq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x40(%rsp), %edi
movslq 0x24(%rsp), %rax
leaq 0x6d9ec(%rip), %rsi     # 0x71230
addq $0x1, %rsi
addq %rax, %rsi
movl $0x2, %edx
movl $0x1, %ecx
callq 0x7e80
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jge 0x3885
movl $0x1, %edi
leaq 0x7d6c(%rip), %rsi      # 0xb5dd
movb $0x0, %al
callq 0x2a50
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
movslq 0x24(%rsp), %rax
addq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x3988
movzbl 0x15(%rsp), %eax
cmpl $0x4, %eax
jne 0x3961
movl 0x40(%rsp), %edi
leaq 0x6d97e(%rip), %rsi     # 0x71230
addq $0x1, %rsi
movl $0x10, %edx
movl $0x1, %ecx
callq 0x7e80
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jge 0x38f0
movl $0x1, %edi
leaq 0x7d66(%rip), %rsi      # 0xb642
movb $0x0, %al
callq 0x2a50
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
movslq 0x24(%rsp), %rax
addq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x40(%rsp), %edi
movslq 0x24(%rsp), %rax
leaq 0x6d921(%rip), %rsi     # 0x71230
addq $0x1, %rsi
addq %rax, %rsi
movl $0x2, %edx
movl $0x1, %ecx
callq 0x7e80
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jge 0x3950
movl $0x1, %edi
leaq 0x7ca1(%rip), %rsi      # 0xb5dd
movb $0x0, %al
callq 0x2a50
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
movslq 0x24(%rsp), %rax
addq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x3986
movzbl 0x15(%rsp), %edx
movl $0x1, %edi
leaq 0x7d03(%rip), %rsi      # 0xb675
movb $0x0, %al
callq 0x2a50
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
jmp 0x3988
jmp 0x398a
leaq 0x6d9af(%rip), %rdi     # 0x71340
xorl %esi, %esi
movl $0x101, %edx            # imm = 0x101
callq 0x2150
movq 0x8(%rsp), %rdx
leaq 0x6d997(%rip), %rdi     # 0x71340
leaq 0x6d880(%rip), %rsi     # 0x71230
callq 0x21c0
movq 0x8(%rsp), %rdx
leaq 0x6d86f(%rip), %rdi     # 0x71230
xorl %esi, %esi
callq 0x2150
movb 0x17(%rsp), %al
movb %al, 0x6d85e(%rip)      # 0x71230
movq 0x8(%rsp), %rdx
leaq 0x6d852(%rip), %rdi     # 0x71230
addq $0x1, %rdi
leaq 0x6d957(%rip), %rsi     # 0x71340
callq 0x21c0
movl 0x40(%rsp), %edi
callq 0x21a0
movl %eax, %edx
movq 0x8(%rsp), %rcx
leaq 0x6d82b(%rip), %rax     # 0x71230
movl %edx, 0x1(%rax,%rcx)
movl 0x3c(%rsp), %edi
movq 0x8(%rsp), %rdx
addq $0x1, %rdx
addq $0x4, %rdx
movq 0x30(%rsp), %rcx
leaq 0x6d80a(%rip), %rsi     # 0x71230
movl $0x6, %r8d
callq 0x2db0
movl %eax, 0x18(%rsp)
cmpl $0x0, 0x18(%rsp)
jge 0x3a56
xorl %edi, %edi
leaq 0x7c6a(%rip), %rsi      # 0xb6af
movb $0x0, %al
callq 0x2a50
movl 0x18(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x3a73
movq 0x28(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x4ee0
movl $0x1, (%rax)
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopl (%rax)