From 8353c1c3dd314294488918d3ed0475a0b3781e5d Mon Sep 17 00:00:00 2001 From: hhd Date: Wed, 1 Dec 2021 18:00:23 +0800 Subject: [PATCH] ifconfig retry --- README-ZH.md | 3 ++- README.md | 3 ++- common.go | 41 +++++++++++++++++++++++------------------ 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/README-ZH.md b/README-ZH.md index ba20bee..6d66a0b 100644 --- a/README-ZH.md +++ b/README-ZH.md @@ -1,4 +1,5 @@ -[English](/README.md)|中文 +[English](/README.md)|中文 +网站: [openp2p.cn](https://openp2p.cn) ## OpenP2P是什么 它是一个开源、免费、轻量级的P2P共享网络。任何设备接入OpenP2P,随时随地访问它们。 我们的目标是:充分利用带宽,利用共享节点转发数据,建设一个远程连接的通用基础设施。 diff --git a/README.md b/README.md index a2b7f20..55a885c 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/common.go b/common.go index 99cb728..d6aaffc 100644 --- a/common.go +++ b/common.go @@ -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 }