Skip to content

Commit

Permalink
Rename package netutil to util
Browse files Browse the repository at this point in the history
  • Loading branch information
enfein committed Sep 4, 2023
1 parent f452725 commit 0736e91
Show file tree
Hide file tree
Showing 42 changed files with 307 additions and 279 deletions.
4 changes: 2 additions & 2 deletions pkg/appctl/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions pkg/cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
})
Expand All @@ -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) {
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/http2socks/http2socks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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{
Expand Down
10 changes: 5 additions & 5 deletions pkg/kcp/kcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
26 changes: 14 additions & 12 deletions pkg/protocolv2/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -42,7 +43,7 @@ type Mux struct {

// ---- server fields ----
users map[string]*appctlpb.User
serverHandler netutil.ConnHandler
serverHandler util.ConnHandler

// ---- common fields ----
endpoints []UnderlayProperties
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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")
}
}
Expand Down Expand Up @@ -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")
}
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
54 changes: 27 additions & 27 deletions pkg/protocolv2/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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).
Expand All @@ -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)
Expand All @@ -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).
Expand All @@ -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)
Expand All @@ -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).
Expand All @@ -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)
}
Expand All @@ -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).
Expand All @@ -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)
}
Expand Down
Loading

0 comments on commit 0736e91

Please sign in to comment.