GetTracemap也适用优选IP

This commit is contained in:
tsosunchia
2023-06-01 15:40:04 +08:00
parent 6476c3aff3
commit 64371fb41a
3 changed files with 45 additions and 8 deletions

View File

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

View File

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

View File

@@ -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 == "" {