-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnormal.c
More file actions
172 lines (143 loc) · 4.01 KB
/
Copy pathnormal.c
File metadata and controls
172 lines (143 loc) · 4.01 KB
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
/*
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, version 2 of the License only.
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, see <http://www.gnu.org/licenses/>.
*/
#include "transockproxy.h"
int writeall(int fd, const char* buffer, int size) {
int pos = 0;
int rc;
do {
rc = write(fd, buffer + pos, size - pos);
if (rc <= 0) return rc;
pos += rc;
} while (pos < size);
return size;
}
void* connthread(void* arg) {
const struct Mapping* map;
struct sockaddr_in addr;
int ssock = 0;
int csock = (long)arg;
char* buffer;
int rc;
char* tok;
char* host = NULL;
fd_set fds;
fd_set rfds;
int tries = 0;
running++;
buffer = (char*)malloc(BUFFERSIZE);
tryagain:
rc = recv(csock, buffer, BUFFERSIZE-1, MSG_PEEK);
if (rc == 0) {
warn("[%d] Client closed connection before sending headers.\n", csock);
goto end;
}
if (rc < 0) {
warn("[%d] Error reading request headers: %m\n", csock);
goto end;
}
buffer[rc] = 0;
/* Technically it's case-insensitive, but these should cover almost all cases. */
if (!strstr(buffer, "Host: ") && !strstr(buffer, "host: ")) {
usleep(10000);
tries++;
if (tries > 1000) {
warn("[%d] Waiting for Host: header timed out.\n", csock);
goto end;
}
if (rc >= BUFFERSIZE-1) {
warn("[%d] Host: header not found within first %d bytes.\n", csock, rc);
goto end;
}
goto tryagain;
}
strtok(buffer, "\r\n");
/* First token should be "GET /foo HTTP/1.1" so we can skip that safely. */
while ((tok = strtok(NULL, "\r\n"))) {
if (!strncasecmp(tok, "Host: ", 6)) {
host = strdup(tok + 6);
break;
}
}
if (host == NULL) {
warn("[%d] Client did not provide Host: header.\n", csock);
goto end;
}
/* Establish SOCKS connection. */
map = findserver(host);
ssock = socket(AF_INET, SOCK_STREAM, 0);
switch (map->proto) {
case INVALID:
goto end;
case DIRECT:
if (!directconnect(csock, ssock, host, "80", map)) goto end;
break;
case SOCKS4:
rc = connect(ssock, (struct sockaddr*)&addr, sizeof(addr));
if (rc) { warn("[%d] Could not connect to server: %m\n", csock); return 0; }
if (!socks4connect(csock, ssock, host, 80)) goto end;
break;
case SOCKS4A:
rc = connect(ssock, (struct sockaddr*)&addr, sizeof(addr));
if (rc) { warn("[%d] Could not connect to server: %m\n", csock); return 0; }
if (!socks4aconnect(csock, ssock, host, 80)) goto end;
break;
case SOCKS5:
rc = connect(ssock, (struct sockaddr*)&addr, sizeof(addr));
if (rc) { warn("[%d] Could not connect to server: %m\n", csock); return 0; }
if (!socks5connect(csock, ssock, host, 80)) goto end;
break;
}
/* Relay data. */
FD_ZERO(&fds);
FD_SET(csock, &fds);
FD_SET(ssock, &fds);
do {
rfds = fds;
rc = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
if (rc < 0) break;
if (FD_ISSET(csock, &rfds)) {
rc = read(csock, buffer, BUFFERSIZE);
if (rc == 0) break;
if (rc < 0) {
warn("[%d] Error reading from client: %m\n", csock);
break;
}
rc = writeall(ssock, buffer, rc);
if (rc <= 0) {
warn("[%d] Error sending to server: %m\n", csock);
break;
}
}
if (FD_ISSET(ssock, &rfds)) {
rc = read(ssock, buffer, BUFFERSIZE);
if (rc == 0) break;
if (rc <= 0) {
warn("[%d] Error reading from server: %m\n", csock);
break;
}
rc = writeall(csock, buffer, rc);
if (rc <= 0) {
warn("[%d] Error sending to client: %m\n", csock);
break;
}
}
} while (exitflag == 0);
end:
if (csock > 0) close(csock);
if (ssock > 0) close(ssock);
if (host) free(host);
if (buffer) free(buffer);
running--;
log("[%d] Relay finished.\n", csock);
return NULL;
}
/* EOF */