common.go 1.6 KB

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