Skip to content

Commit a610a4c

Browse files
authored
Chore: Remove all double gonet import (#5402)
1 parent b451f89 commit a610a4c

File tree

16 files changed

+55
-51
lines changed

16 files changed

+55
-51
lines changed

app/dns/fakedns/fake.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"math"
66
"math/big"
7-
gonet "net"
87
"sync"
98
"time"
109

@@ -17,7 +16,7 @@ import (
1716

1817
type Holder struct {
1918
domainToIP cache.Lru
20-
ipRange *gonet.IPNet
19+
ipRange *net.IPNet
2120
mu *sync.Mutex
2221

2322
config *FakeDnsPool
@@ -79,10 +78,10 @@ func (fkdns *Holder) initializeFromConfig() error {
7978
}
8079

8180
func (fkdns *Holder) initialize(ipPoolCidr string, lruSize int) error {
82-
var ipRange *gonet.IPNet
81+
var ipRange *net.IPNet
8382
var err error
8483

85-
if _, ipRange, err = gonet.ParseCIDR(ipPoolCidr); err != nil {
84+
if _, ipRange, err = net.ParseCIDR(ipPoolCidr); err != nil {
8685
return errors.New("Unable to parse CIDR for Fake DNS IP assignment").Base(err).AtError()
8786
}
8887

app/dns/fakedns/fakedns_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fakedns
22

33
import (
4-
gonet "net"
54
"strconv"
65
"testing"
76

@@ -155,7 +154,7 @@ func TestFakeDNSMulti(t *testing.T) {
155154
assert.True(t, inPool)
156155
})
157156
t.Run("ipv6", func(t *testing.T) {
158-
ip, err := gonet.ResolveIPAddr("ip", "fddd:c5b4:ff5f:f4f0::5")
157+
ip, err := net.ResolveIPAddr("ip", "fddd:c5b4:ff5f:f4f0::5")
159158
assert.Nil(t, err)
160159
inPool := fakeMulti.IsIPInIPPool(net.IPAddress(ip.IP))
161160
assert.True(t, inPool)
@@ -165,7 +164,7 @@ func TestFakeDNSMulti(t *testing.T) {
165164
assert.False(t, inPool)
166165
})
167166
t.Run("ipv6_inverse", func(t *testing.T) {
168-
ip, err := gonet.ResolveIPAddr("ip", "fcdd:c5b4:ff5f:f4f0::5")
167+
ip, err := net.ResolveIPAddr("ip", "fcdd:c5b4:ff5f:f4f0::5")
169168
assert.Nil(t, err)
170169
inPool := fakeMulti.IsIPInIPPool(net.IPAddress(ip.IP))
171170
assert.False(t, inPool)

app/proxyman/inbound/worker.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package inbound
22

33
import (
44
"context"
5-
gonet "net"
65
"sync"
76
"sync/atomic"
87
"time"
@@ -565,12 +564,12 @@ func (w *dsWorker) Close() error {
565564
}
566565

567566
func IsLocal(ip net.IP) bool {
568-
addrs, err := gonet.InterfaceAddrs()
567+
addrs, err := net.InterfaceAddrs()
569568
if err != nil {
570569
return false
571570
}
572571
for _, addr := range addrs {
573-
if ipnet, ok := addr.(*gonet.IPNet); ok {
572+
if ipnet, ok := addr.(*net.IPNet); ok {
574573
if ipnet.IP.Equal(ip) {
575574
return true
576575
}

app/proxyman/outbound/handler.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
goerrors "errors"
77
"io"
88
"math/big"
9-
gonet "net"
109
"os"
1110

1211
"github.com/xtls/xray-core/common/dice"
@@ -398,7 +397,7 @@ func (h *Handler) ProxySettings() *serial.TypedMessage {
398397

399398
func ParseRandomIP(addr net.Address, prefix string) net.Address {
400399

401-
_, ipnet, _ := gonet.ParseCIDR(addr.IP().String() + "/" + prefix)
400+
_, ipnet, _ := net.ParseCIDR(addr.IP().String() + "/" + prefix)
402401

403402
ones, bits := ipnet.Mask.Size()
404403
subnetSize := new(big.Int).Lsh(big.NewInt(1), uint(bits-ones))
@@ -412,5 +411,5 @@ func ParseRandomIP(addr net.Address, prefix string) net.Address {
412411
padded := make([]byte, len(ipnet.IP))
413412
copy(padded[len(padded)-len(rndBytes):], rndBytes)
414413

415-
return net.ParseAddress(gonet.IP(padded).String())
414+
return net.ParseAddress(net.IP(padded).String())
416415
}

common/net/system.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var (
1212

1313
type ListenConfig = net.ListenConfig
1414

15+
type KeepAliveConfig = net.KeepAliveConfig
16+
1517
var (
1618
Listen = net.Listen
1719
ListenTCP = net.ListenTCP
@@ -26,6 +28,12 @@ var FileConn = net.FileConn
2628
// ParseIP is an alias of net.ParseIP
2729
var ParseIP = net.ParseIP
2830

31+
var ParseCIDR = net.ParseCIDR
32+
33+
var ResolveIPAddr = net.ResolveIPAddr
34+
35+
var InterfaceByName = net.InterfaceByName
36+
2937
var SplitHostPort = net.SplitHostPort
3038

3139
var CIDRMask = net.CIDRMask
@@ -51,6 +59,8 @@ type (
5159
UnixConn = net.UnixConn
5260
)
5361

62+
type IPAddr = net.IPAddr
63+
5464
// IP is an alias for net.IP.
5565
type (
5666
IP = net.IP
@@ -82,3 +92,11 @@ var (
8292
)
8393

8494
type Resolver = net.Resolver
95+
96+
var DefaultResolver = net.DefaultResolver
97+
98+
var JoinHostPort = net.JoinHostPort
99+
100+
var InterfaceAddrs = net.InterfaceAddrs
101+
102+
var Interfaces = net.Interfaces

proxy/wireguard/bind.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package wireguard
33
import (
44
"context"
55
"errors"
6-
"net"
76
"net/netip"
87
"strconv"
98
"sync"
109

1110
"golang.zx2c4.com/wireguard/conn"
1211

13-
xnet "github.com/xtls/xray-core/common/net"
12+
"github.com/xtls/xray-core/common/net"
1413
"github.com/xtls/xray-core/features/dns"
1514
"github.com/xtls/xray-core/transport/internet"
1615
)
@@ -51,21 +50,21 @@ func (n *netBind) ParseEndpoint(s string) (conn.Endpoint, error) {
5150
return nil, err
5251
}
5352

54-
addr := xnet.ParseAddress(ipStr)
55-
if addr.Family() == xnet.AddressFamilyDomain {
53+
addr := net.ParseAddress(ipStr)
54+
if addr.Family() == net.AddressFamilyDomain {
5655
ips, _, err := n.dns.LookupIP(addr.Domain(), n.dnsOption)
5756
if err != nil {
5857
return nil, err
5958
} else if len(ips) == 0 {
6059
return nil, dns.ErrEmptyResponse
6160
}
62-
addr = xnet.IPAddress(ips[0])
61+
addr = net.IPAddress(ips[0])
6362
}
6463

65-
dst := xnet.Destination{
64+
dst := net.Destination{
6665
Address: addr,
67-
Port: xnet.Port(portNum),
68-
Network: xnet.Network_UDP,
66+
Port: net.Port(portNum),
67+
Network: net.Network_UDP,
6968
}
7069

7170
return &netEndpoint{
@@ -214,7 +213,7 @@ func (bind *netBindServer) Send(buff [][]byte, endpoint conn.Endpoint) error {
214213
}
215214

216215
type netEndpoint struct {
217-
dst xnet.Destination
216+
dst net.Destination
218217
conn net.Conn
219218
}
220219

@@ -247,7 +246,7 @@ func (e netEndpoint) SrcToString() string {
247246
return ""
248247
}
249248

250-
func toNetIpAddr(addr xnet.Address) netip.Addr {
249+
func toNetIpAddr(addr net.Address) netip.Addr {
251250
if addr.Family().IsIPv4() {
252251
ip := addr.IP()
253252
return netip.AddrFrom4([4]byte{ip[0], ip[1], ip[2], ip[3]})

proxy/wireguard/tun.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package wireguard
33
import (
44
"context"
55
"fmt"
6-
"net"
76
"net/netip"
87
"runtime"
98
"strconv"
@@ -13,7 +12,7 @@ import (
1312

1413
"github.com/xtls/xray-core/common/errors"
1514
"github.com/xtls/xray-core/common/log"
16-
xnet "github.com/xtls/xray-core/common/net"
15+
"github.com/xtls/xray-core/common/net"
1716
"github.com/xtls/xray-core/proxy/wireguard/gvisortun"
1817
"gvisor.dev/gvisor/pkg/tcpip"
1918
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
@@ -28,7 +27,7 @@ import (
2827

2928
type tunCreator func(localAddresses []netip.Addr, mtu int, handler promiscuousModeHandler) (Tunnel, error)
3029

31-
type promiscuousModeHandler func(dest xnet.Destination, conn net.Conn)
30+
type promiscuousModeHandler func(dest net.Destination, conn net.Conn)
3231

3332
type Tunnel interface {
3433
BuildDevice(ipc string, bind conn.Bind) error
@@ -169,7 +168,7 @@ func createGVisorTun(localAddresses []netip.Addr, mtu int, handler promiscuousMo
169168
ep.SocketOptions().SetKeepAlive(true)
170169

171170
// local address is actually destination
172-
handler(xnet.TCPDestination(xnet.IPAddress(id.LocalAddress.AsSlice()), xnet.Port(id.LocalPort)), gonet.NewTCPConn(&wq, ep))
171+
handler(net.TCPDestination(net.IPAddress(id.LocalAddress.AsSlice()), net.Port(id.LocalPort)), gonet.NewTCPConn(&wq, ep))
173172
}(r)
174173
})
175174
stack.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpForwarder.HandlePacket)
@@ -194,7 +193,7 @@ func createGVisorTun(localAddresses []netip.Addr, mtu int, handler promiscuousMo
194193
Timeout: 15 * time.Second,
195194
})
196195

197-
handler(xnet.UDPDestination(xnet.IPAddress(id.LocalAddress.AsSlice()), xnet.Port(id.LocalPort)), gonet.NewUDPConn(&wq, ep))
196+
handler(net.UDPDestination(net.IPAddress(id.LocalAddress.AsSlice()), net.Port(id.LocalPort)), gonet.NewUDPConn(&wq, ep))
198197
}(r)
199198
})
200199
stack.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket)

transport/internet/dialer.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package internet
33
import (
44
"context"
55
"fmt"
6-
gonet "net"
76
"strings"
87

98
"github.com/xtls/xray-core/common"
@@ -183,7 +182,7 @@ func checkAddressPortStrategy(ctx context.Context, dest net.Destination, sockopt
183182
if len(parts) != 3 {
184183
return nil, errors.New("invalid address format", dest.Address.String())
185184
}
186-
_, srvRecords, err := gonet.DefaultResolver.LookupSRV(context.Background(), parts[0][1:], parts[1][1:], parts[2])
185+
_, srvRecords, err := net.DefaultResolver.LookupSRV(context.Background(), parts[0][1:], parts[1][1:], parts[2])
187186
if err != nil {
188187
return nil, errors.New("failed to lookup SRV record").Base(err)
189188
}
@@ -198,7 +197,7 @@ func checkAddressPortStrategy(ctx context.Context, dest net.Destination, sockopt
198197
}
199198
if OverrideBy == "txt" {
200199
errors.LogDebug(ctx, "query TXT record for "+dest.Address.String())
201-
txtRecords, err := gonet.DefaultResolver.LookupTXT(ctx, dest.Address.String())
200+
txtRecords, err := net.DefaultResolver.LookupTXT(ctx, dest.Address.String())
202201
if err != nil {
203202
errors.LogError(ctx, "failed to lookup SRV record: "+err.Error())
204203
return nil, errors.New("failed to lookup SRV record").Base(err)

transport/internet/grpc/dial.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package grpc
22

33
import (
44
"context"
5-
gonet "net"
65
"sync"
76
"time"
87

@@ -99,7 +98,7 @@ func getGrpcClient(ctx context.Context, dest net.Destination, streamSettings *in
9998
},
10099
MinConnectTimeout: 5 * time.Second,
101100
}),
102-
grpc.WithContextDialer(func(gctx context.Context, s string) (gonet.Conn, error) {
101+
grpc.WithContextDialer(func(gctx context.Context, s string) (net.Conn, error) {
103102
select {
104103
case <-gctx.Done():
105104
return nil, gctx.Err()
@@ -180,7 +179,7 @@ func getGrpcClient(ctx context.Context, dest net.Destination, streamSettings *in
180179
}
181180

182181
conn, err := grpc.Dial(
183-
gonet.JoinHostPort(grpcDestHost, dest.Port.String()),
182+
net.JoinHostPort(grpcDestHost, dest.Port.String()),
184183
dialOptions...,
185184
)
186185
globalDialerMap[dialerConf{dest, streamSettings}] = conn

transport/internet/grpc/encoding/hunkconn.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package encoding
33
import (
44
"context"
55
"io"
6-
"net"
76

87
"github.com/xtls/xray-core/common/buf"
98
"github.com/xtls/xray-core/common/errors"
10-
xnet "github.com/xtls/xray-core/common/net"
9+
"github.com/xtls/xray-core/common/net"
1110
"github.com/xtls/xray-core/common/net/cnc"
1211
"github.com/xtls/xray-core/common/signal/done"
1312
"google.golang.org/grpc/metadata"
@@ -55,7 +54,7 @@ func NewHunkConn(hc HunkConn, cancel context.CancelFunc) net.Conn {
5554
if ok {
5655
header := md.Get("x-real-ip")
5756
if len(header) > 0 {
58-
realip := xnet.ParseAddress(header[0])
57+
realip := net.ParseAddress(header[0])
5958
if realip.Family().IsIP() {
6059
rAddr = &net.TCPAddr{
6160
IP: realip.IP(),

0 commit comments

Comments
 (0)