-
Notifications
You must be signed in to change notification settings - Fork 0
/
toralize.c
89 lines (77 loc) · 2.17 KB
/
toralize.c
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
#include "toralize.h"
Req *request(const char *dstip, const int dstport) {
Req *req;
req = malloc(reqsize);
if (!req) {
perror("malloc");
exit(EXIT_FAILURE);
}
req->vn = 4; // SOCKS4
req->cd = 1; // CONNECT
req->dstport = htons(dstport); // Convert to network byte order
req->dstip = inet_addr(dstip); // Convert to network byte order
strncpy((char *)req->username, USERNAME, 7);
req->username[7] = '\0'; // Ensure null-termination
return req;
}
int main(int argc, char *argv[]) {
char *host;
int port, s;
struct sockaddr_in sock;
Req *req;
Res *res;
char buf[ressize];
int success;
if (argc < 3) {
fprintf(stderr, "Usage: %s <host> <port>\n", argv[0]);
return -1;
}
host = argv[1];
port = atoi(argv[2]);
// Create a socket
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("socket");
return -1;
}
// Setup the target address structure
sock.sin_family = AF_INET;
sock.sin_port = htons(PROXYPORT);
sock.sin_addr.s_addr = inet_addr(PROXY);
// Connect to the proxy server
if (connect(s, (struct sockaddr *)&sock, sizeof(sock)) < 0) {
perror("connect");
close(s);
return -1;
}
// Create and send the request
req = request(host, port);
printf("Requesting IP: %s, Port: %d\n", host, port);
printf("Request dstip: %s, dstport: %d\n", inet_ntoa(*(struct in_addr *)&req->dstip), ntohs(req->dstport));
if (write(s, req, reqsize) < 0) {
perror("write");
free(req);
close(s);
return -1;
}
free(req);
// Receive the response
memset(buf, 0, ressize);
if (read(s, buf, ressize) < 1) {
perror("read");
close(s);
return -1;
}
// Interpret the response
res = (Res *)buf;
printf("Response Code: %d\n", res->cd);
success = (res->cd == 90);
if (!success) {
fprintf(stderr, "Error unable to connect to proxy, error code: %d\n", res->cd);
close(s);
return -1;
}
printf("Successfully connected through the proxy to %s:%d\n", host, port);
close(s);
return 0;
}