-
Notifications
You must be signed in to change notification settings - Fork 31
/
net.go
59 lines (48 loc) · 1.1 KB
/
net.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
package net
import (
"errors"
"net"
"strings"
"github.com/emitter-io/address"
"github.com/kelindar/lua"
"github.com/kelindar/talaria/internal/monitor"
)
const ctxTag = "net"
// New creates a new lua module exposing the stats
func New(monitor monitor.Monitor) lua.Module {
l := &libnet{monitor}
m := &lua.NativeModule{
Name: "net",
Version: "1.0",
}
m.Register("getmac", l.GetMac)
m.Register("addr", l.Addr)
return m
}
type libnet struct {
monitor monitor.Monitor
}
// GetMac returns the mac address
func (l *libnet) GetMac() (lua.String, error) {
ifas, err := net.Interfaces()
if err != nil {
return "", err
}
for _, ifa := range ifas {
if addr := ifa.HardwareAddr.String(); addr != "" {
return lua.String(addr), nil
}
}
return "", errors.New("no valid mac address found")
}
// Addr returns the address of a specific host
func (l *libnet) Addr(host lua.String) (lua.String, error) {
if !strings.Contains(string(host), ":") {
host = host + ":80"
}
addr, err := address.Parse(string(host), 0)
if err != nil {
return "", err
}
return lua.String(addr.IP.String()), nil
}