From 6bcac6cb1016295a0722015ec853ece135766cc3 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+rprx@users.noreply.github.com> Date: Mon, 28 Dec 2020 03:20:39 +0800 Subject: [PATCH] Move common/net/connection.go into cnc folder --- app/commander/outbound.go | 3 ++- app/dns/dohdns.go | 7 ++++--- app/proxyman/outbound/handler.go | 3 ++- common/net/{ => cnc}/connection.go | 8 ++++---- core/functions.go | 9 +++++---- transport/internet/http/dialer.go | 9 +++++---- transport/internet/http/hub.go | 13 +++++++------ 7 files changed, 29 insertions(+), 23 deletions(-) rename common/net/{ => cnc}/connection.go (97%) diff --git a/app/commander/outbound.go b/app/commander/outbound.go index 3cc48790..0616933f 100644 --- a/app/commander/outbound.go +++ b/app/commander/outbound.go @@ -6,6 +6,7 @@ import ( "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/net" + "github.com/xtls/xray-core/common/net/cnc" "github.com/xtls/xray-core/common/signal/done" "github.com/xtls/xray-core/transport" ) @@ -79,7 +80,7 @@ func (co *Outbound) Dispatch(ctx context.Context, link *transport.Link) { } closeSignal := done.New() - c := net.NewConnection(net.ConnectionInputMulti(link.Writer), net.ConnectionOutputMulti(link.Reader), net.ConnectionOnClose(closeSignal)) + c := cnc.NewConnection(cnc.ConnectionInputMulti(link.Writer), cnc.ConnectionOutputMulti(link.Reader), cnc.ConnectionOnClose(closeSignal)) co.listener.add(c) co.access.RUnlock() <-closeSignal.Wait() diff --git a/app/dns/dohdns.go b/app/dns/dohdns.go index a9e3b8bf..a0c5a092 100644 --- a/app/dns/dohdns.go +++ b/app/dns/dohdns.go @@ -14,6 +14,7 @@ import ( "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/net" + "github.com/xtls/xray-core/common/net/cnc" "github.com/xtls/xray-core/common/protocol/dns" "github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/common/signal/pubsub" @@ -65,9 +66,9 @@ func NewDoHNameServer(url *url.URL, dispatcher routing.Dispatcher, clientIP net. if err != nil { return nil, err } - return net.NewConnection( - net.ConnectionInputMulti(link.Writer), - net.ConnectionOutputMulti(link.Reader), + return cnc.NewConnection( + cnc.ConnectionInputMulti(link.Writer), + cnc.ConnectionOutputMulti(link.Reader), ), nil }, } diff --git a/app/proxyman/outbound/handler.go b/app/proxyman/outbound/handler.go index 91945ea9..0452ff67 100644 --- a/app/proxyman/outbound/handler.go +++ b/app/proxyman/outbound/handler.go @@ -7,6 +7,7 @@ import ( "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/mux" "github.com/xtls/xray-core/common/net" + "github.com/xtls/xray-core/common/net/cnc" "github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/core" "github.com/xtls/xray-core/features/outbound" @@ -173,7 +174,7 @@ func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Conn downlinkReader, downlinkWriter := pipe.New(opts...) go handler.Dispatch(ctx, &transport.Link{Reader: uplinkReader, Writer: downlinkWriter}) - conn := net.NewConnection(net.ConnectionInputMulti(uplinkWriter), net.ConnectionOutputMulti(downlinkReader)) + conn := cnc.NewConnection(cnc.ConnectionInputMulti(uplinkWriter), cnc.ConnectionOutputMulti(downlinkReader)) if config := tls.ConfigFromStreamSettings(h.streamSettings); config != nil { tlsConfig := config.GetTLSConfig(tls.WithDestination(dest)) diff --git a/common/net/connection.go b/common/net/cnc/connection.go similarity index 97% rename from common/net/connection.go rename to common/net/cnc/connection.go index 2429f695..42b632e7 100644 --- a/common/net/connection.go +++ b/common/net/cnc/connection.go @@ -1,12 +1,12 @@ -package net +package cnc import ( "io" - "net" "time" "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/buf" + "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/signal/done" ) @@ -88,8 +88,8 @@ type connection struct { writer buf.Writer done *done.Instance onClose io.Closer - local Addr - remote Addr + local net.Addr + remote net.Addr } func (c *connection) Read(b []byte) (int, error) { diff --git a/core/functions.go b/core/functions.go index e35bdb57..1de87e48 100644 --- a/core/functions.go +++ b/core/functions.go @@ -6,6 +6,7 @@ import ( "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/net" + "github.com/xtls/xray-core/common/net/cnc" "github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/transport/internet/udp" ) @@ -53,13 +54,13 @@ func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, err if err != nil { return nil, err } - var readerOpt net.ConnectionOption + var readerOpt cnc.ConnectionOption if dest.Network == net.Network_TCP { - readerOpt = net.ConnectionOutputMulti(r.Reader) + readerOpt = cnc.ConnectionOutputMulti(r.Reader) } else { - readerOpt = net.ConnectionOutputMultiUDP(r.Reader) + readerOpt = cnc.ConnectionOutputMultiUDP(r.Reader) } - return net.NewConnection(net.ConnectionInputMulti(r.Writer), readerOpt), nil + return cnc.NewConnection(cnc.ConnectionInputMulti(r.Writer), readerOpt), nil } // DialUDP provides a way to exchange UDP packets through Xray instance to remote servers. diff --git a/transport/internet/http/dialer.go b/transport/internet/http/dialer.go index d8e0f0cc..2255b9f2 100644 --- a/transport/internet/http/dialer.go +++ b/transport/internet/http/dialer.go @@ -10,6 +10,7 @@ import ( "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/net" + "github.com/xtls/xray-core/common/net/cnc" "github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet/tls" "github.com/xtls/xray-core/transport/pipe" @@ -124,10 +125,10 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me bwriter := buf.NewBufferedWriter(pwriter) common.Must(bwriter.SetBuffered(false)) - return net.NewConnection( - net.ConnectionOutput(response.Body), - net.ConnectionInput(bwriter), - net.ConnectionOnClose(common.ChainedClosable{breader, bwriter, response.Body}), + return cnc.NewConnection( + cnc.ConnectionOutput(response.Body), + cnc.ConnectionInput(bwriter), + cnc.ConnectionOnClose(common.ChainedClosable{breader, bwriter, response.Body}), ), nil } diff --git a/transport/internet/http/hub.go b/transport/internet/http/hub.go index fcbe99be..76388b5f 100644 --- a/transport/internet/http/hub.go +++ b/transport/internet/http/hub.go @@ -14,6 +14,7 @@ import ( "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/net" + "github.com/xtls/xray-core/common/net/cnc" http_proto "github.com/xtls/xray-core/common/protocol/http" "github.com/xtls/xray-core/common/serial" "github.com/xtls/xray-core/common/session" @@ -97,12 +98,12 @@ func (l *Listener) ServeHTTP(writer http.ResponseWriter, request *http.Request) } done := done.New() - conn := net.NewConnection( - net.ConnectionOutput(request.Body), - net.ConnectionInput(flushWriter{w: writer, d: done}), - net.ConnectionOnClose(common.ChainedClosable{done, request.Body}), - net.ConnectionLocalAddr(l.Addr()), - net.ConnectionRemoteAddr(remoteAddr), + conn := cnc.NewConnection( + cnc.ConnectionOutput(request.Body), + cnc.ConnectionInput(flushWriter{w: writer, d: done}), + cnc.ConnectionOnClose(common.ChainedClosable{done, request.Body}), + cnc.ConnectionLocalAddr(l.Addr()), + cnc.ConnectionRemoteAddr(remoteAddr), ) l.handler(conn) <-done.Wait()