Winsock SOCKS 4 Server



  • Hi, ich versuche einen Socks Server zu schreiben, dieser funktioniert aber höchstens bei ganz einfachen Websiten nur mit Text, ansonsten lädt der Browser ewig.
    Also ist die Vermittlung in der while(true)-Schleife zwischen Z.77 und Z.110 wahrscheinlich falsch.

    #include <iostream>
    #include <windows.h>
    #include <winsock.h>
    #include <cstdio>
    
    using namespace std;
    
    #define BUF_SIZE 4096
    
    string int_to_str(int i)
    {
        char str[10] = {};
        sprintf(str,"%d",i);
        return str;
    }
    
    DWORD __stdcall socks_server(LPVOID lparam)
    {
        DWORD rc;
        SOCKET sock = *(SOCKET*)lparam;
        SOCKET t_sock;
        SOCKADDR_IN addr;
        int t_port;
        string t_ip = "";
        char* buf = new char[BUF_SIZE];
        char* t_buf = new char[BUF_SIZE];
        fd_set fdset;
    
        recv(sock,buf,9,0);
    
        cout << endl;
    
        for(int i=0;i<9;i++)
        {
            cout << (int)buf[i] << " ";
        }
    
        cout << endl;
    
        t_port = buf[2] * 256;
        t_port += buf[3];
    
        for(int i=4;i<8;i++)
        {
            int ippart = buf[i];
            if(ippart < 0) ippart = 256 + ippart;
            t_ip += int_to_str(ippart);
            if(i != 7) t_ip += ".";
        }
    
        cout << "Target: " << t_ip << ":" << t_port << endl;
    
        t_sock = socket(AF_INET,SOCK_STREAM,0);
    
        memset(&addr,0,sizeof(SOCKADDR_IN));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(t_port);
        addr.sin_addr.s_addr = inet_addr(t_ip.c_str());
    
        rc = connect(t_sock,(SOCKADDR*)&addr,sizeof(SOCKADDR));
    
        if(rc == SOCKET_ERROR)
        {
            printf("Fehler: connect zu target-host gescheitert, fehler code: %d\n",WSAGetLastError());
            return 1;
        }
        else
        {
            printf("Verbunden mit target_host ...\n");
        }
    
        buf[0] = 0;
        buf[1] = 90;
        send(sock,buf,8,0);  // SOCKS accepted
    
        while(true)
        {
            DWORD rc;
            memset(buf,0,BUF_SIZE);
            memset(t_buf,0,BUF_SIZE);
            FD_ZERO(&fdset);
            FD_SET(sock,&fdset);
            FD_SET(t_sock,&fdset);
            select(0,&fdset,0,0,0);
            if(FD_ISSET(sock,&fdset))
            {
                rc = recv(sock,buf,BUF_SIZE,0);
                if(rc <= 0)
                {
                    closesocket(sock);
                    closesocket(t_sock);
                    return 0;
                }
                cout << "\nrequest: " << buf << endl;
                send(t_sock,buf,BUF_SIZE,0);
            }
            if(FD_ISSET(t_sock,&fdset))
            {
                rc = recv(t_sock,t_buf,BUF_SIZE,0);
                if(rc <= 0)
                {
                    closesocket(sock);
                    closesocket(t_sock);
                    return 0;
                }
                cout << endl << "Antwort von target: " << t_buf << endl;
                send(sock,t_buf,BUF_SIZE,0);
            }
        }
    
        return 0;
    }
    
    int startWinsock(void)
    {
      WSADATA wsa;
      return WSAStartup(MAKEWORD(2,0),&wsa);
    }
    
    int main()
    {
        SOCKET acc;
        SOCKET* con;
        SOCKADDR_IN addr;
        DWORD rc;
        rc = startWinsock();
        if(rc!=0)
        {
            printf("Fehler: startWinsock, fehler code: %d\n",rc);
            return 1;
        }
        else
        {
            printf("Winsock gestartet!\n");
        }
    
        acc = socket(AF_INET,SOCK_STREAM,0);
        if(acc == INVALID_SOCKET)
        {
            printf("Fehler: Der Socket konnte nicht erstellt werden, fehler code: %d\n",WSAGetLastError());
            return 1;
        }
        else
        {
            printf("Socket erstellt!\n");
        }
    
        memset(&addr,0,sizeof(SOCKADDR_IN));
        addr.sin_family=AF_INET;
        addr.sin_port=htons(5000);
        addr.sin_addr.s_addr=ADDR_ANY;
        rc=bind(acc,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN));
        if(rc==SOCKET_ERROR)
        {
            printf("Fehler: bind, fehler code: %d\n",WSAGetLastError());
            return 1;
        }
        else
        {
          printf("Socket an port XXX gebunden\n");
        }
    
        rc = listen(acc,10);
        if(rc==SOCKET_ERROR)
        {
        printf("Fehler: listen, fehler code: %d\n",WSAGetLastError());
        return 1;
        }
        else
        {
          printf("acceptSocket ist im listen Modus....\n");
        }
    
        while(true)
        {
            con = new SOCKET();
            *con = accept(acc,NULL,NULL);
            if(*con == INVALID_SOCKET)
            {
                cout << "Fehler: accept, fehler code: " << WSAGetLastError() << endl;
                return 1;
            }
            else
            {
                cout << "Neue Verbindung wurde akzeptiert!" << endl;
                CreateThread(0,0,(LPTHREAD_START_ROUTINE)socks_server,con,0,0);
            }
        }
    
        return 0;
    }
    


  • Aha, interessant.



  • Einfach nur schrecklich. Und sowas nennt sich Programmierer. Rtf rfc


Log in to reply