代码拉取完成,页面将自动刷新
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
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。