-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtunnel.sh
executable file
·161 lines (126 loc) · 5.48 KB
/
tunnel.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
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
# key/cert name
srvkey='proxy/.node.http.tunnel.ecdsa.prime256v1'
# images
imgc='mulekick/tunnel-client:latest'
imgr='mulekick/tunnel-server:latest'
imgp='mulekick/tunnel-proxy:latest'
# bridge networks
internet='ntw-internet'
local='ntw-local'
# server host, proxy, clients containers names
server='server'
proxy='proxy'
client1='client-1'
client2='client-2'
# tmux session name
sname="tunnel-session"
# double check directory
if [[ ! -x "$(pwd)/tunnel.sh" ]]; then
# echo
echo "Please run this script from the node-http-tunnel directory."
# fail
return 1
# create SSL/TLS 1.3 configuration
elif [[ $1 = 'tls' ]]; then
# create server private key
openssl ecparam -param_enc named_curve -name prime256v1 -genkey -noout -outform PEM -out "$srvkey" && \
# create server certificate
openssl req -x509 -key "$srvkey" -new -outform PEM -out "$srvkey.crt" -verbose
# build containers
elif [[ $1 = 'build' ]]; then
# build client, server and proxy images
docker build --no-cache -t $imgc client/.
docker build --no-cache -t $imgr server/.
docker build --no-cache -t $imgp proxy/.
# start service
elif [[ $1 = 'start' ]]; then
if [[ $# -lt 2 ]]; then
echo "please provide server execution mode (tcp or http)"
# failure
return 1
else
# create networks
echo "creating networks ..."
docker network create -d bridge "$internet" && \
docker network create -d bridge "$local" && \
echo "networks creation completed ..."
# start containers (use -t to allocate a pseudo-TTY and have the colors in the docker logs commands)
if [[ $3 = 'debug' ]]; then
# setup server host and proxy containers - debug mode
echo "creating server and proxy containers ..." && \
docker run --name "$server" --rm -t -d --env MODE="$2" --env DEBUG=1 -p 9221:9229 "$imgr" && \
docker run --name "$proxy" --rm -t -d --env MODE="$2" --env DEBUG=1 -p 9220:9229 "$imgp" && \
echo "connecting to networks ..." && \
docker network connect "$local" "$server" && \
docker network connect "$local" "$proxy" && \
docker network connect "$internet" "$proxy" && \
# setup clients containers - debug mode
echo "creating client containers ..." && \
docker run --name "$client1" --rm -t -d --env PROXY="$proxy" --env SERVER="$server" --env DEBUG=1 -p 9219:9229 "$imgc" && \
docker run --name "$client2" --rm -t -d --env PROXY="$proxy" --env SERVER="$server" --env DEBUG=1 "$imgc" && \
echo "connecting to networks ..." && \
docker network connect "$internet" "$client1" && \
docker network connect "$internet" "$client2" && \
echo "setup completed ..."
else
# setup server host and proxy containers
echo "creating server and proxy containers ..." && \
docker run --name "$server" --rm -t -d --env MODE="$2" "$imgr" && \
docker run --name "$proxy" --rm -t -d --env MODE="$2" "$imgp" && \
echo "connecting to networks ..." && \
docker network connect "$local" "$server" && \
docker network connect "$local" "$proxy" && \
docker network connect "$internet" "$proxy" && \
# setup clients containers
echo "creating client containers ..." && \
docker run --name "$client1" --rm -t -d --env PROXY="$proxy" --env SERVER="$server" "$imgc" && \
docker run --name "$client2" --rm -t -d --env PROXY="$proxy" --env SERVER="$server" "$imgc" && \
echo "connecting to networks ..." && \
docker network connect "$internet" "$client1" && \
docker network connect "$internet" "$client2" && \
echo "setup completed ..."
fi
# init session
tmux new-session -ds "$sname"
# extract window id
winid=$(tmux list-windows -t "$sname" | sed -r 's/^([0-9]):.+$/\1/g' -)
# set layout
tmux select-layout -t "$winid.0" even-vertical
# create 3 more panes
for pnid in {0..2}; do
# split current pane
tmux split-window -v -t "$winid.$pnid"
# set layout
tmux select-layout -t "$winid.$(( pnid + 1 ))" even-vertical
# end while
done
# split pane 2 horizontally
tmux split-window -h -t "$winid.2"
# send commands
tmux send-keys -t "$winid.0" "clear && docker logs -f $server" C-m
tmux send-keys -t "$winid.1" "clear && docker logs -f $proxy" C-m
tmux send-keys -t "$winid.2" "clear && docker logs -f $client1" C-m
tmux send-keys -t "$winid.3" "clear && docker logs -f $client2" C-m
# attach session to current terminal
tmux attach-session -t "$sname"
fi
# stop service
elif [[ $1 = 'stop' ]]; then
# remove clients, server and proxy containers
echo "removing client containers ..." && \
docker container rm -f "$client1" "$client2" && \
echo "removing server and proxy containers ..." && \
docker container rm -f "$server" "$proxy" && \
echo "removing networks ..." && \
docker network rm "$internet" "$local" && \
echo "ending tmux session ..."
# kill tmux session
tmux detach-client -s "$sname" && tmux kill-session -t "$sname"
else
echo "no command could be executed"
# failure
return 1
fi
# success
return 0