Merge 06803f3b80
into e332ec93e9
This commit is contained in:
commit
0d9d219d2f
1 changed files with 30 additions and 2 deletions
32
shadowsocks/manager.py
Normal file → Executable file
32
shadowsocks/manager.py
Normal file → Executable file
|
@ -45,6 +45,8 @@ class Manager(object):
|
||||||
self._control_client_addr = None
|
self._control_client_addr = None
|
||||||
try:
|
try:
|
||||||
manager_address = config['manager_address']
|
manager_address = config['manager_address']
|
||||||
|
if hasattr(manager_address, 'decode'):
|
||||||
|
manager_address = manager_address.decode('utf-8')
|
||||||
if ':' in manager_address:
|
if ':' in manager_address:
|
||||||
addr = manager_address.rsplit(':', 1)
|
addr = manager_address.rsplit(':', 1)
|
||||||
addr = addr[0], int(addr[1])
|
addr = addr[0], int(addr[1])
|
||||||
|
@ -107,6 +109,14 @@ class Manager(object):
|
||||||
logging.error("server not exist at %s:%d" % (config['server'],
|
logging.error("server not exist at %s:%d" % (config['server'],
|
||||||
port))
|
port))
|
||||||
|
|
||||||
|
def list_port(self):
|
||||||
|
data_list = list(self._relays.keys())
|
||||||
|
# use compact JSON format (without space)
|
||||||
|
data = common.to_bytes(json.dumps(data_list,
|
||||||
|
separators=(',', ':')))
|
||||||
|
rv = b'ports: ' + data
|
||||||
|
return rv
|
||||||
|
|
||||||
def handle_event(self, sock, fd, event):
|
def handle_event(self, sock, fd, event):
|
||||||
if sock == self._control_socket and event == eventloop.POLL_IN:
|
if sock == self._control_socket and event == eventloop.POLL_IN:
|
||||||
data, self._control_client_addr = sock.recvfrom(BUF_SIZE)
|
data, self._control_client_addr = sock.recvfrom(BUF_SIZE)
|
||||||
|
@ -126,6 +136,9 @@ class Manager(object):
|
||||||
elif command == 'remove':
|
elif command == 'remove':
|
||||||
self.remove_port(a_config)
|
self.remove_port(a_config)
|
||||||
self._send_control_data(b'ok')
|
self._send_control_data(b'ok')
|
||||||
|
elif command == 'list':
|
||||||
|
msg = self.list_port()
|
||||||
|
self._send_control_data(msg)
|
||||||
elif command == 'ping':
|
elif command == 'ping':
|
||||||
self._send_control_data(b'pong')
|
self._send_control_data(b'pong')
|
||||||
else:
|
else:
|
||||||
|
@ -214,6 +227,7 @@ def test():
|
||||||
def run_server():
|
def run_server():
|
||||||
config = {
|
config = {
|
||||||
'server': '127.0.0.1',
|
'server': '127.0.0.1',
|
||||||
|
'server_port': 33379,
|
||||||
'local_port': 1081,
|
'local_port': 1081,
|
||||||
'port_password': {
|
'port_password': {
|
||||||
'8381': 'foobar1',
|
'8381': 'foobar1',
|
||||||
|
@ -236,7 +250,7 @@ def test():
|
||||||
cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
cli.connect(('127.0.0.1', 6001))
|
cli.connect(('127.0.0.1', 6001))
|
||||||
|
|
||||||
# test add and remove
|
# test list, add and remove
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
cli.send(b'add: {"server_port":7001, "password":"asdfadsfasdf"}')
|
cli.send(b'add: {"server_port":7001, "password":"asdfadsfasdf"}')
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
@ -244,6 +258,13 @@ def test():
|
||||||
data, addr = cli.recvfrom(1506)
|
data, addr = cli.recvfrom(1506)
|
||||||
assert b'ok' in data
|
assert b'ok' in data
|
||||||
|
|
||||||
|
cli.send(b'list')
|
||||||
|
time.sleep(1)
|
||||||
|
data, addr = cli.recvfrom(1506)
|
||||||
|
assert b'7001' in data
|
||||||
|
assert b'8381' in data
|
||||||
|
assert b'8382' in data
|
||||||
|
|
||||||
cli.send(b'remove: {"server_port":8381}')
|
cli.send(b'remove: {"server_port":8381}')
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
assert 8381 not in manager._relays
|
assert 8381 not in manager._relays
|
||||||
|
@ -251,8 +272,15 @@ def test():
|
||||||
assert b'ok' in data
|
assert b'ok' in data
|
||||||
logging.info('add and remove test passed')
|
logging.info('add and remove test passed')
|
||||||
|
|
||||||
|
cli.send(b'list')
|
||||||
|
time.sleep(1)
|
||||||
|
data, addr = cli.recvfrom(1506)
|
||||||
|
assert b'7001' in data
|
||||||
|
assert b'8381' not in data
|
||||||
|
assert b'8382' in data
|
||||||
|
|
||||||
# test statistics for TCP
|
# test statistics for TCP
|
||||||
header = common.pack_addr(b'google.com') + struct.pack('>H', 80)
|
header = common.pack_addr(b'baidu.com') + struct.pack('>H', 80)
|
||||||
data = cryptor.encrypt_all(b'asdfadsfasdf', 'aes-256-cfb',
|
data = cryptor.encrypt_all(b'asdfadsfasdf', 'aes-256-cfb',
|
||||||
header + b'GET /\r\n\r\n')
|
header + b'GET /\r\n\r\n')
|
||||||
tcp_cli = socket.socket()
|
tcp_cli = socket.socket()
|
||||||
|
|
Loading…
Add table
Reference in a new issue