使用Tor C ++(套接字)连接到网站

时间:2017-12-23 17:43:48

标签: c++ sockets visual-studio-2017

我试图通过我的C ++应用程序中的Tor连接到网络。首先,我认为获取网站的HTML代码会很棒我下载了tor专家包,它运行正常(firefox是能够将它用作socks5代理) 现在这里是代码:

#include "stdafx.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#define MSG_NOSIGNAL 0


#include <iostream>
#pragma comment(lib, "Ws2_32.lib")
int main()
{

    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        std::cout << "WSAStartup failed.\n";
        system("pause");
        return 1;
}
    std::cout << " [*] Tor" << std::endl;
    std::cout << " [*] Connecting " << std::endl;

    SOCKET      Socket;
    SOCKADDR_IN SocketAddr;

    Socket = socket(AF_INET, SOCK_STREAM, 0);
    SocketAddr.sin_family = AF_INET;
    SocketAddr.sin_port = htons(9050);
    SocketAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    connect(Socket, (SOCKADDR*)&SocketAddr, sizeof(SOCKADDR_IN));

    char Req1[3] =
    {
        0x05, // SOCKS 5
        0x01, // One Authentication Method
        0x00  // No AUthentication
    };
    send(Socket, Req1, 3, MSG_NOSIGNAL);

    char Resp1[2];
    recv(Socket, Resp1, 2, 0);
    if (Resp1[1] != 0x00)
    {
        std::cout << " [*] Error Authenticating " << std::endl;
        return(-1); // Error
    }

    std::cout << " [*] Fetching... " << std::endl;
    char* Domain = "www.google.com";
    char  DomainLen = (char)strlen(Domain);
    short Port = htons(80);

    char TmpReq[4] = {
        0x05, // SOCKS5
        0x01, // CONNECT
        0x00, // RESERVED
        0x03, // DOMAIN
    };

    char* Req2 = new char[4 + 1 + DomainLen + 2];

    memcpy(Req2, TmpReq, 4);                // 5, 1, 0, 3
    memcpy(Req2 + 4, &DomainLen, 1);        // Domain Length
    memcpy(Req2 + 5, Domain, DomainLen);    // Domain
    memcpy(Req2 + 5 + DomainLen, &Port, 2); // Port

    send(Socket, (char*)Req2, 4 + 1 + DomainLen + 2, MSG_NOSIGNAL);

    delete[] Req2;

    char Resp2[10];
    recv(Socket, Resp2, 10, 0);
    if (Resp2[1] != 0x00)
    {
        std::cout << " [*] Error : " << Resp2[1] << std::endl;
        return(-1); // ERROR
    }

    std::cout << " [*] Connected " << std::endl;


    // Test with a HTTP GET Request
    std::cout << " [*] Testing with GET Request \n" << std::endl;
    char * request = "GET / HTTP/1.1\r\nHost: www.google.com\r\nCache-Control: no-cache\r\n\r\n\r\n";
    send(Socket, request, strlen(request), MSG_NOSIGNAL);
    char RecvBuffer[2048];
    int ndatalength;
    ndatalength = 1;

    while (ndatalength != 0) {
        if (ndatalength == -1)
        {
            std::cout << "error on connection" << std::endl;
            return 1;
        }
        ndatalength = recv(Socket, RecvBuffer, 2048, 0);

    }

    std::cout << RecvBuffer;

    return(0);

}

问题是,它不是打印页面的html代码,而是返回这种东西:program然后停止工作。如果我选​​择&#34; undefined&#34;在caracter设置选项而不是Unicode,我得到这个: enter image description here (这是总输出的一部分),之后exe停止工作。 这里有什么问题? 我在Windows 10 Pro Fall创建者更新时使用Visual Studio 2017 64位。

提前致谢

编辑:添加WSAstartup解决(几乎)问题: strange 这些人物是什么?它们是在html代码结束之后......

0 个答案:

没有答案