forked from usbarmory/tamago-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusbnet.go
106 lines (77 loc) · 1.95 KB
/
usbnet.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
// https://github.com/usbarmory/tamago-example
//
// Copyright (c) WithSecure Corporation
// https://foundry.withsecure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
package main
import (
"log"
"time"
usbnet "github.com/usbarmory/imx-usbnet"
"github.com/usbarmory/tamago/soc/imx6/usb"
"github.com/miekg/dns"
)
const (
deviceIP = "10.0.0.1"
deviceMAC = "1a:55:89:a2:69:41"
hostMAC = "1a:55:89:a2:69:42"
resolver = "8.8.8.8:53"
)
var iface *usbnet.Interface
func startNetworking() {
var err error
iface, err = usbnet.Init(deviceIP, deviceMAC, hostMAC, 1)
if err != nil {
log.Fatalf("could not initialize USB networking, %v", err)
}
iface.EnableICMP()
listenerSSH, err := iface.ListenerTCP4(22)
if err != nil {
log.Fatalf("could not initialize SSH listener, %v", err)
}
listenerHTTP, err := iface.ListenerTCP4(80)
if err != nil {
log.Fatalf("could not initialize HTTP listener, %v", err)
}
listenerHTTPS, err := iface.ListenerTCP4(443)
if err != nil {
log.Fatalf("could not initialize HTTP listener, %v", err)
}
// create index.html
setupStaticWebAssets()
go func() {
// see ssh_server.go
startSSHServer(listenerSSH, deviceIP, 22)
}()
go func() {
// see web_server.go
startWebServer(listenerHTTP, deviceIP, 80, false)
}()
go func() {
// see web_server.go
startWebServer(listenerHTTPS, deviceIP, 443, true)
}()
usb.USB1.Init()
usb.USB1.DeviceMode()
usb.USB1.Reset()
// never returns
usb.USB1.Start(iface.Device())
}
func resolve(s string) (r *dns.Msg, rtt time.Duration, err error) {
if s[len(s)-1:] != "." {
s += "."
}
msg := new(dns.Msg)
msg.Id = dns.Id()
msg.RecursionDesired = true
msg.Question = make([]dns.Question, 1)
msg.Question[0] = dns.Question{s, dns.TypeANY, dns.ClassINET}
conn := new(dns.Conn)
if conn.Conn, err = iface.DialTCP4(resolver); err != nil {
return
}
c := new(dns.Client)
return c.ExchangeWithConn(msg, conn)
}