mirror of
https://github.com/nxtrace/NTrace-core.git
synced 2025-08-12 06:26:39 +00:00
GetTracemap也适用优选IP
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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 == "" {
|
||||
|
||||
Reference in New Issue
Block a user