-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit
executable file
·178 lines (142 loc) · 4.21 KB
/
init
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
#!/bin/sh
# todo : 断线重连功能未加入
# 极客模式自动打开及极客密码的显示未处理
PACKAGE_NAME="com.modouwifi.app-ssh-reverse"
PWD="$(cd $(dirname $0) && pwd)"
TITLE="远程协助"
PROGRAM_NAME="app-ssh-reverse"
CUSTOM_BIN="/system/apps/tp/bin/custom"
APPS_CONFIG_DIR="/data/conf"
LAUNCHER_CONFIG_DIR="$APPS_CONFIG_DIR/launcher/conf.d"
LAUNCHER_CONFIG_FILE="$LAUNCHER_CONFIG_DIR/$PROGRAM_NAME.conf"
ICON="./res/icon.png"
PRESSED_ICON="./res/icon_pressed.png"
INSTALL_BIN="/usr/bin/install"
REMOVE="/bin/rm -f"
SERVER="help.imoguyun.com"
KEY_FILE=$PWD/id_rsa
KEY_FILE_OP=$PWD/id_rsa_op
SSH="$PWD/ssh"
SSH_OP="/usr/bin/ssh"
USER="help"
SSH_CONFIG="$PWD/ssh_config"
FLAG="-F $SSH_CONFIG"
EXPERT_PASS=`nvram_get 2860 Password`
if [ -f "/usr/bin/ssh" ] ; then
SSH=$SSH_OP
KEY_FILE=$KEY_FILE_OP
FLAG="-y"
EXPERT_PASS=`uci get system.account.Password`
fi
usage() {
echo "ERROR: action missing"
echo "syntax: $0 <start|stop|restart|status|config|install|uninstall>"
echo "example: $0 start"
}
remote_exec()
{
exec_result=`$SSH -i $KEY_FILE $FLAG $USER@$SERVER "$1"`
}
get_port()
{
local netstat="busy"
local portlist=1
while [ "$netstat" != "" -o "$portlist" != "0" ]
do
# 可以考虑用/dev/random实现,且是1024以上即可
local RAN_NUM=`date +%s`
local t1=`expr $RAN_NUM % 50000`
port_result=`expr $t1 + 1030`
remote_exec "netstat -an | grep $port_result"
netstat=$exec_result
remote_exec "cat port.list | grep $port_result | wc -l"
portlist=$exec_result
done
}
start_service()
{
# 处理ssh端口
get_port;
local port=$port_result
$SSH -i $KEY_FILE $FLAG -g -N -f -R *:$port:*:22 $USER@$SERVER
json4sh.sh set $PWD/conf/data.json current_port value $port
# 处理80端口
get_port;
local port_web=$port_result
$SSH -i $KEY_FILE $FLAG -g -N -f -R *:$port_web:*:80 $USER@$SERVER
json4sh.sh set $PWD/conf/data.json current_port_web value $port_web
json4sh.sh set $PWD/conf/data.json current_pass value $EXPERT_PASS
appInfo.sh set_status "$PACKAGE_NAME" ISRUNNING
}
run() {
local status=`appInfo.sh get_status "$PACKAGE_NAME"`
local new_start=0
# 如果服务还没有开启,则开启服务
if [ "$status" == "NOTRUNNING" ] ; then
new_start=1
# 添加转菊花效果 -- 阿耀 2014-10-29
updateconf $PWD/loading.conf -t State -v 0
loadingapp $PWD/loading.conf &
start_service
fi
# 显示当前服务端口
local port=`json4sh.sh get $PWD/conf/data.json current_port value`
local port_web=`json4sh.sh get $PWD/conf/data.json current_port_web value`
local pass=`json4sh.sh get $PWD/conf/data.json current_pass value`
messagebox 远程协助 "远程协助已开启,服务序号
$port:$port_web。
当前极客密码:$pass
协助结束后请退出。" 1 结束 "$PWD/init stop" &
# 停止转菊花
if [ "$new_start" == "1" ] ; then
updateconf $PWD/loading.conf -t State -v 2
fi
}
start() {
start_service
}
stop() {
killall ssh
json4sh.sh set $PWD/conf/data.json current_port value 空
json4sh.sh set $PWD/conf/data.json current_pass value 空
appInfo.sh set_status "$PACKAGE_NAME" NOTRUNNING
}
config() {
echo "{" > "$PROGRAM_NAME.conf"
echo "\"name\" : \"$TITLE\"," >> "$PROGRAM_NAME.conf"
echo "\"icon\" : \"$PWD/$ICON\"," >> "$PROGRAM_NAME.conf"
echo "\"iconPressed\" : \"$PWD/$PRESSED_ICON\"," >> "$PROGRAM_NAME.conf"
echo "\"exec\" : \"$PWD/init run\"," >> "$PROGRAM_NAME.conf"
echo "\"msgNum\" : 4" >> "$PROGRAM_NAME.conf"
echo "}" >> "$PROGRAM_NAME.conf"
$INSTALL_BIN -d $LAUNCHER_CONFIG_DIR
$INSTALL_BIN "$PROGRAM_NAME.conf" "$LAUNCHER_CONFIG_FILE"
$REMOVE "$PROGRAM_NAME.conf"
chmod 600 $PWD/id_rsa
chmod 600 $PWD/id_rsa_op
}
uninstall() {
$REMOVE "$LAUNCHER_CONFIG_FILE"
}
# main
if [ $# -lt 1 ]; then
usage
exit 255
fi
case "$1" in
"start" )
start;;
"stop" )
stop;;
"restart" )
start
stop;;
"run" )
run;;
"install" )
config;;
"uninstall" )
uninstall;;
* )
usage ;;
esac