// File: renzibei[P]TSProxy/proxyclient.cpp
// Function: startListen(int, ClientContext*)
int startListen(int serverFd, ClientContext *ctx) {
    int on = 1;
    int listen_sd = make_socket(constants::clientListenPort, on, constants::clientBindToLoopback);
    if (listen_sd < 0) {
        LogHelper::log(Error, "Client fail to make listen socket");
        return -1;
    }

    // set nonblocking
    int rc = ioctl(listen_sd, FIONBIO, (char *)&on);
    if (rc < 0) {
        // perror("ioctl() failed");
        LogHelper::log(Error, "failed to ioctl(), %s", strerror(errno));
        close(listen_sd);
        // exit(-1);
        return -2;
    }

    // rc = ioctl(serverFd, FIONBIO, (char *)&on);
    // if (rc < 0) {
    //     // perror("ioctl() failed");
    //     LogHelper::log(Error, "failed to ioctl(), %s", strerror(errno));
    //     close(serverFd);
    //     // exit(-1);
    //     return -2;
    // }

    rc = listen(listen_sd, 1024);
    if (rc < 0) {
        LogHelper::log(Error, "Failed to listen, %s", strerror(errno));
        close(listen_sd);
        return -3;
    }

    fd_set master_set, working_set;
    FD_ZERO(&master_set);
    int max_sd = listen_sd;
    FD_SET(listen_sd, &master_set);

    FD_SET(serverFd, &master_set);
    if (serverFd > max_sd) {
        max_sd = serverFd;
    }

    struct timeval timeout;
    timeout.tv_sec  = 3 * 60;
    timeout.tv_usec = 0;

    LocalFdMap localFdMap;
    LogHelper::log(Info, "Begin loop");
    bool endLoop = false;
    do {
        memcpy(&working_set, &master_set, sizeof(master_set));
        LogHelper::log(Debug, "Client waiting for select");
        rc = select(max_sd + 1, &working_set, NULL, NULL, &timeout);
        if (rc < 0) {
            LogHelper::log(Error, "Error when select, %s", strerror(errno));
            return -4;
        }
        if (rc == 0) {
            LogHelper::log(Debug, "select time out, continue");
            continue;
        }
        int ready_fds = rc;
        LogHelper::log(Debug, "read_fd num: %d", ready_fds);
        for (int i = 0; i <= max_sd && ready_fds > 0; ++i) {
            if (FD_ISSET(i, &working_set)) {
                ready_fds--;
                if (i == listen_sd) {
                    LogHelper::log(Debug, "Listen socket is readable");
                    int new_sd = -1;
                    do {
                        new_sd = accept(listen_sd, NULL, NULL);
                        if (new_sd < 0) {
                            if (errno != EAGAIN) {
                                // perror("  accept() failed");
                                LogHelper::log(Error, "accept failed, %s", strerror(errno));
                                endLoop = true;
                            }
                            break;
                        }

                        LogHelper::log(Debug, "New incoming connection fd: %d\n", new_sd);
                        FD_SET(new_sd, &master_set);
                        if (new_sd > max_sd)
                            max_sd = new_sd;
                        
                        rc = socksHandShake(new_sd, serverFd, ctx, localFdMap);
                        if (rc < 0) {
                            LogHelper::log(Error, "Failed to establish socks connection, clean fd: %d", new_sd);
                            close(new_sd);
                            FD_CLR(new_sd, &master_set);
                            if (new_sd == max_sd) {
                                while (FD_ISSET(max_sd, &master_set) == 0)
                                    max_sd -= 1;
                            }
                            if (localFdMap.find(new_sd) != localFdMap.end()) {
                                localFdMap.erase(new_sd);
                            }
                        }
                    } while (new_sd != -1);
                }
                else if(i == serverFd) {
                    LogHelper::log(Debug, "Server Fd is readable");

                    int handleRet = readAndHandleFromServer(i, localFdMap, ctx, master_set, max_sd);

                    
                }
                else {
                    LogHelper::log(Debug, "Fd %d is ready", i);
                    bool close_conn = false;
                    static char readBuf2[constants::AES_MAX_DATA_LENGTH - 72];
                    memset(readBuf2, 0, sizeof(readBuf2));
                    rc = recv(i, readBuf2, sizeof(readBuf2), 0);
                    if (rc < 0) {
                        if (errno != EAGAIN) {
                            LogHelper::log(Error, "Failed in recv of %d fd", i);
                            close_conn = true;
                        }
                        
                    }
                    else if (rc == 0) {
                        LogHelper::log(Warn, "Fd %d has been closed", i);
                        close_conn = true;
                    }
                    else {
                        LogHelper::log(Debug, "recv from socks user %d bytes, : %s", rc, readBuf2);
                        int sendCode = sendRequestMsg(serverFd, i, (const uint8_t *)readBuf2, rc, ctx);
                        if (sendCode < 0) {
                            LogHelper::log(Error, "Fail to send local data to server");
                            return sendCode;
                        }
                    }

                    if (close_conn) {
                        LogHelper::log(Warn, "Close fd: %d", i);
                        close(i);
                        FD_CLR(i, &master_set);
                        if (i == max_sd) {
                            while (FD_ISSET(max_sd, &master_set) == 0)
                                max_sd -= 1;
                        }
                        if (localFdMap.find(i) != localFdMap.end()) {
                            localFdMap.erase(i);
                        }
                    }
                }
            }
        }

    } while(!endLoop);

    return 0;
}

