mirror of
https://github.com/amnezia-vpn/amnezia-libxray.git
synced 2026-05-17 06:55:44 +03:00
remove findIp
This commit is contained in:
@@ -103,5 +103,3 @@ export xray.
|
||||
[VMessPing](https://github.com/v2fly/vmessping)
|
||||
|
||||
[FreePort](https://github.com/phayes/freeport)
|
||||
|
||||
[SeeIP](https://seeip.org/)
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package nodep
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
@@ -18,23 +16,18 @@ const (
|
||||
// url means the website we use to test speed. "https://www.google.com" is a good choice for most cases.
|
||||
// proxy means the local http/socks5 proxy, like "socks5://[::1]:1080".
|
||||
|
||||
func MeasureDelay(timeout int, url string, proxy string) (int64, string, error) {
|
||||
func MeasureDelay(timeout int, url string, proxy string) (int64, error) {
|
||||
httpTimeout := time.Second * time.Duration(timeout)
|
||||
c, err := coreHTTPClient(httpTimeout, proxy)
|
||||
if err != nil {
|
||||
return PingDelayError, "", err
|
||||
return PingDelayError, err
|
||||
}
|
||||
delay, err := pingHTTPRequest(c, url)
|
||||
if err != nil {
|
||||
return delay, "", err
|
||||
return delay, err
|
||||
}
|
||||
|
||||
ip, err := ipHTTPRequest(c)
|
||||
if err != nil {
|
||||
fmt.Println("get ip error: ", err)
|
||||
}
|
||||
|
||||
return delay, ip, nil
|
||||
return delay, nil
|
||||
}
|
||||
|
||||
func coreHTTPClient(timeout time.Duration, proxy string) (*http.Client, error) {
|
||||
@@ -62,18 +55,3 @@ func pingHTTPRequest(c *http.Client, url string) (int64, error) {
|
||||
}
|
||||
return time.Since(start).Milliseconds(), nil
|
||||
}
|
||||
|
||||
func ipHTTPRequest(c *http.Client) (string, error) {
|
||||
req, _ := http.NewRequest("GET", "https://api.seeip.org/", nil)
|
||||
resp, err := c.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
ip := string(body)
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
49
xray/ping.go
49
xray/ping.go
@@ -2,13 +2,8 @@ package xray
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/xtls/libxray/nodep"
|
||||
"github.com/xtls/xray-core/app/router"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// Ping Xray config and find the delay and country code of its outbound.
|
||||
@@ -21,52 +16,18 @@ func Ping(datDir string, configPath string, timeout int, url string, proxy strin
|
||||
initEnv(datDir)
|
||||
server, err := startXray(configPath)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("%d::%s", nodep.PingDelayError, err)
|
||||
return fmt.Sprintf("%d:%s", nodep.PingDelayError, err)
|
||||
}
|
||||
|
||||
if err := server.Start(); err != nil {
|
||||
return fmt.Sprintf("%d::%s", nodep.PingDelayError, err)
|
||||
return fmt.Sprintf("%d:%s", nodep.PingDelayError, err)
|
||||
}
|
||||
defer server.Close()
|
||||
|
||||
delay, ip, err := nodep.MeasureDelay(timeout, url, proxy)
|
||||
delay, err := nodep.MeasureDelay(timeout, url, proxy)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("%d::%s", delay, err)
|
||||
}
|
||||
country := ""
|
||||
if len(ip) != 0 {
|
||||
code, err := FindCountryCodeOfIp(datDir, ip)
|
||||
if err == nil {
|
||||
country = code
|
||||
}
|
||||
return fmt.Sprintf("%d:%s", delay, err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%d:%s:", delay, country)
|
||||
}
|
||||
|
||||
func FindCountryCodeOfIp(datDir string, ipAddress string) (string, error) {
|
||||
datPath := path.Join(datDir, "geoip.dat")
|
||||
geoipBytes, err := os.ReadFile(datPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var geoipList router.GeoIPList
|
||||
if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, geoip := range geoipList.Entry {
|
||||
m := &router.GeoIPMatcher{}
|
||||
m.SetReverseMatch(geoip.ReverseMatch)
|
||||
if err := m.Init(geoip.Cidr); err != nil {
|
||||
return "", err
|
||||
}
|
||||
ip := net.ParseIP(ipAddress)
|
||||
if ip != nil {
|
||||
if m.Match(ip) {
|
||||
return geoip.CountryCode, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("can not find ip: %s location", ipAddress)
|
||||
return fmt.Sprintf("%d:", delay)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user