mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-01-22 13:38:35 +01:00
25-modify-code-to-use-public-api
Modify the code to use a public IP address by default if none is specified
This commit is contained in:
parent
bf0724f6e5
commit
4c54022f63
@ -237,7 +237,6 @@ func main() {
|
|||||||
newMeshCmd := parser.NewCommand("new-mesh", "Create a new mesh")
|
newMeshCmd := parser.NewCommand("new-mesh", "Create a new mesh")
|
||||||
listMeshCmd := parser.NewCommand("list-meshes", "List meshes the node is connected to")
|
listMeshCmd := parser.NewCommand("list-meshes", "List meshes the node is connected to")
|
||||||
joinMeshCmd := parser.NewCommand("join-mesh", "Join a mesh network")
|
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")
|
enableInterfaceCmd := parser.NewCommand("enable-interface", "Enable A Specific Mesh Interface")
|
||||||
getGraphCmd := parser.NewCommand("get-graph", "Convert a mesh into DOT format")
|
getGraphCmd := parser.NewCommand("get-graph", "Convert a mesh into DOT format")
|
||||||
leaveMeshCmd := parser.NewCommand("leave-mesh", "Leave a mesh network")
|
leaveMeshCmd := parser.NewCommand("leave-mesh", "Leave a mesh network")
|
||||||
|
@ -51,7 +51,13 @@ func (f *MeshNodeFactory) getAddress(params *mesh.MeshNodeFactoryParams) string
|
|||||||
} else if len(f.Config.Endpoint) != 0 {
|
} else if len(f.Config.Endpoint) != 0 {
|
||||||
hostName = f.Config.Endpoint
|
hostName = f.Config.Endpoint
|
||||||
} else {
|
} else {
|
||||||
hostName = lib.GetOutboundIP().String()
|
ip, err := lib.GetPublicIP()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
hostName = ip.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
return hostName
|
return hostName
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetOutboundIP: gets the oubound IP of this packet
|
// GetOutboundIP: gets the oubound IP of this packet
|
||||||
@ -15,3 +18,44 @@ func GetOutboundIP() net.IP {
|
|||||||
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||||
return localAddr.IP
|
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
|
||||||
|
}
|
||||||
|
@ -339,6 +339,7 @@ func (s *MeshManagerImpl) GetSelf(meshId string) (MeshNode, error) {
|
|||||||
return nil, fmt.Errorf("mesh %s does not exist", meshId)
|
return nil, fmt.Errorf("mesh %s does not exist", meshId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logging.Log.WriteInfof(s.HostParameters.HostEndpoint)
|
||||||
node, err := meshInstance.GetNode(s.HostParameters.HostEndpoint)
|
node, err := meshInstance.GetNode(s.HostParameters.HostEndpoint)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -455,7 +456,8 @@ func NewMeshManager(params *NewMeshManagerParams) MeshManager {
|
|||||||
|
|
||||||
switch params.Conf.Endpoint {
|
switch params.Conf.Endpoint {
|
||||||
case "":
|
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:
|
default:
|
||||||
hostParams.HostEndpoint = fmt.Sprintf("%s:%s", params.Conf.Endpoint, params.Conf.GrpcPort)
|
hostParams.HostEndpoint = fmt.Sprintf("%s:%s", params.Conf.Endpoint, params.Conf.GrpcPort)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user