🎨 数据同步前先判断网络连通性 Fix https://github.com/siyuan-note/siyuan/issues/7156

This commit is contained in:
Liang Ding 2023-01-24 15:51:37 +08:00
parent 1a00b75192
commit bdab8961b3
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 25 additions and 21 deletions

View file

@ -17,15 +17,29 @@
package util
import (
"github.com/imroc/req/v3"
"github.com/siyuan-note/httpclient"
"net/http"
"strings"
"time"
"github.com/88250/gulu"
"github.com/gin-gonic/gin"
"github.com/olahol/melody"
)
func IsOnline() bool {
c := req.C().SetTimeout(1 * time.Second)
resp, err := c.R().Get("https://icanhazip.com")
if nil != err {
resp, err = c.R().Get("https://api.ipify.org")
if nil != err {
resp, err = c.R().Get("https://www.baidu.com")
}
}
return nil == err && nil != resp && 200 == resp.StatusCode
}
func GetRemoteAddr(session *melody.Session) string {
ret := session.Request.Header.Get("X-forwarded-for")
ret = strings.TrimSpace(ret)