ganserv.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package ganserv
  2. import (
  3. "net"
  4. "imuslab.com/zoraxy/mod/database"
  5. )
  6. /*
  7. Global Area Network
  8. Server side implementation
  9. This module do a few things to help manage
  10. the system GANs
  11. - Provide DHCP assign to client
  12. - Provide a list of connected nodes in the same VLAN
  13. - Provide proxy of packet if the target VLAN is online but not reachable
  14. Also provide HTTP Handler functions for management
  15. - Create Network
  16. - Update Network Properties (Name / Desc)
  17. - Delete Network
  18. - Authorize Node
  19. - Deauthorize Node
  20. - Set / Get Network Prefered Subnet Mask
  21. - Handle Node ping
  22. */
  23. type Node struct {
  24. Auth bool //If the node is authorized in this network
  25. ClientID string //The client ID
  26. MAC string //The tap MAC this client is using
  27. Name string //Name of the client in this network
  28. Description string //Description text
  29. ManagedIP net.IP //The IP address assigned by this network
  30. LastSeen int64 //Last time it is seen from this host
  31. ClientVersion string //Client application version
  32. PublicIP net.IP //Public IP address as seen from this host
  33. }
  34. type Network struct {
  35. UID string //UUID of the network, must be a 16 char random ASCII string
  36. Name string //Name of the network, ASCII only
  37. Description string //Description of the network
  38. CIDR string //The subnet masked use by this network
  39. Nodes []*Node //The nodes currently attached in this network
  40. }
  41. type NetworkManagerOptions struct {
  42. Database *database.Database
  43. AuthToken string
  44. ApiPort int
  45. }
  46. type NetworkMetaData struct {
  47. Desc string
  48. }
  49. type NetworkManager struct {
  50. authToken string
  51. apiPort int
  52. ControllerID string
  53. option *NetworkManagerOptions
  54. networksMetadata map[string]NetworkMetaData
  55. }
  56. //Create a new GAN manager
  57. func NewNetworkManager(option *NetworkManagerOptions) *NetworkManager {
  58. option.Database.NewTable("ganserv")
  59. //Start the zerotier instance if not exists
  60. //Get controller info
  61. instanceInfo, err := getControllerInfo(option.AuthToken, option.ApiPort)
  62. if err != nil {
  63. panic(err)
  64. }
  65. //Load network metadata
  66. networkMeta := map[string]NetworkMetaData{}
  67. if option.Database.KeyExists("ganserv", "networkmeta") {
  68. option.Database.Read("ganserv", "networkmeta", &networkMeta)
  69. }
  70. return &NetworkManager{
  71. authToken: option.AuthToken,
  72. apiPort: option.ApiPort,
  73. ControllerID: instanceInfo.Address,
  74. option: option,
  75. networksMetadata: networkMeta,
  76. }
  77. }
  78. func (m *NetworkManager) GetNetworkMetaData(netid string) *NetworkMetaData {
  79. md, ok := m.networksMetadata[netid]
  80. if !ok {
  81. return &NetworkMetaData{}
  82. }
  83. return &md
  84. }
  85. func (m *NetworkManager) WriteNetworkMetaData(netid string, meta *NetworkMetaData) {
  86. m.networksMetadata[netid] = *meta
  87. m.option.Database.Write("ganserv", "networkmeta", m.networksMetadata)
  88. }