cleanup old proxy config on peer update

This commit is contained in:
Abhishek Kondur 2022-10-28 00:14:32 +05:30
parent 3f2e059c32
commit 12294c51e2
2 changed files with 17 additions and 28 deletions

View file

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

View file

@ -24,14 +24,26 @@ func NewProxy(config Config) *Proxy {
// proxyToRemote proxies everything from Wireguard to the RemoteKey peer
func (p *Proxy) ProxyToRemote() {
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 {
select {
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" {
_, 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 {
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 {
udpAddr, err := net.ResolveUDPAddr("udp", p.LocalConn.LocalAddr().String())
if err != nil {