onionaddr.go 1.7 KB

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