Skip to content

Commit

Permalink
⚡️ Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
nitezs committed Aug 5, 2024
1 parent 5afb06b commit 89bb0d0
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 106 deletions.
46 changes: 35 additions & 11 deletions model/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
"errors"
"reflect"
"strings"
C "sub2sing-box/constant"
"sub2sing-box/util"
)

type _Outbound struct {
Expand Down Expand Up @@ -83,17 +83,41 @@ func (h *Outbound) MarshalJSON() ([]byte, error) {
if err != nil {
return nil, err
}
result, err := util.MergeAndMarshal(struct {
Type string `json:"type"`
Tag string `json:"tag,omitempty"`
}{
Type: h.Type,
Tag: h.Tag,
}, rawOptions)
if err != nil {
return nil, err
result := make(map[string]any)
result["type"] = h.Type
result["tag"] = h.Tag
optsValue := reflect.ValueOf(rawOptions).Elem()
optsType := optsValue.Type()
for i := 0; i < optsType.NumField(); i++ {
field := optsValue.Field(i)
fieldType := optsType.Field(i)
if fieldType.Anonymous {
embeddedFields := reflect.ValueOf(field.Interface())
if field.Kind() == reflect.Struct {
for j := 0; j < embeddedFields.NumField(); j++ {
embeddedField := embeddedFields.Field(j)
embeddedFieldType := embeddedFields.Type().Field(j)
processField(embeddedField, embeddedFieldType, result)
}
}
} else {
processField(field, fieldType, result)
}
}
return json.Marshal(result)
}

func processField(field reflect.Value, fieldType reflect.StructField, result map[string]any) {
jsonTag := fieldType.Tag.Get("json")
if jsonTag == "-" {
return
}
tagParts := strings.Split(jsonTag, ",")
tagName := tagParts[0]
if len(tagParts) > 1 && tagParts[1] == "omitempty" && field.IsZero() {
return
}
return []byte(result), nil
result[tagName] = field.Interface()
}

func (h *Outbound) UnmarshalJSON(bytes []byte) error {
Expand Down
8 changes: 3 additions & 5 deletions parser/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ type ParseError struct {
type ParseErrorType string

const (
ErrInvalidPrefix ParseErrorType = "invalid url prefix"
ErrInvalidStruct ParseErrorType = "invalid struct"
ErrInvalidPort ParseErrorType = "invalid port number"
ErrCannotParseParams ParseErrorType = "cannot parse query parameters"
ErrInvalidBase64 ParseErrorType = "invalid base64"
ErrInvalidPrefix ParseErrorType = "invalid url prefix"
ErrInvalidStruct ParseErrorType = "invalid struct"
ErrInvalidPort ParseErrorType = "invalid port number"
)

func (e *ParseError) Error() string {
Expand Down
6 changes: 4 additions & 2 deletions parser/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func ParseShadowsocks(proxy string) (model.Outbound, error) {
Raw: proxy,
}
}
method := methodAndPass[0]
password := methodAndPass[1]

query := link.Query()
pluginStr := query.Get("plugin")
Expand Down Expand Up @@ -99,8 +101,8 @@ func ParseShadowsocks(proxy string) (model.Outbound, error) {
Server: server,
ServerPort: port,
},
Method: methodAndPass[0],
Password: methodAndPass[1],
Method: method,
Password: password,
Plugin: plugin,
PluginOptions: options,
},
Expand Down
18 changes: 9 additions & 9 deletions parser/vless.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ func ParseVless(proxy string) (model.Outbound, error) {
ALPN: alpn,
ServerName: sni,
Insecure: insecureBool,
UTLS: &model.OutboundUTLSOptions{
Enabled: enableUTLS,
Fingerprint: fp,
},
},
}
}
Expand All @@ -93,10 +89,6 @@ func ParseVless(proxy string) (model.Outbound, error) {
ALPN: alpn,
ServerName: sni,
Insecure: insecureBool,
UTLS: &model.OutboundUTLSOptions{
Enabled: enableUTLS,
Fingerprint: fp,
},
Reality: &model.OutboundRealityOptions{
Enabled: true,
PublicKey: pbk,
Expand Down Expand Up @@ -141,7 +133,7 @@ func ParseVless(proxy string) (model.Outbound, error) {
hosts, err := url.QueryUnescape(host)
if err != nil {
return model.Outbound{}, &ParseError{
Type: ErrCannotParseParams,
Type: ErrInvalidStruct,
Raw: proxy,
Message: err.Error(),
}
Expand All @@ -153,5 +145,13 @@ func ParseVless(proxy string) (model.Outbound, error) {
},
}
}

if enableUTLS {
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.UTLS = &model.OutboundUTLSOptions{
Enabled: enableUTLS,
Fingerprint: fp,
}
}

return result, nil
}
2 changes: 1 addition & 1 deletion parser/vmess.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ParseVmess(proxy string) (model.Outbound, error) {
proxy = strings.TrimPrefix(proxy, constant.VMessPrefix)
base64, err := util.DecodeBase64(proxy)
if err != nil {
return model.Outbound{}, &ParseError{Type: ErrInvalidBase64, Raw: proxy, Message: err.Error()}
return model.Outbound{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
}

var vmess model.VmessJson
Expand Down
78 changes: 0 additions & 78 deletions util/merge.go

This file was deleted.

0 comments on commit 89bb0d0

Please sign in to comment.