common.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. const (
  15. NmProxyPort = 51722
  16. DefaultCIDR = "127.0.0.1/8"
  17. )
  18. type Conn struct {
  19. Config ConnConfig
  20. Proxy Proxy
  21. }
  22. // ConnConfig is a peer Connection configuration
  23. type ConnConfig struct {
  24. // Key is a public key of a remote peer
  25. Key string
  26. // LocalKey is a public key of a local peer
  27. LocalKey string
  28. LocalWgPort int
  29. RemoteProxyIP net.IP
  30. RemoteWgPort int
  31. RemoteProxyPort int
  32. IsExtClient bool
  33. IsAttachedExtClient bool
  34. IngressGateWay *net.UDPAddr
  35. }
  36. type Config struct {
  37. Port int
  38. BodySize int
  39. Addr string
  40. RemoteKey string
  41. LocalKey string
  42. WgInterface *wg.WGIface
  43. AllowedIps []net.IPNet
  44. PreSharedKey *wgtypes.Key
  45. }
  46. // Proxy - WireguardProxy proxies
  47. type Proxy struct {
  48. Ctx context.Context
  49. Cancel context.CancelFunc
  50. Config Config
  51. RemoteConn *net.UDPAddr
  52. LocalConn net.Conn
  53. }
  54. type RemotePeer struct {
  55. PeerKey string
  56. Interface string
  57. Endpoint *net.UDPAddr
  58. IsExtClient bool
  59. IsAttachedExtClient bool
  60. }
  61. var WgIFaceMap = make(map[string]map[string]*Conn)
  62. var PeerKeyHashMap = make(map[string]RemotePeer)
  63. var WgIfaceKeyMap = make(map[string]RemotePeer)
  64. var RelayPeerMap = make(map[string]map[string]RemotePeer)
  65. var ExtClientsWaitTh = make(map[string][]context.CancelFunc)
  66. // RunCmd - runs a local command
  67. func RunCmd(command string, printerr bool) (string, error) {
  68. args := strings.Fields(command)
  69. cmd := exec.Command(args[0], args[1:]...)
  70. cmd.Wait()
  71. out, err := cmd.CombinedOutput()
  72. if err != nil && printerr {
  73. log.Println("error running command: ", command)
  74. log.Println(strings.TrimSuffix(string(out), "\n"))
  75. }
  76. return string(out), err
  77. }