-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip.go
78 lines (71 loc) · 1.73 KB
/
ip.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
package main
import (
"fmt"
"net"
"strings"
"gopkg.in/telegram-bot-api.v4"
)
// RegisterIP adds "ip" command to bot
func RegisterIP(bot Bot) *Interfaces {
ifaces := &Interfaces{Current: make(map[string][]net.IP)}
bot.Add("ip", func(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
return ifaces.replyToIP(bot, msg, tokens)
})
return ifaces
}
// Interfaces is a map of interfaces to IP addresses
type Interfaces struct {
Current map[string][]net.IP
}
// Update all interfaces and IP Addresses
func (ifaces *Interfaces) Update() error {
netif, err := net.Interfaces()
result := make(map[string][]net.IP)
if err != nil {
return err
}
for _, i := range netif {
addrs, err := i.Addrs()
if err != nil {
return err
}
ips := make([]net.IP, 0, len(addrs))
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ipv4 := ip.To4(); ipv4 != nil {
ips = append(ips, ipv4)
}
}
if len(ips) > 0 {
result[i.Name] = ips
}
}
ifaces.Current = result
return nil
}
// ReplyToIP replies to a message asking for IP Addresses.
func (ifaces *Interfaces) replyToIP(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
if err := ifaces.Update(); err != nil {
return err.Error()
}
return ifaces.String()
}
// ToString converts an ifaceMap to string
func (ifaces *Interfaces) String() string {
lines := make([]string, 0, len(ifaces.Current))
for name, ips := range ifaces.Current {
texts := make([]string, 0, len(ips))
for _, ip := range ips {
texts = append(texts, ip.String())
}
line := fmt.Sprintf("%s: %s", name, strings.Join(texts, ", "))
lines = append(lines, line)
}
return strings.Join(lines, "\n")
}