feat: 调整判断网络状态方式

This commit is contained in:
Mmx
2021-09-19 22:47:53 +08:00
parent bffdadda91
commit 83d020c928
5 changed files with 14 additions and 27 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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")
}
}