common.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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]struct{})
  64. var RelayPeerMap = make(map[string]map[string]RemotePeer)
  65. // RunCmd - runs a local command
  66. func RunCmd(command string, printerr bool) (string, error) {
  67. args := strings.Fields(command)
  68. cmd := exec.Command(args[0], args[1:]...)
  69. cmd.Wait()
  70. out, err := cmd.CombinedOutput()
  71. if err != nil && printerr {
  72. log.Println("error running command: ", command)
  73. log.Println(strings.TrimSuffix(string(out), "\n"))
  74. }
  75. return string(out), err
  76. }