mirror of
https://github.com/fosrl/newt.git
synced 2026-03-06 18:56:41 +00:00
Exit well
This commit is contained in:
7
main.go
7
main.go
@@ -798,10 +798,13 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
||||||
<-sigCh
|
<-sigCh
|
||||||
|
|
||||||
dev.Close()
|
// Close clients first (including WGTester)
|
||||||
|
|
||||||
closeClients()
|
closeClients()
|
||||||
|
|
||||||
|
if dev != nil {
|
||||||
|
dev.Close()
|
||||||
|
}
|
||||||
|
|
||||||
if pm != nil {
|
if pm != nil {
|
||||||
pm.Stop()
|
pm.Stop()
|
||||||
}
|
}
|
||||||
|
|||||||
14
wg/wg.go
14
wg/wg.go
@@ -55,6 +55,7 @@ type WireGuardService struct {
|
|||||||
wgClient *wgctrl.Client
|
wgClient *wgctrl.Client
|
||||||
config WgConfig
|
config WgConfig
|
||||||
key wgtypes.Key
|
key wgtypes.Key
|
||||||
|
keyFilePath string
|
||||||
newtId string
|
newtId string
|
||||||
lastReadings map[string]PeerReading
|
lastReadings map[string]PeerReading
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
@@ -179,6 +180,7 @@ func NewWireGuardService(interfaceName string, mtu int, generateAndSaveKeyTo str
|
|||||||
client: wsClient,
|
client: wsClient,
|
||||||
wgClient: wgClient,
|
wgClient: wgClient,
|
||||||
key: key,
|
key: key,
|
||||||
|
keyFilePath: generateAndSaveKeyTo,
|
||||||
newtId: newtId,
|
newtId: newtId,
|
||||||
host: host,
|
host: host,
|
||||||
lastReadings: make(map[string]PeerReading),
|
lastReadings: make(map[string]PeerReading),
|
||||||
@@ -229,9 +231,11 @@ func (s *WireGuardService) Close(rm bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove the private key file
|
// Remove the private key file
|
||||||
if err := os.Remove(s.key.String()); err != nil {
|
// if s.keyFilePath != "" {
|
||||||
logger.Error("Failed to remove private key file: %v", err)
|
// if err := os.Remove(s.keyFilePath); err != nil {
|
||||||
}
|
// logger.Error("Failed to remove private key file: %v", err)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,7 +255,7 @@ func (s *WireGuardService) SetToken(token string) {
|
|||||||
|
|
||||||
func (s *WireGuardService) LoadRemoteConfig() error {
|
func (s *WireGuardService) LoadRemoteConfig() error {
|
||||||
s.stopGetConfig = s.client.SendMessageInterval("newt/wg/get-config", map[string]interface{}{
|
s.stopGetConfig = s.client.SendMessageInterval("newt/wg/get-config", map[string]interface{}{
|
||||||
"publicKey": fmt.Sprintf("%s", s.key.PublicKey().String()),
|
"publicKey": s.key.PublicKey().String(),
|
||||||
"port": s.Port,
|
"port": s.Port,
|
||||||
}, 2*time.Second)
|
}, 2*time.Second)
|
||||||
|
|
||||||
@@ -638,7 +642,7 @@ func (s *WireGuardService) handleUpdatePeer(msg websocket.WSMessage) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Only update AllowedIPs if provided in the request
|
// Only update AllowedIPs if provided in the request
|
||||||
if request.AllowedIPs != nil && len(request.AllowedIPs) > 0 {
|
if len(request.AllowedIPs) > 0 {
|
||||||
var allowedIPs []net.IPNet
|
var allowedIPs []net.IPNet
|
||||||
for _, ipStr := range request.AllowedIPs {
|
for _, ipStr := range request.AllowedIPs {
|
||||||
_, ipNet, err := net.ParseCIDR(ipStr)
|
_, ipNet, err := net.ParseCIDR(ipStr)
|
||||||
|
|||||||
@@ -119,7 +119,13 @@ func (s *Server) handleConnections() {
|
|||||||
// Just a timeout, keep going
|
// Just a timeout, keep going
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
logger.Error(s.outputPrefix+"Error reading from UDP: %v", err)
|
// Check if we're shutting down and the connection was closed
|
||||||
|
select {
|
||||||
|
case <-s.shutdownCh:
|
||||||
|
return // Don't log error if we're shutting down
|
||||||
|
default:
|
||||||
|
logger.Error(s.outputPrefix+"Error reading from UDP: %v", err)
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user