Browse Source

cleanup old proxy config on peer update

Abhishek Kondur 2 years ago
parent
commit
12294c51e2
2 changed files with 17 additions and 28 deletions
  1. 1 0
      nm-proxy/manager/manager.go
  2. 16 28
      nm-proxy/proxy/wireguard.go

+ 1 - 0
nm-proxy/manager/manager.go

@@ -45,6 +45,7 @@ func StartProxyManager(manageChan chan *ManagerAction) {
 }
 }
 func cleanUp(iface string) {
 func cleanUp(iface string) {
 	if peers, ok := common.WgIFaceMap[iface]; ok {
 	if peers, ok := common.WgIFaceMap[iface]; ok {
+		log.Println("########------------>  CLEANING UP: ", iface)
 		for _, peerI := range peers {
 		for _, peerI := range peers {
 			peerI.Proxy.Cancel()
 			peerI.Proxy.Cancel()
 		}
 		}

+ 16 - 28
nm-proxy/proxy/wireguard.go

@@ -24,14 +24,26 @@ func NewProxy(config Config) *Proxy {
 // proxyToRemote proxies everything from Wireguard to the RemoteKey peer
 // proxyToRemote proxies everything from Wireguard to the RemoteKey peer
 func (p *Proxy) ProxyToRemote() {
 func (p *Proxy) ProxyToRemote() {
 	buf := make([]byte, 1500)
 	buf := make([]byte, 1500)
-	defer p.LocalConn.Close()
-	defer p.RemoteConn.Close()
+
+	go func() {
+		<-p.Ctx.Done()
+		defer p.LocalConn.Close()
+		defer p.RemoteConn.Close()
+	}()
 	for {
 	for {
 		select {
 		select {
 		case <-p.Ctx.Done():
 		case <-p.Ctx.Done():
-			log.Printf("stopped proxying to remote peer %s due to closed connection\n", p.Config.RemoteKey)
+			log.Printf("----------> stopped proxying to remote peer %s due to closed connection\n", p.Config.RemoteKey)
 			if runtime.GOOS == "darwin" {
 			if runtime.GOOS == "darwin" {
-				_, err := common.RunCmd(fmt.Sprintf("ifconfig lo0 -alias %s 255.255.255.255", p.LocalConn.LocalAddr().String()), true)
+				host, _, err := net.SplitHostPort(p.LocalConn.LocalAddr().String())
+				if err != nil {
+					log.Println("Failed to split host: ", p.LocalConn.LocalAddr().String(), err)
+					return
+				}
+				if host == "127.0.0.1" {
+					return
+				}
+				_, err = common.RunCmd(fmt.Sprintf("ifconfig lo0 -alias %s 255.255.255.255", host), true)
 				if err != nil {
 				if err != nil {
 					log.Println("Failed to add alias: ", err)
 					log.Println("Failed to add alias: ", err)
 				}
 				}
@@ -70,30 +82,6 @@ func (p *Proxy) ProxyToRemote() {
 	}
 	}
 }
 }
 
 
-// proxyToLocal proxies everything from the RemoteKey peer to local Wireguard
-func (p *Proxy) ProxyToLocal() {
-
-	buf := make([]byte, 1500)
-	for {
-		select {
-		case <-p.Ctx.Done():
-			log.Printf("stopped proxying from remote peer %s due to closed connection\n", p.Config.RemoteKey)
-			return
-		default:
-
-			n, err := p.RemoteConn.Read(buf)
-			if err != nil {
-				continue
-			}
-			log.Printf("PROXING TO LOCAL!!!---> %s <<<<<<<< %s\n", p.LocalConn.LocalAddr().String(), p.RemoteConn.RemoteAddr().String())
-			_, err = p.LocalConn.Write(buf[:n])
-			if err != nil {
-				continue
-			}
-		}
-	}
-}
-
 func (p *Proxy) updateEndpoint() error {
 func (p *Proxy) updateEndpoint() error {
 	udpAddr, err := net.ResolveUDPAddr("udp", p.LocalConn.LocalAddr().String())
 	udpAddr, err := net.ResolveUDPAddr("udp", p.LocalConn.LocalAddr().String())
 	if err != nil {
 	if err != nil {