clientid.go 933 B

1234567891011121314151617181920212223242526272829
  1. package turbotunnel
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. )
  6. // ClientID is an abstract identifier that binds together all the communications
  7. // belonging to a single client session, even though those communications may
  8. // arrive from multiple IP addresses or over multiple lower-level connections.
  9. // It plays the same role that an (IP address, port number) tuple plays in a
  10. // net.UDPConn: it's the return address pertaining to a long-lived abstract
  11. // client session. The client attaches its ClientID to each of its
  12. // communications, enabling the server to disambiguate requests among its many
  13. // clients. ClientID implements the net.Addr interface.
  14. type ClientID [8]byte
  15. func NewClientID() ClientID {
  16. var id ClientID
  17. _, err := rand.Read(id[:])
  18. if err != nil {
  19. panic(err)
  20. }
  21. return id
  22. }
  23. func (id ClientID) Network() string { return "clientid" }
  24. func (id ClientID) String() string { return hex.EncodeToString(id[:]) }