1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 19:43:49 +08:00

feature custom server address.

This commit is contained in:
Mrs4s 2020-11-13 19:47:54 +08:00
parent 7e3f94ad2e
commit 998b8cb146
2 changed files with 28 additions and 0 deletions

View File

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

View File

@ -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("收到服务器地址更新通知, 根据配置文件已忽略.")