当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
whereareyou.go 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
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
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.8.0

搜索帮助