-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy-handler.c
230 lines (190 loc) · 5.61 KB
/
proxy-handler.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
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "proxy-client-handler.h"
#include "proxy-target-handler.h"
#include "proxy-utils.h"
#include "sockets-handler.h"
#include "proxy-handler.h"
#define BLOCKED_TLS_PORT "443"
void proxy_accept_client(int socket) {
pthread_attr_t attr;
int error;
client_state_t* state = (client_state_t*)calloc(1, sizeof(client_state_t));
if (state == NULL) {
perror("Cannot allocate state for connection");
goto error_malloc;
}
error = pthread_mutex_init(&state->lock, NULL);
if (error) {
proxy_error(error, "Cannot create mutex for client");
goto error_mutex;
}
error = pthread_cond_init(&state->notifier, NULL);
if (error) {
proxy_error(error, "Cannot create condition for client");
goto error_cond;
}
error = pthread_attr_init(&attr);
if (error) {
proxy_error(error, "Cannot create client thread attrs");
goto error_attr;
}
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
state->socket = socket;
http_parser_init(&state->parser, HTTP_REQUEST);
state->parser.data = state;
error = pthread_create(&state->thread, &attr, &client_thread, state);
if (error) {
proxy_error(error, "Cannot create client thread");
goto error_thread;
}
return;
error_thread:
pthread_attr_destroy(&attr);
error_attr:
pthread_cond_destroy(&state->notifier);
error_cond:
pthread_mutex_destroy(&state->lock);
error_mutex:
free(state);
error_malloc:
close(socket);
}
static int connect_target(char* host) {
struct addrinfo hints, *result;
char* split_pos = strrchr(host, ':');
int sock = -1;
char* hostname = host;
char* port = "http";
if (split_pos != NULL) {
hostname = (char*)malloc((size_t)(split_pos - host + 1));
memcpy(hostname, host, (size_t)(split_pos - host));
hostname[split_pos - host] = '\0';
port = host + (split_pos - host) + 1;
}
if (!strncmp(port, BLOCKED_TLS_PORT, sizeof(BLOCKED_TLS_PORT) - 1)) {
proxy_log("Ignore TLS connection to %s\n", host);
if (hostname != host)
free(hostname);
return -1;
}
proxy_log("Connecting to %s...", host);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int error = getaddrinfo(hostname, port, &hints, &result);
if (error) {
fprintf(stderr, "Cannot resolve %s: %s\n", host, gai_strerror(error));
if (hostname != host)
free(hostname);
return -1;
}
sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (sock < 0) {
perror("Cannot create target socket");
goto cleanup;
}
if (connect(sock, result->ai_addr, result->ai_addrlen) < 0) {
perror("Cannot connect to target");
close(sock);
sock = -1;
goto cleanup;
}
proxy_log("Connected to %s with socket %d", host, sock);
cleanup:
if (hostname != host)
free(hostname);
freeaddrinfo(result);
return sock;
}
bool proxy_establish_connection(client_state_t* state, char* host) {
pthread_attr_t attr;
int error;
state->target = (target_state_t*)calloc(1, sizeof(target_state_t));
if (state->target == NULL) {
perror("Cannot allocate target state");
return false;
}
error = pthread_mutex_init(&state->target->lock, NULL);
if (error) {
proxy_error(error, "Cannot create mutex for target");
goto error_mutex;
}
error = pthread_cond_init(&state->target->notifier, NULL);
if (error) {
proxy_error(error, "Cannot create condition for target");
goto error_cond;
}
error = pthread_attr_init(&attr);
if (error) {
proxy_error(error, "Cannot create target thread attrs");
goto error_attr;
}
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
http_parser_init(&state->target->parser, HTTP_RESPONSE);
state->target->parser.data = state->target;
state->target->cache = state->cache;
pstring_init(&state->target->outbuff);
pstring_replace(&state->target->outbuff, state->target_outbuff.str,
state->target_outbuff.len);
if ((state->target->socket = connect_target(host)) < 0)
goto error_socket;
error = pthread_create(&state->target->thread, &attr, &target_thread,
state->target);
if (error) {
proxy_error(error, "Cannot create target thread");
goto error_socket;
}
return true;
error_socket:
pstring_free(&state->target->outbuff);
pthread_attr_destroy(&attr);
error_attr:
pthread_cond_destroy(&state->target->notifier);
error_cond:
pthread_mutex_destroy(&state->target->lock);
error_mutex:
free(state->target);
state->target = NULL;
return false;
}
int send_pstring(int socket, pstring_t* buff) {
size_t offset = 0;
ssize_t result;
while ((result = send(socket, buff->str + offset, buff->len - offset, 0)) !=
buff->len - offset) {
if (result == -1) {
if (errno != EWOULDBLOCK) {
perror("Cannot send data to socket");
return -1;
}
pstring_substring(buff, offset);
return 1;
}
offset += result;
}
pstring_free(buff);
return 0;
}
char* build_header_string(pstring_t* key,
pstring_t* value,
size_t* result_len) {
size_t len = key->len + value->len + 4; // 4 is ": " and "\r\n"
char* output = (char*)malloc(len);
if (output == NULL)
return NULL;
memcpy(output, key->str, key->len);
output[key->len] = ':';
output[key->len + 1] = ' ';
memcpy(output + key->len + 2, value->str, value->len);
output[len - 2] = '\r';
output[len - 1] = '\n';
(*result_len) = len;
return output;
}