client_identity.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package p2p
  2. import (
  3. "fmt"
  4. "runtime"
  5. )
  6. // should be used in Peer handleHandshake, incorporate Caps, ProtocolVersion, Pubkey etc.
  7. type ClientIdentity interface {
  8. String() string
  9. Pubkey() []byte
  10. }
  11. type SimpleClientIdentity struct {
  12. clientIdentifier string
  13. version string
  14. customIdentifier string
  15. os string
  16. implementation string
  17. pubkey string
  18. }
  19. func NewSimpleClientIdentity(clientIdentifier string, version string, customIdentifier string, pubkey string) *SimpleClientIdentity {
  20. clientIdentity := &SimpleClientIdentity{
  21. clientIdentifier: clientIdentifier,
  22. version: version,
  23. customIdentifier: customIdentifier,
  24. os: runtime.GOOS,
  25. implementation: runtime.Version(),
  26. pubkey: pubkey,
  27. }
  28. return clientIdentity
  29. }
  30. func (c *SimpleClientIdentity) init() {
  31. }
  32. func (c *SimpleClientIdentity) String() string {
  33. var id string
  34. if len(c.customIdentifier) > 0 {
  35. id = "/" + c.customIdentifier
  36. }
  37. return fmt.Sprintf("%s/v%s%s/%s/%s",
  38. c.clientIdentifier,
  39. c.version,
  40. id,
  41. c.os,
  42. c.implementation)
  43. }
  44. func (c *SimpleClientIdentity) Pubkey() []byte {
  45. return []byte(c.pubkey)
  46. }
  47. func (c *SimpleClientIdentity) SetCustomIdentifier(customIdentifier string) {
  48. c.customIdentifier = customIdentifier
  49. }
  50. func (c *SimpleClientIdentity) GetCustomIdentifier() string {
  51. return c.customIdentifier
  52. }