-
Notifications
You must be signed in to change notification settings - Fork 12
/
deploy_router_use_direct
executable file
·175 lines (139 loc) · 5.16 KB
/
deploy_router_use_direct
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
#!/bin/bash
# self="$(\curl -sS https://gitlab.com/zw963/deployment_bash/-/raw/v0.8.7/deploy_start.sh)" && eval "$self"
self="$(\cat /home/common/Project/deployment_bash/deploy_start.sh)" && eval "$self"
export_variable target=$1
export_variable arch=${router_arch-linux-arm32-v5}
echo "Set \$router_arch env to e.g. \`linux-arm32-v5' for deploy Xray into ASUS AC5300 router.
For the correct Arch name, check https://github.com/XTLS/Xray-core/releases assets.
Current arch using: ${arch}"
config_folder=./downloaded_binary/${arch}
config=${config_folder}/config.json
if [ ! -f "$config" ];then
echo "Please create ${config} or generate it use ./deploy_server root@some_ip before deploy to router."
exit 1
fi
assets=(geoip.dat geosite.dat geoip-only-cn-private.dat v2ray xray)
for i in "${assets[@]}"; do
if [ ! -f "${config_folder}/$i" ];then
echo "${config_folder}/$i is missing, predownload it use ./deploy_server root@some_ip before deploy to router."
exit 1
fi
done
echo 'Copy downloaded binary to router ...'
copy $config_folder downloaded_binary
if cat $config |grep -qs '"protocol": "vless"'; then
service_name=xray
else
service_name=v2ray
fi
if [ "$use_xtls" ]; then
export_variable service_name=xray
else
export_variable service_name=${service_name}
fi
echo "Set \$use_xtls=true if prefer deploy Xray instead of V2ray.
Current auto-detect: ${service_name}.
"
function postinstall () {
set -u
echo
echo 'Please waiting for 10 seconds to check google available ...'
sleep 10
check_google=$(curl -so /dev/null -w 'Google: %{http_code}\n' google.com)
if [[ "$check_google" =~ 301|200 ]]; then
echo "visit google successful, Your's ip is: $(curl http://ipecho.net/plain; echo)"
else
echo 'visit google failed, something was wrong!'
fi
}
copy router/opt/etc/apply_iptables_rule_redirect.sh /opt/etc/
copy router/opt/etc/clean_iptables_rule.sh /opt/etc
copy router/opt/etc/update_geodata.sh /opt/etc
copy router/opt/etc/restart_dnsmasq.sh /opt/etc
copy router/opt/etc/toggle_proxy.sh /opt/etc
copy router/opt/etc/check_google_use_proxy.sh /opt/etc
copy router/opt/etc/debug_v2ray.sh /opt/etc
deploy_start
set -eu
# --------------------------------------------------------------------------------
#
# Following script will be run on router, please change it to meet your's need.
#
# -------------------------------------------------------------------------------
#
# 如果第一次运行本脚本, 请务必初始化 entware 包管理系统.
# ssh 登陆路由器, 执行 entware-setup.sh, 选择 1'
if ! opkg --version; then
echo "Run \`opkg --version' failed."
echo 'Please initialise entware-ng first if you run this script the first time.'
echo "e.g. plugin your's usb disk, and run \`entware-setup.sh', select 1."
echo "Or run \`amtm' to initialize entware if you use a newer merlin firmware."
exit
fi
if [ ! -e /jffs/scripts/services-start ]; then
cat <<'HEREDOC' > /jffs/scripts/services-start
#!/bin/sh
RC='/opt/etc/init.d/rc.unslung'
i=30
until [ -x "$RC" ] ; do
i=$(($i-1))
if [ "$i" -lt 1 ] ; then
logger "Could not start Entware"
exit
fi
sleep 1
done
$RC start
HEREDOC
fi
cd downloaded_binary
if ! ./$service_name version &>/dev/null; then
echo "Invalid $service version/arch supported by current router, please download correct version."
exit
fi
set +e
chmod -x /opt/etc/init.d/S22v2ray && sh /opt/etc/init.d/S22v2ray stop
set -e
cp $service_name geosite.dat geoip-only-cn-private.dat /opt/sbin/
cp config.json /opt/etc
cat <<HEREDOC > /opt/etc/init.d/S22v2ray
#!/bin/sh
ENABLED=yes
PROCS=$service_name
ARGS="run -config /opt/etc/config.json"
PREARGS=""
DESC=\$PROCS
PATH=/opt/sbin:/opt/bin:/opt/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[ -z "\$(which \$PROCS)" ] && exit 0
. /opt/etc/init.d/rc.func
HEREDOC
# ----------------------------------------------------
#
# 下面执行一些脚本检查与替换。
#
# ---------------------------------------------------
cd /opt/etc
chmod +x apply_iptables_rule_redirect.sh \
clean_iptables_rule.sh \
enable_swap.sh \
update_geodata.sh \
restart_dnsmasq.sh \
toggle_proxy.sh \
/jffs/scripts/services-start \
check_google_use_proxy.sh \
debug_v2ray.sh
# ----------------------------------------------------
#
# 启动所需的脚本
#
# ---------------------------------------------------
# 每隔 3 分钟检测下所有的服务是否运行, 以及 iptables rule 是否失效.
add_service wan-start 'cru a run-services "*/3 * * * * /jffs/scripts/services-start"'
add_service wan-start 'cru a run-iptables "*/1 * * * * /opt/etc/apply_iptables_rule_redirect.sh"'
# 每个周日的 5: 25 升级一次 geosites 数据.
add_service wan-start 'cru a update_geodata "25 5 * * 0 /opt/etc/update_geodata.sh"'
/jffs/scripts/wan-start
# 如果 DHCP 重新分配 IP 地址时, 会清除 iptables rule, 此时重新应用 iptables
add_service dhcpc-event '/opt/etc/apply_iptables_rule_redirect.sh'
add_service services-start '[ -f /tmp/restart_dnsmasq_was_run_at ] || /opt/etc/restart_dnsmasq.sh'
/opt/etc/restart_dnsmasq.sh && echo "Congratulations, [0m[33mDeploy succssful[0m!"