-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathosx-tuntap.sh
executable file
·163 lines (118 loc) · 2.65 KB
/
osx-tuntap.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
162
163
#! /bin/bash
#
# Load tun/tap OSX drivers
#
# For some bizarre reason, org.macports.tuntaposx fails to start on
# Lion. Thus, this script was born.
#
# (c) 2012, 2013 Sudhi Herle <sw at herle.net>
# License: GPLv2
#
# Name for launchd
FNAME=net.herle.tuntaposx
me=`id -u`
if [ $me -ne 0 ]; then
echo "$0: Need root privilege to run; switching to dry-run mode .."
e=echo
fi
#set -x
usage() {
cat 1>&2 <<EOF1
Usage: $0 start|stop|install
start: Loads the tun/tap kext module
stop: Unloads the tun/tap kext module
install: Creates a system startup file in /System/Library/LaunchDaemons/$FNAME.plist
EOF1
exit 1
}
maybe_load() {
local mod=$1
local bn=`basename $mod .kext`
local z=`kextstat | egrep "\.$bn " | wc -l`
if [ $z -eq 0 ]; then
$e kextload $mod
else
echo "$bn already loaded!"
fi
return 0
}
kload() {
maybe_load /opt/local/Library/Extensions/tun.kext
maybe_load /opt/local/Library/Extensions/tap.kext
return 0
}
kunload() {
$e kextunload /opt/local/Library/Extensions/tap.kext
$e kextunload /opt/local/Library/Extensions/tun.kext
return 0
}
# Installs a startup service
install_service() {
local idir=/System/Library/LaunchDaemons
local bn=`basename $0`
local prog=/usr/sbin/$bn
if [ $me -ne 0 ]; then
idir=/tmp/$bn
mkdir -p $idir
fi
local file=$idir/${FNAME}.plist
cat > $file <<EOF2
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>$FNAME</string>
<key>Disabled</key>
<false/>
<key>UserName</key>
<string>root</string>
<key>GroupName</key>
<string>wheel</string>
<key>Debug</key>
<false/>
<key>KeepAlive</key>
<false/>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>$prog</string>
<string>start</string>
</array>
</dict>
</plist>
EOF2
$e cp $0 $prog
$e chmod a+rx,og-w $prog
$e chmod 0644 $file
echo "Installed to $file and $prog .."
# Now, manually load the service - so that launchctl knows about
# this
# $e launchctl load -F $file
return 0
}
if [ -z "$1" ]; then
usage
exit 1
fi
case $1 in
start)
echo "Loading tun/tap OS X drivers .."
kload;
;;
stop)
echo "Unloading tun/tap OS X drivers .."
kunload;
;;
restart)
;;
install)
install_service
;;
*)
usage
exit 1
;;
esac
# vim: expandtab:sw=4:ts=4:tw=72:notextmode: