1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct sockaddr_in server;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed. Error Code: %d", WSAGetLastError());
return 1;
}
// Create a socket.
if ((ConnectSocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
printf("Could not create socket: %d", WSAGetLastError());
}
// Setup address and port of Proxy 1.
server.sin_addr.s_addr = inet_addr("192.111.134.10");
server.sin_family = AF_INET;
server.sin_port = htons(4145);
// Connect to Proxy 1.
if (connect(ConnectSocket, (struct sockaddr *) &server, sizeof(server)) < 0) {
puts("connect error");
return 1;
}
puts("connection to proxy 1");
// Send SOCKS5 initialization request (no authentication).
char initRequest[3] = {0x05, 0x01, 0x00};
if (send(ConnectSocket, initRequest, 3, 0) < 0) {
puts("Send failed");
return 1;
}
// Receive SOCKS5 initialization response.
char initResponse[2];
if (recv(ConnectSocket, initResponse, 2, 0) < 0) {
puts("recv failed");
return 1;
}
// Check SOCKS5 initialization response.
if (initResponse[0] != 0x05 || initResponse[1] != 0x00) {
puts("SOCKS5 initialization failed");
return 1;
}
// Send SOCKS5 connection request (Proxy 1 to Proxy 2).
char connectRequest1[10] = {0x05, 0x01, 0x00, 0x01};
unsigned long proxy2_ip = inet_addr("70.166.167.55");
unsigned short proxy2_port = htons(57745);
memcpy(connectRequest1 + 4, &proxy2_ip, 4);
memcpy(connectRequest1 + 8, &proxy2_port, 2);
if (send(ConnectSocket, connectRequest1, 10, 0) < 0) {
puts("Send failed");
return 1;
}
// Receive SOCKS5 connection response (from Proxy 1).
char connectResponse1[10];
if (recv(ConnectSocket, connectResponse1, 10, 0) < 0) {
puts("recv failed");
return 1;
}
// Check SOCKS5 connection response (from Proxy 1).
if (connectResponse1[0] != 0x05 || connectResponse1[1] != 0x00) {
puts("SOCKS5 connection failed (Proxy 1)");
return 1;
}
puts("successfully linked\n");
// Now we are connected and can send the final HTTP request through the connection.
// Send the HTTP request through the proxy.
char message[100] = "GET /ip HTTP/1.1\r\nHost: 3.227.25.103\r\nConnection: close\r\n\r\n";
if (send(ConnectSocket, message, strlen(message), 0) < 0) {
puts("Send failed");
return 1;
}
// Receive and print the response.
char server_reply[2000];
if (recv(ConnectSocket, server_reply, 2000, 0) < 0) {
puts("recv failed");
}
puts(server_reply);
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
| |