-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsteaming
executable file
·110 lines (94 loc) · 2.84 KB
/
steaming
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
#!/bin/bash
echo "starting script"
debug="false"
silenterr=" 2>/dev/null "
silent=" >/dev/null 2>&1 "
silentvpn=" 2>/dev/null "
openvpn_pidfile="/tmp/openvpn.pid"
packetcapture_pidfile="/tmp/packet_capture.pid"
if [[ "$1" == "--debug" ]];
then
silenterr=""
silent=""
debug="true";
fi
dbg() {
if [[ $debug == "true" ]];
then
echo $@ >&2
fi
}
dbg "checking dependencies"
dependencies="route grep awk head kill pkill openvpn ifconfig tshark script xxd socat"
for dep in $dependencies;
do
if ! command -v "${dep}" >/dev/null;
then
echo "please install ${dep}" >&2
exit 1
fi
done
dbg "dependencies checked"
dbg "check for permissions (root)"
if [[ "$EUID" -ne 0 ]];
then
echo "please run as root (e.g. sudo)" >&2;
exit 2;
fi
dbg "checked for permissions"
dbg "setting default interface"
# will be your default wifi/ethernet device
# I did not yet test this with multiple interfaces,
DEFAULT_INTERFACE=$(/sbin/route 2>/dev/null | grep '\b0.0.0.0\b' | awk '{print $8}' | head -n1)
# probably no need to change this
STEAM_PORT=27036
dbg "default interface set to $DEFAULT_INTERFACE"
# first we kill all the processes we started
# we then use the exit command we all know and love <3
exit() {
echo -n "cleaning up..."
[ -f "${openvpn_pidfile}" ] && sudo kill "$(cat "${openvpn_pidfile}")" 2>/dev/null
[ -f "${packetcapture_pidfile}" ] && sudo pkill -P "$(cat "${packetcapture_pidfile}")"
echo "done"
builtin exit $1
}
trap exit 1 2 3 15
set -x
dbg "starting openvpn"
openvpnlog=">"
[ $debug == "true" ] && tail -F /tmp/openvpn.{log,err} &
openvpn --config /etc/openvpn/client.ovpn --askpass ${passwordfile:-/etc/openvpn/pass} > /tmp/openvpn.log 2>&1 &
echo $! > "${openvpn_pidfile}"
dbg "openvpn started with pid: $(cat "${openvpn_pidfile}")"
dbg "waiting for vpn connection"
set +x
i=0
while ! grep 'Initialization Sequence Completed' /tmp/openvpn.log;
do
sleep 0.3
((i++))
# abort if it takes 30s
if [[ $i -gt 100 ]];
then
echo "Taking too long" >&2
exit 3
fi
done
dbg "vpn connection established"
dbg "finding tap interface"
# will most likely be "tap0"
TAP_INTERFACE=$(ifconfig | grep tap | awk '{print $1}')
dbg "tap interface set to $TAP_INTERFACE"
dbg "starting broadcast packet capture"
# we use:
# tshark to capture the broadcast packets which are used by steam to detect in-home streamers
# xxd to transform the captured hexdecimal packets into binary
# socat to relay the broadcast to the vpn server over the tap interface
bash -c "tshark -i ${DEFAULT_INTERFACE} -T fields -e data -l 'udp and dst port ${STEAM_PORT}' $silenterr \
| script -q /dev/null -c 'xxd -r -p' $silenterr \
| socat - UDP-DATAGRAM:10.8.0.1:${STEAM_PORT},broadcast,so-bindtodevice=${TAP_INTERFACE} $silenterr \
"&
echo $! > "${packetcapture_pidfile}"
dbg "packet capture started with pid: $(cat "${packetcapture_pidfile}")"
echo "script finished, shut down with Ctrl+C"
wait