From 0736e917d63f16d7a186ed3b17a53f28aba696a9 Mon Sep 17 00:00:00 2001 From: enfein <83481737+enfein@users.noreply.github.com> Date: Mon, 4 Sep 2023 06:37:45 +0000 Subject: [PATCH] Rename package netutil to util --- pkg/appctl/server.go | 4 +- pkg/cli/client.go | 14 ++-- pkg/cli/server.go | 4 +- pkg/http2socks/http2socks.go | 6 +- pkg/kcp/kcp_test.go | 10 +-- pkg/protocolv2/mux.go | 26 ++++--- pkg/protocolv2/mux_test.go | 54 ++++++------- pkg/protocolv2/segment.go | 18 ++--- pkg/protocolv2/segment_test.go | 34 ++++---- pkg/protocolv2/session.go | 82 +++++++++++--------- pkg/protocolv2/underlay.go | 14 ++-- pkg/protocolv2/underlay_base.go | 16 ++-- pkg/protocolv2/underlay_tcp.go | 16 ++-- pkg/protocolv2/underlay_udp.go | 58 ++++++++------ pkg/socks5/request.go | 6 +- pkg/socks5/socks5.go | 12 +-- pkg/socks5/socks5_test.go | 14 ++-- pkg/socks5client/socks5.go | 8 +- pkg/socks5client/socks5_test.go | 12 +-- pkg/tcpsession/close_on_err_test.go | 18 ++--- pkg/tcpsession/replay_test.go | 26 +++---- pkg/tcpsession/session.go | 4 +- pkg/tcpsession/session_integration_test.go | 26 +++---- pkg/testtool/rot13.go | 2 +- pkg/udpsession/replay_test.go | 38 ++++----- pkg/udpsession/session.go | 6 +- pkg/udpsession/session_integration_test.go | 26 +++---- pkg/{netutil => util}/addr.go | 2 +- pkg/{netutil => util}/conn.go | 2 +- pkg/{netutil => util}/copy.go | 2 +- pkg/{netutil => util}/hierarchy_conn.go | 2 +- pkg/{netutil => util}/hierarchy_conn_test.go | 2 +- pkg/{netutil => util}/ipdualstack.go | 2 +- pkg/{netutil => util}/ipdualstack_test.go | 2 +- pkg/{netutil => util}/pick_port.go | 2 +- pkg/{netutil => util}/pick_port_test.go | 2 +- pkg/{netutil => util}/sockopt.go | 2 +- pkg/{netutil => util}/sockopt_linux.go | 2 +- pkg/{netutil => util}/time.go | 2 +- pkg/{netutil => util}/time_test.go | 2 +- pkg/{netutil => util}/transport_protocol.go | 2 +- test/cmd/socksudpclient/socksudpclient.go | 4 +- 42 files changed, 307 insertions(+), 279 deletions(-) rename pkg/{netutil => util}/addr.go (98%) rename pkg/{netutil => util}/conn.go (99%) rename pkg/{netutil => util}/copy.go (99%) rename pkg/{netutil => util}/hierarchy_conn.go (98%) rename pkg/{netutil => util}/hierarchy_conn_test.go (98%) rename pkg/{netutil => util}/ipdualstack.go (99%) rename pkg/{netutil => util}/ipdualstack_test.go (98%) rename pkg/{netutil => util}/pick_port.go (98%) rename pkg/{netutil => util}/pick_port_test.go (98%) rename pkg/{netutil => util}/sockopt.go (98%) rename pkg/{netutil => util}/sockopt_linux.go (98%) rename pkg/{netutil => util}/time.go (98%) rename pkg/{netutil => util}/time_test.go (98%) rename pkg/{netutil => util}/transport_protocol.go (98%) diff --git a/pkg/appctl/server.go b/pkg/appctl/server.go index 5e25cc55..5b6b8b3f 100644 --- a/pkg/appctl/server.go +++ b/pkg/appctl/server.go @@ -30,11 +30,11 @@ import ( pb "github.com/enfein/mieru/pkg/appctl/appctlpb" "github.com/enfein/mieru/pkg/log" "github.com/enfein/mieru/pkg/metrics" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/socks5" "github.com/enfein/mieru/pkg/stderror" "github.com/enfein/mieru/pkg/tcpsession" "github.com/enfein/mieru/pkg/udpsession" + "github.com/enfein/mieru/pkg/util" "google.golang.org/grpc" "google.golang.org/protobuf/proto" ) @@ -126,7 +126,7 @@ func (s *serverLifecycleService) Start(ctx context.Context, req *pb.Empty) (*pb. // Run the egress socks5 server in the background. go func() { - socks5Addr := netutil.MaybeDecorateIPv6(netutil.AllIPAddr()) + ":" + strconv.Itoa(int(port)) + socks5Addr := util.MaybeDecorateIPv6(util.AllIPAddr()) + ":" + strconv.Itoa(int(port)) var l net.Listener var err error if protocol == pb.TransportProtocol_TCP { diff --git a/pkg/cli/client.go b/pkg/cli/client.go index ef44373d..bb0c8711 100644 --- a/pkg/cli/client.go +++ b/pkg/cli/client.go @@ -34,11 +34,11 @@ import ( "github.com/enfein/mieru/pkg/http2socks" "github.com/enfein/mieru/pkg/log" "github.com/enfein/mieru/pkg/metrics" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/socks5" "github.com/enfein/mieru/pkg/stderror" "github.com/enfein/mieru/pkg/tcpsession" "github.com/enfein/mieru/pkg/udpsession" + "github.com/enfein/mieru/pkg/util" "google.golang.org/grpc" "google.golang.org/protobuf/proto" ) @@ -359,14 +359,14 @@ var clientRunFunc = func(s []string) error { if bindingInfo.GetProtocol() == appctlpb.TransportProtocol_TCP { proxyConfigs = append(proxyConfigs, socks5.ProxyConfig{ NetworkType: "tcp", - Address: netutil.MaybeDecorateIPv6(proxyHost) + ":" + strconv.Itoa(int(proxyPort)), + Address: util.MaybeDecorateIPv6(proxyHost) + ":" + strconv.Itoa(int(proxyPort)), Password: hashedPassword, Dial: tcpsession.DialWithOptionsReturnConn, }) } else if bindingInfo.GetProtocol() == appctlpb.TransportProtocol_UDP { proxyConfigs = append(proxyConfigs, socks5.ProxyConfig{ NetworkType: "udp", - Address: netutil.MaybeDecorateIPv6(proxyHost) + ":" + strconv.Itoa(int(proxyPort)), + Address: util.MaybeDecorateIPv6(proxyHost) + ":" + strconv.Itoa(int(proxyPort)), Password: hashedPassword, Dial: udpsession.DialWithOptionsReturnConn, }) @@ -393,9 +393,9 @@ var clientRunFunc = func(s []string) error { // Run the local socks5 server in the background. var socks5Addr string if config.GetSocks5ListenLAN() { - socks5Addr = netutil.MaybeDecorateIPv6(netutil.AllIPAddr()) + ":" + strconv.Itoa(int(config.GetSocks5Port())) + socks5Addr = util.MaybeDecorateIPv6(util.AllIPAddr()) + ":" + strconv.Itoa(int(config.GetSocks5Port())) } else { - socks5Addr = netutil.MaybeDecorateIPv6(netutil.LocalIPAddr()) + ":" + strconv.Itoa(int(config.GetSocks5Port())) + socks5Addr = util.MaybeDecorateIPv6(util.LocalIPAddr()) + ":" + strconv.Itoa(int(config.GetSocks5Port())) } wg.Add(1) go func(socks5Addr string) { @@ -418,9 +418,9 @@ var clientRunFunc = func(s []string) error { go func(socks5Addr string) { var httpServerAddr string if config.GetHttpProxyListenLAN() { - httpServerAddr = netutil.MaybeDecorateIPv6(netutil.AllIPAddr()) + ":" + strconv.Itoa(int(config.GetHttpProxyPort())) + httpServerAddr = util.MaybeDecorateIPv6(util.AllIPAddr()) + ":" + strconv.Itoa(int(config.GetHttpProxyPort())) } else { - httpServerAddr = netutil.MaybeDecorateIPv6(netutil.LocalIPAddr()) + ":" + strconv.Itoa(int(config.GetHttpProxyPort())) + httpServerAddr = util.MaybeDecorateIPv6(util.LocalIPAddr()) + ":" + strconv.Itoa(int(config.GetHttpProxyPort())) } httpServer := http2socks.NewHTTPServer(httpServerAddr, &http2socks.Proxy{ ProxyURI: "socks5://" + socks5Addr + "?timeout=10s", diff --git a/pkg/cli/server.go b/pkg/cli/server.go index 4c98cab4..b848a2b3 100644 --- a/pkg/cli/server.go +++ b/pkg/cli/server.go @@ -33,11 +33,11 @@ import ( "github.com/enfein/mieru/pkg/http2socks" "github.com/enfein/mieru/pkg/log" "github.com/enfein/mieru/pkg/metrics" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/socks5" "github.com/enfein/mieru/pkg/stderror" "github.com/enfein/mieru/pkg/tcpsession" "github.com/enfein/mieru/pkg/udpsession" + "github.com/enfein/mieru/pkg/util" "google.golang.org/grpc" "google.golang.org/protobuf/proto" ) @@ -323,7 +323,7 @@ var serverRunFunc = func(s []string) error { // Run the egress socks5 server in the background. go func() { - socks5Addr := netutil.MaybeDecorateIPv6(netutil.AllIPAddr()) + ":" + strconv.Itoa(int(port)) + socks5Addr := util.MaybeDecorateIPv6(util.AllIPAddr()) + ":" + strconv.Itoa(int(port)) var l net.Listener var err error if protocol == appctlpb.TransportProtocol_TCP { diff --git a/pkg/http2socks/http2socks.go b/pkg/http2socks/http2socks.go index df143cda..f8077a77 100644 --- a/pkg/http2socks/http2socks.go +++ b/pkg/http2socks/http2socks.go @@ -25,8 +25,8 @@ import ( "github.com/enfein/mieru/pkg/log" "github.com/enfein/mieru/pkg/metrics" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/socks5client" + "github.com/enfein/mieru/pkg/util" ) var ( @@ -102,14 +102,14 @@ func (p *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) { } // Dial to socks server. - socksConn, err := dialFunc("tcp", netutil.MaybeDecorateIPv6(req.URL.Hostname())+":"+port) + socksConn, err := dialFunc("tcp", util.MaybeDecorateIPv6(req.URL.Hostname())+":"+port) if err != nil { HTTPConnErrors.Add(1) log.Debugf("HTTP proxy dial to socks5 server failed: %v", err) return } httpConn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n")) - netutil.BidiCopy(httpConn, socksConn, true) + util.BidiCopy(httpConn, socksConn, true) } else { // HTTP tr := &http.Transport{ diff --git a/pkg/kcp/kcp_test.go b/pkg/kcp/kcp_test.go index 6ae6fde5..f2c62115 100644 --- a/pkg/kcp/kcp_test.go +++ b/pkg/kcp/kcp_test.go @@ -26,21 +26,21 @@ import ( "time" "github.com/enfein/mieru/pkg/kcp" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/stderror" + "github.com/enfein/mieru/pkg/util" ) func newKCPPipe(t *testing.T) (*net.UDPConn, *net.UDPConn, *kcp.KCP, *kcp.KCP) { t.Helper() mrand.Seed(time.Now().UnixNano()) - port1, err := netutil.UnusedUDPPort() + port1, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } - port2, err := netutil.UnusedUDPPort() + port2, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } convId := uint32(mrand.Int31()) diff --git a/pkg/protocolv2/mux.go b/pkg/protocolv2/mux.go index 7d41e632..5f952deb 100644 --- a/pkg/protocolv2/mux.go +++ b/pkg/protocolv2/mux.go @@ -23,13 +23,14 @@ import ( mrand "math/rand" "net" "sync" + "time" "github.com/enfein/mieru/pkg/appctl/appctlpb" "github.com/enfein/mieru/pkg/cipher" "github.com/enfein/mieru/pkg/log" "github.com/enfein/mieru/pkg/mathext" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/stderror" + "github.com/enfein/mieru/pkg/util" ) // Mux manages the sessions and underlays. @@ -42,7 +43,7 @@ type Mux struct { // ---- server fields ---- users map[string]*appctlpb.User - serverHandler netutil.ConnHandler + serverHandler util.ConnHandler // ---- common fields ---- endpoints []UnderlayProperties @@ -111,7 +112,7 @@ func (m *Mux) SetServerUsers(users map[string]*appctlpb.User) *Mux { return m } -func (m *Mux) SetServerHandler(handler netutil.ConnHandler) *Mux { +func (m *Mux) SetServerHandler(handler util.ConnHandler) *Mux { m.mu.Lock() defer m.mu.Unlock() if m.isClient { @@ -169,7 +170,7 @@ func (m *Mux) Close() error { // Addr is not supported by Mux. func (m *Mux) Addr() net.Addr { - return netutil.NilNetAddr() + return util.NilNetAddr() } // ListenAndServeAll listens on all the server addresses and serves @@ -188,7 +189,7 @@ func (m *Mux) ListenAndServeAll() error { return fmt.Errorf("no server listening endpoint found") } for _, p := range m.endpoints { - if netutil.IsNilNetAddr(p.LocalAddr()) { + if util.IsNilNetAddr(p.LocalAddr()) { return fmt.Errorf("endpoint local address is not set") } } @@ -236,7 +237,7 @@ func (m *Mux) DialContext(ctx context.Context) (net.Conn, error) { return nil, fmt.Errorf("no server listening endpoint found") } for _, p := range m.endpoints { - if netutil.IsNilNetAddr(p.RemoteAddr()) { + if util.IsNilNetAddr(p.RemoteAddr()) { return nil, fmt.Errorf("endpoint remote address is not set") } } @@ -261,7 +262,7 @@ func (m *Mux) DialContext(ctx context.Context) (net.Conn, error) { i := mrand.Intn(len(m.endpoints)) p := m.endpoints[i] switch p.TransportProtocol() { - case netutil.TCPTransport: + case util.TCPTransport: block, err := cipher.BlockCipherFromPassword(m.password, false) if err != nil { return nil, fmt.Errorf("cipher.BlockCipherFromPassword() failed: %v", err) @@ -270,7 +271,7 @@ func (m *Mux) DialContext(ctx context.Context) (net.Conn, error) { if err != nil { return nil, fmt.Errorf("NewTCPUnderlay() failed: %v", err) } - case netutil.UDPTransport: + case util.UDPTransport: block, err := cipher.BlockCipherFromPassword(m.password, true) if err != nil { return nil, fmt.Errorf("cipher.BlockCipherFromPassword() failed: %v", err) @@ -320,7 +321,7 @@ func (m *Mux) acceptUnderlayLoop(properties UnderlayProperties) { switch network { case "tcp", "tcp4", "tcp6": listenConfig := net.ListenConfig{ - Control: netutil.ReuseAddrPort, + Control: util.ReuseAddrPort, } rawListener, err := listenConfig.Listen(context.Background(), network, laddr) if err != nil { @@ -370,9 +371,10 @@ func (m *Mux) acceptUnderlayLoop(properties UnderlayProperties) { } log.Infof("Mux listening to endpoint %s %s", network, laddr) underlay := &UDPUnderlay{ - baseUnderlay: *newBaseUnderlay(false, properties.MTU()), - conn: conn, - users: m.users, + baseUnderlay: *newBaseUnderlay(false, properties.MTU()), + conn: conn, + idleSessionTicker: time.NewTicker(idleSessionTickerInterval), + users: m.users, } log.Debugf("Created new server underlay %v", underlay) UnderlayPassiveOpens.Add(1) diff --git a/pkg/protocolv2/mux_test.go b/pkg/protocolv2/mux_test.go index 6619c42d..2abc00ca 100644 --- a/pkg/protocolv2/mux_test.go +++ b/pkg/protocolv2/mux_test.go @@ -28,9 +28,9 @@ import ( "github.com/enfein/mieru/pkg/appctl/appctlpb" "github.com/enfein/mieru/pkg/cipher" "github.com/enfein/mieru/pkg/log" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/rng" "github.com/enfein/mieru/pkg/testtool" + "github.com/enfein/mieru/pkg/util" "google.golang.org/protobuf/proto" ) @@ -92,14 +92,14 @@ func TestIPv4TCPUnderlay(t *testing.T) { rng.InitSeed() log.SetOutputToTest(t) log.SetLevel("DEBUG") - port, err := netutil.UnusedTCPPort() + port, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } serverDescriptor := underlayDescriptor{ mtu: 1500, - ipVersion: netutil.IPVersion4, - transportProtocol: netutil.TCPTransport, + ipVersion: util.IPVersion4, + transportProtocol: util.TCPTransport, localAddr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: port}, } serverMux := NewMux(false). @@ -116,8 +116,8 @@ func TestIPv4TCPUnderlay(t *testing.T) { clientDescriptor := underlayDescriptor{ mtu: 1500, - ipVersion: netutil.IPVersion4, - transportProtocol: netutil.TCPTransport, + ipVersion: util.IPVersion4, + transportProtocol: util.TCPTransport, remoteAddr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: port}, } runClient(t, clientDescriptor, []byte("xiaochitang"), []byte("kuiranbudong"), 4) @@ -130,14 +130,14 @@ func TestIPv6TCPUnderlay(t *testing.T) { rng.InitSeed() log.SetOutputToTest(t) log.SetLevel("DEBUG") - port, err := netutil.UnusedTCPPort() + port, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } serverDescriptor := underlayDescriptor{ mtu: 1500, - ipVersion: netutil.IPVersion6, - transportProtocol: netutil.TCPTransport, + ipVersion: util.IPVersion6, + transportProtocol: util.TCPTransport, localAddr: &net.TCPAddr{IP: net.ParseIP("::1"), Port: port}, } serverMux := NewMux(false). @@ -154,8 +154,8 @@ func TestIPv6TCPUnderlay(t *testing.T) { clientDescriptor := underlayDescriptor{ mtu: 1500, - ipVersion: netutil.IPVersion6, - transportProtocol: netutil.TCPTransport, + ipVersion: util.IPVersion6, + transportProtocol: util.TCPTransport, remoteAddr: &net.TCPAddr{IP: net.ParseIP("::1"), Port: port}, } runClient(t, clientDescriptor, []byte("xiaochitang"), []byte("kuiranbudong"), 4) @@ -168,14 +168,14 @@ func TestIPv4UDPUnderlay(t *testing.T) { rng.InitSeed() log.SetOutputToTest(t) log.SetLevel("TRACE") - port, err := netutil.UnusedUDPPort() + port, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } serverDescriptor := underlayDescriptor{ mtu: 1500, - ipVersion: netutil.IPVersion4, - transportProtocol: netutil.UDPTransport, + ipVersion: util.IPVersion4, + transportProtocol: util.UDPTransport, localAddr: &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: port}, } serverMux := NewMux(false). @@ -192,11 +192,11 @@ func TestIPv4UDPUnderlay(t *testing.T) { clientDescriptor := underlayDescriptor{ mtu: 1500, - ipVersion: netutil.IPVersion4, - transportProtocol: netutil.UDPTransport, + ipVersion: util.IPVersion4, + transportProtocol: util.UDPTransport, remoteAddr: &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: port}, } - runClient(t, clientDescriptor, []byte("xiaochitang"), []byte("kuiranbudong"), 1) + runClient(t, clientDescriptor, []byte("xiaochitang"), []byte("kuiranbudong"), 4) if err := serverMux.Close(); err != nil { t.Errorf("Server mux close failed: %v", err) } @@ -206,14 +206,14 @@ func TestIPv6UDPUnderlay(t *testing.T) { rng.InitSeed() log.SetOutputToTest(t) log.SetLevel("TRACE") - port, err := netutil.UnusedUDPPort() + port, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } serverDescriptor := underlayDescriptor{ mtu: 1500, - ipVersion: netutil.IPVersion6, - transportProtocol: netutil.UDPTransport, + ipVersion: util.IPVersion6, + transportProtocol: util.UDPTransport, localAddr: &net.UDPAddr{IP: net.ParseIP("::1"), Port: port}, } serverMux := NewMux(false). @@ -230,11 +230,11 @@ func TestIPv6UDPUnderlay(t *testing.T) { clientDescriptor := underlayDescriptor{ mtu: 1500, - ipVersion: netutil.IPVersion6, - transportProtocol: netutil.UDPTransport, + ipVersion: util.IPVersion6, + transportProtocol: util.UDPTransport, remoteAddr: &net.UDPAddr{IP: net.ParseIP("::1"), Port: port}, } - runClient(t, clientDescriptor, []byte("xiaochitang"), []byte("kuiranbudong"), 1) + runClient(t, clientDescriptor, []byte("xiaochitang"), []byte("kuiranbudong"), 4) if err := serverMux.Close(); err != nil { t.Errorf("Server mux close failed: %v", err) } diff --git a/pkg/protocolv2/segment.go b/pkg/protocolv2/segment.go index 45e849d6..85cd196f 100644 --- a/pkg/protocolv2/segment.go +++ b/pkg/protocolv2/segment.go @@ -23,8 +23,8 @@ import ( "github.com/enfein/mieru/pkg/cipher" "github.com/enfein/mieru/pkg/log" "github.com/enfein/mieru/pkg/mathext" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/stderror" + "github.com/enfein/mieru/pkg/util" "github.com/google/btree" ) @@ -40,19 +40,19 @@ const ( ) // MaxFragmentSize returns the maximum payload size in a fragment. -func MaxFragmentSize(mtu int, ipVersion netutil.IPVersion, transport netutil.TransportProtocol) int { - if transport == netutil.TCPTransport { +func MaxFragmentSize(mtu int, ipVersion util.IPVersion, transport util.TransportProtocol) int { + if transport == util.TCPTransport { // No fragment needed. return MaxPDU } res := mtu - udpOverhead - if ipVersion == netutil.IPVersion4 { + if ipVersion == util.IPVersion4 { res -= 20 } else { res -= 40 } - if transport == netutil.UDPTransport { + if transport == util.UDPTransport { res -= 8 } else { res -= 20 @@ -61,19 +61,19 @@ func MaxFragmentSize(mtu int, ipVersion netutil.IPVersion, transport netutil.Tra } // MaxPaddingSize returns the maximum padding size of a segment. -func MaxPaddingSize(mtu int, ipVersion netutil.IPVersion, transport netutil.TransportProtocol, fragmentSize int, existingPaddingSize int) int { - if transport == netutil.TCPTransport { +func MaxPaddingSize(mtu int, ipVersion util.IPVersion, transport util.TransportProtocol, fragmentSize int, existingPaddingSize int) int { + if transport == util.TCPTransport { // No limit. return 255 } res := mtu - fragmentSize - udpOverhead - if ipVersion == netutil.IPVersion4 { + if ipVersion == util.IPVersion4 { res -= 20 } else { res -= 40 } - if transport == netutil.UDPTransport { + if transport == util.UDPTransport { res -= 8 } else { res -= 20 diff --git a/pkg/protocolv2/segment_test.go b/pkg/protocolv2/segment_test.go index 5d7e4959..b0ec1011 100644 --- a/pkg/protocolv2/segment_test.go +++ b/pkg/protocolv2/segment_test.go @@ -22,33 +22,33 @@ import ( "testing" "time" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/rng" + "github.com/enfein/mieru/pkg/util" ) func TestMaxFragmentSize(t *testing.T) { testcases := []struct { mtu int - ipVersion netutil.IPVersion - transport netutil.TransportProtocol + ipVersion util.IPVersion + transport util.TransportProtocol want int }{ { 1500, - netutil.IPVersion6, - netutil.TCPTransport, + util.IPVersion6, + util.TCPTransport, MaxPDU, }, { 1500, - netutil.IPVersion4, - netutil.UDPTransport, + util.IPVersion4, + util.UDPTransport, 1472 - udpOverhead, }, { 1500, - netutil.IPVersionUnknown, - netutil.UnknownTransport, + util.IPVersionUnknown, + util.UnknownTransport, 1440 - udpOverhead, }, } @@ -63,32 +63,32 @@ func TestMaxFragmentSize(t *testing.T) { func TestMaxPaddingSize(t *testing.T) { testcases := []struct { mtu int - ipVersion netutil.IPVersion - transport netutil.TransportProtocol + ipVersion util.IPVersion + transport util.TransportProtocol fragmentSize int existingPaddingSize int want int }{ { 1500, - netutil.IPVersion6, - netutil.TCPTransport, + util.IPVersion6, + util.TCPTransport, MaxPDU, 255, 255, }, { 1500, - netutil.IPVersion4, - netutil.UDPTransport, + util.IPVersion4, + util.UDPTransport, 1472 - udpOverhead - 16, 12, 4, }, { 1500, - netutil.IPVersionUnknown, - netutil.UnknownTransport, + util.IPVersionUnknown, + util.UnknownTransport, 0, 255, 255, diff --git a/pkg/protocolv2/session.go b/pkg/protocolv2/session.go index f3d6c949..c17a7e49 100644 --- a/pkg/protocolv2/session.go +++ b/pkg/protocolv2/session.go @@ -28,18 +28,21 @@ import ( "github.com/enfein/mieru/pkg/log" "github.com/enfein/mieru/pkg/mathext" "github.com/enfein/mieru/pkg/metrics" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/stderror" + "github.com/enfein/mieru/pkg/util" ) const ( - segmentTreeCapacity = 4096 - segmentChanCapacity = 256 - minWindowSize = 16 - maxWindowSize = 4096 + segmentTreeCapacity = 4096 + segmentChanCapacity = 1024 + minWindowSize = 16 + maxWindowSize = 4096 + segmentRetryInterval = 10 * time.Millisecond segmentAckDelay = 50 * time.Millisecond - serverRespTimeout = 10 * time.Second + + serverRespTimeout = 10 * time.Second + sessionHeartbeatInterval = 5 * time.Second ) type sessionState int @@ -78,6 +81,7 @@ type Session struct { nextSeq uint32 // next sequence number to send a segment nextRecv uint32 // next sequence number to receive + lastRXTime time.Time // last timestamp when a segment is received lastTXTime time.Time // last timestamp when a segment is sent unreadBuf []byte // payload removed from the recvQueue that haven't been read by application @@ -105,8 +109,8 @@ func NewSession(id uint32, isClient bool, mtu int) *Session { isClient: isClient, mtu: mtu, state: sessionInit, - readDeadline: netutil.ZeroTime(), - writeDeadline: netutil.ZeroTime(), + readDeadline: util.ZeroTime(), + writeDeadline: util.ZeroTime(), ready: make(chan struct{}), established: make(chan struct{}), done: make(chan struct{}), @@ -117,6 +121,7 @@ func NewSession(id uint32, isClient bool, mtu int) *Session { recvBuf: newSegmentTree(segmentTreeCapacity), recvQueue: newSegmentTree(segmentTreeCapacity), recvChan: make(chan *segment, segmentChanCapacity), + lastRXTime: time.Now(), lastTXTime: time.Now(), rttStat: rttStat, sendAlgorithm: congestion.NewCubicSendAlgorithm(minWindowSize, maxWindowSize), @@ -144,7 +149,7 @@ func (s *Session) Read(b []byte) (n int, err error) { defer s.rLock.Unlock() defer func() { - s.readDeadline = netutil.ZeroTime() + s.readDeadline = util.ZeroTime() }() if log.IsLevelEnabled(log.TraceLevel) { log.Tracef("%v trying to read %d bytes", s, len(b)) @@ -163,7 +168,7 @@ func (s *Session) Read(b []byte) (n int, err error) { } var timeC <-chan time.Time - if !netutil.IsZeroTime(s.readDeadline) { + if !util.IsZeroTime(s.readDeadline) { timeC = time.After(time.Until(s.readDeadline)) } @@ -234,7 +239,7 @@ func (s *Session) Write(b []byte) (n int, err error) { defer s.wLock.Unlock() defer func() { - s.writeDeadline = netutil.ZeroTime() + s.writeDeadline = util.ZeroTime() }() if s.state == sessionAttached { if s.isClient { @@ -289,7 +294,7 @@ func (s *Session) Write(b []byte) (n int, err error) { } var timeC <-chan time.Time - if !netutil.IsZeroTime(s.writeDeadline) { + if !util.IsZeroTime(s.writeDeadline) { timeC = time.After(time.Until(s.writeDeadline)) } @@ -369,19 +374,22 @@ func (s *Session) Close() error { s.wLock.Lock() defer s.wLock.Unlock() - s.forwardStateTo(sessionClosing) - seg := &segment{ - metadata: &sessionStruct{ - baseStruct: baseStruct{ - protocol: closeSessionRequest, + if s.conn.TransportProtocol() == util.TCPTransport { + // Send closeSessionRequest, and wait for closeSessionResponse. + s.forwardStateTo(sessionClosing) + seg := &segment{ + metadata: &sessionStruct{ + baseStruct: baseStruct{ + protocol: closeSessionRequest, + }, + sessionID: s.id, + seq: s.nextSeq, }, - sessionID: s.id, - seq: s.nextSeq, - }, + } + s.nextSeq++ + s.sendQueue.InsertBlocking(seg) + <-s.done } - s.nextSeq++ - s.sendQueue.InsertBlocking(seg) - <-s.done s.forwardStateTo(sessionClosed) return nil } @@ -391,7 +399,7 @@ func (s *Session) LocalAddr() net.Addr { } func (s *Session) RemoteAddr() net.Addr { - if !netutil.IsNilNetAddr(s.remoteAddr) { + if !util.IsNilNetAddr(s.remoteAddr) { return s.remoteAddr } return s.conn.RemoteAddr() @@ -451,7 +459,7 @@ func (s *Session) runOutputLoop(ctx context.Context) error { return lastErr } switch s.conn.TransportProtocol() { - case netutil.TCPTransport: + case util.TCPTransport: for { seg, ok := s.sendQueue.DeleteMin() if !ok { @@ -464,7 +472,7 @@ func (s *Session) runOutputLoop(ctx context.Context) error { return err } } - case netutil.UDPTransport: + case util.UDPTransport: needRetransmission := false hasLoss := false hasTimeout := false @@ -537,8 +545,8 @@ func (s *Session) runOutputLoop(ctx context.Context) error { } } if !needRetransmission && segmentMoved == 0 { - // Send ACK if needed. - if time.Since(s.lastTXTime) > segmentAckDelay { + // Send ACK or heartbeat if needed. + if (s.sendBuf.Len() > 0 && time.Since(s.lastTXTime) > segmentAckDelay) || time.Since(s.lastRXTime) > sessionHeartbeatInterval { baseStruct := baseStruct{} if s.isClient { baseStruct.protocol = ackClientToServer @@ -549,7 +557,7 @@ func (s *Session) runOutputLoop(ctx context.Context) error { metadata: &dataAckStruct{ baseStruct: baseStruct, sessionID: s.id, - seq: uint32(mathext.Max(0, int(s.nextRecv)-1)), + seq: uint32(mathext.Max(0, int(s.nextSeq)-1)), unAckSeq: s.nextRecv, windowSize: uint16(mathext.Max(0, int(s.sendAlgorithm.CongestionWindowSize())-s.recvBuf.Len())), }, @@ -587,6 +595,7 @@ func (s *Session) input(seg *segment) error { if seg.block != nil { s.block = seg.block } + s.lastRXTime = time.Now() if protocol == openSessionRequest || protocol == openSessionResponse || protocol == dataServerToClient || protocol == dataClientToServer { return s.inputData(seg) } else if protocol == ackServerToClient || protocol == ackClientToServer { @@ -599,11 +608,11 @@ func (s *Session) input(seg *segment) error { func (s *Session) inputData(seg *segment) error { switch s.conn.TransportProtocol() { - case netutil.TCPTransport: + case util.TCPTransport: // Deliver the segment directly to recvQueue. s.recvQueue.InsertBlocking(seg) return nil - case netutil.UDPTransport: + case util.UDPTransport: // Delete all previous acknowledged segments from sendBuf. das, ok := seg.metadata.(*dataAckStruct) if ok { @@ -666,10 +675,10 @@ func (s *Session) inputData(seg *segment) error { func (s *Session) inputAck(seg *segment) error { switch s.conn.TransportProtocol() { - case netutil.TCPTransport: + case util.TCPTransport: // Do nothing when receive ACK from TCP protocol. return nil - case netutil.UDPTransport: + case util.UDPTransport: // Delete all previous acknowledged segments from sendBuf. das := seg.metadata.(*dataAckStruct) unAckSeq := das.unAckSeq @@ -735,16 +744,19 @@ func (s *Session) inputClose(seg *segment) error { func (s *Session) output(seg *segment, remoteAddr net.Addr) error { switch s.conn.TransportProtocol() { - case netutil.TCPTransport: + case util.TCPTransport: if err := s.conn.(*TCPUnderlay).writeOneSegment(seg); err != nil { return fmt.Errorf("TCPUnderlay.writeOneSegment() failed: %v", err) } - case netutil.UDPTransport: + case util.UDPTransport: err := s.conn.(*UDPUnderlay).writeOneSegment(seg, remoteAddr.(*net.UDPAddr)) if err != nil { if !stderror.ShouldRetry(err) { return fmt.Errorf("UDPUnderlay.writeOneSegment() failed: %v", err) } + if log.IsLevelEnabled(log.TraceLevel) { + log.Tracef("UDPUnderlay.writeOneSegment() failed: %v. Will retry later.", err) + } return nil } default: diff --git a/pkg/protocolv2/underlay.go b/pkg/protocolv2/underlay.go index 8c52fa6d..ac1e9b66 100644 --- a/pkg/protocolv2/underlay.go +++ b/pkg/protocolv2/underlay.go @@ -20,7 +20,7 @@ import ( "net" "github.com/enfein/mieru/pkg/metrics" - "github.com/enfein/mieru/pkg/netutil" + "github.com/enfein/mieru/pkg/util" ) var ( @@ -38,10 +38,10 @@ type UnderlayProperties interface { MTU() int // The IP version used to establish the underlay. - IPVersion() netutil.IPVersion + IPVersion() util.IPVersion // The transport protocol used to implement the underlay. - TransportProtocol() netutil.TransportProtocol + TransportProtocol() util.TransportProtocol // Implement the LocalAddr() method in net.Conn interface. LocalAddr() net.Addr @@ -77,8 +77,8 @@ type Underlay interface { type underlayDescriptor struct { mtu int - ipVersion netutil.IPVersion - transportProtocol netutil.TransportProtocol + ipVersion util.IPVersion + transportProtocol util.TransportProtocol localAddr net.Addr remoteAddr net.Addr } @@ -89,11 +89,11 @@ func (d underlayDescriptor) MTU() int { return d.mtu } -func (d underlayDescriptor) IPVersion() netutil.IPVersion { +func (d underlayDescriptor) IPVersion() util.IPVersion { return d.ipVersion } -func (d underlayDescriptor) TransportProtocol() netutil.TransportProtocol { +func (d underlayDescriptor) TransportProtocol() util.TransportProtocol { return d.transportProtocol } diff --git a/pkg/protocolv2/underlay_base.go b/pkg/protocolv2/underlay_base.go index 9597530c..616b640c 100644 --- a/pkg/protocolv2/underlay_base.go +++ b/pkg/protocolv2/underlay_base.go @@ -23,8 +23,8 @@ import ( "sync" "github.com/enfein/mieru/pkg/metrics" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/stderror" + "github.com/enfein/mieru/pkg/util" ) // baseUnderlay contains a base implementation of underlay. @@ -80,27 +80,27 @@ func (b *baseUnderlay) Close() error { } func (b *baseUnderlay) Addr() net.Addr { - return netutil.NilNetAddr() + return util.NilNetAddr() } func (b *baseUnderlay) MTU() int { return b.mtu } -func (b *baseUnderlay) IPVersion() netutil.IPVersion { - return netutil.IPVersionUnknown +func (b *baseUnderlay) IPVersion() util.IPVersion { + return util.IPVersionUnknown } -func (b *baseUnderlay) TransportProtocol() netutil.TransportProtocol { - return netutil.UnknownTransport +func (b *baseUnderlay) TransportProtocol() util.TransportProtocol { + return util.UnknownTransport } func (b *baseUnderlay) LocalAddr() net.Addr { - return netutil.NilNetAddr() + return util.NilNetAddr() } func (b *baseUnderlay) RemoteAddr() net.Addr { - return netutil.NilNetAddr() + return util.NilNetAddr() } func (b *baseUnderlay) AddSession(s *Session, remoteAddr net.Addr) error { diff --git a/pkg/protocolv2/underlay_tcp.go b/pkg/protocolv2/underlay_tcp.go index 8e1e0416..8a7119ac 100644 --- a/pkg/protocolv2/underlay_tcp.go +++ b/pkg/protocolv2/underlay_tcp.go @@ -25,16 +25,16 @@ import ( "github.com/enfein/mieru/pkg/cipher" "github.com/enfein/mieru/pkg/log" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/replay" "github.com/enfein/mieru/pkg/rng" "github.com/enfein/mieru/pkg/stderror" + "github.com/enfein/mieru/pkg/util" ) type TCPUnderlay struct { baseUnderlay conn *net.TCPConn - ipVersion netutil.IPVersion + ipVersion util.IPVersion send cipher.BlockCipher recv cipher.BlockCipher @@ -107,18 +107,18 @@ func (t *TCPUnderlay) Addr() net.Addr { return t.LocalAddr() } -func (t *TCPUnderlay) IPVersion() netutil.IPVersion { +func (t *TCPUnderlay) IPVersion() util.IPVersion { if t.conn == nil { - return netutil.IPVersionUnknown + return util.IPVersionUnknown } - if t.ipVersion == netutil.IPVersionUnknown { - t.ipVersion = netutil.GetIPVersion(t.conn.LocalAddr().String()) + if t.ipVersion == util.IPVersionUnknown { + t.ipVersion = util.GetIPVersion(t.conn.LocalAddr().String()) } return t.ipVersion } -func (t *TCPUnderlay) TransportProtocol() netutil.TransportProtocol { - return netutil.TCPTransport +func (t *TCPUnderlay) TransportProtocol() util.TransportProtocol { + return util.TCPTransport } func (t *TCPUnderlay) LocalAddr() net.Addr { diff --git a/pkg/protocolv2/underlay_udp.go b/pkg/protocolv2/underlay_udp.go index fb01698a..455740ad 100644 --- a/pkg/protocolv2/underlay_udp.go +++ b/pkg/protocolv2/underlay_udp.go @@ -27,15 +27,18 @@ import ( "github.com/enfein/mieru/pkg/appctl/appctlpb" "github.com/enfein/mieru/pkg/cipher" "github.com/enfein/mieru/pkg/log" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/replay" "github.com/enfein/mieru/pkg/rng" "github.com/enfein/mieru/pkg/stderror" + "github.com/enfein/mieru/pkg/util" ) const ( udpOverhead = cipher.DefaultNonceSize + metadataLength + cipher.DefaultOverhead*2 udpNonHeaderPosition = cipher.DefaultNonceSize + metadataLength + cipher.DefaultOverhead + + idleSessionTickerInterval = 5 * time.Second + idleSessionTimeout = time.Minute ) var udpReplayCache = replay.NewCache(16*1024*1024, 2*time.Minute) @@ -44,14 +47,16 @@ type UDPUnderlay struct { // ---- common fields ---- baseUnderlay conn *net.UDPConn - ipVersion netutil.IPVersion + ipVersion util.IPVersion // sendMutex is used when write data to the connection. sendMutex sync.Mutex + idleSessionTicker *time.Ticker + // ---- client fields ---- - block cipher.BlockCipher serverAddr *net.UDPAddr + block cipher.BlockCipher // ---- server fields ---- users map[string]*appctlpb.User // registered users @@ -89,10 +94,11 @@ func NewUDPUnderlay(ctx context.Context, network, laddr, raddr string, mtu int, } log.Debugf("Created new client UDP underlay [%v - %v]", conn.LocalAddr(), remoteAddr) return &UDPUnderlay{ - baseUnderlay: *newBaseUnderlay(true, mtu), - conn: conn, - serverAddr: remoteAddr, - block: block, + baseUnderlay: *newBaseUnderlay(true, mtu), + conn: conn, + idleSessionTicker: time.NewTicker(idleSessionTickerInterval), + serverAddr: remoteAddr, + block: block, }, nil } @@ -115,22 +121,23 @@ func (u *UDPUnderlay) Close() error { } log.Debugf("Closing %v", u) + u.idleSessionTicker.Stop() u.baseUnderlay.Close() return u.conn.Close() } -func (u *UDPUnderlay) IPVersion() netutil.IPVersion { +func (u *UDPUnderlay) IPVersion() util.IPVersion { if u.conn == nil { - return netutil.IPVersionUnknown + return util.IPVersionUnknown } - if u.ipVersion == netutil.IPVersionUnknown { - u.ipVersion = netutil.GetIPVersion(u.conn.LocalAddr().String()) + if u.ipVersion == util.IPVersionUnknown { + u.ipVersion = util.GetIPVersion(u.conn.LocalAddr().String()) } return u.ipVersion } -func (u *UDPUnderlay) TransportProtocol() netutil.TransportProtocol { - return netutil.UDPTransport +func (u *UDPUnderlay) TransportProtocol() util.TransportProtocol { + return util.UDPTransport } func (u *UDPUnderlay) LocalAddr() net.Addr { @@ -141,7 +148,7 @@ func (u *UDPUnderlay) RemoteAddr() net.Addr { if u.serverAddr != nil { return u.serverAddr } - return netutil.NilNetAddr() + return util.NilNetAddr() } func (u *UDPUnderlay) AddSession(s *Session, remoteAddr net.Addr) error { @@ -187,6 +194,19 @@ func (u *UDPUnderlay) RunEventLoop(ctx context.Context) error { return nil case <-u.done: return nil + case <-u.idleSessionTicker.C: + // Close idle sessions. + for _, session := range u.sessionMap { + if time.Since(session.lastRXTime) > idleSessionTimeout { + if err := session.Close(); err != nil && !stderror.IsEOF(err) && !stderror.IsClosed(err) { + log.Debugf("%v Close() failed: %v", session, err) + } + session.wg.Wait() + if err := u.RemoveSession(session); err != nil { + log.Debugf("%v RemoveSession() failed: %v", u, err) + } + } + } default: } seg, addr, err := u.readOneSegment() @@ -223,8 +243,6 @@ func (u *UDPUnderlay) RunEventLoop(ctx context.Context) error { continue } session.recvChan <- seg - } else if isCloseConnProtocol(seg.metadata.Protocol()) { - // TODO: Close connection. } // Ignore other protocols. } @@ -330,7 +348,6 @@ func (u *UDPUnderlay) readOneSegment() (*segment, *net.UDPAddr, error) { // Decrypt metadata. var decryptedMeta []byte var blockCipher cipher.BlockCipher - isKnownSession := true if u.isClient { decryptedMeta, err = u.block.Decrypt(encryptedMeta) if err != nil { @@ -358,7 +375,6 @@ func (u *UDPUnderlay) readOneSegment() (*segment, *net.UDPAddr, error) { u.sessionLock.Unlock() if !decrypted { // This is a new session. Try all registered users. - isKnownSession = false for _, user := range u.users { var password []byte password, err = hex.DecodeString(user.GetHashedPassword()) @@ -407,7 +423,7 @@ func (u *UDPUnderlay) readOneSegment() (*segment, *net.UDPAddr, error) { if err != nil { return nil, nil, err } - if !isKnownSession && blockCipher != nil { + if blockCipher != nil { seg.block = blockCipher } return seg, addr, nil @@ -420,13 +436,11 @@ func (u *UDPUnderlay) readOneSegment() (*segment, *net.UDPAddr, error) { if err != nil { return nil, nil, err } - if !isKnownSession && blockCipher != nil { + if blockCipher != nil { seg.block = blockCipher } return seg, addr, nil } - - // TODO: handle close connection return nil, nil, fmt.Errorf("unable to handle protocol %d", p) } } diff --git a/pkg/socks5/request.go b/pkg/socks5/request.go index 97cf59b4..f06c3d6f 100644 --- a/pkg/socks5/request.go +++ b/pkg/socks5/request.go @@ -11,8 +11,8 @@ import ( "sync/atomic" "github.com/enfein/mieru/pkg/log" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/stderror" + "github.com/enfein/mieru/pkg/util" ) const ( @@ -179,7 +179,7 @@ func (s *Server) handleConnect(ctx context.Context, conn io.ReadWriteCloser, req return fmt.Errorf("failed to send reply: %w", err) } - return netutil.BidiCopy(conn, target, false) + return util.BidiCopy(conn, target, false) } // handleBind is used to handle a bind command. @@ -196,7 +196,7 @@ func (s *Server) handleBind(ctx context.Context, conn io.ReadWriteCloser, req *R func (s *Server) handleAssociate(ctx context.Context, conn io.ReadWriteCloser, req *Request) error { // Create a UDP listener on a random port. // All the requests associated to this connection will go through this port. - udpListenerAddr, err := net.ResolveUDPAddr("udp", netutil.MaybeDecorateIPv6(netutil.AllIPAddr())+":0") + udpListenerAddr, err := net.ResolveUDPAddr("udp", util.MaybeDecorateIPv6(util.AllIPAddr())+":0") if err != nil { UDPAssociateErrors.Add(1) return fmt.Errorf("failed to resolve UDP address: %w", err) diff --git a/pkg/socks5/socks5.go b/pkg/socks5/socks5.go index 810f4715..e7f8579e 100644 --- a/pkg/socks5/socks5.go +++ b/pkg/socks5/socks5.go @@ -12,8 +12,8 @@ import ( "github.com/enfein/mieru/pkg/cipher" "github.com/enfein/mieru/pkg/log" "github.com/enfein/mieru/pkg/metrics" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/stderror" + "github.com/enfein/mieru/pkg/util" ) const ( @@ -115,7 +115,7 @@ func New(conf *Config) (*Server, error) { // Provide a default bind IP. if conf.BindIP == nil { - conf.BindIP = net.ParseIP(netutil.AllIPAddr()) + conf.BindIP = net.ParseIP(util.AllIPAddr()) if conf.BindIP == nil { return nil, fmt.Errorf("set socks5 bind IP failed") } @@ -177,7 +177,7 @@ func (s *Server) Serve(l net.Listener) error { // ServeConn is used to serve a single connection. func (s *Server) ServeConn(conn net.Conn) error { - conn = netutil.WrapHierarchyConn(conn) + conn = util.WrapHierarchyConn(conn) defer conn.Close() if log.IsLevelEnabled(log.TraceLevel) { log.Tracef("socks5 server starts to serve connection [%v - %v]", conn.LocalAddr(), conn.RemoteAddr()) @@ -263,14 +263,14 @@ func (s *Server) clientServeConn(conn net.Conn) error { if udpAssociateConn != nil { log.Debugf("UDP association is listening on %v", udpAssociateConn.LocalAddr()) - conn.(netutil.HierarchyConn).AddSubConnection(udpAssociateConn) + conn.(util.HierarchyConn).AddSubConnection(udpAssociateConn) go func() { - netutil.WaitForClose(conn) + util.WaitForClose(conn) conn.Close() }() return BidiCopyUDP(udpAssociateConn, WrapUDPAssociateTunnel(proxyConn)) } - return netutil.BidiCopy(conn, proxyConn, true) + return util.BidiCopy(conn, proxyConn, true) } func (s *Server) serverServeConn(conn net.Conn) error { diff --git a/pkg/socks5/socks5_test.go b/pkg/socks5/socks5_test.go index 42cae66b..75846675 100644 --- a/pkg/socks5/socks5_test.go +++ b/pkg/socks5/socks5_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - "github.com/enfein/mieru/pkg/netutil" + "github.com/enfein/mieru/pkg/util" ) func TestSocks5Connect(t *testing.T) { @@ -58,9 +58,9 @@ func TestSocks5Connect(t *testing.T) { } // Socks server start listening. - serverPort, err := netutil.UnusedTCPPort() + serverPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } go func() { if err := serv.ListenAndServe("tcp", "127.0.0.1:"+strconv.Itoa(serverPort)); err != nil { @@ -173,9 +173,9 @@ func TestSocks5UDPAssociation(t *testing.T) { } // Socks server start listening. - serverPort, err := netutil.UnusedTCPPort() + serverPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } go func() { if err := serv.ListenAndServe("tcp", "127.0.0.1:"+strconv.Itoa(serverPort)); err != nil { @@ -262,9 +262,9 @@ func TestServerGroup(t *testing.T) { t.Fatalf("New() failed: %v", err) } g := NewGroup() - port, err := netutil.UnusedUDPPort() + port, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } if err := g.Add("UDP", port, s1); err != nil { t.Fatalf("Add() failed: %v", err) diff --git a/pkg/socks5client/socks5.go b/pkg/socks5client/socks5.go index 00096f68..bcf7634e 100644 --- a/pkg/socks5client/socks5.go +++ b/pkg/socks5client/socks5.go @@ -7,7 +7,7 @@ import ( "net" "strconv" - "github.com/enfein/mieru/pkg/netutil" + "github.com/enfein/mieru/pkg/util" ) func (cfg *Config) dialSocks5(targetAddr string) (conn net.Conn, err error) { @@ -48,7 +48,7 @@ func (cfg *Config) dialSocks5Long(targetAddr string) (conn net.Conn, udpConn *ne // Process the first response. var resp []byte - resp, err = netutil.SendReceive(ctx, conn, req.Bytes()) + resp, err = util.SendReceive(ctx, conn, req.Bytes()) if err != nil { return nil, nil, nil, err } else if len(resp) != 2 { @@ -69,7 +69,7 @@ func (cfg *Config) dialSocks5Long(targetAddr string) (conn net.Conn, udpConn *ne req.Write([]byte{byte(len(cfg.Auth.Password))}) req.Write([]byte(cfg.Auth.Password)) - resp, err = netutil.SendReceive(ctx, conn, req.Bytes()) + resp, err = util.SendReceive(ctx, conn, req.Bytes()) if err != nil { return nil, nil, nil, err } else if len(resp) != 2 { @@ -122,7 +122,7 @@ func (cfg *Config) dialSocks5Long(targetAddr string) (conn net.Conn, udpConn *ne }) // Process the second response. - resp, err = netutil.SendReceive(ctx, conn, req.Bytes()) + resp, err = util.SendReceive(ctx, conn, req.Bytes()) if err != nil { return } else if len(resp) < 10 { diff --git a/pkg/socks5client/socks5_test.go b/pkg/socks5client/socks5_test.go index a566fc4b..e72fd614 100644 --- a/pkg/socks5client/socks5_test.go +++ b/pkg/socks5client/socks5_test.go @@ -10,12 +10,12 @@ import ( "testing" "time" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/socks5" + "github.com/enfein/mieru/pkg/util" ) var httpTestServer = func() *http.Server { - httpTestPort, err := netutil.UnusedTCPPort() + httpTestPort, err := util.UnusedTCPPort() if err != nil { panic(err) } @@ -90,9 +90,9 @@ func tcpReady(port int, timeout time.Duration) { } func TestSocks5Anonymous(t *testing.T) { - port, err := netutil.UnusedTCPPort() + port, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } newTestSocksServer(port, false) dialSocksProxy := Dial(fmt.Sprintf("socks5://127.0.0.1:%d?timeout=5s", port), ConnectCmd) @@ -113,9 +113,9 @@ func TestSocks5Anonymous(t *testing.T) { } func TestSocks5Auth(t *testing.T) { - port, err := netutil.UnusedTCPPort() + port, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } newTestSocksServer(port, true) dialSocksProxy := Dial(fmt.Sprintf("socks5://test_user:test_pass@127.0.0.1:%d?timeout=5s", port), ConnectCmd) diff --git a/pkg/tcpsession/close_on_err_test.go b/pkg/tcpsession/close_on_err_test.go index 4d036712..8e8da741 100644 --- a/pkg/tcpsession/close_on_err_test.go +++ b/pkg/tcpsession/close_on_err_test.go @@ -24,20 +24,20 @@ import ( "time" "github.com/enfein/mieru/pkg/appctl/appctlpb" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/rng" "github.com/enfein/mieru/pkg/testtool" + "github.com/enfein/mieru/pkg/util" "google.golang.org/protobuf/proto" ) func TestCloseOnErr(t *testing.T) { - serverPort, err := netutil.UnusedTCPPort() + serverPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } - clientPort, err := netutil.UnusedTCPPort() + clientPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } serverAddr := fmt.Sprintf("127.0.0.1:%d", serverPort) clientAddr := fmt.Sprintf("127.0.0.1:%d", clientPort) @@ -133,13 +133,13 @@ func TestCloseOnErr(t *testing.T) { } func TestErrorMetrics(t *testing.T) { - serverPort, err := netutil.UnusedTCPPort() + serverPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } - clientPort, err := netutil.UnusedTCPPort() + clientPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } serverAddr := fmt.Sprintf("127.0.0.1:%d", serverPort) clientAddr := fmt.Sprintf("127.0.0.1:%d", clientPort) diff --git a/pkg/tcpsession/replay_test.go b/pkg/tcpsession/replay_test.go index 2ac34c64..1bf853e2 100644 --- a/pkg/tcpsession/replay_test.go +++ b/pkg/tcpsession/replay_test.go @@ -28,10 +28,10 @@ import ( "github.com/enfein/mieru/pkg/appctl/appctlpb" "github.com/enfein/mieru/pkg/cipher" "github.com/enfein/mieru/pkg/metrics" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/recording" "github.com/enfein/mieru/pkg/replay" "github.com/enfein/mieru/pkg/testtool" + "github.com/enfein/mieru/pkg/util" "google.golang.org/protobuf/proto" ) @@ -134,17 +134,17 @@ func runCloseWaitClient(t *testing.T, laddr, raddr string, username, password [] // the client's request to the server. The monitor should not get any response // from the server. func TestReplayClientRequestToServer(t *testing.T) { - serverPort, err := netutil.UnusedTCPPort() + serverPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } - clientPort, err := netutil.UnusedTCPPort() + clientPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } - attackPort, err := netutil.UnusedTCPPort() + attackPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } serverAddr := fmt.Sprintf("127.0.0.1:%d", serverPort) clientAddr := fmt.Sprintf("127.0.0.1:%d", clientPort) @@ -252,17 +252,17 @@ func TestReplayClientRequestToServer(t *testing.T) { // the server's response back to the server. The monitor should not get any // response from the server. func TestReplayServerResponseToServer(t *testing.T) { - serverPort, err := netutil.UnusedTCPPort() + serverPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } - clientPort, err := netutil.UnusedTCPPort() + clientPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } - attackPort, err := netutil.UnusedTCPPort() + attackPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } serverAddr := fmt.Sprintf("127.0.0.1:%d", serverPort) clientAddr := fmt.Sprintf("127.0.0.1:%d", clientPort) diff --git a/pkg/tcpsession/session.go b/pkg/tcpsession/session.go index 586723b8..1d91491e 100644 --- a/pkg/tcpsession/session.go +++ b/pkg/tcpsession/session.go @@ -32,11 +32,11 @@ import ( "github.com/enfein/mieru/pkg/cipher" "github.com/enfein/mieru/pkg/log" "github.com/enfein/mieru/pkg/metrics" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/recording" "github.com/enfein/mieru/pkg/replay" "github.com/enfein/mieru/pkg/rng" "github.com/enfein/mieru/pkg/stderror" + "github.com/enfein/mieru/pkg/util" ) const ( @@ -524,7 +524,7 @@ func (l *TCPSessionListener) acceptLoop() { // ListenWithOptions creates a new TCPSession listener. func ListenWithOptions(laddr string, users map[string]*appctlpb.User) (*TCPSessionListener, error) { listenConfig := net.ListenConfig{ - Control: netutil.ReuseAddrPort, + Control: util.ReuseAddrPort, } l, err := listenConfig.Listen(context.Background(), "tcp", laddr) if err != nil { diff --git a/pkg/tcpsession/session_integration_test.go b/pkg/tcpsession/session_integration_test.go index a894bac2..bf1673c5 100644 --- a/pkg/tcpsession/session_integration_test.go +++ b/pkg/tcpsession/session_integration_test.go @@ -26,10 +26,10 @@ import ( "github.com/enfein/mieru/pkg/appctl/appctlpb" "github.com/enfein/mieru/pkg/cipher" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/rng" "github.com/enfein/mieru/pkg/tcpsession" "github.com/enfein/mieru/pkg/testtool" + "github.com/enfein/mieru/pkg/util" "google.golang.org/protobuf/proto" ) @@ -97,9 +97,9 @@ func runClient(t *testing.T, laddr, serverAddr string, username, password []byte func TestTCPSessionIPv4(t *testing.T) { rng.InitSeed() - serverPort, err := netutil.UnusedTCPPort() + serverPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } party, err := tcpsession.ListenWithOptions(fmt.Sprintf("127.0.0.1:%d", serverPort), users) if err != nil { @@ -123,13 +123,13 @@ func TestTCPSessionIPv4(t *testing.T) { }() time.Sleep(1 * time.Second) - clientPort1, err := netutil.UnusedTCPPort() + clientPort1, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } - clientPort2, err := netutil.UnusedTCPPort() + clientPort2, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } var wg sync.WaitGroup wg.Add(2) @@ -157,9 +157,9 @@ func TestTCPSessionIPv4(t *testing.T) { func TestTCPSessionIPv6(t *testing.T) { rng.InitSeed() - serverPort, err := netutil.UnusedTCPPort() + serverPort, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } party, err := tcpsession.ListenWithOptions(fmt.Sprintf("[::1]:%d", serverPort), users) if err != nil { @@ -183,13 +183,13 @@ func TestTCPSessionIPv6(t *testing.T) { }() time.Sleep(1 * time.Second) - clientPort1, err := netutil.UnusedTCPPort() + clientPort1, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } - clientPort2, err := netutil.UnusedTCPPort() + clientPort2, err := util.UnusedTCPPort() if err != nil { - t.Fatalf("netutil.UnusedTCPPort() failed: %v", err) + t.Fatalf("util.UnusedTCPPort() failed: %v", err) } var wg sync.WaitGroup wg.Add(2) diff --git a/pkg/testtool/rot13.go b/pkg/testtool/rot13.go index c1e987e3..6b8cd5ed 100644 --- a/pkg/testtool/rot13.go +++ b/pkg/testtool/rot13.go @@ -89,7 +89,7 @@ func TestHelperServeConn(conn net.Conn) error { } } -// TestHelperConnHandler implements netutil.ConnHandler interface. +// TestHelperConnHandler implements util.ConnHandler interface. type TestHelperConnHandler struct{} func (h TestHelperConnHandler) Take(conn net.Conn) (closed bool, err error) { diff --git a/pkg/udpsession/replay_test.go b/pkg/udpsession/replay_test.go index c72a0c92..52788290 100644 --- a/pkg/udpsession/replay_test.go +++ b/pkg/udpsession/replay_test.go @@ -26,10 +26,10 @@ import ( "github.com/enfein/mieru/pkg/appctl/appctlpb" "github.com/enfein/mieru/pkg/cipher" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/recording" "github.com/enfein/mieru/pkg/replay" "github.com/enfein/mieru/pkg/testtool" + "github.com/enfein/mieru/pkg/util" "google.golang.org/protobuf/proto" ) @@ -132,17 +132,17 @@ func runCloseWaitClient(t *testing.T, laddr, raddr string, username, password [] // the server's response back to the client. The client should drop the replay // packet before processing it. func TestReplayServerResponseToClient(t *testing.T) { - serverPort, err := netutil.UnusedUDPPort() + serverPort, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } - clientPort, err := netutil.UnusedUDPPort() + clientPort, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } - attackPort, err := netutil.UnusedUDPPort() + attackPort, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } serverAddr := fmt.Sprintf("127.0.0.1:%d", serverPort) clientAddr := fmt.Sprintf("127.0.0.1:%d", clientPort) @@ -232,17 +232,17 @@ func TestReplayServerResponseToClient(t *testing.T) { // the client's request to the server. The monitor should not get any response // from the server. func TestReplayClientRequestToServer(t *testing.T) { - serverPort, err := netutil.UnusedUDPPort() + serverPort, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } - clientPort, err := netutil.UnusedUDPPort() + clientPort, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } - attackPort, err := netutil.UnusedUDPPort() + attackPort, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } serverAddr := fmt.Sprintf("127.0.0.1:%d", serverPort) clientAddr := fmt.Sprintf("127.0.0.1:%d", clientPort) @@ -351,17 +351,17 @@ func TestReplayClientRequestToServer(t *testing.T) { // the server's response back to the server. The server should drop the replay // packet before processing it. func TestReplayServerResponseToServer(t *testing.T) { - serverPort, err := netutil.UnusedUDPPort() + serverPort, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } - clientPort, err := netutil.UnusedUDPPort() + clientPort, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } - attackPort, err := netutil.UnusedUDPPort() + attackPort, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } serverAddr := fmt.Sprintf("127.0.0.1:%d", serverPort) clientAddr := fmt.Sprintf("127.0.0.1:%d", clientPort) diff --git a/pkg/udpsession/session.go b/pkg/udpsession/session.go index ea13ba8c..53f6c118 100644 --- a/pkg/udpsession/session.go +++ b/pkg/udpsession/session.go @@ -19,12 +19,12 @@ import ( "github.com/enfein/mieru/pkg/kcp" "github.com/enfein/mieru/pkg/log" "github.com/enfein/mieru/pkg/metrics" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/recording" "github.com/enfein/mieru/pkg/replay" "github.com/enfein/mieru/pkg/rng" "github.com/enfein/mieru/pkg/schedule" "github.com/enfein/mieru/pkg/stderror" + "github.com/enfein/mieru/pkg/util" "golang.org/x/net/ipv4" "golang.org/x/net/ipv6" ) @@ -170,7 +170,7 @@ func newUDPSession(conv uint32, conn net.PacketConn, ownConn bool, remote net.Ad sess.kcp.ReserveBytes(kcpReservedSize) - if netutil.GetIPVersion(sess.remote.String()) == netutil.IPVersion4 { + if util.GetIPVersion(sess.remote.String()) == util.IPVersion4 { sess.SetMtu(int(globalMTU) - 20 - 8 - kcp.OuterHeaderSize) } else { sess.SetMtu(int(globalMTU) - 40 - 8 - kcp.OuterHeaderSize) @@ -203,7 +203,7 @@ func newUDPSession(conv uint32, conn net.PacketConn, ownConn bool, remote net.Ad // Read implements net.Conn func (s *UDPSession) Read(b []byte) (n int, err error) { // Clear read deadline after a read. - defer s.SetReadDeadline(netutil.ZeroTime()) + defer s.SetReadDeadline(util.ZeroTime()) for { s.mu.Lock() // When recvBufPtr has remaining data, copy from recvBufPtr. diff --git a/pkg/udpsession/session_integration_test.go b/pkg/udpsession/session_integration_test.go index cfefb40a..77df787b 100644 --- a/pkg/udpsession/session_integration_test.go +++ b/pkg/udpsession/session_integration_test.go @@ -26,10 +26,10 @@ import ( "github.com/enfein/mieru/pkg/appctl/appctlpb" "github.com/enfein/mieru/pkg/cipher" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/rng" "github.com/enfein/mieru/pkg/testtool" "github.com/enfein/mieru/pkg/udpsession" + "github.com/enfein/mieru/pkg/util" "google.golang.org/protobuf/proto" ) @@ -108,9 +108,9 @@ func runClient(t *testing.T, laddr, serverAddr string, username, password []byte // ROT13 (rotate by 13 places) of the data back to the client. func TestKCPSessionsIPv4(t *testing.T) { rng.InitSeed() - serverPort, err := netutil.UnusedUDPPort() + serverPort, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } party, err := udpsession.ListenWithOptions(fmt.Sprintf("127.0.0.1:%d", serverPort), users) if err != nil { @@ -134,13 +134,13 @@ func TestKCPSessionsIPv4(t *testing.T) { }() time.Sleep(1 * time.Second) - clientPort1, err := netutil.UnusedUDPPort() + clientPort1, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } - clientPort2, err := netutil.UnusedUDPPort() + clientPort2, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } var wg sync.WaitGroup wg.Add(2) @@ -169,9 +169,9 @@ func TestKCPSessionsIPv4(t *testing.T) { // TestKCPSessionsIPv6 is similar to TestKCPSessionsIPv4 but running in IPv6. func TestKCPSessionsIPv6(t *testing.T) { rng.InitSeed() - serverPort, err := netutil.UnusedUDPPort() + serverPort, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } party, err := udpsession.ListenWithOptions(fmt.Sprintf("[::1]:%d", serverPort), users) if err != nil { @@ -195,13 +195,13 @@ func TestKCPSessionsIPv6(t *testing.T) { }() time.Sleep(1 * time.Second) - clientPort1, err := netutil.UnusedUDPPort() + clientPort1, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } - clientPort2, err := netutil.UnusedUDPPort() + clientPort2, err := util.UnusedUDPPort() if err != nil { - t.Fatalf("netutil.UnusedUDPPort() failed: %v", err) + t.Fatalf("util.UnusedUDPPort() failed: %v", err) } var wg sync.WaitGroup wg.Add(2) diff --git a/pkg/netutil/addr.go b/pkg/util/addr.go similarity index 98% rename from pkg/netutil/addr.go rename to pkg/util/addr.go index 3c004551..ba49ca79 100644 --- a/pkg/netutil/addr.go +++ b/pkg/util/addr.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package netutil +package util import "net" diff --git a/pkg/netutil/conn.go b/pkg/util/conn.go similarity index 99% rename from pkg/netutil/conn.go rename to pkg/util/conn.go index dfc194b6..d14103b9 100644 --- a/pkg/netutil/conn.go +++ b/pkg/util/conn.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package netutil +package util import ( "context" diff --git a/pkg/netutil/copy.go b/pkg/util/copy.go similarity index 99% rename from pkg/netutil/copy.go rename to pkg/util/copy.go index de578243..12446d55 100644 --- a/pkg/netutil/copy.go +++ b/pkg/util/copy.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package netutil +package util import "io" diff --git a/pkg/netutil/hierarchy_conn.go b/pkg/util/hierarchy_conn.go similarity index 98% rename from pkg/netutil/hierarchy_conn.go rename to pkg/util/hierarchy_conn.go index 17919394..b5358af8 100644 --- a/pkg/netutil/hierarchy_conn.go +++ b/pkg/util/hierarchy_conn.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package netutil +package util import ( "net" diff --git a/pkg/netutil/hierarchy_conn_test.go b/pkg/util/hierarchy_conn_test.go similarity index 98% rename from pkg/netutil/hierarchy_conn_test.go rename to pkg/util/hierarchy_conn_test.go index cc369e77..3b3b08ee 100644 --- a/pkg/netutil/hierarchy_conn_test.go +++ b/pkg/util/hierarchy_conn_test.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package netutil +package util import ( "net" diff --git a/pkg/netutil/ipdualstack.go b/pkg/util/ipdualstack.go similarity index 99% rename from pkg/netutil/ipdualstack.go rename to pkg/util/ipdualstack.go index f783187e..2b709ea5 100644 --- a/pkg/netutil/ipdualstack.go +++ b/pkg/util/ipdualstack.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package netutil +package util import ( "net" diff --git a/pkg/netutil/ipdualstack_test.go b/pkg/util/ipdualstack_test.go similarity index 98% rename from pkg/netutil/ipdualstack_test.go rename to pkg/util/ipdualstack_test.go index a06869b1..d140ef04 100644 --- a/pkg/netutil/ipdualstack_test.go +++ b/pkg/util/ipdualstack_test.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package netutil +package util import ( "testing" diff --git a/pkg/netutil/pick_port.go b/pkg/util/pick_port.go similarity index 98% rename from pkg/netutil/pick_port.go rename to pkg/util/pick_port.go index fb5f8d43..30950138 100644 --- a/pkg/netutil/pick_port.go +++ b/pkg/util/pick_port.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package netutil +package util import ( "fmt" diff --git a/pkg/netutil/pick_port_test.go b/pkg/util/pick_port_test.go similarity index 98% rename from pkg/netutil/pick_port_test.go rename to pkg/util/pick_port_test.go index d8da1ffd..1a30b4a6 100644 --- a/pkg/netutil/pick_port_test.go +++ b/pkg/util/pick_port_test.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package netutil +package util import ( "fmt" diff --git a/pkg/netutil/sockopt.go b/pkg/util/sockopt.go similarity index 98% rename from pkg/netutil/sockopt.go rename to pkg/util/sockopt.go index bc092b81..3354c6e1 100644 --- a/pkg/netutil/sockopt.go +++ b/pkg/util/sockopt.go @@ -15,7 +15,7 @@ //go:build !linux -package netutil +package util import ( "syscall" diff --git a/pkg/netutil/sockopt_linux.go b/pkg/util/sockopt_linux.go similarity index 98% rename from pkg/netutil/sockopt_linux.go rename to pkg/util/sockopt_linux.go index a469ed93..b5a1fb31 100644 --- a/pkg/netutil/sockopt_linux.go +++ b/pkg/util/sockopt_linux.go @@ -15,7 +15,7 @@ //go:build linux -package netutil +package util import ( "syscall" diff --git a/pkg/netutil/time.go b/pkg/util/time.go similarity index 98% rename from pkg/netutil/time.go rename to pkg/util/time.go index f5807993..c46c6d18 100644 --- a/pkg/netutil/time.go +++ b/pkg/util/time.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package netutil +package util import "time" diff --git a/pkg/netutil/time_test.go b/pkg/util/time_test.go similarity index 98% rename from pkg/netutil/time_test.go rename to pkg/util/time_test.go index 23117ea4..117c2cbe 100644 --- a/pkg/netutil/time_test.go +++ b/pkg/util/time_test.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package netutil +package util import ( "testing" diff --git a/pkg/netutil/transport_protocol.go b/pkg/util/transport_protocol.go similarity index 98% rename from pkg/netutil/transport_protocol.go rename to pkg/util/transport_protocol.go index 18b6cd63..977b59ef 100644 --- a/pkg/netutil/transport_protocol.go +++ b/pkg/util/transport_protocol.go @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package netutil +package util type TransportProtocol int diff --git a/test/cmd/socksudpclient/socksudpclient.go b/test/cmd/socksudpclient/socksudpclient.go index ff86db19..0a22a3c1 100644 --- a/test/cmd/socksudpclient/socksudpclient.go +++ b/test/cmd/socksudpclient/socksudpclient.go @@ -25,9 +25,9 @@ import ( "time" "github.com/enfein/mieru/pkg/log" - "github.com/enfein/mieru/pkg/netutil" "github.com/enfein/mieru/pkg/socks5client" "github.com/enfein/mieru/pkg/testtool" + "github.com/enfein/mieru/pkg/util" ) var ( @@ -102,7 +102,7 @@ func DoRequestWithExistingConn(conn *net.UDPConn, proxyAddr, dstAddr *net.UDPAdd payload := testtool.TestHelperGenRot13Input(payloadSize) conn.SetReadDeadline(time.Now().Add(1 * time.Second)) - defer conn.SetReadDeadline(netutil.ZeroTime()) + defer conn.SetReadDeadline(util.ZeroTime()) resp, err := socks5client.SendUDP(conn, proxyAddr, dstAddr, payload) if err != nil { log.Fatalf("socks5client.SendUDP() failed: %v", err)