Add wireguard test

This commit is contained in:
yuhan6665 2024-06-01 19:34:31 -04:00
parent bbf25b14d9
commit 075051a693
2 changed files with 128 additions and 4 deletions

View File

@ -22,14 +22,14 @@ func (c *WireGuardPeerConfig) Build() (proto.Message, error) {
config := new(wireguard.PeerConfig)
if c.PublicKey != "" {
config.PublicKey, err = parseWireGuardKey(c.PublicKey)
config.PublicKey, err = ParseWireGuardKey(c.PublicKey)
if err != nil {
return nil, err
}
}
if c.PreSharedKey != "" {
config.PreSharedKey, err = parseWireGuardKey(c.PreSharedKey)
config.PreSharedKey, err = ParseWireGuardKey(c.PreSharedKey)
if err != nil {
return nil, err
}
@ -64,7 +64,7 @@ func (c *WireGuardConfig) Build() (proto.Message, error) {
config := new(wireguard.DeviceConfig)
var err error
config.SecretKey, err = parseWireGuardKey(c.SecretKey)
config.SecretKey, err = ParseWireGuardKey(c.SecretKey)
if err != nil {
return nil, err
}
@ -132,7 +132,7 @@ func (c *WireGuardConfig) Build() (proto.Message, error) {
return config, nil
}
func parseWireGuardKey(str string) (string, error) {
func ParseWireGuardKey(str string) (string, error) {
var err error
if len(str)%2 == 0 {

View File

@ -0,0 +1,124 @@
package scenarios
import (
"testing"
//"time"
"github.com/xtls/xray-core/app/log"
"github.com/xtls/xray-core/app/proxyman"
"github.com/xtls/xray-core/common"
clog "github.com/xtls/xray-core/common/log"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/serial"
core "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/infra/conf"
"github.com/xtls/xray-core/proxy/dokodemo"
"github.com/xtls/xray-core/proxy/freedom"
"github.com/xtls/xray-core/proxy/wireguard"
"github.com/xtls/xray-core/testing/servers/tcp"
"github.com/xtls/xray-core/testing/servers/udp"
//"golang.org/x/sync/errgroup"
)
func TestWireguard(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
common.Must(err)
defer tcpServer.Close()
serverPrivate, _ := conf.ParseWireGuardKey("EGs4lTSJPmgELx6YiJAmPR2meWi6bY+e9rTdCipSj10=")
serverPublic, _ := conf.ParseWireGuardKey("osAMIyil18HeZXGGBDC9KpZoM+L2iGyXWVSYivuM9B0=")
clientPrivate, _ := conf.ParseWireGuardKey("CPQSpgxgdQRZa5SUbT3HLv+mmDVHLW5YR/rQlzum/2I=")
clientPublic, _ := conf.ParseWireGuardKey("MmLJ5iHFVVBp7VsB0hxfpQ0wEzAbT2KQnpQpj0+RtBw=")
serverPort := udp.PickPort()
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
ErrorLogLevel: clog.Severity_Debug,
ErrorLogType: log.LogType_Console,
}),
},
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&wireguard.DeviceConfig{
IsClient: false,
KernelMode: false,
Endpoint: []string{"10.0.0.1"},
Mtu: 1420,
SecretKey: serverPrivate,
Peers: []*wireguard.PeerConfig{{
PublicKey: serverPublic,
AllowedIps: []string{"0.0.0.0/0", "::0/0"},
}},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
clientPort := tcp.PickPort()
clientConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
ErrorLogLevel: clog.Severity_Debug,
ErrorLogType: log.LogType_Console,
}),
},
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&wireguard.DeviceConfig{
IsClient: true,
KernelMode: false,
Endpoint: []string{"10.0.0.2"},
Mtu: 1420,
SecretKey: clientPrivate,
Peers: []*wireguard.PeerConfig{{
Endpoint: "127.0.0.1:" + serverPort.String(),
PublicKey: clientPublic,
AllowedIps: []string{"0.0.0.0/0", "::0/0"},
}},
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
// FIXME: for some reason wg server does not receive
// var errg errgroup.Group
// for i := 0; i < 1; i++ {
// errg.Go(testTCPConn(clientPort, 1024, time.Second*2))
// }
// if err := errg.Wait(); err != nil {
// t.Error(err)
// }
}