1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-23 22:14:41 -05:00

🎨 Modify some code

This commit is contained in:
Nite07 2024-04-23 14:41:14 +08:00
parent acf547f173
commit 0456c8f802
7 changed files with 45 additions and 26 deletions

View File

@ -35,7 +35,11 @@ func ParseHysteria(proxy string) (model.Outbound, error) {
port, err := ParsePort(portStr) port, err := ParsePort(portStr)
if err != nil { if err != nil {
return model.Outbound{}, err return model.Outbound{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
} }
params, err := url.ParseQuery(urlParts[1]) params, err := url.ParseQuery(urlParts[1])

View File

@ -52,7 +52,11 @@ func ParseHysteria2(proxy string) (model.Outbound, error) {
port, err := ParsePort(portStr) port, err := ParsePort(portStr)
if err != nil { if err != nil {
return model.Outbound{}, err return model.Outbound{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
} }
params, err := url.ParseQuery(paramStr) params, err := url.ParseQuery(paramStr)
@ -74,7 +78,7 @@ func ParseHysteria2(proxy string) (model.Outbound, error) {
Hysteria2Options: model.Hysteria2OutboundOptions{ Hysteria2Options: model.Hysteria2OutboundOptions{
ServerOptions: model.ServerOptions{ ServerOptions: model.ServerOptions{
Server: server, Server: server,
ServerPort: uint16(port), ServerPort: port,
}, },
Password: password, Password: password,
Obfs: &model.Hysteria2Obfs{ Obfs: &model.Hysteria2Obfs{

View File

@ -1,6 +1,7 @@
package parser package parser
import ( import (
"errors"
"strconv" "strconv"
) )
@ -8,16 +9,10 @@ func ParsePort(portStr string) (uint16, error) {
port, err := strconv.Atoi(portStr) port, err := strconv.Atoi(portStr)
if err != nil { if err != nil {
return 0, &ParseError{ return 0, err
Type: ErrInvalidPort,
Message: portStr,
}
} }
if port < 1 || port > 65535 { if port < 1 || port > 65535 {
return 0, &ParseError{ return 0, errors.New("invaild port range")
Type: ErrInvalidPort,
Message: portStr,
}
} }
return uint16(port), nil return uint16(port), nil
} }

View File

@ -57,7 +57,11 @@ func ParseShadowsocks(proxy string) (model.Outbound, error) {
} }
port, err := ParsePort(portStr) port, err := ParsePort(portStr)
if err != nil { if err != nil {
return model.Outbound{}, err return model.Outbound{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
} }
var remarks string var remarks string

View File

@ -54,7 +54,11 @@ func ParseTrojan(proxy string) (model.Outbound, error) {
port, err := ParsePort(portStr) port, err := ParsePort(portStr)
if err != nil { if err != nil {
return model.Outbound{}, err return model.Outbound{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
} }
remarks := "" 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{ result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{ TLS: &model.OutboundTLSOptions{
Enabled: true, 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{ result.TrojanOptions.Transport = &model.V2RayTransportOptions{
Type: "ws", Type: "ws",
WebsocketOptions: model.V2RayWebsocketOptions{ 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{ result.TrojanOptions.Transport = &model.V2RayTransportOptions{
Type: "http", Type: "http",
HTTPOptions: model.V2RayHTTPOptions{ HTTPOptions: model.V2RayHTTPOptions{
Host: []string{host}, Host: []string{host},
Path: params.Get("path"), Path: path,
}, },
} }
} }
if params.Get("type") == "quic" { if network == "quic" {
result.TrojanOptions.Transport = &model.V2RayTransportOptions{ result.TrojanOptions.Transport = &model.V2RayTransportOptions{
Type: "quic", Type: "quic",
QUICOptions: model.V2RayQUICOptions{}, QUICOptions: model.V2RayQUICOptions{},
} }
} }
if params.Get("type") == "grpc" { if network == "grpc" {
result.TrojanOptions.Transport = &model.V2RayTransportOptions{ result.TrojanOptions.Transport = &model.V2RayTransportOptions{
Type: "grpc", Type: "grpc",
GRPCOptions: model.V2RayGRPCOptions{ GRPCOptions: model.V2RayGRPCOptions{

View File

@ -42,7 +42,11 @@ func ParseVless(proxy string) (model.Outbound, error) {
server, portStr := serverAndPort[0], serverAndPort[1] server, portStr := serverAndPort[0], serverAndPort[1]
port, err := ParsePort(portStr) port, err := ParsePort(portStr)
if err != nil { if err != nil {
return model.Outbound{}, err return model.Outbound{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
} }
params, err := url.ParseQuery(serverAndPortAndParams[1]) params, err := url.ParseQuery(serverAndPortAndParams[1])
@ -76,7 +80,7 @@ func ParseVless(proxy string) (model.Outbound, error) {
} }
uuid := strings.TrimSpace(urlParts[0]) 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 != "" enableUTLS := fp != ""
insecureBool := insecure == "1" 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{ result.VLESSOptions.Transport = &model.V2RayTransportOptions{
Type: "ws", Type: "ws",
WebsocketOptions: model.V2RayWebsocketOptions{ 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{ result.VLESSOptions.Transport = &model.V2RayTransportOptions{
Type: "quic", Type: "quic",
QUICOptions: model.V2RayQUICOptions{}, QUICOptions: model.V2RayQUICOptions{},
} }
} }
if params.Get("type") == "grpc" { if _type == "grpc" {
result.VLESSOptions.Transport = &model.V2RayTransportOptions{ result.VLESSOptions.Transport = &model.V2RayTransportOptions{
Type: "grpc", Type: "grpc",
GRPCOptions: model.V2RayGRPCOptions{ 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) hosts, err := url.QueryUnescape(host)
if err != nil { if err != nil {
return model.Outbound{}, &ParseError{ return model.Outbound{}, &ParseError{

View File

@ -32,7 +32,11 @@ func ParseVmess(proxy string) (model.Outbound, error) {
case string: case string:
port, err = ParsePort(vmess.Port.(string)) port, err = ParsePort(vmess.Port.(string))
if err != nil { if err != nil {
return model.Outbound{}, err return model.Outbound{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
} }
case float64: case float64:
port = uint16(vmess.Port.(float64)) port = uint16(vmess.Port.(float64))