feat: 调整判断网络状态方式
This commit is contained in:
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module autoLogin
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/Mmx233/tool v0.2.2
|
||||
github.com/Mmx233/tool v0.2.3
|
||||
github.com/PuerkitoBio/goquery v1.7.1 // indirect
|
||||
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf // indirect
|
||||
)
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,5 +1,5 @@
|
||||
github.com/Mmx233/tool v0.2.2 h1:1Sa/f0a4dZoXiCa12W1O3pItsRgl+qLOhItoGNBO8Do=
|
||||
github.com/Mmx233/tool v0.2.2/go.mod h1:un5MCoI21lH40fFI5S9++aja8nJb+2sjATak7PnGMZ0=
|
||||
github.com/Mmx233/tool v0.2.3 h1:fnjkYrX6Zhc0vcyPF5ihnxkNjXQ8zeAoG1Xm6xo7aBE=
|
||||
github.com/Mmx233/tool v0.2.3/go.mod h1:un5MCoI21lH40fFI5S9++aja8nJb+2sjATak7PnGMZ0=
|
||||
github.com/PuerkitoBio/goquery v1.7.1 h1:oE+T06D+1T7LNrn91B4aERsRIeCLJ/oPSa6xB9FPnz4=
|
||||
github.com/PuerkitoBio/goquery v1.7.1/go.mod h1:XY0pP4kfraEmmV1O7Uf6XyjoslwsneBbgeDjLYuN8xY=
|
||||
github.com/andybalholm/cascadia v1.2.0 h1:vuRCkM5Ozh/BfmsaTm26kbjm0mIOM3yS5Ek/F5h18aE=
|
||||
|
||||
@@ -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