feat: 调整判断网络状态方式
This commit is contained in:
@@ -1,22 +1,17 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
)
|
||||
import "github.com/Mmx233/tool"
|
||||
|
||||
type checker struct{}
|
||||
|
||||
var Checker checker
|
||||
|
||||
func (checker) NetOk() bool {
|
||||
r := &net.Resolver{ //指定DNS,防止本地DNS缓存影响
|
||||
PreferGo: true,
|
||||
Dial: NetDailEr(),
|
||||
}
|
||||
if ip, err := r.LookupIP(context.Background(), "ip4", "www.msftconnecttest.com"); err != nil { //通过DNS确认是否在线
|
||||
return false
|
||||
} else if len(ip) == 0 || ip[0].String() != "13.107.4.52" {
|
||||
h, _, e := tool.HTTP.GetBytes(&tool.GetRequest{
|
||||
Url: "https://www.baidu.com/",
|
||||
Redirect: false,
|
||||
})
|
||||
if e != nil || h.Get("Location") != "" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
@@ -3,8 +3,10 @@ package util
|
||||
import (
|
||||
"autoLogin/global"
|
||||
"autoLogin/models"
|
||||
"github.com/Mmx233/tool"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -31,4 +33,7 @@ func init() {
|
||||
_ = File.WriteJson(Path, c.FillDefault())
|
||||
|
||||
global.Config = &c
|
||||
|
||||
//http工具设定
|
||||
tool.HTTP.Options.Timeout = 3 * time.Second
|
||||
}
|
||||
|
||||
13
util/util.go
13
util/util.go
@@ -1,16 +1,12 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"autoLogin/global"
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Search(reg string, content string) (string, error) {
|
||||
@@ -49,12 +45,3 @@ func Sha1(content string) string {
|
||||
bs := h.Sum(nil)
|
||||
return fmt.Sprintf("%x\n", bs)
|
||||
}
|
||||
|
||||
func NetDailEr() func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
d := net.Dialer{
|
||||
Deadline: time.Now().Add(5 * time.Second),
|
||||
}
|
||||
return d.DialContext(ctx, "udp", global.Config.Settings.Dns+":53")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user