node.go 12 KB

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