From 0456c8f802d1dafac192c46276e7a92363ea95ed Mon Sep 17 00:00:00 2001 From: Nite07 Date: Tue, 23 Apr 2024 14:41:14 +0800 Subject: [PATCH] :art: Modify some code --- parser/hysteria.go | 6 +++++- parser/hysteria2.go | 8 ++++++-- parser/port.go | 11 +++-------- parser/shadowsocks.go | 6 +++++- parser/trojan.go | 18 +++++++++++------- parser/vless.go | 16 ++++++++++------ parser/vmess.go | 6 +++++- 7 files changed, 45 insertions(+), 26 deletions(-) diff --git a/parser/hysteria.go b/parser/hysteria.go index 3fc487e..2b26188 100644 --- a/parser/hysteria.go +++ b/parser/hysteria.go @@ -35,7 +35,11 @@ func ParseHysteria(proxy string) (model.Outbound, error) { port, err := ParsePort(portStr) if err != nil { - return model.Outbound{}, err + return model.Outbound{}, &ParseError{ + Type: ErrInvalidPort, + Message: err.Error(), + Raw: proxy, + } } params, err := url.ParseQuery(urlParts[1]) diff --git a/parser/hysteria2.go b/parser/hysteria2.go index 3afcff5..c3fbc72 100644 --- a/parser/hysteria2.go +++ b/parser/hysteria2.go @@ -52,7 +52,11 @@ func ParseHysteria2(proxy string) (model.Outbound, error) { port, err := ParsePort(portStr) if err != nil { - return model.Outbound{}, err + return model.Outbound{}, &ParseError{ + Type: ErrInvalidPort, + Message: err.Error(), + Raw: proxy, + } } params, err := url.ParseQuery(paramStr) @@ -74,7 +78,7 @@ func ParseHysteria2(proxy string) (model.Outbound, error) { Hysteria2Options: model.Hysteria2OutboundOptions{ ServerOptions: model.ServerOptions{ Server: server, - ServerPort: uint16(port), + ServerPort: port, }, Password: password, Obfs: &model.Hysteria2Obfs{ diff --git a/parser/port.go b/parser/port.go index 88e0e1e..9424833 100644 --- a/parser/port.go +++ b/parser/port.go @@ -1,6 +1,7 @@ package parser import ( + "errors" "strconv" ) @@ -8,16 +9,10 @@ func ParsePort(portStr string) (uint16, error) { port, err := strconv.Atoi(portStr) if err != nil { - return 0, &ParseError{ - Type: ErrInvalidPort, - Message: portStr, - } + return 0, err } if port < 1 || port > 65535 { - return 0, &ParseError{ - Type: ErrInvalidPort, - Message: portStr, - } + return 0, errors.New("invaild port range") } return uint16(port), nil } diff --git a/parser/shadowsocks.go b/parser/shadowsocks.go index e2bb7aa..f81d34b 100644 --- a/parser/shadowsocks.go +++ b/parser/shadowsocks.go @@ -57,7 +57,11 @@ func ParseShadowsocks(proxy string) (model.Outbound, error) { } port, err := ParsePort(portStr) if err != nil { - return model.Outbound{}, err + return model.Outbound{}, &ParseError{ + Type: ErrInvalidPort, + Message: err.Error(), + Raw: proxy, + } } var remarks string diff --git a/parser/trojan.go b/parser/trojan.go index dc09e97..84e3100 100644 --- a/parser/trojan.go +++ b/parser/trojan.go @@ -54,7 +54,11 @@ func ParseTrojan(proxy string) (model.Outbound, error) { port, err := ParsePort(portStr) if err != nil { - return model.Outbound{}, err + return model.Outbound{}, &ParseError{ + Type: ErrInvalidPort, + Message: err.Error(), + Raw: proxy, + } } remarks := "" @@ -98,7 +102,7 @@ func ParseTrojan(proxy string) (model.Outbound, error) { } } - if params.Get("security") == "reality" { + if security == "reality" { result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{ TLS: &model.OutboundTLSOptions{ Enabled: true, @@ -116,7 +120,7 @@ func ParseTrojan(proxy string) (model.Outbound, error) { } } - if params.Get("type") == "ws" { + if network == "ws" { result.TrojanOptions.Transport = &model.V2RayTransportOptions{ Type: "ws", WebsocketOptions: model.V2RayWebsocketOptions{ @@ -128,24 +132,24 @@ func ParseTrojan(proxy string) (model.Outbound, error) { } } - if params.Get("type") == "http" { + if network == "http" { result.TrojanOptions.Transport = &model.V2RayTransportOptions{ Type: "http", HTTPOptions: model.V2RayHTTPOptions{ Host: []string{host}, - Path: params.Get("path"), + Path: path, }, } } - if params.Get("type") == "quic" { + if network == "quic" { result.TrojanOptions.Transport = &model.V2RayTransportOptions{ Type: "quic", QUICOptions: model.V2RayQUICOptions{}, } } - if params.Get("type") == "grpc" { + if network == "grpc" { result.TrojanOptions.Transport = &model.V2RayTransportOptions{ Type: "grpc", GRPCOptions: model.V2RayGRPCOptions{ diff --git a/parser/vless.go b/parser/vless.go index ab26c04..90e84ad 100644 --- a/parser/vless.go +++ b/parser/vless.go @@ -42,7 +42,11 @@ func ParseVless(proxy string) (model.Outbound, error) { server, portStr := serverAndPort[0], serverAndPort[1] port, err := ParsePort(portStr) if err != nil { - return model.Outbound{}, err + return model.Outbound{}, &ParseError{ + Type: ErrInvalidPort, + Message: err.Error(), + Raw: proxy, + } } params, err := url.ParseQuery(serverAndPortAndParams[1]) @@ -76,7 +80,7 @@ func ParseVless(proxy string) (model.Outbound, error) { } uuid := strings.TrimSpace(urlParts[0]) - flow, security, alpnStr, sni, insecure, fp, pbk, sid, path, host, serviceName := params.Get("flow"), params.Get("security"), params.Get("alpn"), params.Get("sni"), params.Get("allowInsecure"), params.Get("fp"), params.Get("pbk"), params.Get("sid"), params.Get("path"), params.Get("host"), params.Get("serviceName") + flow, security, alpnStr, sni, insecure, fp, pbk, sid, path, host, serviceName, _type := params.Get("flow"), params.Get("security"), params.Get("alpn"), params.Get("sni"), params.Get("allowInsecure"), params.Get("fp"), params.Get("pbk"), params.Get("sid"), params.Get("path"), params.Get("host"), params.Get("serviceName"), params.Get("type") enableUTLS := fp != "" insecureBool := insecure == "1" @@ -123,7 +127,7 @@ func ParseVless(proxy string) (model.Outbound, error) { } } - if params.Get("type") == "ws" { + if _type == "ws" { result.VLESSOptions.Transport = &model.V2RayTransportOptions{ Type: "ws", WebsocketOptions: model.V2RayWebsocketOptions{ @@ -138,14 +142,14 @@ func ParseVless(proxy string) (model.Outbound, error) { } } - if params.Get("type") == "quic" { + if _type == "quic" { result.VLESSOptions.Transport = &model.V2RayTransportOptions{ Type: "quic", QUICOptions: model.V2RayQUICOptions{}, } } - if params.Get("type") == "grpc" { + if _type == "grpc" { result.VLESSOptions.Transport = &model.V2RayTransportOptions{ Type: "grpc", GRPCOptions: model.V2RayGRPCOptions{ @@ -154,7 +158,7 @@ func ParseVless(proxy string) (model.Outbound, error) { } } - if params.Get("type") == "http" { + if _type == "http" { hosts, err := url.QueryUnescape(host) if err != nil { return model.Outbound{}, &ParseError{ diff --git a/parser/vmess.go b/parser/vmess.go index a290fd6..c6b26f1 100644 --- a/parser/vmess.go +++ b/parser/vmess.go @@ -32,7 +32,11 @@ func ParseVmess(proxy string) (model.Outbound, error) { case string: port, err = ParsePort(vmess.Port.(string)) if err != nil { - return model.Outbound{}, err + return model.Outbound{}, &ParseError{ + Type: ErrInvalidPort, + Message: err.Error(), + Raw: proxy, + } } case float64: port = uint16(vmess.Port.(float64))