Skip to content

Commit

Permalink
Improve logging on daemon side
Browse files Browse the repository at this point in the history
  • Loading branch information
alebeck committed Nov 2, 2024
1 parent a451f8e commit ef779a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
7 changes: 5 additions & 2 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,13 @@ func openTunnel(s *state, conn net.Conn, t tunnel.Tunnel) {
_, exists := s.tunnels[t.Name]
s.mutex.RUnlock()
if exists {
err = fmt.Errorf("tunnel already running")
err = fmt.Errorf("already running")
log.Errorf("%v: could not open: %v", t.Name, err)
return
}

if err = t.Open(); err != nil {
log.Errorf("%v: could not open: %v", t.Name, err)
return
}

Expand All @@ -179,11 +181,12 @@ func closeTunnel(s *state, conn net.Conn, q tunnel.Tunnel) {
s.mutex.RUnlock()
if !ok {
err = fmt.Errorf("tunnel not running")
log.Errorf("%v: could not close tunnel: %v", t.Name, err)
return
}

if err = t.Close(); err != nil {
err = fmt.Errorf("could not close tunnel: %v", err)
log.Errorf("%v: could not close tunnel: %v", t.Name, err)
return
}
<-t.Closed
Expand Down
33 changes: 19 additions & 14 deletions internal/tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (t *Tunnel) Open() error {

go t.run()

log.Infof("Opened tunnel %v...", t.Name)
log.Infof("%v: opened tunnel", t.Name)
t.Status = Open
t.LastConn = time.Now()
return nil
Expand Down Expand Up @@ -96,7 +96,7 @@ func (t *Tunnel) run() {
disconn := make(chan struct{})
go func() {
t.client.Wait()
log.Infof("Client closed for %v", t.Name)
log.Infof("%v: client closed", t.Name)
close(disconn)
}()

Expand All @@ -106,15 +106,20 @@ func (t *Tunnel) run() {
stopped := false
select {
case <-t.stop:
log.Infof("Received stop signal for %v...", t.Name)
log.Infof("%v: received stop signal", t.Name)
stopped = true
t.client.Close()
case <-disconn:
}
t.listener.Close()
t.wg.Wait()
if !stopped && t.reconnectLoop() == nil {
return
if !stopped {
if err := t.reconnectLoop(); err != nil {
log.Errorf("%v: could not re-connect: %v", t.Name, err)
} else {
// Successfully re-connected
return
}
}
t.Status = Closed
close(t.Closed)
Expand All @@ -128,12 +133,12 @@ func (t *Tunnel) keepAlive(cancel chan struct{}) {
case <-time.After(keepAliveInterval):
_, _, err := t.client.SendRequest("[email protected]", true, nil)
if err != nil {
log.Errorf("Error sending keepalive for tunnel %v: %v", t.Name, err)
log.Errorf("%v: error sending keepalive: %v", t.Name, err)
// Close the client, this triggers the reconnection logic
t.client.Close()
return
}
log.Debugf("Sent keep-alive")
log.Debugf("%v: sent keep-alive", t.Name)
}
}
}
Expand All @@ -152,7 +157,7 @@ func (t *Tunnel) handleForward() {
for {
conn1, err := t.listener.Accept()
if err != nil {
log.Errorf("could not accept: %v", err)
log.Errorf("%v: could not accept: %v", t.Name, err)
return
}
go t.waitFor(func() {
Expand All @@ -162,7 +167,7 @@ func (t *Tunnel) handleForward() {
}
conn2, err := t.dial(netw, addr)
if err != nil {
log.Errorf("could not dial: %v", err)
log.Errorf("%v: could not dial: %v", t.Name, err)
return
}
tunnel(conn1, conn2)
Expand Down Expand Up @@ -197,7 +202,7 @@ func (t *Tunnel) handleSocks() {
for {
conn, err := t.listener.Accept()
if err != nil {
log.Errorf("could not accept: %v", err)
log.Errorf("%v: could not accept: %v", t.Name, err)
return
}
go t.waitFor(func() { serv.ServeConn(conn) })
Expand All @@ -213,16 +218,16 @@ func (t *Tunnel) reconnectLoop() error {
for {
select {
case <-timeout:
return fmt.Errorf("reconnect timeout")
return fmt.Errorf("re-connect timeout")
case <-t.stop:
return fmt.Errorf("reconnect interrupted")
return fmt.Errorf("re-connect interrupted by stop signal")
case <-wait.C:
log.Infof("Reconnecting tunnel %v...", t.Name)
log.Infof("%v: try re-connect...", t.Name)
err := t.Open()
if err == nil {
return nil
}
log.Errorf("could not reconnect tunnel %v: %v. Retrying in %v...",
log.Errorf("%v: could not re-connect: %v. Retrying in %v...",
t.Name, err, waitTime)
wait.Reset(waitTime)
waitTime *= 2
Expand Down

0 comments on commit ef779a0

Please sign in to comment.