-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyoutline.py
191 lines (159 loc) · 5.45 KB
/
pyoutline.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
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
import click
from getpass import getpass
from subprocess import Popen
from pathlib import Path
from traceback import format_exception
from sys import exit, platform, argv as sys_argv
from pyoutline_tools import (
get_free_port, OutlineKey
)
COLORS = [
'red','cyan','blue','green',
'white','yellow','magenta',
'bright_black','bright_red'
]
for color in COLORS:
# No problem with using exec function here
exec(f'{color} = lambda t: click.style(t, fg="{color}", bold=True)')
@click.group()
def cli():
pass
def safe_cli():
try:
cli()
except Exception as e:
e = ''.join(format_exception(
etype = None,
value = e,
tb = e.__traceback__
))
click.echo(red(e))
exit(1)
@cli.command()
@click.option(
'--outline-key', '-k', required=True, prompt=True,
help='Outline VPN access Key (ss://...), can be path to file with keys'
)
@click.option(
'--random-port', '-r', is_flag=True,
help='Will set random listener port, otherwise 53735'
)
@click.option(
'--port', '-p', type=click.IntRange(49152,65535),
help='Listener proxy port'
)
def to_ss(outline_key, random_port, port):
"""Will transform Outline Proxy Key/file with Keys to ShadowSocks"""
keys = Path(outline_key)
if keys.exists():
keys = open(keys).read().strip().split('\n')
keys = [key.strip() for key in keys]
else:
keys = [outline_key]
for key in keys:
try:
ok = OutlineKey(key)
ss = ok.shadowsocks(random_port=random_port, port=port)
click.echo(bright_black(ss))
except ValueError:
click.echo(red('Invalid Key specified!'))
@cli.command()
@click.option(
'--outline-key', '-k', required=True, prompt=True,
help='Outline VPN access Key (ss://...), can be path to file with keys'
)
@click.option(
'--offset', '-o', type=int, default=1,
help='Key file offset. We will start from this Key'
)
@click.option(
'--random-port', '-r', is_flag=True,
help='Will set random listener port, otherwise 25250'
)
@click.option(
'--port', '-p', type=click.IntRange(49152,65535),
help='Listener proxy port'
)
def client(outline_key, random_port, port, offset):
"""Will start Outline Proxy from Key / file with Keys"""
keys = Path(outline_key)
if keys.exists():
keys = open(keys).read().strip().split('\n')
keys = [key.strip() for key in keys]
else:
keys = [outline_key]
if offset < 1 or offset > len(keys):
click.echo(red('Invalid offset specified!'))
exit()
enter, next_ = blue('ENTER'), blue('NEXT')
if platform.startswith('win'):
ctrld, previ = magenta('CTRL+D & ENTER'), magenta('PREVIOUS')
else:
ctrld, previ = magenta('CTRL+D'), magenta('PREVIOUS')
ctrlc, exit_ = cyan('CTRL+C'), cyan('EXIT')
if len(keys) > 1:
click.echo(
f"""\n@ Press {enter} to select {next_} Key\n"""
f"""@ Press {ctrld} to select {previ} Key\n"""
f"""@ Press {ctrlc} to close connection and {exit_}"""
)
else:
click.echo(f"\n@ Press {ctrlc} to close connection and {exit_}")
replace_ss = False # If ss-local fails will be used sslocal
current_key_position = 0 if not offset else offset - 1
if random_port:
port = get_free_port()
while True:
if current_key_position + 1 > len(keys):
current_key_position -= 1
key_num = current_key_position + 1
key = keys[current_key_position]
ss_process = None
click.echo(yellow(f'\r\nTrying [#{key_num}] ({key[:32]}...)'))
try:
ok = OutlineKey(key)
if not ok.is_alive:
click.echo(red(f'Outline Key [#{key_num}] is offline or not valid.\n'))
continue
else:
click.echo(green(f'Outline Key [#{key_num}] is OK! Connecting...\n'))
ss = ok.shadowsocks(port=port)
try:
if replace_ss:
ss = ss.replace('ss-local', 'sslocal')
ss_process = Popen(ss.replace('"','').split(' '))
if len(keys) == 1:
ss_process.wait()
exit()
else:
code = getpass(prompt='')
if code in ('\x04', '\xa1'):
raise EOFError # Windows-only: CTRL-D+ENTER
ss_process.terminate()
print('\x1b[1A', end='') # This will move cursor up
current_key_position += 1; continue
except EOFError:
if current_key_position > 0:
current_key_position -= 1
ss_process.terminate()
continue
except KeyboardInterrupt:
ss_process.terminate()
print(); exit(0)
except ValueError:
click.echo(red('Invalid Key specified!'))
exit(1)
except FileNotFoundError:
if replace_ss:
click.echo(red('You should install ShadowSocks. See pypi.org/project/pyoutline'))
exit(1)
else:
click.echo(yellow('Can\'t find ss-local. Trying to use sslocal...'))
replace_ss = True
except Exception as e:
click.echo(red(e))
if ss_process:
ss_process.terminate()
exit(1)
if __name__ == '__main__':
safe_cli()