-
Notifications
You must be signed in to change notification settings - Fork 104
/
corkscrew.c
295 lines (260 loc) · 5.95 KB
/
corkscrew.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
#include "config.h"
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#if HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#if __STDC__
# ifndef NOPROTOS
# define PARAMS(args) args
# endif
#endif
#ifndef PARAMS
# define PARAMS(args) ()
#endif
char *base64_encodei PARAMS((char *in));
void usage PARAMS((void));
int sock_connect PARAMS((const char *hname, int port));
int main PARAMS((int argc, char *argv[]));
#define BUFSIZE 4096
/*
char linefeed[] = "\x0A\x0D\x0A\x0D";
*/
char linefeed[] = "\r\n\r\n"; /* it is better and tested with oops & squid */
/*
** base64.c
** Copyright (C) 2001 Tamas SZERB <toma@rulez.org>
*/
const static char base64[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/* the output will be allocated automagically */
#ifdef ANSI_FUNC
char *base64_encode (char *in)
#else
char * base64_encode (in)
char *in;
#endif
{
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;
}
#ifdef ANSI_FUNC
void usage (void)
#else
void usage ()
#endif
{
printf("corkscrew %s (agroman@agroman.net)\n\n", VERSION);
printf("usage: corkscrew <proxyhost> <proxyport> <desthost> <destport> [authfile]\n");
}
#ifdef ANSI_FUNC
int sock_connect (const char *hname, int port)
#else
int sock_connect (hname, port)
const char *hname;
int port;
#endif
{
int fd;
struct sockaddr_in addr;
struct hostent *hent;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1)
return -1;
hent = gethostbyname(hname);
if (hent == NULL)
addr.sin_addr.s_addr = inet_addr(hname);
else
memcpy(&addr.sin_addr, hent->h_addr, hent->h_length);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)))
return -1;
return fd;
}
#ifdef ANSI_FUNC
int main (int argc, char *argv[])
#else
int main (argc, argv)
int argc;
char *argv[];
#endif
{
#ifdef ANSI_FUNC
char uri[BUFSIZE] = "", buffer[BUFSIZE] = "", version[BUFSIZE] = "", descr[BUFSIZE] = "";
#else
char uri[BUFSIZE], buffer[BUFSIZE], version[BUFSIZE], descr[BUFSIZE];
#endif
char *host = NULL, *desthost = NULL, *destport = NULL;
char *up = NULL, line[4096];
int port, sent, setup, code, csock;
fd_set rfd, sfd;
struct timeval tv;
ssize_t len;
FILE *fp;
port = 80;
if (argc == 5 || argc == 6) {
if (argc == 5) {
host = argv[1];
port = atoi(argv[2]);
desthost = argv[3];
destport = argv[4];
up = getenv("CORKSCREW_AUTH");
}
if (argc == 6) {
host = argv[1];
port = atoi(argv[2]);
desthost = argv[3];
destport = argv[4];
fp = fopen(argv[5], "r");
if (fp == NULL) {
fprintf(stderr, "Error opening %s: %s\n", argv[5], strerror(errno));
exit(-1);
} else {
if (!fscanf(fp, "%4095s", line)) {
fprintf(stderr, "Error reading auth file's content\n");
exit(-1);
}
up = line;
fclose(fp);
}
}
} else {
usage();
exit(-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.0", sizeof(uri) - strlen(uri) - 1);
if (up != NULL) {
strncat(uri, "\nProxy-Authorization: Basic ", sizeof(uri) - strlen(uri) - 1);
strncat(uri, base64_encode(up), sizeof(uri) - strlen(uri) - 1);
}
strncat(uri, linefeed, sizeof(uri) - strlen(uri) - 1);
csock = sock_connect(host, port);
if(csock == -1) {
fprintf(stderr, "Couldn't establish connection to proxy: %s\n", strerror(errno));
exit(-1);
}
sent = 0;
setup = 0;
for(;;) {
FD_ZERO(&sfd);
FD_ZERO(&rfd);
if ((setup == 0) && (sent == 0)) {
FD_SET(csock, &sfd);
}
FD_SET(csock, &rfd);
FD_SET(0, &rfd);
tv.tv_sec = 5;
tv.tv_usec = 0;
if(select(csock+1,&rfd,&sfd,NULL,&tv) == -1) break;
/* there's probably a better way to do this */
if (setup == 0) {
if (FD_ISSET(csock, &rfd)) {
len = read(csock, buffer, sizeof(buffer));
if (len<=0)
break;
else {
memset(descr, 0, sizeof(descr));
sscanf(buffer,"%s%d%[^\n]",version,&code,descr);
if ((strncmp(version,"HTTP/",5) == 0) && (code >= 200) && (code < 300))
setup = 1;
else {
if ((strncmp(version,"HTTP/",5) == 0) && (code >= 407)) {
}
fprintf(stderr, "Proxy could not open connection to %s: %s\n", desthost, descr);
exit(-1);
}
}
}
if (FD_ISSET(csock, &sfd) && (sent == 0)) {
len = write(csock, uri, strlen(uri));
if (len<=0)
break;
else
sent = 1;
}
} else {
if (FD_ISSET(csock, &rfd)) {
len = read(csock, buffer, sizeof(buffer));
if (len<=0) break;
len = write(1, buffer, len);
if (len<=0) break;
}
if (FD_ISSET(0, &rfd)) {
len = read(0, buffer, sizeof(buffer));
if (len<=0) break;
len = write(csock, buffer, len);
if (len<=0) break;
}
}
}
exit(0);
}