common.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package common
  2. import (
  3. "context"
  4. "log"
  5. "net"
  6. "os/exec"
  7. "strings"
  8. "github.com/gravitl/netmaker/nm-proxy/wg"
  9. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  10. )
  11. var IsHostNetwork bool
  12. var IsRelay bool
  13. var IsIngressGateway bool
  14. var IsRelayed bool
  15. const (
  16. NmProxyPort = 51722
  17. DefaultCIDR = "127.0.0.1/8"
  18. )
  19. type Conn struct {
  20. Config ConnConfig
  21. Proxy Proxy
  22. }
  23. // ConnConfig is a peer Connection configuration
  24. type ConnConfig struct {
  25. // Key is a public key of a remote peer
  26. Key string
  27. // LocalKey is a public key of a local peer
  28. LocalKey string
  29. LocalWgPort int
  30. RemoteProxyIP net.IP
  31. RemoteWgPort int
  32. RemoteProxyPort int
  33. IsExtClient bool
  34. IsRelayed bool
  35. RelayedEndpoint *net.UDPAddr
  36. IsAttachedExtClient bool
  37. IngressGateWay *net.UDPAddr
  38. }
  39. type Config struct {
  40. Port int
  41. BodySize int
  42. Addr string
  43. RemoteKey string
  44. LocalKey string
  45. WgInterface *wg.WGIface
  46. PeerConf *wgtypes.PeerConfig
  47. }
  48. // Proxy - WireguardProxy proxies
  49. type Proxy struct {
  50. Status bool
  51. Ctx context.Context
  52. Cancel context.CancelFunc
  53. Config Config
  54. RemoteConn *net.UDPAddr
  55. LocalConn net.Conn
  56. }
  57. type RemotePeer struct {
  58. PeerKey string
  59. Interface string
  60. Endpoint *net.UDPAddr
  61. IsExtClient bool
  62. IsAttachedExtClient bool
  63. }
  64. type WgIfaceConf struct {
  65. Iface *wgtypes.Device
  66. PeerMap map[string]*Conn
  67. }
  68. var WgIFaceMap = make(map[string]WgIfaceConf)
  69. var PeerKeyHashMap = make(map[string]RemotePeer)
  70. var WgIfaceKeyMap = make(map[string]RemotePeer)
  71. var RelayPeerMap = make(map[string]map[string]RemotePeer)
  72. var ExtClientsWaitTh = make(map[string][]context.CancelFunc)
  73. var PeerAddrMap = make(map[string]map[string]*Conn)
  74. // RunCmd - runs a local command
  75. func RunCmd(command string, printerr bool) (string, error) {
  76. args := strings.Fields(command)
  77. cmd := exec.Command(args[0], args[1:]...)
  78. cmd.Wait()
  79. out, err := cmd.CombinedOutput()
  80. if err != nil && printerr {
  81. log.Println("error running command: ", command)
  82. log.Println(strings.TrimSuffix(string(out), "\n"))
  83. }
  84. return string(out), err
  85. }