-
Notifications
You must be signed in to change notification settings - Fork 1
/
ifconfig.go
106 lines (88 loc) · 2.15 KB
/
ifconfig.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
package ifconfig
import (
"net"
"net/http"
"github.com/docker/docker/pkg/version"
"github.com/hacking-thursday/sysd/mods"
// "fmt"
)
func init() {
mods.Register("GET", "/ifconfig", ifconfig)
mods.Register("GET", "/network/ifce", ifconfig)
}
type iface_t struct {
Index int
MTU int
Name string
IP []string
HwAddr string
Flag struct {
Up bool
Broadcast bool
Loopback bool
PointToPoint bool
Multicast bool
}
Counters Counters
}
type Counters struct {
BytesRecv int
PacketsRecv int
Errin int
Dropin int
BytesSent int
PacketsSent int
Errout int
Dropout int
}
func ifconfig(engine interface{}, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) (err error) {
var (
out []byte
outIfaces []iface_t
outIface iface_t
ifaces []net.Interface
iface net.Interface
addrs []net.Addr
addr net.Addr
ip string
)
if ifaces, err = net.Interfaces(); err != nil {
mods.HttpError(w, err)
return
}
iface_counter_map := net_io_counters()
for _, iface = range ifaces {
outIface = iface_t{
Index: iface.Index,
MTU: iface.MTU,
Name: iface.Name,
HwAddr: iface.HardwareAddr.String(),
}
outIface.Flag.Up = iface.Flags&net.FlagUp != 0
outIface.Flag.Broadcast = iface.Flags&net.FlagBroadcast != 0
outIface.Flag.Loopback = iface.Flags&net.FlagLoopback != 0
outIface.Flag.PointToPoint = iface.Flags&net.FlagPointToPoint != 0
outIface.Flag.Multicast = iface.Flags&net.FlagMulticast != 0
addrs, err = iface.Addrs()
if err != nil {
mods.HttpError(w, err)
return
}
for _, addr = range addrs {
ip = addr.String()
outIface.IP = append(outIface.IP, ip)
}
outIface.Counters = iface_counter_map[iface.Name]
// fmt.Println(iface_counter_map[iface.Name])
outIfaces = append(outIfaces, outIface)
}
if out, err = mods.Marshal(r, outIfaces); err != nil {
mods.HttpError(w, err)
return
}
if _, err = w.Write(out); err != nil {
mods.HttpError(w, err)
return
}
return
}