♻️ Refactor

This commit is contained in:
2024-10-03 01:24:54 +08:00
parent 573d0056d0
commit 3729675031
32 changed files with 284 additions and 829 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/sub2sing-box/model"
"github.com/sagernet/sing-box/option"
)
func ParseHysteria(proxy string) (model.Outbound, error) {
@@ -70,11 +71,11 @@ func ParseHysteria(proxy string) (model.Outbound, error) {
}
remarks = strings.TrimSpace(remarks)
return model.Outbound{
return model.Outbound{Outbound: option.Outbound{
Type: "hysteria",
Tag: remarks,
HysteriaOptions: model.HysteriaOutboundOptions{
ServerOptions: model.ServerOptions{
HysteriaOptions: option.HysteriaOutboundOptions{
ServerOptions: option.ServerOptions{
Server: server,
ServerPort: port,
},
@@ -82,14 +83,14 @@ func ParseHysteria(proxy string) (model.Outbound, error) {
Down: downmbps,
Auth: []byte(auth),
Obfs: obfs,
Network: protocol,
OutboundTLSOptionsContainer: model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{
Network: option.NetworkList(protocol),
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
Insecure: insecureBool,
ALPN: alpn,
},
},
},
}, nil
}}, nil
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/sub2sing-box/model"
"github.com/sagernet/sing-box/option"
)
func ParseHysteria2(proxy string) (model.Outbound, error) {
@@ -64,29 +65,31 @@ func ParseHysteria2(proxy string) (model.Outbound, error) {
remarks = strings.TrimSpace(remarks)
result := model.Outbound{
Type: "hysteria2",
Tag: strings.TrimSpace(remarks),
Hysteria2Options: model.Hysteria2OutboundOptions{
ServerOptions: model.ServerOptions{
Server: server,
ServerPort: port,
},
Password: password,
OutboundTLSOptionsContainer: model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{
Enabled: enableTLS,
Insecure: insecureBool,
ServerName: sni,
Outbound: option.Outbound{
Type: "hysteria2",
Tag: strings.TrimSpace(remarks),
Hysteria2Options: option.Hysteria2OutboundOptions{
ServerOptions: option.ServerOptions{
Server: server,
ServerPort: port,
},
Password: password,
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: enableTLS,
Insecure: insecureBool,
ServerName: sni,
},
},
Network: option.NetworkList(network),
},
Network: network,
},
}
if pinSHA256 != "" {
result.Hysteria2Options.OutboundTLSOptionsContainer.TLS.Certificate = []string{pinSHA256}
}
if obfs != "" {
result.Hysteria2Options.Obfs = &model.Hysteria2Obfs{
result.Hysteria2Options.Obfs = &option.Hysteria2Obfs{
Type: obfs,
Password: obfsPassword,
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/sub2sing-box/model"
"github.com/nitezs/sub2sing-box/util"
"github.com/sagernet/sing-box/option"
)
func ParseShadowsocks(proxy string) (model.Outbound, error) {
@@ -95,17 +96,19 @@ func ParseShadowsocks(proxy string) (model.Outbound, error) {
remarks = strings.TrimSpace(remarks)
result := model.Outbound{
Type: "shadowsocks",
Tag: remarks,
ShadowsocksOptions: model.ShadowsocksOutboundOptions{
ServerOptions: model.ServerOptions{
Server: server,
ServerPort: port,
Outbound: option.Outbound{
Type: "shadowsocks",
Tag: remarks,
ShadowsocksOptions: option.ShadowsocksOutboundOptions{
ServerOptions: option.ServerOptions{
Server: server,
ServerPort: port,
},
Method: method,
Password: password,
Plugin: plugin,
PluginOptions: options,
},
Method: method,
Password: password,
Plugin: plugin,
PluginOptions: options,
},
}
return result, nil

View File

@@ -7,6 +7,7 @@ import (
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/sub2sing-box/model"
"github.com/sagernet/sing-box/option"
)
func ParseTrojan(proxy string) (model.Outbound, error) {
@@ -68,22 +69,22 @@ func ParseTrojan(proxy string) (model.Outbound, error) {
enableUTLS := fp != ""
result := model.Outbound{
result := model.Outbound{Outbound: option.Outbound{
Type: "trojan",
Tag: remarks,
TrojanOptions: model.TrojanOutboundOptions{
ServerOptions: model.ServerOptions{
TrojanOptions: option.TrojanOutboundOptions{
ServerOptions: option.ServerOptions{
Server: server,
ServerPort: port,
},
Password: password,
Network: network,
Network: option.NetworkList(network),
},
}
}}
if security == "xtls" || security == "tls" || sni != "" {
result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{
result.TrojanOptions.OutboundTLSOptionsContainer = option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
ALPN: alpn,
ServerName: sni,
@@ -93,16 +94,16 @@ func ParseTrojan(proxy string) (model.Outbound, error) {
}
if security == "reality" {
result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{
result.TrojanOptions.OutboundTLSOptionsContainer = option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
ServerName: sni,
Reality: &model.OutboundRealityOptions{
Reality: &option.OutboundRealityOptions{
Enabled: true,
PublicKey: pbk,
ShortID: sid,
},
UTLS: &model.OutboundUTLSOptions{
UTLS: &option.OutboundUTLSOptions{
Enabled: enableUTLS,
Fingerprint: fp,
},
@@ -112,21 +113,21 @@ func ParseTrojan(proxy string) (model.Outbound, error) {
}
if network == "ws" {
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
result.TrojanOptions.Transport = &option.V2RayTransportOptions{
Type: "ws",
WebsocketOptions: model.V2RayWebsocketOptions{
WebsocketOptions: option.V2RayWebsocketOptions{
Path: path,
Headers: map[string]string{
"Host": host,
Headers: map[string]option.Listable[string]{
"Host": {host},
},
},
}
}
if network == "http" {
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
result.TrojanOptions.Transport = &option.V2RayTransportOptions{
Type: "http",
HTTPOptions: model.V2RayHTTPOptions{
HTTPOptions: option.V2RayHTTPOptions{
Host: []string{host},
Path: path,
},
@@ -134,16 +135,16 @@ func ParseTrojan(proxy string) (model.Outbound, error) {
}
if network == "quic" {
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
result.TrojanOptions.Transport = &option.V2RayTransportOptions{
Type: "quic",
QUICOptions: model.V2RayQUICOptions{},
QUICOptions: option.V2RayQUICOptions{},
}
}
if network == "grpc" {
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
result.TrojanOptions.Transport = &option.V2RayTransportOptions{
Type: "grpc",
GRPCOptions: model.V2RayGRPCOptions{
GRPCOptions: option.V2RayGRPCOptions{
ServiceName: serviceName,
},
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/sub2sing-box/model"
"github.com/sagernet/sing-box/option"
)
func ParseVless(proxy string) (model.Outbound, error) {
@@ -59,22 +60,22 @@ func ParseVless(proxy string) (model.Outbound, error) {
}
remarks = strings.TrimSpace(remarks)
result := model.Outbound{
result := model.Outbound{Outbound: option.Outbound{
Type: "vless",
Tag: remarks,
VLESSOptions: model.VLESSOutboundOptions{
ServerOptions: model.ServerOptions{
VLESSOptions: option.VLESSOutboundOptions{
ServerOptions: option.ServerOptions{
Server: server,
ServerPort: port,
},
UUID: uuid,
Flow: flow,
},
}
}}
if security == "tls" {
result.VLESSOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{
result.VLESSOptions.OutboundTLSOptionsContainer = option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
ALPN: alpn,
ServerName: sni,
@@ -84,13 +85,13 @@ func ParseVless(proxy string) (model.Outbound, error) {
}
if security == "reality" {
result.VLESSOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{
result.VLESSOptions.OutboundTLSOptionsContainer = option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
ALPN: alpn,
ServerName: sni,
Insecure: insecureBool,
Reality: &model.OutboundRealityOptions{
Reality: &option.OutboundRealityOptions{
Enabled: true,
PublicKey: pbk,
ShortID: sid,
@@ -100,31 +101,31 @@ func ParseVless(proxy string) (model.Outbound, error) {
}
if _type == "ws" {
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
result.VLESSOptions.Transport = &option.V2RayTransportOptions{
Type: "ws",
WebsocketOptions: model.V2RayWebsocketOptions{
WebsocketOptions: option.V2RayWebsocketOptions{
Path: path,
},
}
if host != "" {
if result.VLESSOptions.Transport.WebsocketOptions.Headers == nil {
result.VLESSOptions.Transport.WebsocketOptions.Headers = make(map[string]string)
result.VLESSOptions.Transport.WebsocketOptions.Headers = make(map[string]option.Listable[string])
}
result.VLESSOptions.Transport.WebsocketOptions.Headers["Host"] = host
result.VLESSOptions.Transport.WebsocketOptions.Headers["Host"] = option.Listable[string]{host}
}
}
if _type == "quic" {
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
result.VLESSOptions.Transport = &option.V2RayTransportOptions{
Type: "quic",
QUICOptions: model.V2RayQUICOptions{},
QUICOptions: option.V2RayQUICOptions{},
}
}
if _type == "grpc" {
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
result.VLESSOptions.Transport = &option.V2RayTransportOptions{
Type: "grpc",
GRPCOptions: model.V2RayGRPCOptions{
GRPCOptions: option.V2RayGRPCOptions{
ServiceName: serviceName,
},
}
@@ -139,16 +140,16 @@ func ParseVless(proxy string) (model.Outbound, error) {
Message: err.Error(),
}
}
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
result.VLESSOptions.Transport = &option.V2RayTransportOptions{
Type: "http",
HTTPOptions: model.V2RayHTTPOptions{
HTTPOptions: option.V2RayHTTPOptions{
Host: strings.Split(hosts, ","),
},
}
}
if enableUTLS {
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.UTLS = &model.OutboundUTLSOptions{
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.UTLS = &option.OutboundUTLSOptions{
Enabled: enableUTLS,
Fingerprint: fp,
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/sub2sing-box/model"
"github.com/nitezs/sub2sing-box/util"
"github.com/sagernet/sing-box/option"
)
func ParseVmess(proxy string) (model.Outbound, error) {
@@ -64,16 +65,18 @@ func ParseVmess(proxy string) (model.Outbound, error) {
}
result := model.Outbound{
Type: "vmess",
Tag: name,
VMessOptions: model.VMessOutboundOptions{
ServerOptions: model.ServerOptions{
Server: vmess.Add,
ServerPort: port,
Outbound: option.Outbound{
Type: "vmess",
Tag: name,
VMessOptions: option.VMessOutboundOptions{
ServerOptions: option.ServerOptions{
Server: vmess.Add,
ServerPort: port,
},
UUID: vmess.Id,
AlterId: aid,
Security: vmess.Scy,
},
UUID: vmess.Id,
AlterId: aid,
Security: vmess.Scy,
},
}
@@ -84,10 +87,10 @@ func ParseVmess(proxy string) (model.Outbound, error) {
} else {
alpn = nil
}
result.VMessOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{
result.VMessOptions.OutboundTLSOptionsContainer = option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
UTLS: &model.OutboundUTLSOptions{
UTLS: &option.OutboundUTLSOptions{
Fingerprint: vmess.Fp,
},
ALPN: alpn,
@@ -95,7 +98,7 @@ func ParseVmess(proxy string) (model.Outbound, error) {
},
}
if vmess.Fp != "" {
result.VMessOptions.OutboundTLSOptionsContainer.TLS.UTLS = &model.OutboundUTLSOptions{
result.VMessOptions.OutboundTLSOptionsContainer.TLS.UTLS = &option.OutboundUTLSOptions{
Enabled: true,
Fingerprint: vmess.Fp,
}
@@ -109,42 +112,42 @@ func ParseVmess(proxy string) (model.Outbound, error) {
if vmess.Host == "" {
vmess.Host = vmess.Add
}
result.VMessOptions.Transport = &model.V2RayTransportOptions{
result.VMessOptions.Transport = &option.V2RayTransportOptions{
Type: "ws",
WebsocketOptions: model.V2RayWebsocketOptions{
WebsocketOptions: option.V2RayWebsocketOptions{
Path: vmess.Path,
Headers: map[string]string{
"Host": vmess.Host,
Headers: map[string]option.Listable[string]{
"Host": {vmess.Host},
},
},
}
}
if vmess.Net == "quic" {
quic := model.V2RayQUICOptions{}
result.VMessOptions.Transport = &model.V2RayTransportOptions{
quic := option.V2RayQUICOptions{}
result.VMessOptions.Transport = &option.V2RayTransportOptions{
Type: "quic",
QUICOptions: quic,
}
}
if vmess.Net == "grpc" {
grpc := model.V2RayGRPCOptions{
grpc := option.V2RayGRPCOptions{
ServiceName: vmess.Path,
PermitWithoutStream: true,
}
result.VMessOptions.Transport = &model.V2RayTransportOptions{
result.VMessOptions.Transport = &option.V2RayTransportOptions{
Type: "grpc",
GRPCOptions: grpc,
}
}
if vmess.Net == "h2" {
httpOps := model.V2RayHTTPOptions{
httpOps := option.V2RayHTTPOptions{
Host: strings.Split(vmess.Host, ","),
Path: vmess.Path,
}
result.VMessOptions.Transport = &model.V2RayTransportOptions{
result.VMessOptions.Transport = &option.V2RayTransportOptions{
Type: "http",
HTTPOptions: httpOps,
}