-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsocks5server.cc
223 lines (209 loc) · 6.84 KB
/
socks5server.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
//
// socks5server.cc
//
// Created by 吴怡生 on 13/03/2018.
// Copyright © 2018 yeshen.org. All rights reserved.
//
#include <stdlib.h>
#include <iostream>
#include <inttypes.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cerrno>
#include <sys/types.h>
#include <stdio.h>
#include "socks5server.h"
#include "epollwapper.h"
#include "socks5.h"
#include "common.h"
using namespace std;
Socks5Server::Socks5Server(){
this->wapper_ = new EpollWapper();
};
void Socks5Server::forever(uint16_t port){
cout << "start" << endl;
int fd;
this->wapper_->create(this);
this->wapper_->watchTcp(port,&fd,NULL);
this->wapper_->wait();
};
void Socks5Server::onAccept(int fd,int type,void* data){
struct Section* section = new Section();
section->fd = fd;
section->tcp = ((type & TYPE_TCP) != 0);
section->status = SS_INIT;
section->watch_fd = -1;
((struct EpollData*)data)->ptr = section;
}
void Socks5Server::onDestory(void* ptr,int fd){
if(!ptr || fd < 0)return;
this->wapper_->remove(fd);
struct Section* section = (struct Section*)ptr;
if(section->watch_fd > 0){
this->wapper_->remove(section->watch_fd);
}
struct UDPPair* forwardlist = section->udp;
while(forwardlist){
this->wapper_->remove(forwardlist->in_fd);
this->wapper_->remove(forwardlist->out_fd);
forwardlist = forwardlist->next;
}
delete section;
};
void Socks5Server::onWritable(void* ptr,int fd){
if(!ptr || fd < 0)return;
struct Section* section = (struct Section*)ptr;
if(section->status==SS_REDY){
if(section->tcp){
int to_fd = fd == section->fd ? section->watch_fd : section->fd;
struct LinkBuff* next = fd == section->fd ? section->insidebuff : section->outsidebuff;
cout << "to:" << to_fd << ",from:" << fd;
while(next){
if(send(fd,next->buf,next->size,0) == -1){
if(errno==EAGAIN){
if(fd==section->fd){
section->insidebuff = next;
}else{
section->outsidebuff = next;
}
return;
}
}else{
struct LinkBuff* tmp = next->next;
free(next->buf);
delete next;
next=tmp;
}
}
}else{
}
}
}
void Socks5Server::onData(struct LinkBuff* buf,void* ptr,int fd){
cout << "onData:" << fd << ptr << endl;
if(!ptr || !buf ) return;
struct Section* section = (struct Section*)ptr;
switch(section->status){
case SS_INIT:{
cout << "SS_INIT" << fd << endl;
struct RequestVersion* version = new RequestVersion();
if(!Socks5::decodeRequestVersion(version,buf)){
perror("decode error");
this->wapper_->remove(fd);
LinkBuffUtil::dofree(buf);
return;
}
if(version->VER != ACCEPT_VERSION){
this->wapper_->remove(fd);
LinkBuffUtil::dofree(buf);
return;
}
uint8_t* response = Socks5::createReplytVersion();
if(send(fd,response,2,0) == -1){
perror("send error");
LinkBuffUtil::dofree(buf);
return;
}
section->status = SS_REQUEST;
LinkBuffUtil::dofree(buf);
break;
}
case SS_REQUEST:{
cout << "SS_REQUEST" << fd << endl;
struct Request* request = new Request();
if(!Socks5::decodeRequest(request,buf)){
perror("decode error");
this->wapper_->remove(fd);
LinkBuffUtil::dofree(buf);
return;
}
if(request->CMD == '\x01'){
uint8_t result = this->wapper_->createTcp(
request->DST_ADDR,
request->DST_PORT,
&(section->watch_fd),
section);
uint8_t* response = Socks5::createReplies(result);
if(send(fd,response,10,0) == -1){
perror("send error");
//double check
//*(this->wapper_)->remove(fd);
LinkBuffUtil::dofree(buf);
return;
}
section->status = SS_REDY;
}else if(request->CMD == '\x03'){
// uint8_t result = this->wapper_->watchUdp ...
}
LinkBuffUtil::dofree(buf);
break;
}
case SS_REDY:{
cout << "SS_REDY" << fd << endl;
if(section->tcp){
if(section->watch_fd < 0) throw "connection not ready!";
int to_fd = fd == section->fd ? section->watch_fd : section->fd;
//forward
struct LinkBuff* cache = fd==section->fd ? section->insidebuff : section->outsidebuff;
struct LinkBuff* next;
if(cache){
next = cache;
while(next->next){
next=next->next;
}
next->next=buf;
next = cache;
}else{
next = buf;
}
cout << "try send:" << fd << ","<< next->size << endl;
while(next){
if(send(to_fd,next->buf,next->size,0) == -1){
if(errno==EAGAIN){
if(fd==section->fd){
section->insidebuff = next;
}else{
section->outsidebuff = next;
}
return;
}else{
perror("send error");
return;
}
}else{
struct LinkBuff* tmp = next->next;
free(next->buf);
delete next;
next=tmp;
}
}
}else{
cout << "udp: forwarding" << endl;
//udp associate
//TODO
}
break;
}
case SS_CLOSE:{
this->onDestory(ptr,fd);
break;
}
default:{
break;
}
}
};
static volatile int keepRunning = 1;
void sig_handler(int sig){
if(sig == SIGINT){
keepRunning = 0;
}
}
int main(int argc, const char * argv[]) {
signal(SIGINT,sig_handler);
Socks5Server* server=new Socks5Server();
server->forever((uint16_t)1080);
}