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

112 lines
2.3 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package parser
import (
2024-08-11 11:55:47 -04:00
"fmt"
2023-09-12 06:40:24 -04:00
"net/url"
"strings"
"github.com/nitezs/sub2clash/constant"
"github.com/nitezs/sub2clash/model"
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
2024-08-11 11:55:47 -04:00
link, err := url.Parse(proxy)
if err != nil {
2024-04-23 01:36:33 -04:00
return model.Proxy{}, &ParseError{
Type: ErrInvalidStruct,
2024-08-11 11:55:47 -04:00
Message: "url parse error",
2024-04-23 01:36:33 -04:00
Raw: proxy,
}
2023-09-12 06:40:24 -04:00
}
2024-04-23 01:36:33 -04:00
2024-08-11 11:55:47 -04:00
password := link.User.Username()
server := link.Hostname()
if server == "" {
2024-04-23 01:36:33 -04:00
return model.Proxy{}, &ParseError{
Type: ErrInvalidStruct,
2024-08-11 11:55:47 -04:00
Message: "missing server host",
2024-04-23 01:36:33 -04:00
Raw: proxy,
}
}
2024-08-11 11:55:47 -04:00
portStr := link.Port()
if portStr == "" {
2024-04-23 01:36:33 -04:00
return model.Proxy{}, &ParseError{
Type: ErrInvalidStruct,
2024-08-11 11:55:47 -04:00
Message: "missing server port",
2024-04-23 01:36:33 -04:00
Raw: proxy,
}
}
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
2024-08-11 11:55:47 -04:00
remarks := link.Fragment
if remarks == "" {
remarks = fmt.Sprintf("%s:%s", server, portStr)
2024-04-23 01:36:33 -04:00
}
2024-08-11 11:55:47 -04:00
remarks = strings.TrimSpace(remarks)
2024-04-23 01:36:33 -04:00
2024-08-11 11:55:47 -04:00
query := link.Query()
network, security, alpnStr, sni, pbk, sid, fp, path, host, serviceName := query.Get("type"), query.Get("security"), query.Get("alpn"), query.Get("sni"), query.Get("pbk"), query.Get("sid"), query.Get("fp"), query.Get("path"), query.Get("host"), query.Get("serviceName")
2024-04-23 01:36:33 -04:00
var alpn []string
if strings.Contains(alpnStr, ",") {
alpn = strings.Split(alpnStr, ",")
} else {
alpn = nil
}
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,
},
}
}
if network == "grpc" {
result.GrpcOpts = model.GrpcOptions{
GrpcServiceName: serviceName,
}
}
2023-09-12 06:40:24 -04:00
return result, nil
}