Handle port correctly and delete interface

This commit is contained in:
Owen
2025-03-27 22:12:54 -04:00
parent f08378b67e
commit c5978d9c4e
2 changed files with 47 additions and 6 deletions

View File

@@ -23,9 +23,8 @@ import (
)
type WgConfig struct {
ListenPort int `json:"listenPort"`
IpAddress string `json:"ipAddress"`
Peers []Peer `json:"peers"`
IpAddress string `json:"ipAddress"`
Peers []Peer `json:"peers"`
}
type Peer struct {
@@ -176,6 +175,10 @@ func NewWireGuardService(interfaceName string, mtu int, generateAndSaveKeyTo str
func (s *WireGuardService) Close() {
s.wgClient.Close()
// Remove the WireGuard interface
if err := s.removeInterface(); err != nil {
logger.Error("Failed to remove WireGuard interface: %v", err)
}
}
func (s *WireGuardService) SetServerPubKey(serverPubKey string) {
@@ -188,8 +191,16 @@ func (s *WireGuardService) SetToken(token string) {
func (s *WireGuardService) LoadRemoteConfig() error {
err := s.client.SendMessage("newt/wg/get-config", map[string]interface{}{
// get the exising wireguard port
device, err := s.wgClient.Device(s.interfaceName)
if err == nil {
s.Port = uint16(device.ListenPort)
logger.Info("WireGuard interface %s already exists with port %d\n", s.interfaceName, s.Port)
}
err = s.client.SendMessage("newt/wg/get-config", map[string]interface{}{
"publicKey": fmt.Sprintf("%s", s.key.PublicKey().String()),
"port": s.Port,
})
if err != nil {
logger.Error("Failed to send registration message: %v", err)
@@ -771,3 +782,20 @@ func (s *WireGuardService) keepSendingUDPHolePunch(host string) {
}
}
}
func (s *WireGuardService) removeInterface() error {
// Remove the WireGuard interface
link, err := netlink.LinkByName(s.interfaceName)
if err != nil {
return fmt.Errorf("failed to get interface: %v", err)
}
err = netlink.LinkDel(link)
if err != nil {
return fmt.Errorf("failed to delete interface: %v", err)
}
logger.Info("WireGuard interface %s removed successfully", s.interfaceName)
return nil
}