-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorkwin.c
280 lines (238 loc) · 6.53 KB
/
corkwin.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <conio.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
char *base64_encode(char *in);
void usage(void);
int __cdecl main(int argc, char **argv);
#define BUFSIZE 4096
char linefeed[] = "\r\n\r\n";
/*
** base64.c
** Copyright (C) 2001 Tamas SZERB <[email protected]>
*/
const static char base64[64] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/* the output will be allocated automagically */
char *base64_encode(char *in) {
char *src, *end;
char *buf, *ret;
unsigned int tmp;
int i, len;
len = strlen(in);
if (!in)
return NULL;
else
len = strlen(in);
end = in + len;
buf = malloc(4 * ((len + 2) / 3) + 1);
if (!buf)
return NULL;
ret = buf;
for (src = in; src < end - 3;) {
tmp = *src++ << 24;
tmp |= *src++ << 16;
tmp |= *src++ << 8;
*buf++ = base64[tmp >> 26];
tmp <<= 6;
*buf++ = base64[tmp >> 26];
tmp <<= 6;
*buf++ = base64[tmp >> 26];
tmp <<= 6;
*buf++ = base64[tmp >> 26];
}
tmp = 0;
for (i = 0; src < end; i++)
tmp |= *src++ << (24 - 8 * i);
switch (i) {
case 3:
*buf++ = base64[tmp >> 26];
tmp <<= 6;
*buf++ = base64[tmp >> 26];
tmp <<= 6;
*buf++ = base64[tmp >> 26];
tmp <<= 6;
*buf++ = base64[tmp >> 26];
break;
case 2:
*buf++ = base64[tmp >> 26];
tmp <<= 6;
*buf++ = base64[tmp >> 26];
tmp <<= 6;
*buf++ = base64[tmp >> 26];
*buf++ = '=';
break;
case 1:
*buf++ = base64[tmp >> 26];
tmp <<= 6;
*buf++ = base64[tmp >> 26];
*buf++ = '=';
*buf++ = '=';
break;
}
*buf = 0;
return ret;
}
void usage(void) {
printf("Usage: corkwin <proxyhost> <proxyport> <desthost> <destport>\n");
}
/*
* connect.c -- Make socket connection using SOCKS4/5 and HTTP tunnel.
*
* Copyright (c) 2000-2006, 2012 Shun-ichi Goto
* Copyright (c) 2002, J. Grant (English Corrections)
*/
int stdindatalen(void) {
DWORD len = 0;
struct stat st;
fstat(0, &st);
if (st.st_mode & _S_IFIFO) {
/* in case of PIPE */
if (!PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE), NULL, 0, NULL, &len,
NULL)) {
if (GetLastError() == ERROR_BROKEN_PIPE) {
/* PIPE source is closed */
/* read() will detects EOF */
len = 1;
} else {
fprintf(stderr, "PeekNamedPipe() failed, errno=%d\n", GetLastError());
}
}
} else if (st.st_mode & _S_IFREG) {
/* in case of regular file (redirected) */
len = 1; /* always data ready */
} else if (_kbhit()) {
/* in case of console */
len = 1;
}
return len;
}
int __cdecl main(int argc, char **argv) {
WSADATA wsaData;
int iResult;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL, *ptr = NULL, hints;
char uri[BUFSIZE] = "", buffer[BUFSIZE] = "", version[BUFSIZE] = "",
descr[BUFSIZE] = "";
char *host = NULL, *port = NULL, *desthost = NULL, *destport = NULL;
int code;
fd_set rfd;
struct timeval tv;
ssize_t len;
FILE *fp;
if (argc == 5) {
host = argv[1];
port = argv[2];
desthost = argv[3];
destport = argv[4];
} else {
usage();
return -1;
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
fprintf(stderr, "WSAStartup failed: %d\n", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(host, port, &hints, &result);
if (iResult != 0) {
fprintf(stderr, "getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
fprintf(stderr, "socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
fprintf(stderr, "Unable to connect to server!\n");
WSACleanup();
return 1;
}
strncpy(uri, "CONNECT ", sizeof(uri));
strncat(uri, desthost, sizeof(uri) - strlen(uri) - 1);
strncat(uri, ":", sizeof(uri) - strlen(uri) - 1);
strncat(uri, destport, sizeof(uri) - strlen(uri) - 1);
strncat(uri, " HTTP/1.1", sizeof(uri) - strlen(uri) - 1);
strncat(uri, linefeed, sizeof(uri) - strlen(uri) - 1);
iResult = send(ConnectSocket, uri, (int)strlen(uri), 0);
if (iResult == SOCKET_ERROR) {
fprintf(stderr, "send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
iResult = recv(ConnectSocket, buffer, sizeof(buffer), 0);
if (iResult > 0) {
memset(descr, 0, sizeof(descr));
sscanf(buffer, "%s%d%[^\n]", version, &code, descr);
if ((strncmp(version, "HTTP/", 5) != 0) ||
!((code >= 200) && (code < 300))) {
fprintf(stderr, "Proxy could not open connection to %s: %s\n", desthost,
descr);
return -1;
}
} else if (iResult == 0)
fprintf(stderr, "Connection closed\n");
else
fprintf(stderr, "recv failed with error: %d\n", WSAGetLastError());
_setmode(_fileno(stdin), O_BINARY);
_setmode(_fileno(stdout), O_BINARY);
while (1) {
FD_ZERO(&rfd);
FD_SET(ConnectSocket, &rfd);
tv.tv_sec = 0;
tv.tv_usec = 10 * 1000;
if (select(ConnectSocket + 1, &rfd, NULL, NULL, &tv) == -1) {
fprintf(stderr, "select failed with error %d\n", WSAGetLastError());
break;
};
if (0 < stdindatalen()) {
FD_SET(_fileno(stdin), &rfd); /* fake */
}
if (FD_ISSET(ConnectSocket, &rfd)) {
len = recv(ConnectSocket, buffer, (int)sizeof(buffer), 0);
if (len <= 0)
break;
len = write(_fileno(stdout), buffer, (int)len);
if (len <= 0)
break;
}
if (FD_ISSET(_fileno(stdin), &rfd)) {
len = read(_fileno(stdin), buffer, (int)sizeof(buffer));
if (len <= 0)
break;
len = send(ConnectSocket, buffer, (int)len, 0);
if (len <= 0)
break;
}
}
// cleanup
closesocket(ConnectSocket);
WSACleanup();
return 0;
}