From 2eef1f515364e01ca6cda4bbe4fa1a2ed36c094b Mon Sep 17 00:00:00 2001 From: Vinicius Fortuna Date: Thu, 26 Oct 2023 13:39:35 -0400 Subject: [PATCH] Add tls to config. --- x/config/config.go | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/x/config/config.go b/x/config/config.go index a2dca887..c9e9870f 100644 --- a/x/config/config.go +++ b/x/config/config.go @@ -26,8 +26,22 @@ import ( "github.com/Jigsaw-Code/outline-sdk/transport" "github.com/Jigsaw-Code/outline-sdk/transport/socks5" "github.com/Jigsaw-Code/outline-sdk/transport/split" + "github.com/Jigsaw-Code/outline-sdk/transport/tls" + "github.com/Jigsaw-Code/outline-sdk/x/tls" ) +func parseConfigPart(oneDialerConfig string) (*url.URL, error) { + // Make it ":" it it's only "" to parse as a URL. + if !strings.Contains(oneDialerConfig, ":") { + oneDialerConfig += ":" + } + url, err := url.Parse(oneDialerConfig) + if err != nil { + return nil, fmt.Errorf("failed to parse config part: %w", err) + } + return url, nil +} + // NewStreamDialer creates a new [transport.StreamDialer] according to the given config. func NewStreamDialer(transportConfig string) (dialer transport.StreamDialer, err error) { dialer = &transport.TCPStreamDialer{} @@ -51,19 +65,17 @@ func newStreamDialerFromPart(innerDialer transport.StreamDialer, oneDialerConfig return nil, errors.New("empty config part") } - url, err := url.Parse(oneDialerConfig) + url, err := parseConfigPart(oneDialerConfig) if err != nil { return nil, fmt.Errorf("failed to parse config part: %w", err) } + // Please keep scheme list sorted. switch url.Scheme { case "socks5": endpoint := transport.StreamDialerEndpoint{Dialer: innerDialer, Address: url.Host} return socks5.NewStreamDialer(&endpoint) - case "ss": - return newShadowsocksStreamDialerFromURL(innerDialer, url) - case "split": prefixBytesStr := url.Opaque prefixBytes, err := strconv.Atoi(prefixBytesStr) @@ -72,6 +84,12 @@ func newStreamDialerFromPart(innerDialer transport.StreamDialer, oneDialerConfig } return split.NewStreamDialer(innerDialer, int64(prefixBytes)) + case "ss": + return newShadowsocksStreamDialerFromURL(innerDialer, url) + + case "tls": + return tls.NewStreamDialer(innerDialer), nil + default: return nil, fmt.Errorf("config scheme '%v' is not supported", url.Scheme) } @@ -100,20 +118,24 @@ func newPacketDialerFromPart(innerDialer transport.PacketDialer, oneDialerConfig return nil, errors.New("empty config part") } - url, err := url.Parse(oneDialerConfig) + url, err := parseConfigPart(oneDialerConfig) if err != nil { return nil, fmt.Errorf("failed to parse config part: %w", err) } + // Please keep scheme list sorted. switch url.Scheme { case "socks5": return nil, errors.New("socks5 is not supported for PacketDialers") + case "split": + return nil, errors.New("split is not supported for PacketDialers") + case "ss": return newShadowsocksPacketDialerFromURL(innerDialer, url) - case "split": - return nil, errors.New("split is not supported for PacketDialers") + case "tls": + return nil, errors.New("tls is not yet supported for PacketDialers") default: return nil, fmt.Errorf("config scheme '%v' is not supported", url.Scheme)