mirror of
https://github.com/fosrl/newt.git
synced 2026-10-21 03:59:13 +02:00
Add the remote subnets to the sync
This commit is contained in:
@@ -137,6 +137,11 @@ func (n *Newt) handleSync(msg websocket.WSMessage) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sync remote exit node subnets
|
||||||
|
if n.dev != nil {
|
||||||
|
n.updateRemoteExitNodeSubnets(syncData.RemoteExitNodeSubnets)
|
||||||
|
}
|
||||||
|
|
||||||
// Sync health check targets
|
// Sync health check targets
|
||||||
if err := n.healthMonitor.SyncTargets(syncData.HealthCheckTargets); err != nil {
|
if err := n.healthMonitor.SyncTargets(syncData.HealthCheckTargets); err != nil {
|
||||||
logger.Error("Failed to sync health check targets: %v", err)
|
logger.Error("Failed to sync health check targets: %v", err)
|
||||||
|
|||||||
+1
-49
@@ -405,55 +405,7 @@ func (n *Newt) registerHandlers(ctx context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.config.UseNativeMainInterface && len(n.activeRemoteSubnets) > 0 {
|
n.updateRemoteExitNodeSubnets(data.Subnets)
|
||||||
toRemove := make([]string, 0)
|
|
||||||
newSet := make(map[string]bool, len(data.Subnets))
|
|
||||||
for _, s := range data.Subnets {
|
|
||||||
newSet[s] = true
|
|
||||||
}
|
|
||||||
for _, s := range n.activeRemoteSubnets {
|
|
||||||
if !newSet[s] {
|
|
||||||
toRemove = append(toRemove, s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(toRemove) > 0 {
|
|
||||||
if err := network.RemoveRoutes(toRemove); err != nil {
|
|
||||||
logger.Warn("Failed to remove old subnet routes: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if n.wgData.PublicKey != "" {
|
|
||||||
lines := fmt.Sprintf("public_key=%s\nreplace_allowed_ips=true\nallowed_ip=%s/32",
|
|
||||||
util.FixKey(n.wgData.PublicKey), n.wgData.ServerIP)
|
|
||||||
for _, s := range data.Subnets {
|
|
||||||
lines += "\nallowed_ip=" + s
|
|
||||||
}
|
|
||||||
if err := n.dev.IpcSet(lines); err != nil {
|
|
||||||
logger.Warn("Failed to update WireGuard AllowedIPs: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if n.config.UseNativeMainInterface && len(data.Subnets) > 0 {
|
|
||||||
existing := make(map[string]bool, len(n.activeRemoteSubnets))
|
|
||||||
for _, s := range n.activeRemoteSubnets {
|
|
||||||
existing[s] = true
|
|
||||||
}
|
|
||||||
toAdd := make([]string, 0)
|
|
||||||
for _, s := range data.Subnets {
|
|
||||||
if !existing[s] {
|
|
||||||
toAdd = append(toAdd, s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(toAdd) > 0 {
|
|
||||||
if err := network.AddRoutes(toAdd, n.config.NativeMainInterfaceName); err != nil {
|
|
||||||
logger.Warn("Failed to add new subnet routes: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
n.activeRemoteSubnets = append([]string{}, data.Subnets...)
|
|
||||||
logger.Info("Updated remote exit node subnets: %d total", len(data.Subnets))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
n.client.RegisterHandler("newt/wg/subnets/remove", func(msg websocket.WSMessage) {
|
n.client.RegisterHandler("newt/wg/subnets/remove", func(msg websocket.WSMessage) {
|
||||||
|
|||||||
@@ -1,10 +1,68 @@
|
|||||||
package newt
|
package newt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/fosrl/newt/logger"
|
"github.com/fosrl/newt/logger"
|
||||||
"github.com/fosrl/newt/network"
|
"github.com/fosrl/newt/network"
|
||||||
|
"github.com/fosrl/newt/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// updateRemoteExitNodeSubnets replaces the set of active remote exit node
|
||||||
|
// subnets with the given list, updating WireGuard AllowedIPs and native
|
||||||
|
// routes to match.
|
||||||
|
func (n *Newt) updateRemoteExitNodeSubnets(subnets []string) {
|
||||||
|
if n.config.UseNativeMainInterface && len(n.activeRemoteSubnets) > 0 {
|
||||||
|
toRemove := make([]string, 0)
|
||||||
|
newSet := make(map[string]bool, len(subnets))
|
||||||
|
for _, s := range subnets {
|
||||||
|
newSet[s] = true
|
||||||
|
}
|
||||||
|
for _, s := range n.activeRemoteSubnets {
|
||||||
|
if !newSet[s] {
|
||||||
|
toRemove = append(toRemove, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(toRemove) > 0 {
|
||||||
|
if err := network.RemoveRoutes(toRemove); err != nil {
|
||||||
|
logger.Warn("Failed to remove old subnet routes: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if n.dev != nil && n.wgData.PublicKey != "" {
|
||||||
|
lines := fmt.Sprintf("public_key=%s\nreplace_allowed_ips=true\nallowed_ip=%s/32",
|
||||||
|
util.FixKey(n.wgData.PublicKey), n.wgData.ServerIP)
|
||||||
|
for _, s := range subnets {
|
||||||
|
lines += "\nallowed_ip=" + s
|
||||||
|
}
|
||||||
|
if err := n.dev.IpcSet(lines); err != nil {
|
||||||
|
logger.Warn("Failed to update WireGuard AllowedIPs: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if n.config.UseNativeMainInterface && len(subnets) > 0 {
|
||||||
|
existing := make(map[string]bool, len(n.activeRemoteSubnets))
|
||||||
|
for _, s := range n.activeRemoteSubnets {
|
||||||
|
existing[s] = true
|
||||||
|
}
|
||||||
|
toAdd := make([]string, 0)
|
||||||
|
for _, s := range subnets {
|
||||||
|
if !existing[s] {
|
||||||
|
toAdd = append(toAdd, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(toAdd) > 0 {
|
||||||
|
if err := network.AddRoutes(toAdd, n.config.NativeMainInterfaceName); err != nil {
|
||||||
|
logger.Warn("Failed to add new subnet routes: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
n.activeRemoteSubnets = append([]string{}, subnets...)
|
||||||
|
logger.Info("Updated remote exit node subnets: %d total", len(subnets))
|
||||||
|
}
|
||||||
|
|
||||||
func (n *Newt) closeWgTunnel() {
|
func (n *Newt) closeWgTunnel() {
|
||||||
if n.pingStopChan != nil {
|
if n.pingStopChan != nil {
|
||||||
close(n.pingStopChan)
|
close(n.pingStopChan)
|
||||||
|
|||||||
+3
-2
@@ -62,6 +62,7 @@ type BlueprintResult struct {
|
|||||||
|
|
||||||
// Define the sync data structure
|
// Define the sync data structure
|
||||||
type SyncData struct {
|
type SyncData struct {
|
||||||
Targets TargetsByType `json:"targets"`
|
Targets TargetsByType `json:"targets"`
|
||||||
HealthCheckTargets []healthcheck.Config `json:"healthCheckTargets"`
|
HealthCheckTargets []healthcheck.Config `json:"healthCheckTargets"`
|
||||||
|
RemoteExitNodeSubnets []string `json:"remoteExitNodeSubnets"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user