-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh-tunnel
executable file
·181 lines (146 loc) · 4.36 KB
/
ssh-tunnel
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
#!/bin/bash
source conf
echo "set the apt proxy:"
echo "======================================================"
echo "echo '"
echo "Acquire::http::proxy \"http://$BASIC_PROXY/\";
Acquire::https::proxy \"https://$BASIC_PROXY/\";
Acquire::ftp::proxy \"ftp://$BASIC_PROXY/\";"
echo "' >> /etc/apt/apt.conf"
echo "
======================================================
"
IPSOCKS='
iptables -t nat -A POSTROUTING -s 172.18.0.0/16 ! -o docker0 -j MASQUERADE
iptables -t nat -N REDSOCKS || true
iptables -t nat -F 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 -p tcp -m tcp --dport 6667 -j DNAT --to-destination 127.0.0.1:7777
iptables -t nat -A REDSOCKS -p udp -m udp --dport 6667 -j DNAT --to-destination 127.0.0.1:9999
iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 6666
iptables -t nat -A REDSOCKS -p udp -j REDIRECT --to-ports 8888
iptables -t nat -A OUTPUT -p tcp -j REDSOCKS
iptables -t nat -A PREROUTING -p tcp -j REDSOCKS'
SOCKS_CONF="
redsocks {
local_ip = 127.0.0.1;
local_port = 7777;
ip = $INTERNAL_PROXY_IP;
port = 1080;
type = socks5;
}
redudp {
local_ip = 127.0.0.1;
local_port = 9999;
ip = $INTERNAL_PROXY_IP;
port = 1080;
}
redsocks {
local_ip = 127.0.0.1;
local_port = 6666;
ip = 127.0.0.1;
port = 10800;
// ip = $INTERNAL_PROXY_IP;
// port = 1080;
type = socks5;
}
redudp {
local_ip = 127.0.0.1;
local_port = 8888;
ip = 127.0.0.1;
port = 10800;
// ip = $INTERNAL_PROXY_IP;
// port = 1080;
}"
HOST="
Host $TUNNEL_HOSTNAME
User $TUNNEL_USER
IdentityFile ~/.ssh/rd_rsa.jf"
DIR_SSH="$HOME/.ssh"
if [ ! -d $DIR_SSH ]; then
echo "create "$DIR_SSH
mkdir $DIR_SSH
fi
if [ ! -f $DIR_SSH/rd_rsa.jf ]; then
echo "create "$DIR_SSH/rd_rsa.jf
echo -e "$RSA" > $DIR_SSH/rd_rsa.jf
fi
chmod 0600 $DIR_SSH/rd_rsa.jf
if [ ! -f $DIR_SSH/config ]; then
echo "touch $DIR_SSH/config"
touch $DIR_SSH/config
fi
IS_HOST=`grep $TUNNEL_HOSTNAME $DIR_SSH/config`
if [ -z "$IS_HOST" ]; then
echo "config $TUNNEL_HOSTNAME"
echo -e "$HOST" >> $DIR_SSH/config
fi
if [ ! -f ipsocks ]; then
echo "generate ipsocks"
echo -e "$IPSOCKS" > ipsocks
sudo chmod a+x ipsocks
fi
RELEASE=""
if [ -f /etc/lsb-release ]; then
RELEASE=`grep DISTRIB_ID /etc/lsb-release |cut -d = -f2`
fi
echo "The release dist is "$RELEASE
if [ x$RELEASE == "xUbuntu" ]; then
SERVICE=`dpkg -l redsocks | grep redsocks`
if [ $? != 0 ]; then
echo $SERVICE
sudo apt-get install redsocks
fi
SERVICE=`dpkg -l autossh | grep autossh`
if [ $? != 0 ]; then
echo $SERVICE
sudo apt-get install autossh
fi
fi
CentOS_RELEASE=`cat /etc/redhat-release |cut -d " " -f1`
if [ x$CentOS_RELEASE == "xCentOS" ]; then
echo "The release dist is "$CentOS_RELEASE
if [ ! -f "redsocks" ]; then
git clone http://github.com/darkk/redsocks.git
fi
cd redsocks
make
cd -
SERVICE=`yum list autossh | grep autossh |cut -d " " -f1`
if [ $? != 0 ]; then
echo $SERVICE
sudo yum install autossh
fi
fi
if [ x$RELEASE == "xUbuntu" ]; then
IS_PROXY=`grep 10800 /etc/redsocks.conf`
if [ -z "$IS_PROXY" ]; then
echo "config redsocks conf"
echo -e "$SOCKS_CONF" |sudo tee -a /etc/redsocks.conf
fi
fi
sudo ./ipsocks
sudo iptables -t nat -L REDSOCKS
# settings for docker
sudo sysctl -w net.ipv4.conf.default.route_localnet=1
sudo sysctl -w net.ipv4.conf.all.route_localnet=1
grep "route_localnet" /etc/sysctl.conf
if [ $? -ne 0 ]; then
sudo tee -a /etc/sysctl.conf >/dev/null << EOF
net.ipv4.conf.all.route_localnet = 1
net.ipv4.conf.default.route_localnet = 1
EOF
fi
if [ x$RELEASE == "xUbuntu" ]; then
sudo service redsocks restart
fi
# ssh -N -L 127.0.0.1:10800:TUNNEL_HOSTNAME:1080 $TUNNEL_HOSTNAME -vvv
PID=`ps -ef |grep autossh.*$TUNNEL_HOSTNAME |grep -v grep |awk '{print $2}'`
echo "kill the process $PID"
sudo kill -9 $PID
autossh -f -M 20000 -N -L 127.0.0.1:10800:TUNNEL_HOSTNAME:1080 $TUNNEL_HOSTNAME