forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whereareyou.go
69 lines (54 loc) · 1.21 KB
/
whereareyou.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
package whereareyou
/*
Whereareyou command for the Micro Bot
usage: where are you?
*/
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"github.com/micro/go-bot/command"
)
func init() {
command.Commands[`^where are you\??`] = WhereAreYou()
}
func WhereAreYou() command.Command {
usage := "where are you?"
desc := "Returns the location of the bot"
getIp := func() string {
ifaces, _ := net.Interfaces()
for _, i := range ifaces {
addrs, _ := i.Addrs()
for _, addr := range addrs {
if ip, ok := addr.(*net.IPNet); ok && ip.IP.IsLoopback() {
continue
}
switch v := addr.(type) {
case *net.IPNet:
return v.IP.String()
case *net.IPAddr:
return v.IP.String()
}
}
}
return "127.0.0.1"
}
return command.NewCommand("whereareyou", usage, desc, func(args ...string) ([]byte, error) {
rsp, err := http.Get("http://myexternalip.com/raw")
if err != nil {
return nil, err
}
defer rsp.Body.Close()
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, err
}
host, _ := os.Hostname()
exIp := string(b)
inIp := getIp()
val := fmt.Sprintf("hostname: %s\ninternal ip: %s\nexternal ip: %s", host, inIp, exIp)
return []byte(val), nil
})
}