common.go 2.2 KB

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