-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathnet.go
More file actions
133 lines (102 loc) · 2.89 KB
/
net.go
File metadata and controls
133 lines (102 loc) · 2.89 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
// Copyright (c) The go-boot authors. All Rights Reserved.
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
//go:build net
package cmd
import (
"fmt"
"net"
"net/http"
_ "net/http/pprof"
"regexp"
"strings"
"github.com/gliderlabs/ssh"
"github.com/usbarmory/go-net"
"github.com/usbarmory/go-boot/shell"
"github.com/usbarmory/go-boot/uefi"
"github.com/usbarmory/go-boot/uefi/x64"
// maintained set of TLD roots for any potential TLS client request
_ "golang.org/x/crypto/x509roots/fallback"
)
// Resolver represents the default name server
var Resolver = "8.8.8.8:53"
const receiveMask = uefi.EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
uefi.EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST |
uefi.EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS
func init() {
shell.Add(shell.Cmd{
Name: "net",
Args: 4,
Pattern: regexp.MustCompile(`^net (\S+) (\S+) (\S+)( debug)?$`),
Syntax: "<ip> <mac> <gw> (debug)?",
Help: "start UEFI networking",
Fn: netCmd,
})
shell.Add(shell.Cmd{
Name: "dns",
Args: 1,
Pattern: regexp.MustCompile(`^dns (.*)`),
Syntax: "<host>",
Help: "resolve domain",
Fn: dnsCmd,
})
net.SetDefaultNS([]string{Resolver})
}
func netCmd(_ *shell.Interface, arg []string) (res string, err error) {
nic, err := x64.UEFI.Boot.GetNetwork()
if err != nil {
return "", fmt.Errorf("could not locate network protocol, %v", err)
}
// clean up from previous initializations
nic.Shutdown()
nic.Stop()
nic.Start()
if err = nic.Initialize(); err != nil {
return "", fmt.Errorf("could not initialize interface, %v", err)
}
if err = nic.ReceiveFilters(receiveMask, 0); err != nil {
return "", fmt.Errorf("could not set receive filters, %v", err)
}
iface := gnet.Interface{}
if arg[1] == ":" {
arg[1] = ""
}
if err := iface.Init(nic, arg[0], arg[1], arg[2]); err != nil {
return "", fmt.Errorf("could not initialize networking, %v", err)
}
if err = nic.StationAddress(false, iface.NIC.MAC); err != nil {
fmt.Errorf("could not set permanent station address, %v\n", err)
}
iface.EnableICMP()
go iface.NIC.Start()
// hook interface into Go runtime
net.SocketFunc = iface.Socket
if len(arg[3]) > 0 {
ip, _, _ := strings.Cut(arg[0], `/`)
fmt.Printf("starting debug servers:\n")
fmt.Printf("\thttp://%s:80/debug/pprof\n", ip)
fmt.Printf("\tssh://%s:22\n", ip)
go func() {
ssh.Handle(func(s ssh.Session) {
c := &shell.Interface{
Banner: Banner,
ReadWriter: s,
}
c.Start(true)
})
ssh.ListenAndServe(":22", nil)
}()
go func() {
http.ListenAndServe(":80", nil)
}()
}
return fmt.Sprintf("network initialized (%s %s)", arg[0], iface.NIC.MAC), nil
}
func dnsCmd(_ *shell.Interface, arg []string) (res string, err error) {
cname, err := net.LookupHost(arg[0])
if err != nil {
return "", fmt.Errorf("query error: %v", err)
}
return fmt.Sprintf("%+v", cname), nil
}