diff --git a/libv2ray_main.go b/libv2ray_main.go index 4a47da4..5724e00 100644 --- a/libv2ray_main.go +++ b/libv2ray_main.go @@ -16,9 +16,9 @@ import ( mobasset "golang.org/x/mobile/asset" - v2core "github.com/xtls/xray-core/core" v2net "github.com/xtls/xray-core/common/net" v2filesystem "github.com/xtls/xray-core/common/platform/filesystem" + v2core "github.com/xtls/xray-core/core" v2stats "github.com/xtls/xray-core/features/stats" v2serial "github.com/xtls/xray-core/infra/conf/serial" _ "github.com/xtls/xray-core/main/distro/all" @@ -29,11 +29,12 @@ import ( ) const ( - v2Asset = "xray.location.asset" + v2Asset = "xray.location.asset" xudpBaseKey = "xray.xudp.basekey" ) -/*V2RayPoint V2Ray Point Server +/* +V2RayPoint V2Ray Point Server This is territory of Go, so no getter and setters! */ type V2RayPoint struct { @@ -115,7 +116,7 @@ func (v *V2RayPoint) StopLoop() (err error) { return } -//Delegate Funcation +// Delegate Funcation func (v V2RayPoint) QueryStats(tag string, direct string) int64 { if v.statsManager == nil { return 0 @@ -165,7 +166,7 @@ func (v *V2RayPoint) pointloop() error { return nil } -func (v *V2RayPoint) MeasureDelay() (int64, error) { +func (v *V2RayPoint) MeasureDelay(url string) (int64, error) { ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second) go func() { @@ -177,7 +178,7 @@ func (v *V2RayPoint) MeasureDelay() (int64, error) { } }() - return measureInstDelay(ctx, v.Vpoint) + return measureInstDelay(ctx, v.Vpoint, url) } // InitV2Env set v2 asset path @@ -201,13 +202,7 @@ func InitV2Env(envPath string, key string) { } } -//Delegate Funcation -func TestConfig(ConfigureFileContent string) error { - _, err := v2serial.LoadJSONConfig(strings.NewReader(ConfigureFileContent)) - return err -} - -func MeasureOutboundDelay(ConfigureFileContent string) (int64, error) { +func MeasureOutboundDelay(ConfigureFileContent string, url string) (int64, error) { config, err := v2serial.LoadJSONConfig(strings.NewReader(ConfigureFileContent)) if err != nil { return -1, err @@ -225,7 +220,7 @@ func MeasureOutboundDelay(ConfigureFileContent string) (int64, error) { } inst.Start() - delay, err := measureInstDelay(context.Background(), inst) + delay, err := measureInstDelay(context.Background(), inst, url) inst.Close() return delay, err } @@ -248,15 +243,16 @@ func NewV2RayPoint(s V2RayVPNServiceSupportsSet, adns bool) *V2RayPoint { } } -/*CheckVersionX string +/* +CheckVersionX string This func will return libv2ray binding version and V2Ray version used. */ func CheckVersionX() string { - var version = 26 + var version = 26 return fmt.Sprintf("Lib v%d, Xray-core v%s", version, v2core.Version()) } -func measureInstDelay(ctx context.Context, inst *v2core.Instance) (int64, error) { +func measureInstDelay(ctx context.Context, inst *v2core.Instance, url string) (int64, error) { if inst == nil { return -1, errors.New("core instance nil") } @@ -278,14 +274,18 @@ func measureInstDelay(ctx context.Context, inst *v2core.Instance) (int64, error) Timeout: 12 * time.Second, } - req, _ := http.NewRequestWithContext(ctx, "GET", "https://www.google.com/generate_204", nil) + if len(url) <= 0 { + url = "https://www.google.com/generate_204" + } + req, _ := http.NewRequestWithContext(ctx, "GET", url, nil) start := time.Now() resp, err := c.Do(req) if err != nil { return -1, err } - if resp.StatusCode != http.StatusNoContent { - return -1, fmt.Errorf("status != 204: %s", resp.Status) + + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent { + return -1, fmt.Errorf("status != 20x: %s", resp.Status) } resp.Body.Close() return time.Since(start).Milliseconds(), nil @@ -312,4 +312,4 @@ func createStdoutLogWriter() v2commlog.WriterCreator { return &consoleLogWriter{ logger: log.New(os.Stdout, "", 0)} } -} \ No newline at end of file +}