1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-24 12:44:42 -05:00
sub2clash/parser/trojan.go

135 lines
3.0 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package parser
import (
"net/url"
"strings"
2024-04-23 01:36:33 -04:00
"sub2clash/constant"
2023-09-12 12:46:17 -04:00
"sub2clash/model"
2023-09-12 06:40:24 -04:00
)
2024-04-23 01:36:33 -04:00
// ParseTrojan 解析给定的Trojan代理URL并返回Proxy结构。
2023-09-12 06:40:24 -04:00
func ParseTrojan(proxy string) (model.Proxy, error) {
2024-04-23 01:36:33 -04:00
if !strings.HasPrefix(proxy, constant.TrojanPrefix) {
return model.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
2023-09-12 06:40:24 -04:00
}
2024-04-23 01:36:33 -04:00
proxy = strings.TrimPrefix(proxy, constant.TrojanPrefix)
urlParts := strings.SplitN(proxy, "@", 2)
if len(urlParts) != 2 {
return model.Proxy{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing character '@' in url",
Raw: proxy,
}
2023-09-12 06:40:24 -04:00
}
2024-04-23 01:36:33 -04:00
password := strings.TrimSpace(urlParts[0])
serverInfo := strings.SplitN(urlParts[1], "#", 2)
2023-09-12 06:40:24 -04:00
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
2024-04-23 01:36:33 -04:00
if len(serverAndPortAndParams) != 2 {
return model.Proxy{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing character '?' in url",
Raw: proxy,
}
}
2023-09-12 06:40:24 -04:00
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
2024-04-23 01:36:33 -04:00
if len(serverAndPort) != 2 {
return model.Proxy{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing server host or port",
Raw: proxy,
}
}
server, portStr := serverAndPort[0], serverAndPort[1]
2023-09-12 06:40:24 -04:00
params, err := url.ParseQuery(serverAndPortAndParams[1])
if err != nil {
2024-04-23 01:36:33 -04:00
return model.Proxy{}, &ParseError{
Type: ErrCannotParseParams,
Raw: proxy,
Message: err.Error(),
}
2023-09-12 06:40:24 -04:00
}
2024-04-23 01:36:33 -04:00
port, err := ParsePort(portStr)
2023-09-12 06:40:24 -04:00
if err != nil {
2024-04-23 02:39:16 -04:00
return model.Proxy{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
2023-09-12 06:40:24 -04:00
}
2024-04-23 01:36:33 -04:00
remarks := ""
if len(serverInfo) == 2 {
remarks, _ = url.QueryUnescape(strings.TrimSpace(serverInfo[1]))
} else {
remarks = serverAndPort[0]
}
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 != ""
// 构建Proxy结构体
2023-09-12 06:40:24 -04:00
result := model.Proxy{
Type: "trojan",
2024-04-23 01:36:33 -04:00
Server: server,
2023-09-12 06:40:24 -04:00
Port: port,
2024-04-23 01:36:33 -04:00
Password: password,
Name: remarks,
Network: network,
2023-09-12 06:40:24 -04:00
}
2024-04-23 01:36:33 -04:00
if security == "xtls" || security == "tls" {
result.Alpn = alpn
result.Sni = sni
result.TLS = true
}
if security == "reality" {
result.TLS = true
result.Sni = sni
result.RealityOpts = model.RealityOptions{
PublicKey: pbk,
ShortID: sid,
}
result.Fingerprint = fp
2023-09-12 06:40:24 -04:00
}
2024-04-23 01:36:33 -04:00
if network == "ws" {
result.Network = "ws"
result.WSOpts = model.WSOptions{
Path: path,
Headers: map[string]string{
"Host": host,
},
}
}
2024-04-23 02:39:16 -04:00
// if network == "http" {
// // 未查到相关支持文档
// }
2024-04-23 01:36:33 -04:00
2024-04-23 02:39:16 -04:00
// if network == "quic" {
// // 未查到相关支持文档
// }
2024-04-23 01:36:33 -04:00
if network == "grpc" {
result.GrpcOpts = model.GrpcOptions{
GrpcServiceName: serviceName,
}
}
2023-09-12 06:40:24 -04:00
return result, nil
}