-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
383 lines (324 loc) · 8.5 KB
/
main.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package main
import (
"bytes"
"crypto/sha1"
"fmt"
"io/ioutil"
"net"
"os"
"path"
"strconv"
"strings"
"syscall"
"text/template"
"time"
"unicode/utf8"
log "github.com/Sirupsen/logrus"
"github.com/docker/libcontainer/netlink"
flags "github.com/jessevdk/go-flags"
"golang.org/x/crypto/pbkdf2"
)
type network struct {
Name string
SSID string
Password string
}
func getSSID(configPath string) string {
filename := path.Join(configPath, "box_name")
data, err := ioutil.ReadFile(filename)
if err != nil {
return "Protonet-default"
}
return strings.Trim(trimSSIDTo32Bytes(data), " \n\r\t")
}
// trimSSIDTo32Bytes trims an SSID to 32 bytes, making sure no UTF-8 rune is cut
func trimSSIDTo32Bytes(input []byte) string {
for len(input) > 32 {
_, lastRuneLen := utf8.DecodeLastRune(input)
l := len(input)
input = input[0 : l-lastRuneLen]
}
return string(input)
}
// was 'network_config'
func getNeededNetworks(configPath string) ([]network, error) {
log.Debugln("fetching networks")
ssid := getSSID(configPath)
var networks []network
log.Debugf("Looking for private network at %v", path.Join(configPath, "system", "wifi", "enabled"))
_, err := os.Stat(path.Join(configPath, "system", "wifi", "enabled"))
if err == nil {
log.Debugln("Private network found, configuring...")
passwdData, err2 := ioutil.ReadFile(path.Join(configPath, "system", "wifi", "password"))
if err2 != nil {
return nil, err2
}
networks = append(networks,
network{
Name: "wl_private",
SSID: ssid,
Password: strings.Trim(string(passwdData), " \n\r\t"),
})
} else {
if !os.IsNotExist(err) {
return nil, err
}
}
log.Debugf("Looking for public network at %v\n", path.Join(configPath, "system", "wifi", "guest", "enabled"))
_, err = os.Stat(path.Join(configPath, "system", "wifi", "guest", "enabled"))
if err == nil {
log.Debugln("Public network found, configuring...")
passwdData, err2 := ioutil.ReadFile(path.Join(configPath, "system", "wifi", "guest", "password"))
if err2 != nil {
return nil, err2
}
networks = append(networks,
network{
Name: "wl_public",
SSID: ssid + " (public)",
Password: strings.Trim(string(passwdData), " \n\r\t"),
})
} else {
if !os.IsNotExist(err) {
return nil, err
}
}
return networks, nil
}
func getBSSID(ifName string) (string, error) {
i, err := net.InterfaceByName(ifName)
if err != nil {
return "", err
}
mac := i.HardwareAddr
mac[0] |= 0x02
mac[5] = 0x01
return mac.String(), nil
}
func generateConfigFile(networks []network, configPath string, has5GHz bool, htcaps *htCapabilities, bssid string) (string, error) {
log.Debugln("hostapd configure")
type cfgData struct {
IEEE80211N bool
Channel uint
HTCap string
Name string
SSID string
Pass string
SecondName string
FirstBSSID string
SecondSSID string
SecondPass string
}
cfg := cfgData{
IEEE80211N: has5GHz,
Channel: getConfiguredChannel(configPath),
HTCap: htcaps.AsConfigString(configPath),
Name: networks[0].Name,
SSID: networks[0].SSID,
Pass: wpaPassphrase(networks[0].SSID, networks[0].Password),
}
if len(networks) == 2 {
cfg.SecondName = networks[1].Name
cfg.FirstBSSID = bssid
cfg.SecondSSID = networks[1].SSID
cfg.SecondPass = wpaPassphrase(networks[1].SSID, networks[1].Password)
}
templateString := `ctrl_interface=/var/run/hostapd
driver=nl80211
hw_mode=g
ieee80211n={{if .IEEE80211N}}1{{else}}0{{end}}
ieee80211d=1
ieee80211h=0
country_code=US
wme_enabled=1
wmm_enabled=1
channel={{.Channel}}
ht_capab={{.HTCap}}
interface={{.Name}}
logger_stdout=-1
logger_stdout_level=2
ssid={{.SSID}}
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
wpa_psk={{.Pass}}
{{if ne .SecondName ""}}
bss={{.SecondName}}
bssid={{.FirstBSSID}}
ssid={{.SecondSSID}}
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
wpa_psk={{.SecondPass}}
{{end}}
`
tmpl, err := template.New("cfg").Parse(templateString)
if err != nil {
return "", err
}
var bufferData []byte
buffer := bytes.NewBuffer(bufferData)
err = tmpl.Execute(buffer, cfg)
if err != nil {
return "", err
}
return buffer.String(), nil
}
func prepareAndGenerateConfigFile(configPath string, sleepTime int) (string, error) {
networks, err := getNeededNetworks(configPath)
if err != nil {
return "", fmt.Errorf("Failed to get network list: %v\n", err.Error())
}
if len(networks) == 0 {
log.Println("No WiFi neworks are enabled. Exitting")
os.Exit(0)
}
if len(networks) != 0 {
log.Infoln("Found wifi networks:")
for _, n := range networks {
log.Infof(" - %s", n.Name)
}
}
err = ensureInterfaceExist(networks[0].Name, sleepTime)
if err != nil {
return "", err
}
has5GHz, err := has5GHzSupport()
if err != nil {
return "", err
}
phys, err := getPhysicalInterfaces()
if err != nil {
return "", err
}
if len(phys) == 0 {
return "", fmt.Errorf("No WiFi physical interfaces found")
}
htcaps, err := getHTCapabilities(phys[0])
if err != nil {
return "", err
}
var bssid string
if len(networks) == 2 {
var err2 error
bssid, err2 = getBSSID(networks[0].Name)
if err2 != nil {
return "", err2
}
}
cfg, err := generateConfigFile(networks, configPath, has5GHz, htcaps[0], bssid)
if err != nil {
return "", fmt.Errorf("Failed to generate config file: %v", err.Error())
}
return cfg, nil
}
func wpaPassphrase(ssid, passphrase string) string {
pass := []byte(passphrase)
salt := []byte(ssid)
keyData := pbkdf2.Key(pass, salt, 4096, 32, sha1.New)
return fmt.Sprintf("%x", keyData)
}
func getConfiguredChannel(configPath string) uint {
filename := path.Join(configPath, "system", "wifi", "channel")
data, err := ioutil.ReadFile(filename)
if err != nil {
return 1
}
i, err := strconv.Atoi(string(data))
if err != nil {
panic(fmt.Sprintf("Channel %s is not an int", string(data)))
}
if i <= 0 {
panic(fmt.Sprintf("Channel %d is negative", i))
}
return uint(i)
}
func renameInterface(from string, to string) error {
i, err := net.InterfaceByName(from)
if err != nil {
return err
}
err = netlink.NetworkLinkDown(i)
if err != nil {
return fmt.Errorf("netlink.NetworkLinkDown(\"%s\"): %s", i.Name, err.Error())
}
err = netlink.NetworkChangeName(i, to)
if err != nil {
return fmt.Errorf("netlink.NetworkChangeName(\"%s\", \"%s\"): %s", i.Name, to, err.Error())
}
err = netlink.NetworkLinkUp(i)
if err != nil {
return fmt.Errorf("netlink.NetworkLinkUp(\"%s\"): %s", to, err.Error())
}
return err
}
// was parts of 'interface_name'
func ensureInterfaceExist(name string, sleepTime int) error {
interfaces, err := getLogicalInterfaces()
if err != nil {
return err
}
if len(interfaces) == 0 {
return fmt.Errorf("Found no WiFi interfaces")
}
var interfaceIsThere = false
for _, i := range interfaces {
if i == name {
interfaceIsThere = true
break
}
}
if !interfaceIsThere {
log.Infof("Interface %s doesn't exist. Renaming %s -> %s", name, interfaces[0], name)
err = renameInterface(interfaces[0], name)
if err != nil {
return err
}
time.Sleep(time.Second * time.Duration(sleepTime))
log.Debug("Restarting systemd-networkd.")
err = restartNetworkD(5)
if err != nil {
return fmt.Errorf("ensureInterfaceExist(): %s", err.Error())
}
time.Sleep(time.Second * time.Duration(sleepTime))
}
return nil
}
func main() {
var opts struct {
ConfigFile string `long:"config-file" required:"true" description:"path to hostapd.conf"`
Binary string `long:"hostapd-binary" required:"true" description:"path to hostapd binary"`
SKVSPath string `long:"skvs-dir" required:"true" decription:"path to SKVS root directory mountpoint"`
Debug bool `long:"debug" description:"enable debug mode"`
SleepTime int `long:"sleep-time" default:"5" description:"sleep time when retrying a systemd-networkd restart"`
}
_, err := flags.Parse(&opts)
if err != nil {
os.Exit(1)
}
if opts.Debug {
log.SetLevel(log.DebugLevel)
log.Debugln("Debug mode enabled.")
}
cfg, err := prepareAndGenerateConfigFile(opts.SKVSPath, opts.SleepTime)
if err != nil {
log.Fatalln(err)
}
log.Debugf("Generated config file:\n%s", cfg)
log.Infof("Writing hostapd config to '%s'", opts.ConfigFile)
err = ioutil.WriteFile(opts.ConfigFile, []byte(cfg), 0644)
if err != nil {
log.Fatalf("Failed to save config file: %s", err.Error())
}
log.Info("Starting hostapd")
err = syscall.Exec(opts.Binary, []string{opts.Binary, opts.ConfigFile}, []string{})
if err != nil {
log.Fatal(err)
}
}