node.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. package models
  2. import (
  3. "bytes"
  4. "math/rand"
  5. "net"
  6. "strings"
  7. "time"
  8. "golang.org/x/crypto/bcrypt"
  9. )
  10. const TEN_YEARS_IN_SECONDS = 300000000
  11. const MAX_NAME_LENGTH = 62
  12. // == ACTIONS == (can only be set by GRPC)
  13. const NODE_UPDATE_KEY = "updatekey"
  14. const NODE_SERVER_NAME = "netmaker"
  15. const NODE_DELETE = "delete"
  16. const NODE_IS_PENDING = "pending"
  17. const NODE_NOOP = "noop"
  18. var seededRand *rand.Rand = rand.New(
  19. rand.NewSource(time.Now().UnixNano()))
  20. // node struct
  21. type Node struct {
  22. ID string `json:"id,omitempty" bson:"id,omitempty"`
  23. Address string `json:"address" bson:"address" yaml:"address" validate:"omitempty,ipv4"`
  24. Address6 string `json:"address6" bson:"address6" yaml:"address6" validate:"omitempty,ipv6"`
  25. LocalAddress string `json:"localaddress" bson:"localaddress" yaml:"localaddress" validate:"omitempty,ip"`
  26. Name string `json:"name" bson:"name" yaml:"name" validate:"omitempty,max=62,in_charset"`
  27. NetworkSettings Network `json:"networksettings" bson:"networksettings" yaml:"networksettings" validate:"-"`
  28. ListenPort int32 `json:"listenport" bson:"listenport" yaml:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
  29. PublicKey string `json:"publickey" bson:"publickey" yaml:"publickey" validate:"required,base64"`
  30. Endpoint string `json:"endpoint" bson:"endpoint" yaml:"endpoint" validate:"required,ip"`
  31. PostUp string `json:"postup" bson:"postup" yaml:"postup"`
  32. PostDown string `json:"postdown" bson:"postdown" yaml:"postdown"`
  33. AllowedIPs []string `json:"allowedips" bson:"allowedips" yaml:"allowedips"`
  34. PersistentKeepalive int32 `json:"persistentkeepalive" bson:"persistentkeepalive" yaml:"persistentkeepalive" validate:"omitempty,numeric,max=1000"`
  35. SaveConfig string `json:"saveconfig" bson:"saveconfig" yaml:"saveconfig" validate:"checkyesorno"`
  36. AccessKey string `json:"accesskey" bson:"accesskey" yaml:"accesskey"`
  37. Interface string `json:"interface" bson:"interface" yaml:"interface"`
  38. LastModified int64 `json:"lastmodified" bson:"lastmodified" yaml:"lastmodified"`
  39. KeyUpdateTimeStamp int64 `json:"keyupdatetimestamp" bson:"keyupdatetimestamp" yaml:"keyupdatetimestamp"`
  40. ExpirationDateTime int64 `json:"expdatetime" bson:"expdatetime" yaml:"expdatetime"`
  41. LastPeerUpdate int64 `json:"lastpeerupdate" bson:"lastpeerupdate" yaml:"lastpeerupdate"`
  42. LastCheckIn int64 `json:"lastcheckin" bson:"lastcheckin" yaml:"lastcheckin"`
  43. MacAddress string `json:"macaddress" bson:"macaddress" yaml:"macaddress" validate:"required,min=5,macaddress_unique"`
  44. // checkin interval is depreciated at the network level. Set on server with CHECKIN_INTERVAL
  45. CheckInInterval int32 `json:"checkininterval" bson:"checkininterval" yaml:"checkininterval"`
  46. Password string `json:"password" bson:"password" yaml:"password" validate:"required,min=6"`
  47. Network string `json:"network" bson:"network" yaml:"network" validate:"network_exists"`
  48. IsRelayed string `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
  49. IsPending string `json:"ispending" bson:"ispending" yaml:"ispending"`
  50. IsRelay string `json:"isrelay" bson:"isrelay" yaml:"isrelay" validate:"checkyesorno"`
  51. IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway" yaml:"isegressgateway"`
  52. IsIngressGateway string `json:"isingressgateway" bson:"isingressgateway" yaml:"isingressgateway"`
  53. EgressGatewayRanges []string `json:"egressgatewayranges" bson:"egressgatewayranges" yaml:"egressgatewayranges"`
  54. RelayAddrs []string `json:"relayaddrs" bson:"relayaddrs" yaml:"relayaddrs"`
  55. IngressGatewayRange string `json:"ingressgatewayrange" bson:"ingressgatewayrange" yaml:"ingressgatewayrange"`
  56. IsStatic string `json:"isstatic" bson:"isstatic" yaml:"isstatic" validate:"checkyesorno"`
  57. UDPHolePunch string `json:"udpholepunch" bson:"udpholepunch" yaml:"udpholepunch" validate:"checkyesorno"`
  58. PullChanges string `json:"pullchanges" bson:"pullchanges" yaml:"pullchanges" validate:"checkyesorno"`
  59. DNSOn string `json:"dnson" bson:"dnson" yaml:"dnson" validate:"checkyesorno"`
  60. IsDualStack string `json:"isdualstack" bson:"isdualstack" yaml:"isdualstack" validate:"checkyesorno"`
  61. IsServer string `json:"isserver" bson:"isserver" yaml:"isserver" validate:"checkyesorno"`
  62. Action string `json:"action" bson:"action" yaml:"action"`
  63. IsLocal string `json:"islocal" bson:"islocal" yaml:"islocal" validate:"checkyesorno"`
  64. LocalRange string `json:"localrange" bson:"localrange" yaml:"localrange"`
  65. Roaming string `json:"roaming" bson:"roaming" yaml:"roaming" validate:"checkyesorno"`
  66. IPForwarding string `json:"ipforwarding" bson:"ipforwarding" yaml:"ipforwarding" validate:"checkyesorno"`
  67. OS string `json:"os" bson:"os" yaml:"os"`
  68. MTU int32 `json:"mtu" bson:"mtu" yaml:"mtu"`
  69. }
  70. // NodesArray - used for node sorting
  71. type NodesArray []Node
  72. // NodesArray.Len - gets length of node array
  73. func (a NodesArray) Len() int { return len(a) }
  74. // NodesArray.Less - gets returns lower rank of two node addresses
  75. func (a NodesArray) Less(i, j int) bool { return isLess(a[i].Address, a[j].Address) }
  76. // NodesArray.Swap - swaps two nodes in array
  77. func (a NodesArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  78. func isLess(ipA string, ipB string) bool {
  79. ipNetA := net.ParseIP(ipA)
  80. ipNetB := net.ParseIP(ipB)
  81. return bytes.Compare(ipNetA, ipNetB) < 0
  82. }
  83. // Node.SetDefaultMTU - sets default MTU of a node
  84. func (node *Node) SetDefaultMTU() {
  85. if node.MTU == 0 {
  86. node.MTU = 1280
  87. }
  88. }
  89. // Node.SetDefaulIsPending - sets ispending default
  90. func (node *Node) SetDefaulIsPending() {
  91. if node.IsPending == "" {
  92. node.IsPending = "no"
  93. }
  94. }
  95. func (node *Node) SetDefaultIsRelayed() {
  96. if node.IsRelayed == "" {
  97. node.IsRelayed = "no"
  98. }
  99. }
  100. func (node *Node) SetDefaultIsRelay() {
  101. if node.IsRelay == "" {
  102. node.IsRelay = "no"
  103. }
  104. }
  105. func (node *Node) SetDefaultEgressGateway() {
  106. if node.IsEgressGateway == "" {
  107. node.IsEgressGateway = "no"
  108. }
  109. }
  110. func (node *Node) SetDefaultIngressGateway() {
  111. if node.IsIngressGateway == "" {
  112. node.IsIngressGateway = "no"
  113. }
  114. }
  115. func (node *Node) SetDefaultAction() {
  116. if node.Action == "" {
  117. node.Action = NODE_NOOP
  118. }
  119. }
  120. func (node *Node) SetRoamingDefault() {
  121. if node.Roaming == "" {
  122. node.Roaming = "yes"
  123. }
  124. }
  125. func (node *Node) SetPullChangesDefault() {
  126. if node.PullChanges == "" {
  127. node.PullChanges = "no"
  128. }
  129. }
  130. func (node *Node) SetIPForwardingDefault() {
  131. if node.IPForwarding == "" {
  132. node.IPForwarding = "yes"
  133. }
  134. }
  135. func (node *Node) SetIsLocalDefault() {
  136. if node.IsLocal == "" {
  137. node.IsLocal = "no"
  138. }
  139. }
  140. func (node *Node) SetDNSOnDefault() {
  141. if node.DNSOn == "" {
  142. node.DNSOn = "yes"
  143. }
  144. }
  145. func (node *Node) SetIsDualStackDefault() {
  146. if node.IsDualStack == "" {
  147. node.IsDualStack = "no"
  148. }
  149. }
  150. func (node *Node) SetIsServerDefault() {
  151. if node.IsServer != "yes" {
  152. node.IsServer = "no"
  153. }
  154. }
  155. func (node *Node) SetIsStaticDefault() {
  156. if node.IsServer == "yes" {
  157. node.IsStatic = "yes"
  158. } else if node.IsStatic != "yes" {
  159. node.IsStatic = "no"
  160. }
  161. }
  162. func (node *Node) SetLastModified() {
  163. node.LastModified = time.Now().Unix()
  164. }
  165. func (node *Node) SetLastCheckIn() {
  166. node.LastCheckIn = time.Now().Unix()
  167. }
  168. func (node *Node) SetLastPeerUpdate() {
  169. node.LastPeerUpdate = time.Now().Unix()
  170. }
  171. func (node *Node) SetExpirationDateTime() {
  172. node.ExpirationDateTime = time.Now().Unix() + TEN_YEARS_IN_SECONDS
  173. }
  174. func (node *Node) SetDefaultName() {
  175. if node.Name == "" {
  176. node.Name = GenerateNodeName()
  177. }
  178. }
  179. func (newNode *Node) Fill(currentNode *Node) {
  180. if newNode.ID == "" {
  181. newNode.ID = currentNode.ID
  182. }
  183. if newNode.Address == "" && newNode.IsStatic != "yes" {
  184. newNode.Address = currentNode.Address
  185. }
  186. if newNode.Address6 == "" && newNode.IsStatic != "yes" {
  187. newNode.Address6 = currentNode.Address6
  188. }
  189. if newNode.LocalAddress == "" {
  190. newNode.LocalAddress = currentNode.LocalAddress
  191. }
  192. if newNode.Name == "" {
  193. newNode.Name = currentNode.Name
  194. }
  195. if newNode.ListenPort == 0 && newNode.IsStatic != "yes" {
  196. newNode.ListenPort = currentNode.ListenPort
  197. }
  198. if newNode.PublicKey == "" && newNode.IsStatic != "yes" {
  199. newNode.PublicKey = currentNode.PublicKey
  200. } else {
  201. newNode.KeyUpdateTimeStamp = time.Now().Unix()
  202. }
  203. if newNode.Endpoint == "" && newNode.IsStatic != "yes" {
  204. newNode.Endpoint = currentNode.Endpoint
  205. }
  206. if newNode.PostUp == "" {
  207. newNode.PostUp = currentNode.PostUp
  208. }
  209. if newNode.PostDown == "" {
  210. newNode.PostDown = currentNode.PostDown
  211. }
  212. if newNode.AllowedIPs == nil {
  213. newNode.AllowedIPs = currentNode.AllowedIPs
  214. }
  215. if newNode.PersistentKeepalive == 0 {
  216. newNode.PersistentKeepalive = currentNode.PersistentKeepalive
  217. }
  218. if newNode.SaveConfig == "" {
  219. newNode.SaveConfig = currentNode.SaveConfig
  220. }
  221. if newNode.AccessKey == "" {
  222. newNode.AccessKey = currentNode.AccessKey
  223. }
  224. if newNode.Interface == "" {
  225. newNode.Interface = currentNode.Interface
  226. }
  227. if newNode.LastModified == 0 {
  228. newNode.LastModified = currentNode.LastModified
  229. }
  230. if newNode.KeyUpdateTimeStamp == 0 {
  231. newNode.LastModified = currentNode.LastModified
  232. }
  233. if newNode.ExpirationDateTime == 0 {
  234. newNode.ExpirationDateTime = currentNode.ExpirationDateTime
  235. }
  236. if newNode.LastPeerUpdate == 0 {
  237. newNode.LastPeerUpdate = currentNode.LastPeerUpdate
  238. }
  239. if newNode.LastCheckIn == 0 {
  240. newNode.LastCheckIn = currentNode.LastCheckIn
  241. }
  242. if newNode.MacAddress == "" {
  243. newNode.MacAddress = currentNode.MacAddress
  244. }
  245. if newNode.CheckInInterval == 0 {
  246. newNode.CheckInInterval = currentNode.CheckInInterval
  247. }
  248. if newNode.Password != "" {
  249. err := bcrypt.CompareHashAndPassword([]byte(newNode.Password), []byte(currentNode.Password))
  250. if err != nil && currentNode.Password != newNode.Password {
  251. hash, err := bcrypt.GenerateFromPassword([]byte(newNode.Password), 5)
  252. if err == nil {
  253. newNode.Password = string(hash)
  254. }
  255. }
  256. } else {
  257. newNode.Password = currentNode.Password
  258. }
  259. if newNode.Network == "" {
  260. newNode.Network = currentNode.Network
  261. }
  262. if newNode.IsPending == "" {
  263. newNode.IsPending = currentNode.IsPending
  264. }
  265. if newNode.IsEgressGateway == "" {
  266. newNode.IsEgressGateway = currentNode.IsEgressGateway
  267. }
  268. if newNode.IsIngressGateway == "" {
  269. newNode.IsIngressGateway = currentNode.IsIngressGateway
  270. }
  271. if newNode.EgressGatewayRanges == nil {
  272. newNode.EgressGatewayRanges = currentNode.EgressGatewayRanges
  273. }
  274. if newNode.IngressGatewayRange == "" {
  275. newNode.IngressGatewayRange = currentNode.IngressGatewayRange
  276. }
  277. if newNode.IsStatic == "" {
  278. newNode.IsStatic = currentNode.IsStatic
  279. }
  280. if newNode.UDPHolePunch == "" {
  281. newNode.UDPHolePunch = currentNode.SaveConfig
  282. }
  283. if newNode.DNSOn == "" {
  284. newNode.DNSOn = currentNode.DNSOn
  285. }
  286. if newNode.IsDualStack == "" {
  287. newNode.IsDualStack = currentNode.IsDualStack
  288. }
  289. if newNode.IsLocal == "" {
  290. newNode.IsLocal = currentNode.IsLocal
  291. }
  292. if newNode.IPForwarding == "" {
  293. newNode.IPForwarding = currentNode.IPForwarding
  294. }
  295. if newNode.PullChanges == "" {
  296. newNode.PullChanges = currentNode.PullChanges
  297. }
  298. if newNode.Roaming == "" {
  299. newNode.Roaming = currentNode.Roaming
  300. }
  301. if newNode.Action == "" {
  302. newNode.Action = currentNode.Action
  303. }
  304. if newNode.IsServer == "" {
  305. newNode.IsServer = currentNode.IsServer
  306. }
  307. if newNode.IsServer == "yes" {
  308. newNode.IsStatic = "yes"
  309. }
  310. if newNode.MTU == 0 {
  311. newNode.MTU = currentNode.MTU
  312. }
  313. if newNode.OS == "" {
  314. newNode.OS = currentNode.OS
  315. }
  316. if newNode.RelayAddrs == nil {
  317. newNode.RelayAddrs = currentNode.RelayAddrs
  318. }
  319. if newNode.IsRelay == "" {
  320. newNode.IsRelay = currentNode.IsRelay
  321. }
  322. if newNode.IsRelayed == "" {
  323. newNode.IsRelayed = currentNode.IsRelayed
  324. }
  325. }
  326. func StringWithCharset(length int, charset string) string {
  327. b := make([]byte, length)
  328. for i := range b {
  329. b[i] = charset[seededRand.Intn(len(charset))]
  330. }
  331. return string(b)
  332. }
  333. //Check for valid IPv4 address
  334. //Note: We dont handle IPv6 AT ALL!!!!! This definitely is needed at some point
  335. //But for iteration 1, lets just stick to IPv4. Keep it simple stupid.
  336. func IsIpv4Net(host string) bool {
  337. return net.ParseIP(host) != nil
  338. }
  339. func (node *Node) NameInNodeCharSet() bool {
  340. charset := "abcdefghijklmnopqrstuvwxyz1234567890-"
  341. for _, char := range node.Name {
  342. if !strings.Contains(charset, strings.ToLower(string(char))) {
  343. return false
  344. }
  345. }
  346. return true
  347. }