I love boringproxy project. I use it daily to connect remote computers deployed in the field.
I want to share my work to reestablish the ssh connection after the connection is lost.
TL;DR
I’ve marked the tunnel as not connected in case of ssh failure. During the periodic HTTPS connection to the server, I killed the unconnected sessions. Deleting the tunnel from the tunnel list forced the sync procedure to establish a new tunneling connection. After my change, the tunnel survived internet disconnection and server reboot.
I’m not a GO programmer; therefore, any comments are appreciated.
Details
I’ve created a method that marks the tunnel as not connected. It is done by setting the serverport
to 0
func (c *Client) MarkForDelete(tunnel Tunnel) {
log.Println("<NIR> mark for delete:", tunnel.Domain)
// Mark for delete
var tmp = c.tunnels[tunnel.Domain]
tmp.ServerPort = 0
c.tunnels[tunnel.Domain] = tmp
}
I’m marking the tunnel in case of an error during the connection:
func (c *Client) SyncTunnels(ctx context.Context, serverTunnels map[string]Tunnel) {
...
go func(closureCtx context.Context, tun Tunnel, k string) {
err := c.BoreTunnel(closureCtx, tun, k)
if err != nil {
log.Println("BoreTunnel error: ", err)
c.MarkForDelete(tun)
}
}(cancelCtx, newTun, k)
...
}
and
func (c *Client) BoreTunnel(ctx context.Context, tunnel Tunnel, k string) error {
...
if err != nil {
c.MarkForDelete(tunnel)
break
}
...
}
During periodic status updates:
func (c *Client) Run(ctx context.Context) error {
...
for k, _ := range c.tunnels {
if c.tunnels[k].ServerPort == 0 {
log.Println("<NIR> Kill tunnel:", k)
c.cancelFuncsMutex.Lock()
c.cancelFuncs[k]()
c.cancelFuncsMutex.Unlock()
delete(c.cancelFuncs, k)
delete(c.tunnels, k)
c.previousEtag = "" // Force resync
}
}
...
}