Skip to content

Commit

Permalink
ifconfig retry
Browse files Browse the repository at this point in the history
  • Loading branch information
hhd committed Dec 1, 2021
1 parent a925073 commit 8353c1c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
3 changes: 2 additions & 1 deletion README-ZH.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[English](/README.md)|中文
[English](/README.md)|中文
网站: [openp2p.cn](https://openp2p.cn)
## OpenP2P是什么
它是一个开源、免费、轻量级的P2P共享网络。任何设备接入OpenP2P,随时随地访问它们。
我们的目标是:充分利用带宽,利用共享节点转发数据,建设一个远程连接的通用基础设施。
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
English|[中文](/README-ZH.md)
English|[中文](/README-ZH.md)
Website: [openp2p.cn](https://openp2p.cn)
## What is OpenP2P
It is an open source, free, and lightweight P2P sharing network. As long as any device joins in, you can access them anywhere.
Our goal is to make full use of bandwidth, use shared nodes to relay data, and build a common infrastructure for remote connections.
Expand Down
41 changes: 23 additions & 18 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,28 @@ func netInfo() *NetInfo {
// return d.DialContext(ctx, "tcp6", addr)
// },
}
client := &http.Client{Transport: tr, Timeout: time.Second * 5}
r, err := client.Get("https://ifconfig.co/json")
if err != nil {
gLog.Println(LevelINFO, "netInfo error:", err)
return nil
}
defer r.Body.Close()
buf := make([]byte, 1024*64)
n, err := r.Body.Read(buf)
if err != nil {
gLog.Println(LevelINFO, "netInfo error:", err)
return nil
}
rsp := NetInfo{}
err = json.Unmarshal(buf[:n], &rsp)
if err != nil {
gLog.Printf(LevelERROR, "wrong NetInfo:%s", err)
// sometime will be failed, retry
for i := 0; i < 2; i++ {
client := &http.Client{Transport: tr, Timeout: time.Second * 10}
r, err := client.Get("https://ifconfig.co/json")
if err != nil {
gLog.Println(LevelINFO, "netInfo error:", err)
continue
}
defer r.Body.Close()
buf := make([]byte, 1024*64)
n, err := r.Body.Read(buf)
if err != nil {
gLog.Println(LevelINFO, "netInfo error:", err)
continue
}
rsp := NetInfo{}
err = json.Unmarshal(buf[:n], &rsp)
if err != nil {
gLog.Printf(LevelERROR, "wrong NetInfo:%s", err)
continue
}
return &rsp
}
return &rsp
return nil
}

0 comments on commit 8353c1c

Please sign in to comment.