1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-25 17:00:53 -05:00
sub2sing-box/parser/trojan.go

115 lines
3.0 KiB
Go
Raw Normal View History

2024-03-10 15:13:42 -04:00
package parser
import (
2024-03-11 12:13:59 -04:00
"errors"
2024-03-10 15:13:42 -04:00
"net/url"
"strconv"
"strings"
2024-03-20 08:54:23 -04:00
model2 "sub2sing-box/model"
2024-03-10 15:13:42 -04:00
)
2024-03-20 08:54:23 -04:00
func ParseTrojan(proxy string) (model2.Proxy, error) {
2024-03-10 15:13:42 -04:00
if !strings.HasPrefix(proxy, "trojan://") {
2024-03-20 08:54:23 -04:00
return model2.Proxy{}, errors.New("invalid trojan Url")
2024-03-10 15:13:42 -04:00
}
parts := strings.SplitN(strings.TrimPrefix(proxy, "trojan://"), "@", 2)
if len(parts) != 2 {
2024-03-20 08:54:23 -04:00
return model2.Proxy{}, errors.New("invalid trojan Url")
2024-03-10 15:13:42 -04:00
}
serverInfo := strings.SplitN(parts[1], "#", 2)
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
params, err := url.ParseQuery(serverAndPortAndParams[1])
if err != nil {
2024-03-20 08:54:23 -04:00
return model2.Proxy{}, err
2024-03-10 15:13:42 -04:00
}
if len(serverAndPort) != 2 {
2024-03-20 08:54:23 -04:00
return model2.Proxy{}, errors.New("invalid trojan Url")
2024-03-10 15:13:42 -04:00
}
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
if err != nil {
2024-03-20 08:54:23 -04:00
return model2.Proxy{}, err
2024-03-10 15:13:42 -04:00
}
remarks := ""
if len(serverInfo) == 2 {
remarks, _ = url.QueryUnescape(strings.TrimSpace(serverInfo[1]))
} else {
remarks = serverAndPort[0]
}
server := strings.TrimSpace(serverAndPort[0])
password := strings.TrimSpace(parts[0])
2024-03-20 08:54:23 -04:00
result := model2.Proxy{
2024-03-10 15:13:42 -04:00
Type: "trojan",
2024-03-11 09:00:13 -04:00
Tag: remarks,
2024-03-20 08:54:23 -04:00
Trojan: model2.Trojan{
2024-03-10 15:13:42 -04:00
Server: server,
ServerPort: uint16(port),
2024-03-11 00:53:58 -04:00
Password: password,
Network: params.Get("type"),
2024-03-10 15:13:42 -04:00
},
}
2024-03-11 00:53:58 -04:00
if params.Get("security") == "xtls" || params.Get("security") == "tls" {
2024-03-11 12:13:59 -04:00
var alpn []string
if strings.Contains(params.Get("alpn"), ",") {
alpn = strings.Split(params.Get("alpn"), ",")
} else {
alpn = nil
}
2024-03-20 08:54:23 -04:00
result.Trojan.TLS = &model2.OutboundTLSOptions{
2024-03-11 00:53:58 -04:00
Enabled: true,
2024-03-11 12:13:59 -04:00
ALPN: alpn,
2024-03-11 00:53:58 -04:00
ServerName: params.Get("sni"),
}
}
if params.Get("security") == "reality" {
2024-03-20 08:54:23 -04:00
result.Trojan.TLS = &model2.OutboundTLSOptions{
2024-03-11 00:53:58 -04:00
Enabled: true,
ServerName: params.Get("sni"),
2024-03-20 08:54:23 -04:00
Reality: &model2.OutboundRealityOptions{
2024-03-11 00:53:58 -04:00
Enabled: true,
PublicKey: params.Get("pbk"),
ShortID: params.Get("sid"),
},
2024-03-20 08:54:23 -04:00
UTLS: &model2.OutboundUTLSOptions{
2024-03-11 00:53:58 -04:00
Enabled: params.Get("fp") != "",
Fingerprint: params.Get("fp"),
},
}
}
if params.Get("type") == "ws" {
2024-03-20 08:54:23 -04:00
result.Trojan.Transport = &model2.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "ws",
2024-03-20 08:54:23 -04:00
WebsocketOptions: model2.V2RayWebsocketOptions{
2024-03-11 00:53:58 -04:00
Path: params.Get("path"),
Headers: map[string]string{
"Host": params.Get("host"),
},
},
}
}
if params.Get("type") == "http" {
2024-03-20 08:54:23 -04:00
result.Trojan.Transport = &model2.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "http",
2024-03-20 08:54:23 -04:00
HTTPOptions: model2.V2RayHTTPOptions{
2024-03-11 00:53:58 -04:00
Host: []string{params.Get("host")},
Path: params.Get("path"),
},
}
}
if params.Get("type") == "quic" {
2024-03-20 08:54:23 -04:00
result.Trojan.Transport = &model2.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "quic",
2024-03-20 08:54:23 -04:00
QUICOptions: model2.V2RayQUICOptions{},
2024-03-11 00:53:58 -04:00
}
}
if params.Get("type") == "grpc" {
2024-03-20 08:54:23 -04:00
result.Trojan.Transport = &model2.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "grpc",
2024-03-20 08:54:23 -04:00
GRPCOptions: model2.V2RayGRPCOptions{
2024-03-11 00:53:58 -04:00
ServiceName: params.Get("serviceName"),
},
}
}
2024-03-10 15:13:42 -04:00
return result, nil
}