From 998b8cb146d39fb2d054362413e6ada2434be105 Mon Sep 17 00:00:00 2001 From: Mrs4s <1844812067@qq.com> Date: Fri, 13 Nov 2020 19:47:54 +0800 Subject: [PATCH] feature custom server address. --- global/fs.go | 20 ++++++++++++++++++++ main.go | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/global/fs.go b/global/fs.go index bdf842c..f7661c5 100644 --- a/global/fs.go +++ b/global/fs.go @@ -10,10 +10,12 @@ import ( "github.com/dustin/go-humanize" log "github.com/sirupsen/logrus" "io/ioutil" + "net" "net/url" "os" "path" "runtime" + "strconv" "strings" ) @@ -110,6 +112,24 @@ func DelFile(path string) bool { } } +func ReadAddrFile(path string) []*net.TCPAddr { + d, err := ioutil.ReadFile(path) + if err != nil { + return nil + } + str := string(d) + lines := strings.Split(str, "\n") + var ret []*net.TCPAddr + for _, l := range lines { + ip := strings.Split(l, ":") + if len(ip) == 2 { + port, _ := strconv.Atoi(ip[1]) + ret = append(ret, &net.TCPAddr{IP: net.ParseIP(ip[0]), Port: port}) + } + } + return ret +} + type WriteCounter struct { Total uint64 } diff --git a/main.go b/main.go index 1c89263..8c54053 100644 --- a/main.go +++ b/main.go @@ -246,6 +246,14 @@ func main() { log.Debug("Protocol -> " + e.Message) } }) + if global.PathExists("address.txt") { + log.Infof("检测到 address.txt 文件. 将覆盖目标IP.") + addr := global.ReadAddrFile("address.txt") + if len(addr) > 0 { + cli.SetCustomServer(addr) + } + log.Infof("读取到 %v 个自定义地址.", len(addr)) + } cli.OnServerUpdated(func(bot *client.QQClient, e *client.ServerUpdatedEvent) bool { if !conf.UseSSOAddress { log.Infof("收到服务器地址更新通知, 根据配置文件已忽略.")