mirror of
https://github.com/fosrl/newt.git
synced 2026-03-08 03:36:40 +00:00
Make linux clients build correctly
This commit is contained in:
80
linux.go
Normal file
80
linux.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
//go:build linux
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fosrl/newt/logger"
|
||||||
|
"github.com/fosrl/newt/proxy"
|
||||||
|
"github.com/fosrl/newt/websocket"
|
||||||
|
"github.com/fosrl/newt/wg"
|
||||||
|
"github.com/fosrl/newt/wgtester"
|
||||||
|
)
|
||||||
|
|
||||||
|
var wgService *wg.WireGuardService
|
||||||
|
var wgTesterServer *wgtester.Server
|
||||||
|
|
||||||
|
func setupClients(client *websocket.Client) {
|
||||||
|
var host = endpoint
|
||||||
|
if strings.HasPrefix(host, "http://") {
|
||||||
|
host = strings.TrimPrefix(host, "http://")
|
||||||
|
} else if strings.HasPrefix(host, "https://") {
|
||||||
|
host = strings.TrimPrefix(host, "https://")
|
||||||
|
}
|
||||||
|
|
||||||
|
host = strings.TrimSuffix(host, "/")
|
||||||
|
|
||||||
|
// Create WireGuard service
|
||||||
|
wgService, err = wg.NewWireGuardService(interfaceName, mtuInt, generateAndSaveKeyTo, host, id, client)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal("Failed to create WireGuard service: %v", err)
|
||||||
|
}
|
||||||
|
defer wgService.Close(rm)
|
||||||
|
|
||||||
|
wgTesterServer = wgtester.NewServer("0.0.0.0", wgService.Port, id) // TODO: maybe make this the same ip of the wg server?
|
||||||
|
err := wgTesterServer.Start()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to start WireGuard tester server: %v", err)
|
||||||
|
} else {
|
||||||
|
// Make sure to stop the server on exit
|
||||||
|
defer wgTesterServer.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
client.OnTokenUpdate(func(token string) {
|
||||||
|
wgService.SetToken(token)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func closeClients() {
|
||||||
|
if wgService != nil {
|
||||||
|
wgService.Close(rm)
|
||||||
|
wgService = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if wgTesterServer != nil {
|
||||||
|
wgTesterServer.Stop()
|
||||||
|
wgTesterServer = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func clientsHandleNewtConnection(publicKey string) {
|
||||||
|
if wgService != nil {
|
||||||
|
wgService.SetServerPubKey(publicKey)
|
||||||
|
} else {
|
||||||
|
logger.Error("WireGuard service is not initialized, cannot set server public key")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func clientsOnConnect() {
|
||||||
|
if wgService != nil {
|
||||||
|
wgService.LoadRemoteConfig()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func clientsAddProxyTarget(pm *proxy.ProxyManager, tunnelIp string) {
|
||||||
|
// add a udp proxy for localost and the wgService port
|
||||||
|
// TODO: make sure this port is not used in a target
|
||||||
|
pm.AddTarget("udp", tunnelIp, int(wgService.Port), fmt.Sprintf("127.0.0.1:%d", wgService.Port))
|
||||||
|
}
|
||||||
58
main.go
58
main.go
@@ -19,8 +19,6 @@ import (
|
|||||||
"github.com/fosrl/newt/proxy"
|
"github.com/fosrl/newt/proxy"
|
||||||
"github.com/fosrl/newt/updates"
|
"github.com/fosrl/newt/updates"
|
||||||
"github.com/fosrl/newt/websocket"
|
"github.com/fosrl/newt/websocket"
|
||||||
"github.com/fosrl/newt/wg"
|
|
||||||
"github.com/fosrl/newt/wgtester"
|
|
||||||
|
|
||||||
"golang.zx2c4.com/wireguard/conn"
|
"golang.zx2c4.com/wireguard/conn"
|
||||||
"golang.zx2c4.com/wireguard/device"
|
"golang.zx2c4.com/wireguard/device"
|
||||||
@@ -255,14 +253,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create TUN device and network stack
|
// Create TUN device and network stack
|
||||||
var wgService *wg.WireGuardService
|
|
||||||
var tun tun.Device
|
var tun tun.Device
|
||||||
var tnet *netstack.Net
|
var tnet *netstack.Net
|
||||||
var dev *device.Device
|
var dev *device.Device
|
||||||
var pm *proxy.ProxyManager
|
var pm *proxy.ProxyManager
|
||||||
var connected bool
|
var connected bool
|
||||||
var wgData WgData
|
var wgData WgData
|
||||||
var wgTesterServer *wgtester.Server
|
|
||||||
|
|
||||||
if acceptClients {
|
if acceptClients {
|
||||||
// make sure we are running on linux
|
// make sure we are running on linux
|
||||||
@@ -271,30 +267,7 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
var host = endpoint
|
setupClients(client)
|
||||||
if strings.HasPrefix(host, "http://") {
|
|
||||||
host = strings.TrimPrefix(host, "http://")
|
|
||||||
} else if strings.HasPrefix(host, "https://") {
|
|
||||||
host = strings.TrimPrefix(host, "https://")
|
|
||||||
}
|
|
||||||
|
|
||||||
host = strings.TrimSuffix(host, "/")
|
|
||||||
|
|
||||||
// Create WireGuard service
|
|
||||||
wgService, err = wg.NewWireGuardService(interfaceName, mtuInt, generateAndSaveKeyTo, host, id, client)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Failed to create WireGuard service: %v", err)
|
|
||||||
}
|
|
||||||
defer wgService.Close(rm)
|
|
||||||
|
|
||||||
wgTesterServer = wgtester.NewServer("0.0.0.0", wgService.Port, id) // TODO: maybe make this the same ip of the wg server?
|
|
||||||
err := wgTesterServer.Start()
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("Failed to start WireGuard tester server: %v", err)
|
|
||||||
} else {
|
|
||||||
// Make sure to stop the server on exit
|
|
||||||
defer wgTesterServer.Stop()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var pingWithRetryStopChan chan struct{}
|
var pingWithRetryStopChan chan struct{}
|
||||||
@@ -349,9 +322,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if wgService != nil {
|
clientsHandleNewtConnection(wgData.PublicKey)
|
||||||
wgService.SetServerPubKey(wgData.PublicKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Info("Received: %+v", msg)
|
logger.Info("Received: %+v", msg)
|
||||||
tun, tnet, err = netstack.CreateNetTUN(
|
tun, tnet, err = netstack.CreateNetTUN(
|
||||||
@@ -423,12 +394,7 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
updateTargets(pm, "add", wgData.TunnelIP, "udp", TargetData{Targets: wgData.Targets.UDP})
|
updateTargets(pm, "add", wgData.TunnelIP, "udp", TargetData{Targets: wgData.Targets.UDP})
|
||||||
}
|
}
|
||||||
|
|
||||||
// first make sure the wpgService has a port
|
clientsAddProxyTarget(pm, wgData.TunnelIP)
|
||||||
if wgService != nil {
|
|
||||||
// add a udp proxy for localost and the wgService port
|
|
||||||
// TODO: make sure this port is not used in a target
|
|
||||||
pm.AddTarget("udp", wgData.TunnelIP, int(wgService.Port), fmt.Sprintf("127.0.0.1:%d", wgService.Port))
|
|
||||||
}
|
|
||||||
|
|
||||||
err = pm.Start()
|
err = pm.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -734,9 +700,7 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
// request from the server the list of nodes to ping at newt/ping/request
|
// request from the server the list of nodes to ping at newt/ping/request
|
||||||
stopFunc = client.SendMessageInterval("newt/ping/request", map[string]interface{}{}, 3*time.Second)
|
stopFunc = client.SendMessageInterval("newt/ping/request", map[string]interface{}{}, 3*time.Second)
|
||||||
|
|
||||||
if wgService != nil {
|
clientsOnConnect()
|
||||||
wgService.LoadRemoteConfig()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send registration message to the server for backward compatibility
|
// Send registration message to the server for backward compatibility
|
||||||
@@ -755,12 +719,6 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
client.OnTokenUpdate(func(token string) {
|
|
||||||
if wgService != nil {
|
|
||||||
wgService.SetToken(token)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Connect to the WebSocket server
|
// Connect to the WebSocket server
|
||||||
if err := client.Connect(); err != nil {
|
if err := client.Connect(); err != nil {
|
||||||
logger.Fatal("Failed to connect to server: %v", err)
|
logger.Fatal("Failed to connect to server: %v", err)
|
||||||
@@ -774,13 +732,7 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
|
|
||||||
dev.Close()
|
dev.Close()
|
||||||
|
|
||||||
if wgService != nil {
|
closeClients()
|
||||||
wgService.Close(rm)
|
|
||||||
}
|
|
||||||
|
|
||||||
if wgTesterServer != nil {
|
|
||||||
wgTesterServer.Stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
if pm != nil {
|
if pm != nil {
|
||||||
pm.Stop()
|
pm.Stop()
|
||||||
|
|||||||
32
stub.go
Normal file
32
stub.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
//go:build !linux
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/fosrl/newt/proxy"
|
||||||
|
"github.com/fosrl/newt/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setupClients(client *websocket.Client) {
|
||||||
|
return // This function is not implemented for non-Linux systems.
|
||||||
|
}
|
||||||
|
|
||||||
|
func closeClients() {
|
||||||
|
// This function is not implemented for non-Linux systems.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func clientsHandleNewtConnection(publicKey string) {
|
||||||
|
// This function is not implemented for non-Linux systems.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func clientsOnConnect() {
|
||||||
|
// This function is not implemented for non-Linux systems.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func clientsAddProxyTarget(pm *proxy.ProxyManager, tunnelIp string) {
|
||||||
|
// This function is not implemented for non-Linux systems.
|
||||||
|
return
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user