Chore: remove darwin code on test because orbstack can run it

This commit is contained in:
Dreamacro 2023-08-27 00:35:45 +08:00
parent 45c10a17a6
commit e4cd3e5306
5 changed files with 9 additions and 102 deletions

View File

@ -8,9 +8,9 @@ import (
"fmt"
"io"
"net"
"net/netip"
"os"
"path/filepath"
"runtime"
"sync"
"testing"
"time"
@ -39,7 +39,7 @@ const (
var (
waitTime = time.Second
localIP = net.ParseIP("127.0.0.1")
localIP = netip.MustParseAddr("127.0.0.1")
defaultExposedPorts = nat.PortSet{
"10002/tcp": struct{}{},
@ -53,7 +53,6 @@ var (
{HostPort: "10002", HostIP: "0.0.0.0"},
},
}
isDarwin = runtime.GOOS == "darwin"
)
func init() {
@ -64,13 +63,6 @@ func init() {
homeDir := filepath.Join(currentDir, "config")
C.SetHomeDir(homeDir)
if isDarwin {
localIP, err = defaultRouteIP()
if err != nil {
panic(err)
}
}
c, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
@ -302,7 +294,7 @@ func testPingPongWithPacketConn(t *testing.T, pc net.PacketConn) error {
require.NoError(t, err)
defer l.Close()
rAddr := &net.UDPAddr{IP: localIP, Port: 10001}
rAddr := &net.UDPAddr{IP: localIP.AsSlice(), Port: 10001}
pingCh, pongCh, test := newPingPongPair()
go func() {
@ -437,7 +429,7 @@ func testLargeDataWithPacketConn(t *testing.T, pc net.PacketConn) error {
require.NoError(t, err)
defer l.Close()
rAddr := &net.UDPAddr{IP: localIP, Port: 10001}
rAddr := &net.UDPAddr{IP: localIP.AsSlice(), Port: 10001}
times := 50
chunkSize := int64(1024)
@ -570,7 +562,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) {
pc, err := proxy.ListenPacketContext(context.Background(), &C.Metadata{
NetWork: C.UDP,
DstIP: localIP,
DstIP: localIP.AsSlice(),
DstPort: 10001,
})
require.NoError(t, err)
@ -580,7 +572,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) {
pc, err = proxy.ListenPacketContext(context.Background(), &C.Metadata{
NetWork: C.UDP,
DstIP: localIP,
DstIP: localIP.AsSlice(),
DstPort: 10001,
})
require.NoError(t, err)
@ -590,7 +582,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) {
pc, err = proxy.ListenPacketContext(context.Background(), &C.Metadata{
NetWork: C.UDP,
DstIP: localIP,
DstIP: localIP.AsSlice(),
DstPort: 10001,
})
require.NoError(t, err)

View File

@ -15,10 +15,7 @@ func startContainer(cfg *container.Config, hostCfg *container.HostConfig, name s
}
defer c.Close()
if !isDarwin {
hostCfg.NetworkMode = "host"
}
container, err := c.ContainerCreate(context.Background(), cfg, hostCfg, nil, nil, name)
if err != nil {
return "", err

View File

@ -9,7 +9,6 @@ require (
github.com/miekg/dns v1.1.55
github.com/stretchr/testify v1.8.4
go.uber.org/automaxprocs v1.5.3
golang.org/x/net v0.14.0
)
replace github.com/Dreamacro/clash => ../
@ -48,6 +47,7 @@ require (
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect

View File

@ -1,70 +0,0 @@
package main
import (
"errors"
"fmt"
"net"
"syscall"
"golang.org/x/net/route"
)
func defaultRouteIP() (net.IP, error) {
idx, err := defaultRouteInterfaceIndex()
if err != nil {
return nil, err
}
iface, err := net.InterfaceByIndex(idx)
if err != nil {
return nil, err
}
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
ip := addr.(*net.IPNet).IP
if ip.To4() != nil {
return ip, nil
}
}
return nil, errors.New("no ipv4 addr")
}
func defaultRouteInterfaceIndex() (int, error) {
rib, err := route.FetchRIB(syscall.AF_UNSPEC, syscall.NET_RT_DUMP2, 0)
if err != nil {
return 0, fmt.Errorf("route.FetchRIB: %w", err)
}
msgs, err := route.ParseRIB(syscall.NET_RT_IFLIST2, rib)
if err != nil {
return 0, fmt.Errorf("route.ParseRIB: %w", err)
}
for _, message := range msgs {
routeMessage := message.(*route.RouteMessage)
if routeMessage.Flags&(syscall.RTF_UP|syscall.RTF_GATEWAY|syscall.RTF_STATIC) == 0 {
continue
}
addresses := routeMessage.Addrs
destination, ok := addresses[0].(*route.Inet4Addr)
if !ok {
continue
}
if destination.IP != [4]byte{0, 0, 0, 0} {
continue
}
switch addresses[1].(type) {
case *route.Inet4Addr:
return routeMessage.Index, nil
default:
continue
}
}
return 0, fmt.Errorf("ambiguous gateway interfaces found")
}

View File

@ -1,12 +0,0 @@
//go:build !darwin
package main
import (
"errors"
"net"
)
func defaultRouteIP() (net.IP, error) {
return nil, errors.New("not supported")
}