Repository navigation
Expand file tree
/
Copy pathvpn_setup.sh
More file actions
executable file
·188 lines (169 loc) · 8.6 KB
/
Copy pathvpn_setup.sh
File metadata and controls
executable file
·188 lines (169 loc) · 8.6 KB
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
182
183
184
185
186
187
#!/bin/bash
# NOTE: this script uses ufw and iptables to setup relaying rules.
# Please ensure both are available to use on the system where
# you plan to run this script.
######### SCRIPT PARAMETERS - MUST BE CONFIGURED PROPERLY ###########
# IP of the LAN player
LAN_PLAYER_IP="10.0.0.2"
# IP of the VPN player
VPN_PLAYER_IP="25.0.0.2"
# VPN player is hosting the game
VPN_PLAYER_IS_HOSTING=false
# LAN network with netmask
LAN_NETWORK="10.0.0.0/24"
# VPN network with netmask
VPN_NETWORK="25.0.0.0/8"
# local LAN broadcast address
LAN_BROADCAST_IP="10.0.0.255"
# local LAN interface name
LAN_INTF="eth0"
# local VPN interface name
VPN_INTF="ham0"
# path to the Wookiee Broadcaster script on the local host
LOCAL_WB_PATH="/home/username/wookiee_broadcaster.py"
# Wookiee Broadcaster script of binary name
LOCAL_WB_NAME=$(basename $LOCAL_WB_PATH)
# local VPN IP
VPN_LOCAL_IP=$(ip -4 addr show $VPN_INTF 2>/dev/null | grep -w inet | awk '{print $2}' | cut -d '/' -f 1)
# can only ever happen if an invalid VPN_INTF is used
if [ -z $VPN_LOCAL_IP ]
then
echo "Unable to detect VPN IP address. Check the VPN_INTF parameter and retry."
exit 1
fi
######################### IMPORTANT NOTE ############################
# #
# The following also need to be true on the LAN_PLAYER_IP host for #
# the forwarding to work properly: #
# #
# a) The local (LAN) IP of the host that is running the VPN #
# (and this script) must be configured as gateway on #
# LAN_PLAYER_IP's network interface #
# b) No ports should be blocked/firewalled between the two hosts, #
# especially not the ones used for direct/broadcast traffic #
# c) 255.255.255.255 traffic must also be allowed on both hosts #
# #
#####################################################################
echo "*** WinterSnowfall's VPN relay setup script for Linux ***"
echo ""
echo ">>> LAN IP is set to : $LAN_PLAYER_IP"
echo ">>> VPN IP is set to : $VPN_PLAYER_IP"
echo ""
echo "####################################################"
echo "# #"
echo "# (1) Anno 1404: Venice #"
echo "# (2) Divinty Original Sin - Enhanced Edition #"
echo "# (3) Majesty 2 #"
echo "# (4) Torchlight 2 #"
echo "# (5) Worms Armageddon #"
echo "# #"
echo "####################################################"
echo ""
read -p ">>> Pick a game from the list: " game
# ensure ipv4 forwarding between adapters is enabled
sudo sysctl -w net.ipv4.ip_forward=1
# add generic ufw rules to allow traffic
sudo ufw allow from $LAN_NETWORK to any
sudo ufw allow from $VPN_NETWORK to any
# switch default forwarding policy to ACCEPT
sudo iptables -P FORWARD ACCEPT
# add game-specific broadcasting/NAT rules
case $game in
1)
echo ">>> Setting up Anno 1404: Venice..."
# broadcast NAT
if $VPN_PLAYER_IS_HOSTING
then
sudo iptables -t mangle -A PREROUTING -i $VPN_INTF -p udp -s $VPN_PLAYER_IP -d 255.255.255.255 --dport 9100 -j TEE --gateway $LAN_BROADCAST_IP
else
$LOCAL_WB_PATH -p 9100 -i $LAN_INTF -o $VPN_INTF >> wookiee_broadcaster.log 2>&1 &
fi
# regular NAT
sudo iptables -t nat -A PREROUTING -i $VPN_INTF -p udp -s $VPN_PLAYER_IP --dport 16000 -j DNAT --to-destination $LAN_PLAYER_IP
sudo iptables -t nat -A PREROUTING -i $VPN_INTF -p udp -s $VPN_PLAYER_IP --dport 9100:9103 -j DNAT --to-destination $LAN_PLAYER_IP
sudo iptables -t nat -A POSTROUTING -o $VPN_INTF -p udp -s $LAN_PLAYER_IP -d $VPN_PLAYER_IP --dport 16000 -j SNAT --to-source $VPN_LOCAL_IP
sudo iptables -t nat -A POSTROUTING -o $VPN_INTF -p udp -s $LAN_PLAYER_IP -d $VPN_PLAYER_IP --dport 9100:9103 -j SNAT --to-source $VPN_LOCAL_IP
;;
2)
echo ">>> Setting up Divinty Original Sin - Enhanced Edition..."
# broadcast NAT
if $VPN_PLAYER_IS_HOSTING
then
sudo iptables -t mangle -A PREROUTING -i $VPN_INTF -p udp -s $VPN_PLAYER_IP -d 255.255.255.255 --dport 23243:23262 -j TEE --gateway $LAN_BROADCAST_IP
else
$LOCAL_WB_PATH -p 23243:23262 -i $LAN_INTF -o $VPN_INTF -q >> wookiee_broadcaster.log 2>&1 &
fi
# regular NAT
sudo iptables -t nat -A PREROUTING -i $VPN_INTF -p udp -s $VPN_PLAYER_IP --dport 23243:23262 -j DNAT --to-destination $LAN_PLAYER_IP
sudo iptables -t nat -A POSTROUTING -o $VPN_INTF -p udp -s $LAN_PLAYER_IP -d $VPN_PLAYER_IP --dport 23243:23262 -j SNAT --to-source $VPN_LOCAL_IP
;;
3)
echo ">>> Setting up Majesty 2..."
# broadcast NAT
if $VPN_PLAYER_IS_HOSTING
then
sudo iptables -t mangle -A PREROUTING -i $VPN_INTF -p udp -s $VPN_PLAYER_IP -d 255.255.255.255 --dport 3210 -j TEE --gateway $LAN_BROADCAST_IP
else
$LOCAL_WB_PATH -p 3210 -i $LAN_INTF -o $VPN_INTF >> wookiee_broadcaster.log 2>&1 &
fi
# regular NAT
sudo iptables -t nat -A PREROUTING -i $VPN_INTF -p udp -s $VPN_PLAYER_IP --dport 3210 -j DNAT --to-destination $LAN_PLAYER_IP
sudo iptables -t nat -A POSTROUTING -o $VPN_INTF -p udp -s $LAN_PLAYER_IP -d $VPN_PLAYER_IP --dport 3210 -j SNAT --to-source $VPN_LOCAL_IP
;;
4)
echo ">>> Setting up Torchlight 2..."
# broadcast NAT
if $VPN_PLAYER_IS_HOSTING
then
sudo iptables -t mangle -A PREROUTING -i $VPN_INTF -p udp -s $VPN_PLAYER_IP -d 255.255.255.255 --dport 4549 -j TEE --gateway $LAN_BROADCAST_IP
else
$LOCAL_WB_PATH -p 4549 -i $LAN_INTF -o $VPN_INTF >> wookiee_broadcaster.log 2>&1 &
fi
# regular NAT
sudo iptables -t nat -A PREROUTING -i $VPN_INTF -p udp -s $VPN_PLAYER_IP --dport 4549 -j DNAT --to-destination $LAN_PLAYER_IP
sudo iptables -t nat -A PREROUTING -i $VPN_INTF -p udp -s $VPN_PLAYER_IP --dport 30000:65000 -j DNAT --to-destination $LAN_PLAYER_IP
sudo iptables -t nat -A POSTROUTING -o $VPN_INTF -p udp -s $LAN_PLAYER_IP -d $VPN_PLAYER_IP --dport 4549 -j SNAT --to-source $VPN_LOCAL_IP
sudo iptables -t nat -A POSTROUTING -o $VPN_INTF -p udp -s $LAN_PLAYER_IP -d $VPN_PLAYER_IP --dport 30000:65000 -j SNAT --to-source $VPN_LOCAL_IP
;;
5)
echo ">>> Setting up Worms Armageddon..."
# broadcast NAT
if $VPN_PLAYER_IS_HOSTING
then
sudo iptables -t mangle -A PREROUTING -i $VPN_INTF -p udp -s $VPN_PLAYER_IP -d 255.255.255.255 --dport 17012 -j TEE --gateway $LAN_BROADCAST_IP
else
$LOCAL_WB_PATH -p 17012 -i $LAN_INTF -o $VPN_INTF >> wookiee_broadcaster.log 2>&1 &
fi
# regular NAT
sudo iptables -t nat -A PREROUTING -i $VPN_INTF -p udp -s $VPN_PLAYER_IP --dport 17012 -j DNAT --to-destination $LAN_PLAYER_IP
sudo iptables -t nat -A PREROUTING -i $VPN_INTF -p tcp -s $VPN_PLAYER_IP --dport 17011 -j DNAT --to-destination $LAN_PLAYER_IP
sudo iptables -t nat -A PREROUTING -i $VPN_INTF -p tcp -s $VPN_PLAYER_IP --dport 50000:55000 -j DNAT --to-destination $LAN_PLAYER_IP
sudo iptables -t nat -A POSTROUTING -o $VPN_INTF -p udp -s $LAN_PLAYER_IP -d $VPN_PLAYER_IP --dport 17012 -j SNAT --to-source $VPN_LOCAL_IP
sudo iptables -t nat -A POSTROUTING -o $VPN_INTF -p tcp -s $LAN_PLAYER_IP -d $VPN_PLAYER_IP --dport 17011 -j SNAT --to-source $VPN_LOCAL_IP
sudo iptables -t nat -A POSTROUTING -o $VPN_INTF -p tcp -s $LAN_PLAYER_IP -d $VPN_PLAYER_IP --dport 50000:55000 -j SNAT --to-source $VPN_LOCAL_IP
;;
*)
echo ">>> Invalid option!"
exit 2
;;
esac
echo ">>> VPN relay setup complete!"
read -p ">>> Press any key to terminate..."
# terminate all broadcast replication processes
pkill -f $LOCAL_WB_NAME > /dev/null 2>&1
# remove game-specific broadcasting/NAT rules
#
# careful with flushing the nat tables, some services
# (e.g. docker) will also add some rules here -
# it is better to individually remove above rules in this case
#
sudo iptables -t nat -F
sudo iptables -t mangle -F
# switch default forwarding policy to DROP
sudo iptables -P FORWARD DROP
# remove generic rules
sudo ufw delete allow from $VPN_NETWORK to any
sudo ufw delete allow from $LAN_NETWORK to any
# disable ipv4 forwarding (if needed)
sudo sysctl -w net.ipv4.ip_forward=0
echo ">>> VPN relay has been deconfigured."