mirror of
https://github.com/fosrl/olm.git
synced 2026-02-08 05:56:41 +00:00
Add test mode
This commit is contained in:
2
go.mod
2
go.mod
@@ -7,7 +7,7 @@ toolchain go1.23.2
|
||||
require (
|
||||
github.com/fosrl/newt v0.0.0-20250215225251-76503f3f2cd8
|
||||
golang.org/x/net v0.33.0
|
||||
golang.org/x/sys v0.30.0
|
||||
golang.org/x/sys v0.33.0
|
||||
golang.zx2c4.com/wireguard v0.0.0-20231211153847-12269c276173
|
||||
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20241231184526-a9ab2273dd10
|
||||
)
|
||||
|
||||
2
go.sum
2
go.sum
@@ -25,6 +25,8 @@ golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ=
|
||||
golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 h1:B82qJJgjvYKsXS9jeunTOisW56dUokqW/FOteYJJ/yg=
|
||||
|
||||
46
main.go
46
main.go
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/fosrl/olm/httpserver"
|
||||
"github.com/fosrl/olm/peermonitor"
|
||||
"github.com/fosrl/olm/websocket"
|
||||
"github.com/fosrl/olm/wgtester"
|
||||
|
||||
"golang.zx2c4.com/wireguard/device"
|
||||
"golang.zx2c4.com/wireguard/tun"
|
||||
@@ -36,6 +37,8 @@ func main() {
|
||||
interfaceName string
|
||||
enableHTTP bool
|
||||
httpAddr string
|
||||
testMode bool // Add this var for the test flag
|
||||
testTarget string // Add this var for test target
|
||||
)
|
||||
|
||||
stopHolepunch = make(chan struct{})
|
||||
@@ -78,6 +81,8 @@ func main() {
|
||||
}
|
||||
|
||||
flag.BoolVar(&enableHTTP, "http", false, "Enable HTTP server")
|
||||
flag.BoolVar(&testMode, "test", false, "Test WireGuard connectivity to a target")
|
||||
flag.StringVar(&testTarget, "test-target", "", "Target server:port for test mode")
|
||||
|
||||
// do a --version check
|
||||
version := flag.Bool("version", false, "Print the version")
|
||||
@@ -93,6 +98,35 @@ func main() {
|
||||
loggerLevel := parseLogLevel(logLevel)
|
||||
logger.GetLogger().SetLevel(parseLogLevel(logLevel))
|
||||
|
||||
// Handle test mode
|
||||
if testMode {
|
||||
if testTarget == "" {
|
||||
logger.Fatal("Test mode requires -test-target to be set to a server:port")
|
||||
}
|
||||
|
||||
logger.Info("Running in test mode, connecting to %s", testTarget)
|
||||
|
||||
// Create a new tester client
|
||||
tester, err := wgtester.NewClient(testTarget)
|
||||
if err != nil {
|
||||
logger.Fatal("Failed to create tester client: %v", err)
|
||||
}
|
||||
defer tester.Close()
|
||||
|
||||
// Test connection with a 2-second timeout
|
||||
connected, rtt := tester.TestConnectionWithTimeout(2 * time.Second)
|
||||
|
||||
if connected {
|
||||
logger.Info("Connection test successful! RTT: %v", rtt)
|
||||
fmt.Printf("Connection test successful! RTT: %v\n", rtt)
|
||||
os.Exit(0)
|
||||
} else {
|
||||
logger.Error("Connection test failed - no response received")
|
||||
fmt.Println("Connection test failed - no response received")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
var httpServer *httpserver.HTTPServer
|
||||
if enableHTTP {
|
||||
httpServer = httpserver.NewHTTPServer(httpAddr)
|
||||
@@ -112,7 +146,6 @@ func main() {
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// wait until we have a client id and secret and endpoint
|
||||
for id == "" || secret == "" || endpoint == "" {
|
||||
logger.Debug("Waiting for client ID, secret, and endpoint...")
|
||||
@@ -324,7 +357,6 @@ func main() {
|
||||
logger.Error("Failed to add route for peer: %v", err)
|
||||
return
|
||||
}
|
||||
// this causes routing issues on Windows
|
||||
// err = WindowsAddRoute(site.ServerIP, "", interfaceName)
|
||||
// if err != nil {
|
||||
// logger.Error("Failed to add route for peer: %v", err)
|
||||
@@ -423,11 +455,11 @@ func main() {
|
||||
logger.Error("Failed to add route for new peer: %v", err)
|
||||
return
|
||||
}
|
||||
err = WindowsAddRoute(siteConfig.ServerIP, "", interfaceName)
|
||||
if err != nil {
|
||||
logger.Error("Failed to add route for new peer: %v", err)
|
||||
return
|
||||
}
|
||||
// err = WindowsAddRoute(siteConfig.ServerIP, "", interfaceName)
|
||||
// if err != nil {
|
||||
// logger.Error("Failed to add route for new peer: %v", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
// Add successful
|
||||
logger.Info("Successfully added peer for site %d", addData.SiteId)
|
||||
|
||||
Reference in New Issue
Block a user