proxy.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package models
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "fmt"
  6. "net"
  7. "sync"
  8. "time"
  9. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  10. )
  11. // ProxyAction - type for proxy action
  12. type ProxyAction string
  13. const (
  14. // default proxy port
  15. NmProxyPort = 51722
  16. // default CIDR for proxy peers
  17. DefaultCIDR = "127.0.0.1/8"
  18. // PersistentKeepaliveInterval - default keepalive for wg peer
  19. DefaultPersistentKeepaliveInterval = time.Duration(time.Second * 20)
  20. // ProxyUpdate - constant for proxy update action
  21. ProxyUpdate ProxyAction = "PROXY_UPDATE"
  22. // ProxyDeletePeers - constant for proxy delete peers action
  23. ProxyDeletePeers ProxyAction = "PROXY_DELETE"
  24. // ProxyDeleteAllPeers - constant for proxy delete all peers action
  25. ProxyDeleteAllPeers ProxyAction = "PROXY_DELETE_ALL"
  26. // NoProxy - constant for no ProxyAction
  27. NoProxy ProxyAction = "NO_PROXY"
  28. )
  29. // PeerConnMap - type for peer conn config map
  30. type PeerConnMap map[string]*Conn
  31. // Proxy - struct for proxy config
  32. type Proxy struct {
  33. PeerPublicKey wgtypes.Key
  34. IsExtClient bool
  35. PeerConf wgtypes.PeerConfig
  36. PeerEndpoint *net.UDPAddr
  37. RemoteConnAddr *net.UDPAddr
  38. LocalConnAddr *net.UDPAddr
  39. ListenPort int
  40. ProxyStatus bool
  41. }
  42. // Conn is a peer Connection configuration
  43. type Conn struct {
  44. // Key is a public key of a remote peer
  45. Key wgtypes.Key
  46. IsExtClient bool
  47. IsRelayed bool
  48. RelayedEndpoint *net.UDPAddr
  49. Config Proxy
  50. StopConn func()
  51. ResetConn func()
  52. LocalConn net.Conn
  53. Mutex *sync.RWMutex
  54. NetworkSettings map[string]Settings
  55. ServerMap map[string]struct{}
  56. }
  57. // RemotePeer - struct remote peer data
  58. type RemotePeer struct {
  59. PeerKey string
  60. Endpoint *net.UDPAddr
  61. IsExtClient bool
  62. LocalConn net.Conn
  63. CancelFunc context.CancelFunc
  64. CommChan chan *net.UDPAddr
  65. }
  66. // HostInfo - struct for host information
  67. type HostInfo struct {
  68. PublicIp net.IP
  69. PrivIp net.IP
  70. PubPort int
  71. PrivPort int
  72. ProxyEnabled bool
  73. }
  74. // RelayedConf - struct relayed peers config
  75. type RelayedConf struct {
  76. RelayedPeerEndpoint *net.UDPAddr `json:"relayed_peer_endpoint"`
  77. RelayedPeerPubKey string `json:"relayed_peer_pub_key"`
  78. Peers []wgtypes.PeerConfig `json:"relayed_peers"`
  79. }
  80. // PeerConf - struct for peer config in the network
  81. type PeerConf struct {
  82. Proxy bool `json:"proxy"`
  83. PublicListenPort int32 `json:"public_listen_port"`
  84. IsExtClient bool `json:"is_ext_client"`
  85. Address net.IP `json:"address"`
  86. ExtInternalIp net.IP `json:"ext_internal_ip"`
  87. IsRelayed bool `json:"is_relayed"`
  88. RelayedTo *net.UDPAddr `json:"relayed_to"`
  89. }
  90. // ConvPeerKeyToHash - converts peer key to a md5 hash
  91. func ConvPeerKeyToHash(peerKey string) string {
  92. return fmt.Sprintf("%x", md5.Sum([]byte(peerKey)))
  93. }
  94. // IsPublicIP indicates whether IP is public or not.
  95. func IsPublicIP(ip net.IP) bool {
  96. if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() || ip.IsPrivate() {
  97. return false
  98. }
  99. return true
  100. }
  101. // ProxyManagerPayload - struct for proxy manager payload
  102. type ProxyManagerPayload struct {
  103. Action ProxyAction `json:"action"`
  104. InterfaceName string `json:"interface_name"`
  105. Server string `json:"server"`
  106. //WgAddr string `json:"wg_addr"`
  107. Peers []wgtypes.PeerConfig `json:"peers"`
  108. PeerMap map[string]PeerConf `json:"peer_map"`
  109. IsIngress bool `json:"is_ingress"`
  110. IsRelayed bool `json:"is_relayed"`
  111. RelayedTo *net.UDPAddr `json:"relayed_to"`
  112. IsRelay bool `json:"is_relay"`
  113. RelayedPeerConf map[string]RelayedConf `json:"relayed_conf"`
  114. }
  115. // Metric - struct for metric data
  116. type ProxyMetric struct {
  117. NodeConnectionStatus map[string]bool `json:"node_connection_status"`
  118. LastRecordedLatency uint64 `json:"last_recorded_latency"`
  119. TrafficSent int64 `json:"traffic_sent"` // stored in MB
  120. TrafficRecieved int64 `json:"traffic_recieved"` // stored in MB
  121. }
  122. // Settings - struct for host settings
  123. type Settings struct {
  124. IsRelay bool
  125. IsIngressGateway bool
  126. IsRelayed bool
  127. RelayedTo *net.UDPAddr
  128. }