1 Star 0 Fork 0

李梦凡/kite

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
registerurl.go 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
package kite
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
)
const publicEcho = "http://echoip.com"
// RegisterURL returns a URL that is either local or public. It's an helper
// method to get a Registration URL that can be passed to Kontrol (via the
// methods Register(), RegisterToProxy(), etc.) It needs to be called after all
// configurations are done (like TLS, Port,etc.). If local is true a local IP
// is used, otherwise a public IP is being used.
func (k *Kite) RegisterURL(local bool) *url.URL {
var ip net.IP
var err error
if local {
ip, err = localIP()
if err != nil {
return nil
}
} else {
ip, err = publicIP()
if err != nil {
return nil
}
}
scheme := "http"
if k.TLSConfig != nil {
scheme = "https"
}
return &url.URL{
Scheme: scheme,
Host: ip.String() + ":" + strconv.Itoa(k.Config.Port),
Path: "/" + k.name + "-" + k.version + "/kite",
}
}
// localIp returns a local IP from one of the local interfaces.
func localIP() (net.IP, error) {
tt, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, t := range tt {
aa, err := t.Addrs()
if err != nil {
return nil, err
}
for _, a := range aa {
ipnet, ok := a.(*net.IPNet)
if !ok {
continue
}
v4 := ipnet.IP.To4()
if v4 == nil || v4[0] == 127 { // loopback address
continue
}
return v4, nil
}
}
return nil, errors.New("cannot find local IP address")
}
// publicIP returns an IP that is supposed to be Public.
func publicIP() (net.IP, error) {
resp, err := http.Get(publicEcho)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// The ip address is 16 chars long, we read more
// to account for excessive whitespace.
p, err := ioutil.ReadAll(io.LimitReader(resp.Body, 24))
if err != nil {
return nil, err
}
n := net.ParseIP(string(bytes.TrimSpace(p)))
if n == nil {
return nil, fmt.Errorf("cannot parse ip %s", p)
}
return n, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/vanlemon/kite.git
git@gitee.com:vanlemon/kite.git
vanlemon
kite
kite
master

搜索帮助