nm-proxy.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package nmproxy
  2. import (
  3. "context"
  4. "log"
  5. "net"
  6. "os"
  7. "github.com/gravitl/netmaker/nm-proxy/common"
  8. "github.com/gravitl/netmaker/nm-proxy/manager"
  9. "github.com/gravitl/netmaker/nm-proxy/server"
  10. "github.com/gravitl/netmaker/nm-proxy/stun"
  11. )
  12. // Comm Channel to configure proxy
  13. /* Actions -
  14. 1. Add - new interface and its peers
  15. 2. Delete - remove close all conns for the interface,cleanup
  16. */
  17. func Start(ctx context.Context, mgmChan chan *manager.ManagerAction, apiServerAddr string) {
  18. log.Println("Starting Proxy...")
  19. common.IsHostNetwork = (os.Getenv("HOST_NETWORK") == "" || os.Getenv("HOST_NETWORK") == "on")
  20. hInfo := stun.GetHostInfo(apiServerAddr)
  21. stun.Host = hInfo
  22. log.Printf("HOSTINFO: %+v", hInfo)
  23. if IsPublicIP(hInfo.PrivIp) {
  24. log.Println("Host is public facing!!!")
  25. }
  26. // start the netclient proxy server
  27. err := server.NmProxyServer.CreateProxyServer(0, 0, hInfo.PrivIp.String())
  28. if err != nil {
  29. log.Fatal("failed to create proxy: ", err)
  30. }
  31. go manager.StartProxyManager(mgmChan)
  32. server.NmProxyServer.Listen(ctx)
  33. }
  34. // IsPublicIP indicates whether IP is public or not.
  35. func IsPublicIP(ip net.IP) bool {
  36. if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() || ip.IsPrivate() {
  37. return false
  38. }
  39. return true
  40. }