-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
start
executable file
·238 lines (190 loc) · 4.98 KB
/
start
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/sh
path_to_script () {
TARGET="$1"
BASENAME="$(basename "$TARGET")"
(
cd -P "$(dirname "$TARGET")"
if [ -h "$BASENAME" ]
then
path_to_script "$(readlink "$BASENAME")"
else
echo "$PWD"
fi
)
}
goto_script_path() {
PATH_TO_SCRIPT="$(path_to_script "$0")"
# Fails to start if the install path contains spaces, so it must be quoted.
cd "$PATH_TO_SCRIPT"
}
goto_script_path
if ! type python > /dev/null 2>&1; then
PYTHON="python3"
elif python -V 2>&1| grep -q "Python 3" ;then
PYTHON="python"
else
PYTHON="python3"
fi
if [ -f code/version.txt ]; then
VERSION=`cat code/version.txt`
else
VERSION="default"
fi
if [ ! -f "code/$VERSION/launcher/start.py" ]; then
VERSION="default"
fi
REAL_VERSION=`cat code/$VERSION/version.txt`
echo "Start version:$REAL_VERSION"
# launch xx-net service by ignore hungup signal
launchWithNoHungup() {
nohup ${PYTHON} code/${VERSION}/launcher/start.py $@>/dev/null 2>&1 &
}
# launch xx-net service by hungup signal
launchWithHungup() {
${PYTHON} code/${VERSION}/launcher/start.py $@
}
# check command avalibility
has_command() {
type $1 > /dev/null 2>&1
}
openwrt_setup_env()
{
echo "It is OpenWrt."
#TODO: Setup
# check mount usb disk
# install python, python-openssl, ipset to usb device
# opkg --dest usb install python python-pyopenssl
# setup dns
# set auto start
# echo "It is openwrt."
}
# Install Packages
# get operating system name
os_name=`uname -s`
if [ $os_name = 'Linux' ]; then
PYTHON3="python3/bin/python3"
if test -f "$PYTHON3"; then
echo "Linux using embedded python."
PYTHON="$PYTHON3"
else
if ! ${PYTHON} -c 'import brotli' 2> /dev/null; then
echo 'Installing brotli for your system... Please type in your password if requested'
if has_command zypper; then
# openSUSE
sudo zypper in -y python3-brotli
elif has_command apt-get; then
# Debian or Debian-like
sudo apt-get install -y python3-brotli
elif has_command dnf; then
# Fedora
sudo dnf install -y python3-brotli
elif has_command yum; then
# RedHat
sudo yum install -y pybrotli
elif has_command pacman; then
# ArchLinux
sudo pacman -S --noconfirm python-brotli
elif has_command opkg; then
# OpenWrt
echo "install python"
opkg install python3 python3-brotli
fi
pip install brotli
fi
fi
elif [ $os_name = 'Darwin' ]; then
PYTHON3="python3/bin/python3"
if test -f "$PYTHON3"; then
echo "Darwin using embedded python."
PYTHON="$PYTHON3"
else
if ! ${PYTHON} -c 'import PyObjCTools, AppKit, SystemConfiguration' 2> /dev/null; then
sudo pip3 install -U PyObjC Pillow
fi
fi
if [ ! -d "data/launcher" ]; then
# remove the mask for download file warning
xattr -c -r *
fi
fi
help()
{
echo "
USAGE:
start -h
show help.
start
-allow_remote
enable remote connect.
-no_mess_system
Don't mess the system, include not install CA to browser, not add shortcut to desktop automatically.
-f
Run in foreground, in Mac will run in background as default.
-hungup
start with nohup to run in background.
start set_iptables [interface]
set iptables for transparent proxy.
interface The network interface which will be redirected
Default is br-lan
start unset_iptables [interface]
"
exit 0
}
set_iptables(){
if [ "$1" = '' ]; then
INF="br-lan"
else
INF=$1
fi
echo "set interface $INF"
# TODO: check if rule exist
iptables -t nat -N REDSOCKS
iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN
iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN
iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 8086
iptables -t nat -A PREROUTING --in-interface $INF -p tcp -j REDSOCKS
exit 0
}
unset_iptables(){
if [ "$1" = '' ]; then
INF="br-lan"
else
INF=$1
fi
iptables -t nat -D PREROUTING --in-interface $INF -p tcp -j REDSOCKS
iptables -t nat -X REDSOCKS
}
ARGS=$@
if [ $os_name = 'Darwin' ] ; then
HANGUP='1'
else
HANGUP='0'
fi
while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;;
-help) help;shift 1;;
--help) help;shift 1;;
-\?) help;shift 1;;
-allow_remote)shift 1;;
-no_mess_system)shift 1;;
-f)HANGUP='0'; shift 1;;
-hungup)HANGUP='1';shift 1;;
set_iptables) set_iptables $2;;
unset_iptables) unset_iptables $2;;
*) echo "unknown option $1."; shift 1;;
esac
done
# Start Application
if [ $HANGUP = '1' ]; then
echo "Run XX-Net in background."
launchWithNoHungup $ARGS
else
launchWithHungup $ARGS
fi