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

153 lines
3.4 KiB
Go
Raw Normal View History

2024-03-10 15:13:42 -04:00
package parser
import (
2024-08-04 05:01:09 -04:00
"fmt"
2024-03-10 15:13:42 -04:00
"net/url"
"strings"
2024-09-19 06:12:24 -04:00
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/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
2024-08-04 05:01:09 -04:00
link, err := url.Parse(proxy)
if err != nil {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
2024-08-04 05:01:09 -04:00
Message: "url parse error",
2024-03-22 04:10:15 -04:00
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
2024-08-04 05:01:09 -04:00
password := link.User.Username()
server := link.Hostname()
if server == "" {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
2024-08-04 05:01:09 -04:00
Message: "missing server host",
2024-03-22 04:10:15 -04:00
Raw: proxy,
}
}
2024-08-04 05:01:09 -04:00
portStr := link.Port()
if portStr == "" {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
2024-08-04 05:01:09 -04:00
Message: "missing server port",
2024-03-22 04:10:15 -04:00
Raw: proxy,
}
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 {
2024-04-23 02:41:14 -04:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
2024-08-04 05:01:09 -04:00
remarks := link.Fragment
if remarks == "" {
remarks = fmt.Sprintf("%s:%s", server, portStr)
2024-03-10 15:13:42 -04:00
}
2024-08-04 05:01:09 -04:00
remarks = strings.TrimSpace(remarks)
2024-03-22 04:10:15 -04:00
2024-08-04 05:01:09 -04:00
query := link.Query()
network, security, alpnStr, sni, pbk, sid, fp, path, host, serviceName, allowInsecure := 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"), query.Get("allowInsecure")
2024-03-22 04:10:15 -04:00
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
2024-08-04 05:01:09 -04:00
if security == "xtls" || security == "tls" || sni != "" {
result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{
Enabled: true,
ALPN: alpn,
2024-03-22 04:10:15 -04:00
ServerName: sni,
2024-08-04 05:01:09 -04:00
Insecure: allowInsecure == "1",
},
2024-03-11 00:53:58 -04:00
}
}
2024-03-22 04:10:15 -04:00
2024-04-23 02:41:14 -04:00
if 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-08-04 05:01:09 -04:00
Insecure: allowInsecure == "1",
2024-03-11 00:53:58 -04:00
},
}
}
2024-03-22 04:10:15 -04:00
2024-04-23 02:41:14 -04:00
if network == "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-04-23 02:41:14 -04:00
if network == "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-04-23 02:41:14 -04:00
Path: path,
2024-03-11 00:53:58 -04:00
},
}
}
2024-03-22 04:10:15 -04:00
2024-04-23 02:41:14 -04:00
if network == "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-04-23 02:41:14 -04:00
if network == "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
}