// Assembly:
subq $0x248, %rsp            # imm = 0x248
movl %edi, 0x240(%rsp)
movq %rsi, 0x238(%rsp)
movl $0x1, 0x234(%rsp)
movl 0x234(%rsp), %esi
movl $0x2b48, %edi           # imm = 0x2B48
xorl %edx, %edx
callq 0x7d10
movl %eax, 0x230(%rsp)
cmpl $0x0, 0x230(%rsp)
jge 0x4015
xorl %edi, %edi
leaq 0x7903(%rip), %rsi      # 0xb901
movb $0x0, %al
callq 0x2a50
movl $0xffffffff, 0x244(%rsp) # imm = 0xFFFFFFFF
jmp 0x4a96
movl 0x230(%rsp), %edi
movl $0x5421, %esi           # imm = 0x5421
leaq 0x234(%rsp), %rdx
movb $0x0, %al
callq 0x2330
movl %eax, 0x22c(%rsp)
cmpl $0x0, 0x22c(%rsp)
jge 0x407c
callq 0x2040
movl (%rax), %edi
callq 0x20f0
movq %rax, %rdx
xorl %edi, %edi
leaq 0x78ca(%rip), %rsi      # 0xb923
movb $0x0, %al
callq 0x2a50
movl 0x230(%rsp), %edi
callq 0x23e0
movl $0xfffffffe, 0x244(%rsp) # imm = 0xFFFFFFFE
jmp 0x4a96
movl 0x230(%rsp), %edi
movl $0x400, %esi            # imm = 0x400
callq 0x2270
movl %eax, 0x22c(%rsp)
cmpl $0x0, 0x22c(%rsp)
jge 0x40d9
callq 0x2040
movl (%rax), %edi
callq 0x20f0
movq %rax, %rdx
xorl %edi, %edi
leaq 0x7883(%rip), %rsi      # 0xb939
movb $0x0, %al
callq 0x2a50
movl 0x230(%rsp), %edi
callq 0x23e0
movl $0xfffffffd, 0x244(%rsp) # imm = 0xFFFFFFFD
jmp 0x4a96
jmp 0x40db
leaq 0x1a8(%rsp), %rax
movq %rax, 0x110(%rsp)
movl $0x0, 0x11c(%rsp)
movl 0x11c(%rsp), %eax
cmpq $0x10, %rax
jae 0x412d
movq 0x110(%rsp), %rax
movl 0x11c(%rsp), %ecx
movq $0x0, (%rax,%rcx,8)
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x40f6
jmp 0x412f
movl 0x230(%rsp), %eax
movl %eax, 0x10c(%rsp)
movl 0x230(%rsp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl 0x230(%rsp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq 0x1a8(%rsp,%rax,8), %rcx
movq %rcx, 0x1a8(%rsp,%rax,8)
movl 0x240(%rsp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl 0x240(%rsp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq 0x1a8(%rsp,%rax,8), %rcx
movq %rcx, 0x1a8(%rsp,%rax,8)
movl 0x240(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jle 0x41d9
movl 0x240(%rsp), %eax
movl %eax, 0x10c(%rsp)
movq $0xb4, 0xf8(%rsp)
movq $0x0, 0x100(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x4fc0
leaq 0x7749(%rip), %rsi      # 0xb94e
xorl %eax, %eax
movl $0x2, %edi
callq 0x2a50
jmp 0x4213
movb $0x0, 0xb3(%rsp)
movups 0x218(%rsp), %xmm0
movaps %xmm0, 0x190(%rsp)
movups 0x208(%rsp), %xmm0
movaps %xmm0, 0x180(%rsp)
movups 0x1f8(%rsp), %xmm0
movaps %xmm0, 0x170(%rsp)
movups 0x1e8(%rsp), %xmm0
movaps %xmm0, 0x160(%rsp)
movups 0x1a8(%rsp), %xmm0
movups 0x1b8(%rsp), %xmm1
movups 0x1c8(%rsp), %xmm2
movups 0x1d8(%rsp), %xmm3
movaps %xmm3, 0x150(%rsp)
movaps %xmm2, 0x140(%rsp)
movaps %xmm1, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
leaq 0x76b7(%rip), %rsi      # 0xb959
xorl %eax, %eax
movl $0x3, %edi
callq 0x2a50
jmp 0x42b0
movl 0x10c(%rsp), %edi
incl %edi
xorl %eax, %eax
movl %eax, %ecx
leaq 0x120(%rsp), %rsi
leaq 0xf8(%rsp), %r8
movq %rcx, %rdx
callq 0x2220
movl %eax, 0x6c(%rsp)
jmp 0x42db
movl 0x6c(%rsp), %eax
movl %eax, 0x22c(%rsp)
cmpl $0x0, 0x22c(%rsp)
jge 0x4353
callq 0x2040
movl (%rax), %edi
callq 0x20f0
movq %rax, %rdx
leaq 0x766d(%rip), %rsi      # 0xb973
xorl %edi, %edi
movb %dil, %al
callq 0x2a50
jmp 0x4312
movl $0xfffffffc, 0x244(%rsp) # imm = 0xFFFFFFFC
movl $0x1, 0xac(%rsp)
jmp 0x4a89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x4ff0
jmp 0x4aa5
cmpl $0x0, 0x22c(%rsp)
jne 0x4377
leaq 0x7625(%rip), %rsi      # 0xb989
xorl %eax, %eax
movl $0x3, %edi
callq 0x2a50
jmp 0x4372
jmp 0x4a62
movl 0x22c(%rsp), %eax
movl %eax, 0xa8(%rsp)
movl 0xa8(%rsp), %edx
leaq 0x7610(%rip), %rsi      # 0xb9a3
xorl %eax, %eax
movl $0x3, %edi
callq 0x2a50
jmp 0x43a1
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %ecx
xorl %eax, %eax
cmpl 0x10c(%rsp), %ecx
movb %al, 0x6b(%rsp)
jg 0x43d1
cmpl $0x0, 0xa8(%rsp)
setg %al
movb %al, 0x6b(%rsp)
movb 0x6b(%rsp), %al
testb $0x1, %al
jne 0x43de
jmp 0x4a60
movl 0xa4(%rsp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq 0x120(%rsp,%rax,8), %rax
movq %rax, 0x60(%rsp)
movl 0xa4(%rsp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq 0x60(%rsp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x4a48
movl 0xa8(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xa8(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x230(%rsp), %eax
jne 0x46ff
leaq 0x755d(%rip), %rsi      # 0xb9b3
xorl %eax, %eax
movl $0x3, %edi
callq 0x2a50
jmp 0x4464
movl $0xffffffff, 0xa0(%rsp) # imm = 0xFFFFFFFF
movl 0x230(%rsp), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x20d0
movl %eax, 0x5c(%rsp)
jmp 0x4488
movl 0x5c(%rsp), %eax
movl %eax, 0xa0(%rsp)
cmpl $0x0, 0xa0(%rsp)
jge 0x44d6
callq 0x2040
cmpl $0xb, (%rax)
je 0x44d1
callq 0x2040
movl (%rax), %edi
callq 0x20f0
movq %rax, %rdx
leaq 0x7510(%rip), %rsi      # 0xb9cd
xorl %edi, %edi
movb %dil, %al
callq 0x2a50
jmp 0x44c9
movb $0x1, 0xb3(%rsp)
jmp 0x46fa
movl 0xa0(%rsp), %edx
leaq 0x74fb(%rip), %rsi      # 0xb9df
xorl %eax, %eax
movl $0x3, %edi
callq 0x2a50
jmp 0x44f2
movl 0xa0(%rsp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl 0xa0(%rsp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq 0x1a8(%rsp,%rax,8), %rcx
movq %rcx, 0x1a8(%rsp,%rax,8)
movl 0xa0(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jle 0x454f
movl 0xa0(%rsp), %eax
movl %eax, 0x10c(%rsp)
movl 0xa0(%rsp), %edi
movl 0x240(%rsp), %esi
movq 0x238(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
callq 0x33f0
movl %eax, 0x58(%rsp)
jmp 0x4578
movl 0x58(%rsp), %eax
movl %eax, 0x22c(%rsp)
cmpl $0x0, 0x22c(%rsp)
jge 0x46ea
movl 0xa0(%rsp), %edx
leaq 0x7460(%rip), %rsi      # 0xb9ff
xorl %edi, %edi
movb %dil, %al
callq 0x2a50
jmp 0x45ab
movl 0xa0(%rsp), %edi
callq 0x23e0
jmp 0x45b9
movl 0xa0(%rsp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
xorq $-0x1, %rcx
movl 0xa0(%rsp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
andq 0x1a8(%rsp,%rax,8), %rcx
movq %rcx, 0x1a8(%rsp,%rax,8)
movl 0xa0(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jne 0x4676
jmp 0x460e
movl 0x10c(%rsp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq 0x1a8(%rsp,%rax,8), %rax
movq %rax, 0x50(%rsp)
movl 0x10c(%rsp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq 0x50(%rsp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x4674
movl 0x10c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x10c(%rsp)
jmp 0x460e
jmp 0x4676
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x4f30
movq %rax, 0x48(%rsp)
jmp 0x4692
movq 0x48(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x4f60
movq %rax, 0x90(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x4fd0
testb $0x1, %al
jne 0x46cf
jmp 0x46e8
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x4f90
jmp 0x46e6
jmp 0x46e8
jmp 0x46ea
jmp 0x46ec
cmpl $-0x1, 0xa0(%rsp)
jne 0x446f
jmp 0x4a46
movl 0xa4(%rsp), %eax
cmpl 0x240(%rsp), %eax
jne 0x4766
leaq 0x731c(%rip), %rsi      # 0xba32
xorl %eax, %eax
movl $0x3, %edi
callq 0x2a50
jmp 0x4724
movl 0xa4(%rsp), %edi
movq 0x238(%rsp), %rdx
leaq 0xc0(%rsp), %rsi
leaq 0x1a8(%rsp), %rcx
leaq 0x10c(%rsp), %r8
callq 0x3a80
movl %eax, 0x44(%rsp)
jmp 0x4756
movl 0x44(%rsp), %eax
movl %eax, 0x8c(%rsp)
jmp 0x4a44
movl 0xa4(%rsp), %edx
leaq 0x72d4(%rip), %rsi      # 0xba48
xorl %eax, %eax
movl $0x3, %edi
callq 0x2a50
jmp 0x4782
movb $0x0, 0x8b(%rsp)
leaq 0x7cdbf(%rip), %rdi     # 0x81550
movq %rdi, 0x20(%rsp)
xorl %esi, %esi
movl %esi, 0x34(%rsp)
movl $0xff84, %edx           # imm = 0xFF84
movq %rdx, 0x28(%rsp)
callq 0x2150
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movl 0x34(%rsp), %ecx
movl 0xa4(%rsp), %edi
callq 0x20e0
movq %rax, 0x38(%rsp)
jmp 0x47cc
movq 0x38(%rsp), %rax
movl %eax, 0x22c(%rsp)
cmpl $0x0, 0x22c(%rsp)
jge 0x4813
callq 0x2040
cmpl $0xb, (%rax)
je 0x480e
movl 0xa4(%rsp), %edx
leaq 0x725d(%rip), %rsi      # 0xba57
xorl %edi, %edi
movb %dil, %al
callq 0x2a50
jmp 0x4806
movb $0x1, 0x8b(%rsp)
jmp 0x48e5
cmpl $0x0, 0x22c(%rsp)
jne 0x4846
movl 0xa4(%rsp), %edx
leaq 0x7244(%rip), %rsi      # 0xba6f
xorl %eax, %eax
movl $0x1, %edi
callq 0x2a50
jmp 0x4839
movb $0x1, 0x8b(%rsp)
jmp 0x48e3
movl 0x22c(%rsp), %edx
leaq 0x7231(%rip), %rsi      # 0xba85
leaq 0x7ccf5(%rip), %rcx     # 0x81550
xorl %eax, %eax
movl $0x3, %edi
callq 0x2a50
jmp 0x4869
movl 0x240(%rsp), %edi
movl 0xa4(%rsp), %esi
movslq 0x22c(%rsp), %rcx
movq 0x238(%rsp), %r8
leaq 0x7ccc2(%rip), %rdx     # 0x81550
callq 0x2ec0
movq %rax, 0x18(%rsp)
jmp 0x489a
movq 0x18(%rsp), %rax
movl %eax, 0x84(%rsp)
cmpl $0x0, 0x84(%rsp)
jge 0x48e1
leaq 0x71f2(%rip), %rsi      # 0xbaa9
xorl %edi, %edi
movb %dil, %al
callq 0x2a50
jmp 0x48c3
movl 0x84(%rsp), %eax
movl %eax, 0x244(%rsp)
movl $0x1, 0xac(%rsp)
jmp 0x4a89
jmp 0x48e3
jmp 0x48e5
testb $0x1, 0x8b(%rsp)
je 0x4a42
movl 0xa4(%rsp), %edx
leaq 0x71ca(%rip), %rsi      # 0xbacb
xorl %eax, %eax
movl $0x1, %edi
callq 0x2a50
jmp 0x490f
movl 0xa4(%rsp), %edi
callq 0x23e0
jmp 0x491d
movl 0xa4(%rsp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
xorq $-0x1, %rcx
movl 0xa4(%rsp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
andq 0x1a8(%rsp,%rax,8), %rcx
movq %rcx, 0x1a8(%rsp,%rax,8)
movl 0xa4(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jne 0x49da
jmp 0x4972
movl 0x10c(%rsp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq 0x1a8(%rsp,%rax,8), %rax
movq %rax, 0x10(%rsp)
movl 0x10c(%rsp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq 0x10(%rsp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x49d8
movl 0x10c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x10c(%rsp)
jmp 0x4972
jmp 0x49da
leaq 0xc0(%rsp), %rdi
leaq 0xa4(%rsp), %rsi
callq 0x4f30
movq %rax, 0x8(%rsp)
jmp 0x49f6
movq 0x8(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x4f60
movq %rax, 0x70(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x4fd0
testb $0x1, %al
jne 0x4a27
jmp 0x4a40
leaq 0xc0(%rsp), %rdi
leaq 0xa4(%rsp), %rsi
callq 0x4f90
jmp 0x4a3e
jmp 0x4a40
jmp 0x4a42
jmp 0x4a44
jmp 0x4a46
jmp 0x4a48
jmp 0x4a4a
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x43ac
jmp 0x4a62
movb 0xb3(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x421b
movl $0x0, 0x244(%rsp)
movl $0x1, 0xac(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x4ff0
movl 0x244(%rsp), %eax
addq $0x248, %rsp            # imm = 0x248
retq
movq 0xb8(%rsp), %rdi
callq 0x23a0
nopw %cs:(%rax,%rax)