diff --git a/cmd/wg-mesh/main.go b/cmd/wg-mesh/main.go index 86b6a86..498d5c0 100644 --- a/cmd/wg-mesh/main.go +++ b/cmd/wg-mesh/main.go @@ -237,7 +237,6 @@ func main() { newMeshCmd := parser.NewCommand("new-mesh", "Create a new mesh") listMeshCmd := parser.NewCommand("list-meshes", "List meshes the node is connected to") joinMeshCmd := parser.NewCommand("join-mesh", "Join a mesh network") - // getMeshCmd := parser.NewCommand("get-mesh", "Get a mesh network") enableInterfaceCmd := parser.NewCommand("enable-interface", "Enable A Specific Mesh Interface") getGraphCmd := parser.NewCommand("get-graph", "Convert a mesh into DOT format") leaveMeshCmd := parser.NewCommand("leave-mesh", "Leave a mesh network") diff --git a/pkg/automerge/factory.go b/pkg/automerge/factory.go index b6861f8..e23520c 100644 --- a/pkg/automerge/factory.go +++ b/pkg/automerge/factory.go @@ -51,7 +51,13 @@ func (f *MeshNodeFactory) getAddress(params *mesh.MeshNodeFactoryParams) string } else if len(f.Config.Endpoint) != 0 { hostName = f.Config.Endpoint } else { - hostName = lib.GetOutboundIP().String() + ip, err := lib.GetPublicIP() + + if err != nil { + return "" + } + + hostName = ip.String() } return hostName diff --git a/pkg/lib/ip.go b/pkg/lib/ip.go index ef1524e..0bd3740 100644 --- a/pkg/lib/ip.go +++ b/pkg/lib/ip.go @@ -1,8 +1,11 @@ package lib import ( + "encoding/json" + "io" "log" "net" + "net/http" ) // GetOutboundIP: gets the oubound IP of this packet @@ -15,3 +18,44 @@ func GetOutboundIP() net.IP { localAddr := conn.LocalAddr().(*net.UDPAddr) return localAddr.IP } + +const IP_SERVICE = "https://api.ipify.org?format=json" + +type IpResponse struct { + Ip string `json:"ip"` +} + +func (i *IpResponse) GetIP() net.IP { + return net.ParseIP(i.Ip) +} + +// GetPublicIP: get the nodes public IP address. For when a node is behind NAT +func GetPublicIP() (net.IP, error) { + req, err := http.NewRequest(http.MethodGet, IP_SERVICE, nil) + + if err != nil { + return nil, err + } + + res, err := http.DefaultClient.Do(req) + + if err != nil { + return nil, err + } + + resBody, err := io.ReadAll(res.Body) + + if err != nil { + return nil, err + } + + var jsonResponse IpResponse + + err = json.Unmarshal([]byte(resBody), &jsonResponse) + + if err != nil { + return nil, err + } + + return jsonResponse.GetIP(), nil +} diff --git a/pkg/mesh/manager.go b/pkg/mesh/manager.go index 1fb9edf..70b1e05 100644 --- a/pkg/mesh/manager.go +++ b/pkg/mesh/manager.go @@ -339,6 +339,7 @@ func (s *MeshManagerImpl) GetSelf(meshId string) (MeshNode, error) { return nil, fmt.Errorf("mesh %s does not exist", meshId) } + logging.Log.WriteInfof(s.HostParameters.HostEndpoint) node, err := meshInstance.GetNode(s.HostParameters.HostEndpoint) if err != nil { @@ -455,7 +456,8 @@ func NewMeshManager(params *NewMeshManagerParams) MeshManager { switch params.Conf.Endpoint { case "": - hostParams.HostEndpoint = fmt.Sprintf("%s:%s", lib.GetOutboundIP().String(), params.Conf.GrpcPort) + ip, _ := lib.GetPublicIP() + hostParams.HostEndpoint = fmt.Sprintf("%s:%s", ip.String(), params.Conf.GrpcPort) default: hostParams.HostEndpoint = fmt.Sprintf("%s:%s", params.Conf.Endpoint, params.Conf.GrpcPort) }