Python port of ShadowsocksR
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.1 KiB

#!/usr/bin/python
# -*- coding: utf-8 -*-
10 years ago
# Copyright (c) 2014 clowwindy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import absolute_import, division, print_function, \
with_statement
11 years ago
import sys
import os
import signal
import select
import time
from subprocess import Popen, PIPE
10 years ago
sys.path.insert(0, './')
if sys.argv[-3] == '-c':
client_config = sys.argv[-1]
server_config = sys.argv[-2]
elif sys.argv[-2] == '-c':
client_config = sys.argv[-1]
server_config = sys.argv[-1]
else:
raise Exception('usage: test.py -c server_conf [client_conf]')
p1 = Popen(['python', 'shadowsocks/server.py', '-c', server_config],
stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
p2 = Popen(['python', 'shadowsocks/local.py', '-c', client_config],
stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
11 years ago
p3 = None
try:
11 years ago
local_ready = False
server_ready = False
fdset = [p1.stdout, p2.stdout, p1.stderr, p2.stderr]
while True:
r, w, e = select.select(fdset, [], fdset)
if e:
break
10 years ago
for fd in r:
line = fd.readline()
if bytes != str:
line = str(line, 'utf8')
sys.stdout.write(line)
11 years ago
if line.find('starting local') >= 0:
local_ready = True
if line.find('starting server') >= 0:
server_ready = True
if local_ready and server_ready and p3 is None:
time.sleep(1)
10 years ago
break
11 years ago
10 years ago
p3 = Popen(['curl', 'http://www.example.com/', '-v', '-L',
'--socks5-hostname', '127.0.0.1:1081'], close_fds=True)
if p3 is not None:
r = p3.wait()
10 years ago
if r != 0:
sys.exit(r)
else:
sys.exit(1)
p4 = Popen(['socksify', 'dig', '@8.8.8.8', 'www.google.com'],
close_fds=True)
if p4 is not None:
r = p4.wait()
if r != 0:
sys.exit(r)
else:
sys.exit(1)
print('test passed')
11 years ago
finally:
for p in [p1, p2]:
try:
os.kill(p.pid, signal.SIGTERM)
except OSError:
pass