From 265a6b9b685ec01073fb63951efaea4f5a153b21 Mon Sep 17 00:00:00 2001 From: Kiva Date: Fri, 29 Sep 2023 08:51:13 +0800 Subject: [PATCH] chore: reduce string split immediately after string concat (#773) --- adapter/outbound/hysteria.go | 2 +- adapter/outbound/hysteria2.go | 2 +- adapter/outbound/shadowsocks.go | 4 ++-- adapter/outbound/singmux.go | 2 +- adapter/outbound/vmess.go | 6 +++--- transport/hysteria/core/client.go | 6 +----- 6 files changed, 9 insertions(+), 13 deletions(-) diff --git a/adapter/outbound/hysteria.go b/adapter/outbound/hysteria.go index 8a9d6258..5a41274c 100644 --- a/adapter/outbound/hysteria.go +++ b/adapter/outbound/hysteria.go @@ -46,7 +46,7 @@ type Hysteria struct { } func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) { - tcpConn, err := h.client.DialTCP(metadata.RemoteAddress(), h.genHdc(ctx, opts...)) + tcpConn, err := h.client.DialTCP(metadata.String(), metadata.DstPort, h.genHdc(ctx, opts...)) if err != nil { return nil, err } diff --git a/adapter/outbound/hysteria2.go b/adapter/outbound/hysteria2.go index 57c15a12..6ed4cd8c 100644 --- a/adapter/outbound/hysteria2.go +++ b/adapter/outbound/hysteria2.go @@ -55,7 +55,7 @@ type Hysteria2Option struct { func (h *Hysteria2) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) { options := h.Base.DialOptions(opts...) h.dialer.SetDialer(dialer.NewDialer(options...)) - c, err := h.client.DialConn(ctx, M.ParseSocksaddr(metadata.RemoteAddress())) + c, err := h.client.DialConn(ctx, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort)) if err != nil { return nil, err } diff --git a/adapter/outbound/shadowsocks.go b/adapter/outbound/shadowsocks.go index f744ec53..40868d04 100644 --- a/adapter/outbound/shadowsocks.go +++ b/adapter/outbound/shadowsocks.go @@ -123,9 +123,9 @@ func (ss *ShadowSocks) StreamConnContext(ctx context.Context, c net.Conn, metada } } if useEarly { - return ss.method.DialEarlyConn(c, M.ParseSocksaddr(metadata.RemoteAddress())), nil + return ss.method.DialEarlyConn(c, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort)), nil } else { - return ss.method.DialConn(c, M.ParseSocksaddr(metadata.RemoteAddress())) + return ss.method.DialConn(c, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort)) } } diff --git a/adapter/outbound/singmux.go b/adapter/outbound/singmux.go index c9f50ce9..fb6b1c29 100644 --- a/adapter/outbound/singmux.go +++ b/adapter/outbound/singmux.go @@ -42,7 +42,7 @@ type ProxyBase interface { func (s *SingMux) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) { options := s.base.DialOptions(opts...) s.dialer.SetDialer(dialer.NewDialer(options...)) - c, err := s.client.DialContext(ctx, "tcp", M.ParseSocksaddr(metadata.RemoteAddress())) + c, err := s.client.DialContext(ctx, "tcp", M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort)) if err != nil { return nil, err } diff --git a/adapter/outbound/vmess.go b/adapter/outbound/vmess.go index db654580..326f0c6f 100644 --- a/adapter/outbound/vmess.go +++ b/adapter/outbound/vmess.go @@ -260,10 +260,10 @@ func (v *Vmess) streamConn(c net.Conn, metadata *C.Metadata) (conn net.Conn, err } else { if N.NeedHandshake(c) { conn = v.client.DialEarlyConn(c, - M.ParseSocksaddr(metadata.RemoteAddress())) + M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort)) } else { conn, err = v.client.DialConn(c, - M.ParseSocksaddr(metadata.RemoteAddress())) + M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort)) } } if err != nil { @@ -284,7 +284,7 @@ func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d safeConnClose(c, err) }(c) - c, err = v.client.DialConn(c, M.ParseSocksaddr(metadata.RemoteAddress())) + c, err = v.client.DialConn(c, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort)) if err != nil { return nil, err } diff --git a/transport/hysteria/core/client.go b/transport/hysteria/core/client.go index a219e76c..ecc8a6f1 100644 --- a/transport/hysteria/core/client.go +++ b/transport/hysteria/core/client.go @@ -194,11 +194,7 @@ func (c *Client) openStreamWithReconnect(dialer utils.PacketDialer) (quic.Connec return c.quicSession, &wrappedQUICStream{stream}, err } -func (c *Client) DialTCP(addr string, dialer utils.PacketDialer) (net.Conn, error) { - host, port, err := utils.SplitHostPort(addr) - if err != nil { - return nil, err - } +func (c *Client) DialTCP(host string, port uint16, dialer utils.PacketDialer) (net.Conn, error) { session, stream, err := c.openStreamWithReconnect(dialer) if err != nil { return nil, err