1
0

ganserv.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package ganserv
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "imuslab.com/zoraxy/mod/database"
  7. )
  8. /*
  9. Global Area Network
  10. Server side implementation
  11. This module do a few things to help manage
  12. the system GANs
  13. - Provide DHCP assign to client
  14. - Provide a list of connected nodes in the same VLAN
  15. - Provide proxy of packet if the target VLAN is online but not reachable
  16. Also provide HTTP Handler functions for management
  17. - Create Network
  18. - Update Network Properties (Name / Desc)
  19. - Delete Network
  20. - Authorize Node
  21. - Deauthorize Node
  22. - Set / Get Network Prefered Subnet Mask
  23. - Handle Node ping
  24. */
  25. type Node struct {
  26. Auth bool //If the node is authorized in this network
  27. ClientID string //The client ID
  28. MAC string //The tap MAC this client is using
  29. Name string //Name of the client in this network
  30. Description string //Description text
  31. ManagedIP net.IP //The IP address assigned by this network
  32. LastSeen int64 //Last time it is seen from this host
  33. ClientVersion string //Client application version
  34. PublicIP net.IP //Public IP address as seen from this host
  35. }
  36. type Network struct {
  37. UID string //UUID of the network, must be a 16 char random ASCII string
  38. Name string //Name of the network, ASCII only
  39. Description string //Description of the network
  40. CIDR string //The subnet masked use by this network
  41. Nodes []*Node //The nodes currently attached in this network
  42. }
  43. type NetworkManagerOptions struct {
  44. Database *database.Database
  45. }
  46. type NetworkManager struct {
  47. option *NetworkManagerOptions
  48. networks []*Network
  49. }
  50. //Create a new GAN manager
  51. func NewNetworkManager(option *NetworkManagerOptions) *NetworkManager {
  52. option.Database.NewTable("ganserv")
  53. existingNetworks := []*Network{}
  54. if option.Database.KeyExists("ganserv", "networks") {
  55. option.Database.Read("ganserv", "networks", &existingNetworks)
  56. }
  57. return &NetworkManager{
  58. option: option,
  59. networks: existingNetworks,
  60. }
  61. }
  62. //List all the networks within this network manager
  63. func (m *NetworkManager) ListNetworks() []*Network {
  64. return m.networks
  65. }
  66. func (m *NetworkManager) AddNetwork(n *Network) error {
  67. if m.NetworkExists(n.UID) {
  68. return errors.New("network with given id already exists")
  69. }
  70. m.networks = append(m.networks, n)
  71. //Write the latest network list into database
  72. m.option.Database.Write("ganserv", "networks", m.networks)
  73. return nil
  74. }
  75. func (m *NetworkManager) RemoveNetwork(id string) error {
  76. for i, n := range m.networks {
  77. if n.UID == id {
  78. m.networks = append(m.networks[:i], m.networks[i+1:]...)
  79. //Write the latest network list into database
  80. m.option.Database.Write("ganserv", "networks", m.networks)
  81. return nil
  82. }
  83. }
  84. return fmt.Errorf("network not found: %s", id)
  85. }
  86. func (m *NetworkManager) GetNetworkByID(id string) (*Network, error) {
  87. for _, n := range m.networks {
  88. if n.UID == id {
  89. return n, nil
  90. }
  91. }
  92. return nil, fmt.Errorf("network not found: %s", id)
  93. }
  94. func (m *NetworkManager) NetworkExists(id string) bool {
  95. _, err := m.GetNetworkByID(id)
  96. return err == nil
  97. }