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

154 lines
3.5 KiB
Go
Raw Permalink 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-10-02 13:24:54 -04:00
"github.com/sagernet/sing-box/option"
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 != ""
2024-10-02 13:24:54 -04:00
result := model.Outbound{Outbound: option.Outbound{
2024-03-10 15:13:42 -04:00
Type: "trojan",
2024-03-11 09:00:13 -04:00
Tag: remarks,
2024-10-02 13:24:54 -04:00
TrojanOptions: option.TrojanOutboundOptions{
ServerOptions: option.ServerOptions{
Server: server,
2024-03-22 04:10:15 -04:00
ServerPort: port,
},
Password: password,
2024-10-02 13:24:54 -04:00
Network: option.NetworkList(network),
2024-03-10 15:13:42 -04:00
},
2024-10-02 13:24:54 -04:00
}}
2024-03-22 04:10:15 -04:00
2024-08-04 05:01:09 -04:00
if security == "xtls" || security == "tls" || sni != "" {
2024-10-02 13:24:54 -04:00
result.TrojanOptions.OutboundTLSOptionsContainer = option.OutboundTLSOptionsContainer{
TLS: &option.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" {
2024-10-02 13:24:54 -04:00
result.TrojanOptions.OutboundTLSOptionsContainer = option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
2024-03-22 04:10:15 -04:00
ServerName: sni,
2024-10-02 13:24:54 -04:00
Reality: &option.OutboundRealityOptions{
Enabled: true,
2024-03-22 04:10:15 -04:00
PublicKey: pbk,
ShortID: sid,
},
2024-10-02 13:24:54 -04:00
UTLS: &option.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" {
2024-10-02 13:24:54 -04:00
result.TrojanOptions.Transport = &option.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "ws",
2024-10-02 13:24:54 -04:00
WebsocketOptions: option.V2RayWebsocketOptions{
2024-03-22 04:10:15 -04:00
Path: path,
2024-10-02 13:24:54 -04:00
Headers: map[string]option.Listable[string]{
"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" {
2024-10-02 13:24:54 -04:00
result.TrojanOptions.Transport = &option.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "http",
2024-10-02 13:24:54 -04:00
HTTPOptions: option.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" {
2024-10-02 13:24:54 -04:00
result.TrojanOptions.Transport = &option.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "quic",
2024-10-02 13:24:54 -04:00
QUICOptions: option.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" {
2024-10-02 13:24:54 -04:00
result.TrojanOptions.Transport = &option.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "grpc",
2024-10-02 13:24:54 -04:00
GRPCOptions: option.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
}