-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse-tunnel-client.sh
151 lines (120 loc) · 4.36 KB
/
reverse-tunnel-client.sh
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
#!/bin/bash
# Reverse tunnel client setup
# One-stop script to install and set up https://github.com/snsinfu/reverse-tunnel
# Copyright (c) 2021 Dovi Cowan (Fully Networking UK) - dovi@fullynetworking.co.uk
# MIT License
# Run "# bash <(curl -s https://raw.githubusercontent.com/dcowan-london/public-scripts/main/reverse-tunnel-client.sh)"
# on a new Debian 10 server with a public IP address to set up
cd
echo "REVERSE TUNNEL CLIENT SETUP"
echo "One-stop script to install and set up https://github.com/snsinfu/reverse-tunnel"
echo ""
echo "Reverse Tunnel Copyright (c) 2018, 2021 snsinfu MIT License"
echo "Setup script Copyright (c) 2021 Dovi Cowan MIT License"
echo ""
echo "Permission is hereby granted, free of charge, to any person obtaining a copy"
echo "of this software and associated documentation files (the "Software"), to deal"
echo "in the Software without restriction, including without limitation the rights"
echo "to use, copy, modify, merge, publish, distribute, sublicense, and/or sell"
echo "copies of the Software, and to permit persons to whom the Software is"
echo "furnished to do so, subject to the following conditions:"
echo ""
echo "The above copyright notice and this permission notice shall be included in all"
echo "copies or substantial portions of the Software."
echo ""
echo "THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR"
echo "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,"
echo "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE"
echo "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER"
echo "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,"
echo "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE"
echo "SOFTWARE."
echo
if [ ! -d "reverse-tunnel/" ]; then
# Get prerequisites
apt update
apt upgrade -y
apt install git build-essential golang -y
# Get reverse-tunnel
git clone https://github.com/snsinfu/reverse-tunnel
cd reverse-tunnel
# Build reverse-tunnel
make
# Get server IP
SERVER_IP=0
IP_CORRECT=0
read -p "Enter server IP address/FQDN: " SERVER_IP
while [[ $IP_CORRECT -ne 1 ]]; do
read -n 1 -p "Please confirm server IP address [$SERVER_IP] [y/n] "
echo
if [[ ${REPLY} == "n" ]]; then
read -p "Enter correct server IP address/FQDN: " SERVER_IP
elif [[ ${REPLY} == "y" ]]; then
IP_CORRECT=1
else
echo "You must enter y or n!"
fi
done;
# Get client key
read -p "Paste the Client Key genarated by the server: " KEY
# Write client config
tee rtun.yml >/dev/null <<EOF
gateway_url: ws://$(echo $SERVER_IP):10000
auth_key: $(echo $KEY)
forwards:
EOF
# Create systemd service
read -N 1 -p "Create service? [y/n] " SERVICE
if [[ $SERVICE == "y" ]]; then
# Write systemd service
tee /etc/systemd/system/rtun-client.service >/dev/null <<EOF
[Unit]
Description=RTUN Client
[Service]
User=root
WorkingDirectory=/root/reverse-tunnel
ExecStartPre=/bin/sleep 5
ExecStart=/root/reverse-tunnel/rtun
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# Enable systemd service
systemctl daemon-reload
systemctl enable rtun-client
read -N 1 -p "Start service now? [y/n]"
echo
if [[ ${REPLY} == "y" ]]; then
systemctl start rtun-client
echo "Started server"
fi
fi
fi
cd
cd reverse-tunnel/
# Add ports to forward
ADD_FORWARD=1
while [[ $ADD_FORWARD -eq 1 ]]; do
read -N 1 -p "Add forward? [y/n] " CREATE && echo
if [[ $CREATE == "y" ]]; then
echo
echo "You will be asked below to enter the server port and the local port."
echo "The server port is the port on the server connections will be made to"
echo "and the local port is the port that connection will be forwarded to."
echo "The local port does not necessarily have to be on the local machine."
echo
read -p "Enter server port (in format port/protocol, eg 80/tcp). One port only: " SERVER_PORT
read -p "Enter client port (in format IP:port, eg 127.0.0.1:80): " CLIENT_PORT
echo
# Write port to client config
tee -a rtun.yml >/dev/null <<EOF
- port: $(echo $SERVER_PORT)
destination: $(echo $CLIENT_PORT)
EOF
elif [[ $CREATE == "n" ]]; then
ADD_FORWARD=0
else
echo "You must enter y or n!"
fi
done;
echo "Done. Run \"./rtun-client\" to start the server."