github.com/decred/dcrlnd@v0.7.6/tor/onionaddr.go (about)

     1  package tor
     2  
     3  import (
     4  	"encoding/base32"
     5  	"net"
     6  	"strconv"
     7  )
     8  
     9  const (
    10  	// base32Alphabet is the alphabet used for encoding and decoding v2 and
    11  	// v3 onion addresses.
    12  	base32Alphabet = "abcdefghijklmnopqrstuvwxyz234567"
    13  
    14  	// OnionSuffix is the ".onion" suffix for v2 and v3 onion addresses.
    15  	OnionSuffix = ".onion"
    16  
    17  	// OnionSuffixLen is the length of the ".onion" suffix.
    18  	OnionSuffixLen = len(OnionSuffix)
    19  
    20  	// V2DecodedLen is the length of a decoded v2 onion service.
    21  	V2DecodedLen = 10
    22  
    23  	// V2Len is the length of a v2 onion service including the ".onion"
    24  	// suffix.
    25  	V2Len = 22
    26  
    27  	// V3DecodedLen is the length of a decoded v3 onion service.
    28  	V3DecodedLen = 35
    29  
    30  	// V3Len is the length of a v3 onion service including the ".onion"
    31  	// suffix.
    32  	V3Len = 62
    33  )
    34  
    35  var (
    36  	// Base32Encoding represents the Tor's base32-encoding scheme for v2 and
    37  	// v3 onion addresses.
    38  	Base32Encoding = base32.NewEncoding(base32Alphabet)
    39  )
    40  
    41  // OnionAddr represents a Tor network end point onion address.
    42  type OnionAddr struct {
    43  	// OnionService is the host of the onion address.
    44  	OnionService string
    45  
    46  	// Port is the port of the onion address.
    47  	Port int
    48  }
    49  
    50  // A compile-time check to ensure that OnionAddr implements the net.Addr
    51  // interface.
    52  var _ net.Addr = (*OnionAddr)(nil)
    53  
    54  // String returns the string representation of an onion address.
    55  func (o *OnionAddr) String() string {
    56  	return net.JoinHostPort(o.OnionService, strconv.Itoa(o.Port))
    57  }
    58  
    59  // Network returns the network that this implementation of net.Addr will use.
    60  // In this case, because Tor only allows TCP connections, the network is "tcp".
    61  func (o *OnionAddr) Network() string {
    62  	return "tcp"
    63  }