netmaker/nm-proxy/nm-proxy.go

48 lines
1.2 KiB
Go
Raw Normal View History

2022-10-27 19:50:06 +08:00
package nmproxy
import (
2022-11-07 23:55:58 +08:00
"context"
"log"
"net"
"os"
"github.com/gravitl/netmaker/nm-proxy/common"
"github.com/gravitl/netmaker/nm-proxy/manager"
2022-10-27 19:50:06 +08:00
"github.com/gravitl/netmaker/nm-proxy/server"
"github.com/gravitl/netmaker/nm-proxy/stun"
)
// Comm Channel to configure proxy
/* Actions -
1. Add - new interface and its peers
2. Delete - remove close all conns for the interface,cleanup
*/
2022-11-07 23:55:58 +08:00
func Start(ctx context.Context, mgmChan chan *manager.ManagerAction, apiServerAddr string) {
log.Println("Starting Proxy...")
common.IsHostNetwork = (os.Getenv("HOST_NETWORK") == "" || os.Getenv("HOST_NETWORK") == "on")
2022-11-14 16:40:52 +08:00
2022-11-07 23:55:58 +08:00
hInfo := stun.GetHostInfo(apiServerAddr)
stun.Host = hInfo
log.Printf("HOSTINFO: %+v", hInfo)
2022-10-31 12:43:04 +08:00
if IsPublicIP(hInfo.PrivIp) {
log.Println("Host is public facing!!!")
}
// start the netclient proxy server
err := server.NmProxyServer.CreateProxyServer(0, 0, hInfo.PrivIp.String())
if err != nil {
log.Fatal("failed to create proxy: ", err)
}
2022-11-14 16:40:52 +08:00
go manager.StartProxyManager(mgmChan)
2022-11-07 23:55:58 +08:00
server.NmProxyServer.Listen(ctx)
}
// IsPublicIP indicates whether IP is public or not.
func IsPublicIP(ip net.IP) bool {
if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() || ip.IsPrivate() {
return false
}
return true
}