diff --git a/tracemap/tracemap.go b/tracemap/tracemap.go index 6151215..89d2337 100644 --- a/tracemap/tracemap.go +++ b/tracemap/tracemap.go @@ -1,21 +1,60 @@ package tracemap import ( + "crypto/tls" "errors" "fmt" "github.com/fatih/color" + "github.com/xgadget-lab/nexttrace/config" + "github.com/xgadget-lab/nexttrace/util" "io" + "net" "net/http" + "net/url" + "runtime" "strings" + "time" ) func GetMapUrl(r string) (string, error) { - url := "https://api.leo.moe/tracemap/api" - resp, err := http.Post(url, "application/json", strings.NewReader(r)) + host, port := util.GetHostAndPort() + fastIp := "api.leo.moe" + // 如果 host 是一个 IP 使用默认域名 + if valid := net.ParseIP(host); valid != nil { + host = "api.leo.moe" + } else { + // 默认配置完成,开始寻找最优 IP + fastIp = util.GetFastIP(host, port, false) + } + u := url.URL{Scheme: "https", Host: fastIp + ":" + port, Path: "/tracemap/api"} + tracemapUrl := u.String() + tracemapUrl = "https://api.leo.moe/tracemap/api" + + client := &http.Client{ + Timeout: 5 * time.Second, + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + ServerName: host, + }, + }, + } + req, err := http.NewRequest("POST", tracemapUrl, strings.NewReader(r)) if err != nil { return "", errors.New("an issue occurred while connecting to the tracemap API") } - defer resp.Body.Close() + req.Header.Add("User-Agent", fmt.Sprintf("NextTrace %s/%s/%s", config.Version, runtime.GOOS, runtime.GOARCH)) + req.Host = host + req.Header.Add("Content-Type", "application/json") + resp, err := client.Do(req) + if err != nil { + return "", errors.New("an issue occurred while connecting to the tracemap API") + } + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + return + } + }(resp.Body) body, err := io.ReadAll(resp.Body) if err != nil { return "", errors.New("an issue occurred while connecting to the tracemap API") diff --git a/util/latency.go b/util/latency.go index 58e81c4..c54e483 100644 --- a/util/latency.go +++ b/util/latency.go @@ -29,7 +29,6 @@ func GetFastIP(domain string, port string, enableOutput bool) string { select { case result = <-results: case <-time.After(1 * time.Second): - } if result == "" { log.Fatal("IP connection has been timeout, please check your network") diff --git a/wshandle/client.go b/wshandle/client.go index 81c4f94..f05c738 100644 --- a/wshandle/client.go +++ b/wshandle/client.go @@ -163,14 +163,13 @@ func createWsConn() *WsConn { interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, os.Interrupt) host, port = util.GetHostAndPort() - // 默认配置完成,开始寻找最优 IP - fastIp = util.GetFastIP(host, port, true) - // 如果 host 是一个 IP 使用默认域名 if valid := net.ParseIP(host); valid != nil { host = "api.leo.moe" + } else { + // 默认配置完成,开始寻找最优 IP + fastIp = util.GetFastIP(host, port, true) } - jwtToken, ua := envToken, []string{"Privileged Client"} err := error(nil) if envToken == "" {