-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.cc
403 lines (339 loc) · 10.6 KB
/
proxy.cc
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// std
#include <stdio.h>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <string>
// network
#include <arpa/inet.h>
#include <sys/socket.h>
// system
#include <signal.h>
#include <pthread.h>
#include <sys/epoll.h>
// common
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <memory>
#include <sched.h>
const int MAX_EPOLL_SIZE = 10000;
const int PACKET_BUFFER_SIZE = 1000000;
class BufferPool {
public:
char* buffer;
int capacity;
char* current;
char* start;
char* end;
char* buffer_end;
BufferPool(int);
BufferPool();
~BufferPool();
};
class Connection {
public:
struct sockaddr_in listen_address;
struct sockaddr_in target_address;
int listen_fd;
int accepted_fd;
BufferPool buffer_pool;
int listen_port;
char* upstream_ip;
int upstream_port;
int upstream_fd;
Connection() {};
Connection(int listen_fd, int accepted_fd, \
char* upstream_ip, int upstream_port, int upstream_fd):
listen_fd(listen_fd), accepted_fd(accepted_fd), \
upstream_ip(upstream_ip), upstream_port(upstream_port), upstream_fd(upstream_fd) {}
};
using ConnectionPtr = std::shared_ptr<Connection>;
class Upstream {
public:
int upstream_port;
char* upstream_ip;
Upstream(int upstream_port, char* upstream_ip): upstream_ip(upstream_ip), upstream_port(upstream_port) {};
Upstream() {};
~Upstream() {};
};
using UpstreamPtr = std::shared_ptr<Upstream>;
class Config {
public:
int listen_port;
char* upstream_ip;
int upstream_port;
Config(int listen_port, char* upstream_ip, int upstream_port):listen_port(listen_port), upstream_ip(upstream_ip), upstream_port(upstream_port) {};
};
class Worker {
private:
int epoll_fd;
std::unordered_map<int, ConnectionPtr> connection_mapping; //fd: connection
std::unordered_map<int, int> fd_mapping;
std::unordered_map<int, UpstreamPtr> listen_mapping; //listen fd : upstream
std::vector<Config>& configs;
public:
int cpu;
pthread_t pid;
Worker(std::vector<Config>& configs, int cpu): configs(configs), cpu(cpu) {};
~Worker() {};
int Serve();
int buildListener(Config&);
int onConnection(std::unordered_map<int, UpstreamPtr>::iterator& iter);
int onDataIn(int fd);
int onDataOut(int fd);
};
class Handler {
private:
std::vector<Config> configs;
std::vector<Worker> workers;
static int number;
public:
void Handle();
int addProxy(int listen_port, char* upstream_ip, int upstream_port);
int startWorkers(int worker_number);
static void* threadProcess(void * arg);
};
int Worker::onConnection(std::unordered_map<int, UpstreamPtr>::iterator& iter) {
UpstreamPtr upstream = iter->second;
int listen_fd = iter->first;
//accept
struct sockaddr_in _addr;
int socklen = sizeof(sockaddr_in);
int accept_fd = accept(listen_fd, (struct sockaddr *)&_addr, (socklen_t*) & socklen);
//Connection Upstream
struct sockaddr_in target_address;
target_address.sin_family = AF_INET;
target_address.sin_port = htons(upstream->upstream_port);
if (inet_pton(AF_INET, upstream->upstream_ip, &target_address.sin_addr) <= 0) { return -1; }
const int flag = 1;
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
if(setsockopt(client_fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) < 0) { return -1; }
if(connect(client_fd,(struct sockaddr*) &target_address, sizeof(struct sockaddr_in)) < 0) { return -1; }
//New Connection;
auto connection = std::make_shared<Connection>(listen_fd, accept_fd, upstream->upstream_ip, upstream->upstream_port, client_fd);
connection_mapping[accept_fd] = connection;
connection_mapping[client_fd] = connection;
fd_mapping[accept_fd] = client_fd;
fd_mapping[client_fd] = accept_fd;
int flags;
flags = fcntl(accept_fd, F_GETFL, 0);
fcntl(accept_fd, F_SETFL, flags | O_NONBLOCK);
flags = fcntl(client_fd, F_GETFL, 0);
fcntl(client_fd, F_SETFL, flags | O_NONBLOCK);
// event
struct epoll_event ev;
ev.data.fd = accept_fd;
ev.events = EPOLLIN;
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, accept_fd, &ev);
ev.data.fd = client_fd;
ev.events = EPOLLIN;
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
printf("OPEN: %d <--> %d\n", accept_fd, client_fd);
return 0;
}
int Worker::buildListener(Config& config) {
struct sockaddr_in listen_address;
listen_address.sin_family = AF_INET;
listen_address.sin_port = htons(config.listen_port);
listen_address.sin_addr.s_addr = htonl(INADDR_ANY);
// if (src_host == NULL) {
// src_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// } else {
// if (inet_pton(AF_INET, src_host, &src_addr.sin_addr) <= 0) { return false; }
// }
// listen
const int flag = 1;
int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEPORT, &flag, sizeof(flag)) < 0) { return -1; }
if (bind(listen_fd,(const struct sockaddr*)&(listen_address), sizeof(struct sockaddr_in)) < 0) { return -1; }
if (listen(listen_fd, SOMAXCONN) < 0) { return -1; }
listen_mapping[listen_fd] = std::make_shared<Upstream>(config.upstream_port, config.upstream_ip);;
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = listen_fd;
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &ev);
return 0;
}
int Worker::onDataIn(int in_fd) {
ConnectionPtr connection = connection_mapping[in_fd];
if (connection->buffer_pool.current != connection->buffer_pool.start) {
//buffer is in used.
return 0;
}
// recv
int ret = recv(in_fd, connection->buffer_pool.start, PACKET_BUFFER_SIZE, 0);
// std::cout << "read size " << ret << std::endl;
if (ret <= 0) {
if (errno == EAGAIN) {
return 0;
} else {
std::cout << "Errno: " << errno << std::endl;
close(in_fd);
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, in_fd, 0);
return -1;
}
}
connection->buffer_pool.end = connection->buffer_pool.start + ret;
int out_fd = fd_mapping[in_fd];
bool finished = true;
while(connection->buffer_pool.current < connection->buffer_pool.end) {
ret = send(out_fd, connection->buffer_pool.current, connection->buffer_pool.end-connection->buffer_pool.current, 0);
// std::cout << "onDataIn: sent " << ret << std::endl;
if (ret <= 0) {
if (errno == EAGAIN) {
finished = false;
break;
} else {
close(out_fd);
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, out_fd, 0);
return -1;
}
} else {
// Normal
connection->buffer_pool.current += ret;
}
}
// Drain out
if (finished) {
connection->buffer_pool.current = connection->buffer_pool.start;
connection->buffer_pool.end = connection->buffer_pool.start;
} else {
// start watching EPOLLOUT, because we want to know when kernel writing buffer is ready....
struct epoll_event ev;
ev.data.fd = out_fd;
ev.events = EPOLLIN|EPOLLOUT;
epoll_ctl(epoll_fd, EPOLL_CTL_MOD, out_fd, &ev);
}
}
int Worker::onDataOut(int out_fd) {
bool finished = true;
int ret;
ConnectionPtr connection = connection_mapping[out_fd];
while(connection->buffer_pool.current < connection->buffer_pool.end) {
ret = send(out_fd, connection->buffer_pool.current, connection->buffer_pool.end-connection->buffer_pool.current, 0);
// std::cout << "onDataOut: sent " << ret << std::endl;
if (ret < 0) {
if (errno == EAGAIN) {
finished = false;
break;
} else {
return -1;
}
} else {
// Normal
connection->buffer_pool.current += ret;
}
}
if (!finished) {
// keep watch EPOLLOUT
} else {
struct epoll_event ev;
ev.data.fd = out_fd;
ev.events = EPOLLIN; //Remove EPOLLOUT
epoll_ctl(epoll_fd, EPOLL_CTL_MOD, out_fd, &ev);
if (ret < 0) {
// FIXME: error, should close
}
connection->buffer_pool.current = connection->buffer_pool.start;
connection->buffer_pool.end = connection->buffer_pool.start;
}
return 0;
}
int Worker::Serve() {
epoll_fd = epoll_create(MAX_EPOLL_SIZE); // epoll_create(int size); size is no longer used
for (auto& config: configs) {
buildListener(config);
}
struct epoll_event events[MAX_EPOLL_SIZE];
int count = 0;
//serving
while(true) {
count = epoll_wait(epoll_fd, events, MAX_EPOLL_SIZE, -1);
if (count < 0) {
if (errno == EINTR) {
continue;
} else {
printf("epoll error\n");
return -1;
}
}
for (int i = 0; i < count; i ++) {
// std::cout << events[i].data.fd << std::endl;
auto it = listen_mapping.find(events[i].data.fd);
if (it != listen_mapping.end()) {
onConnection(it);
} else {
if (events[i].events & EPOLLOUT) {
onDataOut(events[i].data.fd);
}
if (events[i].events & EPOLLIN) {
onDataIn(events[i].data.fd);
}
}
}
}
}
BufferPool::BufferPool() {
capacity = PACKET_BUFFER_SIZE;
start = new char[capacity];
current = start;
end = start;
buffer_end = start + capacity;
}
BufferPool::BufferPool(int capacity) : capacity(capacity) {
start = new char[capacity];
current = start;
end = start;
buffer_end = start + capacity;
}
BufferPool::~BufferPool() {
delete []buffer;
buffer = nullptr;
}
void Handler::Handle() {
while(true) {
}
}
int Handler::addProxy(int listen_port, char* upstream_ip, int upstream_port) {
Config config(listen_port, upstream_ip, upstream_port);
configs.push_back(config);
return 0;
}
void* Handler::threadProcess(void * arg) {
Worker* worker = (Worker *) arg;
cpu_set_t cpuset;
//the CPU we whant to use
int cpu = worker->cpu;
CPU_ZERO(&cpuset); //clears the cpuset
CPU_SET( cpu , &cpuset); //set CPU 2 on cpuset
/*
* cpu affinity for the calling thread
* first parameter is the pid, 0 = calling thread
* second parameter is the size of your cpuset
* third param is the cpuset in which your thread will be
* placed. Each bit represents a CPU
*/
sched_setaffinity(0, sizeof(cpuset), &cpuset);
worker->Serve();
return NULL;
}
int Handler::startWorkers(int worker_number) {
for (int i = 0; i < worker_number; ++i) {
Worker* worker = new Worker(configs, i);
pthread_create(&(worker->pid), NULL, &threadProcess, worker); // remember to pthread_join
// workers.push_back(worker);
}
}
int main(int argc, char const *argv[])
{
Handler handler;
handler.addProxy(8080, "127.0.0.1", 80);
handler.addProxy(8000, "127.0.0.1", 81);
handler.startWorkers(20);
handler.Handle();
return 0;
}