github.com/decred/dcrlnd@v0.7.6/healthcheck/tor_connection.go (about)

     1  package healthcheck
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"syscall"
     8  
     9  	"github.com/decred/dcrlnd/tor"
    10  )
    11  
    12  // CheckTorServiceStatus checks whether the onion service is reachable by
    13  // sending a GETINFO command to the Tor daemon using our tor controller.
    14  // We will get an EOF or a broken pipe error if the Tor daemon is
    15  // stopped/restarted as the previously created socket connection is no longer
    16  // open. In this case, we will attempt a restart on our tor controller. If the
    17  // tor daemon comes back, a new socket connection will then be created.
    18  func CheckTorServiceStatus(tc *tor.Controller,
    19  	createService func() error) error {
    20  
    21  	// Send a cmd using GETINFO onions/current and checks that our known
    22  	// onion serviceID can be found.
    23  	err := tc.CheckOnionService()
    24  	if err == nil {
    25  		return nil
    26  	}
    27  
    28  	log.Debugf("Checking tor service got: %v", err)
    29  
    30  	switch {
    31  	// We will get an EOF if the connection is lost. In this case, we will
    32  	// return an error and wait for the Tor daemon to come back. We won't
    33  	// attempt to make a new connection since we know Tor daemon is down.
    34  	case errors.Is(err, io.EOF), errors.Is(err, syscall.ECONNREFUSED):
    35  		return fmt.Errorf("Tor daemon connection lost, " +
    36  			"check if Tor is up and running")
    37  
    38  	// Once Tor daemon is down, we will get a broken pipe error when we use
    39  	// the existing connection to make a GETINFO request since that socket
    40  	// has now been closed. As Tor daemon might not be running yet, we will
    41  	// attempt to make a new connection till Tor daemon is back.
    42  	case errors.Is(err, syscall.EPIPE):
    43  		log.Warnf("Tor connection lost, attempting a tor controller " +
    44  			"re-connection...")
    45  
    46  		// If the restart fails, we will attempt again during our next
    47  		// healthcheck cycle.
    48  		return restartTorController(tc, createService)
    49  
    50  	// If this is not a connection layer error, such as
    51  	// ErrServiceNotCreated or ErrServiceIDMismatch, there's little we can
    52  	// do but to report the error to the user.
    53  	default:
    54  		return err
    55  	}
    56  }
    57  
    58  // restartTorController attempts to make a new connection to the Tor daemon and
    59  // re-create the Hidden Service.
    60  func restartTorController(tc *tor.Controller,
    61  	createService func() error) error {
    62  
    63  	err := tc.Reconnect()
    64  
    65  	// If we get a connection refused error, it means Tor daemon might not
    66  	// be started.
    67  	if errors.Is(err, syscall.ECONNREFUSED) {
    68  		return fmt.Errorf("check if Tor daemon is running")
    69  	}
    70  
    71  	// Otherwise, we get an unexpected and return it.
    72  	if err != nil {
    73  		log.Errorf("Re-connectting tor got err: %v", err)
    74  		return err
    75  	}
    76  
    77  	// Recreate the Hidden Service.
    78  	if err := createService(); err != nil {
    79  		log.Errorf("Re-create service tor got err: %v", err)
    80  		return err
    81  	}
    82  
    83  	log.Info("Successfully restarted tor connection!")
    84  
    85  	return nil
    86  }