2022-10-27 19:50:06 +08:00
|
|
|
package nmproxy
|
2022-10-27 17:50:29 +08:00
|
|
|
|
|
|
|
import (
|
2022-11-07 23:55:58 +08:00
|
|
|
"context"
|
2022-10-27 17:50:29 +08:00
|
|
|
"log"
|
|
|
|
"net"
|
2022-11-03 15:18:03 +08:00
|
|
|
"os"
|
2022-10-27 17:50:29 +08:00
|
|
|
|
2022-11-03 15:18:03 +08:00
|
|
|
"github.com/gravitl/netmaker/nm-proxy/common"
|
2022-10-28 01:47:51 +08:00
|
|
|
"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"
|
2022-10-27 17:50:29 +08:00
|
|
|
)
|
|
|
|
|
2022-10-28 01:47:51 +08:00
|
|
|
// 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) {
|
2022-10-27 17:50:29 +08:00
|
|
|
log.Println("Starting Proxy...")
|
2022-11-03 15:18:03 +08:00
|
|
|
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)
|
2022-10-27 17:50:29 +08:00
|
|
|
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!!!")
|
|
|
|
}
|
2022-10-27 17:50:29 +08:00
|
|
|
// start the netclient proxy server
|
2022-10-28 01:47:51 +08:00
|
|
|
err := server.NmProxyServer.CreateProxyServer(0, 0, hInfo.PrivIp.String())
|
2022-10-27 17:50:29 +08:00
|
|
|
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)
|
|
|
|
|
2022-10-27 17:50:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|