1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-24 13:04:41 -05:00
sub2sing-box/parser/trojan.go

158 lines
3.8 KiB
Go
Raw Normal View History

2024-03-10 15:13:42 -04:00
package parser
import (
"net/url"
"strings"
2024-03-22 04:10:15 -04:00
"sub2sing-box/constant"
"sub2sing-box/model"
2024-03-10 15:13:42 -04:00
)
func ParseTrojan(proxy string) (model.Outbound, error) {
2024-03-22 04:10:15 -04:00
if !strings.HasPrefix(proxy, constant.TrojanPrefix) {
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
proxy = strings.TrimPrefix(proxy, constant.TrojanPrefix)
urlParts := strings.SplitN(proxy, "@", 2)
if len(urlParts) != 2 {
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing character '@' in url",
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
password := strings.TrimSpace(urlParts[0])
serverInfo := strings.SplitN(urlParts[1], "#", 2)
2024-03-10 15:13:42 -04:00
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
2024-03-22 04:10:15 -04:00
if len(serverAndPortAndParams) != 2 {
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing character '?' in url",
Raw: proxy,
}
}
2024-03-10 15:13:42 -04:00
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
2024-03-22 04:10:15 -04:00
if len(serverAndPort) != 2 {
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing server host or port",
Raw: proxy,
}
}
server, portStr := serverAndPort[0], serverAndPort[1]
2024-03-10 15:13:42 -04:00
params, err := url.ParseQuery(serverAndPortAndParams[1])
if err != nil {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
Type: ErrCannotParseParams,
Raw: proxy,
Message: err.Error(),
}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
port, err := ParsePort(portStr)
2024-03-10 15:13:42 -04:00
if err != nil {
return model.Outbound{}, err
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
2024-03-10 15:13:42 -04:00
remarks := ""
if len(serverInfo) == 2 {
remarks, _ = url.QueryUnescape(strings.TrimSpace(serverInfo[1]))
} else {
remarks = serverAndPort[0]
}
2024-03-22 04:10:15 -04:00
network, security, alpnStr, sni, pbk, sid, fp, path, host, serviceName := params.Get("type"), params.Get("security"), params.Get("alpn"), params.Get("sni"), params.Get("pbk"), params.Get("sid"), params.Get("fp"), params.Get("path"), params.Get("host"), params.Get("serviceName")
var alpn []string
if strings.Contains(alpnStr, ",") {
alpn = strings.Split(alpnStr, ",")
} else {
alpn = nil
}
enableUTLS := fp != ""
result := model.Outbound{
2024-03-10 15:13:42 -04:00
Type: "trojan",
2024-03-11 09:00:13 -04:00
Tag: remarks,
TrojanOptions: model.TrojanOutboundOptions{
ServerOptions: model.ServerOptions{
Server: server,
2024-03-22 04:10:15 -04:00
ServerPort: port,
},
Password: password,
2024-03-22 04:10:15 -04:00
Network: network,
2024-03-10 15:13:42 -04:00
},
}
2024-03-22 04:10:15 -04:00
if security == "xtls" || security == "tls" {
result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{
Enabled: true,
ALPN: alpn,
2024-03-22 04:10:15 -04:00
ServerName: sni,
},
2024-03-11 00:53:58 -04:00
}
}
2024-03-22 04:10:15 -04:00
2024-03-11 00:53:58 -04:00
if params.Get("security") == "reality" {
result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{
Enabled: true,
2024-03-22 04:10:15 -04:00
ServerName: sni,
Reality: &model.OutboundRealityOptions{
Enabled: true,
2024-03-22 04:10:15 -04:00
PublicKey: pbk,
ShortID: sid,
},
UTLS: &model.OutboundUTLSOptions{
2024-03-22 04:10:15 -04:00
Enabled: enableUTLS,
Fingerprint: fp,
},
2024-03-11 00:53:58 -04:00
},
}
}
2024-03-22 04:10:15 -04:00
2024-03-11 00:53:58 -04:00
if params.Get("type") == "ws" {
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "ws",
WebsocketOptions: model.V2RayWebsocketOptions{
2024-03-22 04:10:15 -04:00
Path: path,
2024-03-11 00:53:58 -04:00
Headers: map[string]string{
2024-03-22 04:10:15 -04:00
"Host": host,
2024-03-11 00:53:58 -04:00
},
},
}
}
2024-03-22 04:10:15 -04:00
2024-03-11 00:53:58 -04:00
if params.Get("type") == "http" {
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "http",
HTTPOptions: model.V2RayHTTPOptions{
2024-03-22 04:10:15 -04:00
Host: []string{host},
2024-03-11 00:53:58 -04:00
Path: params.Get("path"),
},
}
}
2024-03-22 04:10:15 -04:00
2024-03-11 00:53:58 -04:00
if params.Get("type") == "quic" {
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "quic",
QUICOptions: model.V2RayQUICOptions{},
2024-03-11 00:53:58 -04:00
}
}
2024-03-22 04:10:15 -04:00
2024-03-11 00:53:58 -04:00
if params.Get("type") == "grpc" {
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "grpc",
GRPCOptions: model.V2RayGRPCOptions{
2024-03-22 04:10:15 -04:00
ServiceName: serviceName,
2024-03-11 00:53:58 -04:00
},
}
}
2024-03-10 15:13:42 -04:00
return result, nil
}