-
Notifications
You must be signed in to change notification settings - Fork 2
/
u2nl.c
330 lines (277 loc) · 6.52 KB
/
u2nl.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* u2nl - universal tunnel
* Author: Christian Reitwiessner <christian@reitwiessner.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <netdb.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/netfilter_ipv4.h>
#define BUF_SIZE 2048
static char *packet_buffer = NULL;
static struct sockaddr_in proxy_addr;
static int forward(int from, int to)
{
int len = read(from, packet_buffer, BUF_SIZE);
if (len == 0) {
return -1;
} else if (len < 0) {
perror("read");
return -1;
}
if (write(to, packet_buffer, len) != len) {
perror("write");
return -1;
}
return 0;
}
static void forwardLoop(int csock, int ssock)
{
fd_set fds;
int maxfd = csock > ssock ? csock + 1 : ssock + 1;
packet_buffer = malloc(BUF_SIZE);
if (packet_buffer == NULL) {
perror("Out of memory");
return;
}
FD_ZERO(&fds);
FD_SET(csock, &fds);
FD_SET(ssock, &fds);
while (1) {
int err;
fd_set selfds;
memcpy(&selfds, &fds, sizeof(fds));
err = select(maxfd, &selfds, NULL, NULL, NULL);
if (err <= 0) {
perror("select");
return;
}
if (FD_ISSET(csock, &selfds)) {
if (forward(csock, ssock) != 0) {
exit(0);
}
}
if (FD_ISSET(ssock, &selfds)) {
if (forward(ssock, csock) != 0) {
exit(0);
}
}
}
}
static int connectHttps(int s, struct sockaddr_in *addr, int c)
{
char buf[512];
char *headerend = NULL;
int header_length;
int len = snprintf(buf, 512, "CONNECT %s:%d HTTP/1.0\r\n\r\n",
inet_ntoa(addr->sin_addr),
ntohs(addr->sin_port));
if (write(s, buf, len) != len) {
perror("Error writing to proxy");
return -1;
}
while (headerend == NULL) {
len = read(s, buf, 512);
if (len <= 0) {
perror("Error reading from proxy");
return -1;
}
headerend = strstr(buf, "\r\n\r\n");
}
header_length = headerend + 4 - buf;
if (header_length < len) {
if (write(c, headerend + 4,
len - header_length) != (len - header_length)) {
perror("Error writing to client");
return -1;
}
}
return 0;
}
static int createServerSocket(char *host, int port)
{
int sock;
struct hostent *he;
struct sockaddr_in addr;
int one = 1;
he = gethostbyname(host);
if (he == 0)
return -1;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
return -1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(char *) &one, sizeof(one)) < 0) {
close(sock);
return -1;
}
if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
close(sock);
return -1;
}
if (listen(sock, 4) != 0) {
close(sock);
return -1;
}
return sock;
}
static int getOrigDst(int fd, struct sockaddr_in *dst)
{
size_t sock_sz = sizeof(*dst);
memset(dst, 0, sizeof(*dst));
return getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, dst, &sock_sz);
}
static void newConnection(int csock)
{
struct sockaddr_in dst;
int ssock;
if (getOrigDst(csock, &dst) != 0) {
perror("Unable to get original destination of connection");
close(csock);
return;
}
printf("Handling connection to %s.\n",
inet_ntoa(dst.sin_addr));
ssock = socket(AF_INET, SOCK_STREAM, 0);
if (ssock < 0) {
perror("Unable to create new server socket");
close(csock);
return;
}
if (connect(ssock, (struct sockaddr *) &proxy_addr,
sizeof(proxy_addr)) != 0) {
perror("Unable to connect to proxy");
close(csock);
close(ssock);
return;
}
if (connectHttps(ssock, &dst, csock) != 0) {
close(csock);
close(ssock);
return;
}
forwardLoop(csock, ssock);
}
static int acceptLoop(int fd)
{
int err;
while (1) {
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
int csock;
csock = accept(fd, (struct sockaddr *) &addr, &addrlen);
if (csock < 0) {
perror("Error accepting connection");
return -1;
}
switch (fork()) {
case -1:
perror("Unable to fork");
return -1;
case 0:
switch (fork()) {
case -1:
perror("Unable to fork");
return -1;
case 0: /* grandchild */
close(fd);
newConnection(csock);
exit(1);
default: /* child */
exit(0);
}
default: /* parent */
wait();
close(csock);
break;
}
}
return err;
}
static int createProxyAddr(char *host, int port)
{
struct hostent *he;
he = gethostbyname(host);
if (he == 0)
return -1;
memset(&proxy_addr, 0, sizeof(proxy_addr));
proxy_addr.sin_family = AF_INET;
proxy_addr.sin_port = htons(port);
memcpy(&proxy_addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
return 0;
}
int main(int argc, char *argv[])
{
int err;
int fd;
char *listen_addr;
int listen_port;
if (argc == 4) {
listen_addr = "0.0.0.0";
listen_port = atoi(argv[3]);
} else if (argc == 5) {
listen_addr = argv[3];
listen_port = atoi(argv[4]);
} else {
printf("Usage: %s <proxy host> <proxy port> "
"[<listen address>] <listen port>\n"
"Tunnels all TCP connections redirected "
"by the local linux iptables firewall\n"
"to <listen port> via the http proxy "
"<proxy host>, which has to support the\n"
"HTTPS/SSL CONNECT command.\n"
"Use\n# iptables -t nat -A OUTPUT -p tcp "
"-d ! <proxy host> \\\n"
"\t-j REDIRECT --to-port <listen port>\n"
"or a similar command to configure "
"the firewall.\n",
argv[0]);
exit(1);
}
fd = createServerSocket(listen_addr, listen_port);
if (fd < 0) {
perror("Error opening server socket");
return fd;
}
if (createProxyAddr(argv[1], atoi(argv[2])) != 0) {
perror("Error resolving proxy address");
close(fd);
return -1;
}
err = acceptLoop(fd);
if (err < 0) {
perror("Error in accept loop");
close(fd);
return err;
}
close(fd);
return 0;
}