ganserv.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. return &NetworkManager{
  53. option: option,
  54. }
  55. }
  56. //List all the networks within this network manager
  57. func (m *NetworkManager) ListNetworks() []*Network {
  58. return m.networks
  59. }
  60. func (m *NetworkManager) AddNetwork(n *Network) error {
  61. if m.NetworkExists(n.UID) {
  62. return errors.New("network with given id already exists")
  63. }
  64. m.networks = append(m.networks, n)
  65. return nil
  66. }
  67. func (m *NetworkManager) RemoveNetwork(id string) error {
  68. for i, n := range m.networks {
  69. if n.UID == id {
  70. m.networks = append(m.networks[:i], m.networks[i+1:]...)
  71. return nil
  72. }
  73. }
  74. return fmt.Errorf("network not found: %s", id)
  75. }
  76. func (m *NetworkManager) GetNetworkByID(id string) (*Network, error) {
  77. for _, n := range m.networks {
  78. if n.UID == id {
  79. return n, nil
  80. }
  81. }
  82. return nil, fmt.Errorf("network not found: %s", id)
  83. }
  84. func (m *NetworkManager) NetworkExists(id string) bool {
  85. _, err := m.GetNetworkByID(id)
  86. return err == nil
  87. }