1 changed files with 68 additions and 0 deletions
@ -0,0 +1,68 @@ |
|||
import glob |
|||
import logging |
|||
import os |
|||
import unittest |
|||
import subprocess |
|||
import requests |
|||
import dotenv |
|||
import time |
|||
import itertools |
|||
|
|||
dotenv.load_dotenv() |
|||
|
|||
|
|||
def get_ip(version=None): |
|||
"""provider = 'https://%swtfismyip.com/text' |
|||
prefix = { |
|||
None: '', |
|||
4: 'ipv4.', |
|||
6: 'ipv6.' |
|||
}[version]""" |
|||
provider = 'https://%sipify.org' |
|||
prefix = { |
|||
None: 'api64.', |
|||
4: 'api4.', |
|||
6: 'api6.' |
|||
}[version] |
|||
result = requests.Session().get(provider % prefix).text.strip() |
|||
return result |
|||
|
|||
|
|||
def get_tool_path(): |
|||
default = glob.glob(os.path.join(os.path.dirname(__file__), '..', 'target', '*', 'tun2proxy')) |
|||
default = default[0] if len(default) > 0 else 'tun2proxy' |
|||
return os.environ.get('TOOL_PATH', default) |
|||
|
|||
|
|||
def start_process(*args): |
|||
pass |
|||
|
|||
|
|||
class Tun2ProxyTest(unittest.TestCase): |
|||
@staticmethod |
|||
def _test(ip_version, dns, proxy_var): |
|||
ip_noproxy = get_ip(ip_version) |
|||
print(ip_noproxy) |
|||
additional = ['-6'] if ip_version == 6 else [] |
|||
p = subprocess.Popen([get_tool_path(), "--proxy", os.getenv(proxy_var), '--setup', '-v', 'trace', '--dns', dns, *additional]) |
|||
try: |
|||
time.sleep(1) |
|||
ip_withproxy = get_ip(ip_version) |
|||
print(ip_withproxy) |
|||
|
|||
assert ip_noproxy != ip_withproxy |
|||
except Exception as e: |
|||
raise e |
|||
finally: |
|||
p.terminate() |
|||
p.wait() |
|||
|
|||
@classmethod |
|||
def add_tests(cls): |
|||
for ip_version, dns, proxy_var in itertools.product([None, 4, 6], ['virtual', 'over-tcp'], ['SOCKS5_PROXY', 'HTTP_PROXY']): |
|||
setattr(cls, 'test_ipv%s_dns%s_proxy%s' % (ip_version, dns, proxy_var), lambda self: cls._test(ip_version, dns, proxy_var)) |
|||
|
|||
|
|||
if __name__ == '__main__': |
|||
Tun2ProxyTest.add_tests() |
|||
unittest.main() |
|||
Loading…
Reference in new issue