nm-proxy.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. /*
  13. TODO:
  14. 1. Mutex locks for maps
  15. 2. CRUD funcs on Maps
  16. 3. Comments
  17. */
  18. func Start(ctx context.Context, mgmChan chan *manager.ManagerAction, apiServerAddr string) {
  19. log.Println("Starting Proxy...")
  20. common.IsHostNetwork = (os.Getenv("HOST_NETWORK") == "" || os.Getenv("HOST_NETWORK") == "on")
  21. hInfo := stun.GetHostInfo(apiServerAddr)
  22. stun.Host = hInfo
  23. log.Printf("HOSTINFO: %+v", hInfo)
  24. if IsPublicIP(hInfo.PrivIp) {
  25. log.Println("Host is public facing!!!")
  26. }
  27. // start the netclient proxy server
  28. err := server.NmProxyServer.CreateProxyServer(0, 0, hInfo.PrivIp.String())
  29. if err != nil {
  30. log.Fatal("failed to create proxy: ", err)
  31. }
  32. go manager.StartProxyManager(mgmChan)
  33. server.NmProxyServer.Listen(ctx)
  34. }
  35. // IsPublicIP indicates whether IP is public or not.
  36. func IsPublicIP(ip net.IP) bool {
  37. if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() || ip.IsPrivate() {
  38. return false
  39. }
  40. return true
  41. }