mirror of
https://github.com/boringproxy/boringproxy.git
synced 2025-02-25 18:55:29 -06:00
Start client tunnels concurrently
This commit is contained in:
parent
1abc141d13
commit
7f595c2b0d
31
client.go
31
client.go
@ -138,12 +138,23 @@ func (c *BoringProxyClient) SyncTunnels(serverTunnels map[string]Tunnel) {
|
|||||||
log.Println("New tunnel", k)
|
log.Println("New tunnel", k)
|
||||||
c.tunnels[k] = newTun
|
c.tunnels[k] = newTun
|
||||||
cancel := c.BoreTunnel(newTun)
|
cancel := c.BoreTunnel(newTun)
|
||||||
|
|
||||||
|
c.cancelFuncsMutex.Lock()
|
||||||
c.cancelFuncs[k] = cancel
|
c.cancelFuncs[k] = cancel
|
||||||
|
c.cancelFuncsMutex.Unlock()
|
||||||
|
|
||||||
} else if newTun != tun {
|
} else if newTun != tun {
|
||||||
log.Println("Restart tunnel", k)
|
log.Println("Restart tunnel", k)
|
||||||
|
|
||||||
|
c.cancelFuncsMutex.Lock()
|
||||||
c.cancelFuncs[k]()
|
c.cancelFuncs[k]()
|
||||||
|
c.cancelFuncsMutex.Unlock()
|
||||||
|
|
||||||
cancel := c.BoreTunnel(newTun)
|
cancel := c.BoreTunnel(newTun)
|
||||||
|
|
||||||
|
c.cancelFuncsMutex.Lock()
|
||||||
c.cancelFuncs[k] = cancel
|
c.cancelFuncs[k] = cancel
|
||||||
|
c.cancelFuncsMutex.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,15 +163,20 @@ func (c *BoringProxyClient) SyncTunnels(serverTunnels map[string]Tunnel) {
|
|||||||
_, exists := serverTunnels[k]
|
_, exists := serverTunnels[k]
|
||||||
if !exists {
|
if !exists {
|
||||||
log.Println("Kill tunnel", k)
|
log.Println("Kill tunnel", k)
|
||||||
|
c.cancelFuncsMutex.Lock()
|
||||||
c.cancelFuncs[k]()
|
c.cancelFuncs[k]()
|
||||||
delete(c.tunnels, k)
|
|
||||||
delete(c.cancelFuncs, k)
|
delete(c.cancelFuncs, k)
|
||||||
|
c.cancelFuncsMutex.Unlock()
|
||||||
|
delete(c.tunnels, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *BoringProxyClient) BoreTunnel(tunnel Tunnel) context.CancelFunc {
|
func (c *BoringProxyClient) BoreTunnel(tunnel Tunnel) context.CancelFunc {
|
||||||
|
|
||||||
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
go func() {
|
||||||
signer, err := ssh.ParsePrivateKey([]byte(tunnel.TunnelPrivateKey))
|
signer, err := ssh.ParsePrivateKey([]byte(tunnel.TunnelPrivateKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to parse private key: %v", err)
|
log.Fatalf("unable to parse private key: %v", err)
|
||||||
@ -195,14 +211,6 @@ func (c *BoringProxyClient) BoreTunnel(tunnel Tunnel) context.CancelFunc {
|
|||||||
}
|
}
|
||||||
//defer listener.Close()
|
//defer listener.Close()
|
||||||
|
|
||||||
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
<-ctx.Done()
|
|
||||||
listener.Close()
|
|
||||||
client.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
conn, err := listener.Accept()
|
conn, err := listener.Accept()
|
||||||
@ -219,6 +227,11 @@ func (c *BoringProxyClient) BoreTunnel(tunnel Tunnel) context.CancelFunc {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
<-ctx.Done()
|
||||||
|
listener.Close()
|
||||||
|
client.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
return cancelFunc
|
return cancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user