forked from yinghuocho/gsocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
43 lines (32 loc) · 1.21 KB
/
server.py
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
# socks5 frontend
import logging
from gevent import socket
from gevent.server import StreamServer
from gevent.pool import Pool
log = logging.getLogger(__name__)
class SocksServer(object):
def __init__(self, ip, port, relayfactory, timeout=30, maxclient=200):
self.ip = ip
self.port = port
self.timeout = timeout
self.relayfactory = relayfactory
self.pool = Pool(maxclient)
addrinfo = socket.getaddrinfo(ip, port, 0, socket.SOCK_STREAM, socket.SOL_TCP) # @UndefinedVariable
_, _, _, _, localaddr = addrinfo[0]
self.server = StreamServer(localaddr, self._handle, spawn=self.pool)
def _handle(self, sock, addr):
try:
sock.settimeout(self.timeout)
session = self.relayfactory.create_relay_session(sock, addr)
session.process()
except Exception, e:
log.error("[Exception][SocksServer]: %s" % str(e))
def stop(self):
return self.server.stop()
@property
def closed(self):
return self.server.closed
def start(self):
self.server.start()
def run(self):
self.server.serve_forever()