diff --git a/.DS_Store b/.DS_Store index 4ed94e3..6a4aff2 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Bindings/Client.go b/Bindings/Client.go index 13d9f30..f584882 100644 --- a/Bindings/Client.go +++ b/Bindings/Client.go @@ -7,8 +7,10 @@ import ( "github.com/Akilan1999/p2p-rendering-computation/client" "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" "github.com/Akilan1999/p2p-rendering-computation/p2p" + "github.com/Akilan1999/p2p-rendering-computation/p2p/frp" "github.com/Akilan1999/p2p-rendering-computation/plugin" "github.com/Akilan1999/p2p-rendering-computation/server" + "time" ) // The Client package where data-types @@ -19,8 +21,8 @@ import ( // --------------------------------- Container Control ---------------------------------------- //export StartContainer -func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string, baseImage string) (output *C.char) { - container, err := client.StartContainer(IP, NumPorts, GPU, ContainerName, baseImage) +func StartContainer(IP string) (output *C.char) { + container, err := client.StartContainer(IP, 0, false, "", "") if err != nil { return C.CString(err.Error()) } @@ -115,6 +117,34 @@ func UpdateIPTable() (output *C.char) { return C.CString("Success") } +//export EscapeFirewall +func EscapeFirewall(HostOutsideNATIP string, HostOutsideNATPort string, internalPort string) (output *C.char) { + // Get free port from P2PRC server node + serverPort, err := frp.GetFRPServerPort("http://" + HostOutsideNATIP + ":" + HostOutsideNATPort) + + if err != nil { + return C.CString(err.Error()) + } + + time.Sleep(5 * time.Second) + + ExposedPort, err := frp.StartFRPClientForServer(HostOutsideNATIP+":"+HostOutsideNATPort, serverPort, internalPort, "") + if err != nil { + return C.CString(err.Error()) + } + + return C.CString(ExposedPort) +} + +//export MapPort +func MapPort(Port string) *C.char { + port, err := abstractions.MapPort(Port) + if err != nil { + return C.CString(err.Error()) + } + return C.CString(port) +} + // --------------------------------- Controlling Server ---------------------------------------- //export Server diff --git a/Bindings/python/.DS_Store b/Bindings/python/.DS_Store new file mode 100644 index 0000000..d364a57 Binary files /dev/null and b/Bindings/python/.DS_Store differ diff --git a/abstractions/base.go b/abstractions/base.go new file mode 100644 index 0000000..3414a37 --- /dev/null +++ b/abstractions/base.go @@ -0,0 +1,32 @@ +package abstractions + +import ( + "github.com/Akilan1999/p2p-rendering-computation/config" + "github.com/Akilan1999/p2p-rendering-computation/config/generate" + "github.com/Akilan1999/p2p-rendering-computation/server" + "github.com/gin-gonic/gin" +) + +// Init Initialises p2prc +func Init(customConfig interface{}) (config *config.Config, err error) { + // set the config file with default paths + config, err = generate.SetDefaults("P2PRC", false, customConfig, false) + if err != nil { + return + } + return +} + +// Start p2prc in a server mode +func Start() (*gin.Engine, error) { + engine, err := server.Server() + if err != nil { + return nil, err + } + return engine, nil +} + +func MapPort(port string) (mapPort string, err error) { + mapPort, err = server.MapPort(port) + return +} diff --git a/abstractions/init.go b/abstractions/init.go deleted file mode 100644 index 81ac369..0000000 --- a/abstractions/init.go +++ /dev/null @@ -1,27 +0,0 @@ -package abstractions - -import ( - "github.com/Akilan1999/p2p-rendering-computation/config" - "github.com/Akilan1999/p2p-rendering-computation/config/generate" - "github.com/Akilan1999/p2p-rendering-computation/server" - "github.com/gin-gonic/gin" -) - -// Init Initialises p2prc -func Init(customConfig interface{}) (config *config.Config, err error) { - // set the config file with default paths - config, err = generate.SetDefaults("P2PRC", false, customConfig, false) - if err != nil { - return - } - return -} - -// Start p2prc in a server mode -func Start() (*gin.Engine, error) { - engine, err := server.Server() - if err != nil { - return nil, err - } - return engine, nil -} diff --git a/artwork/.DS_Store b/artwork/.DS_Store new file mode 100644 index 0000000..5b80bbd Binary files /dev/null and b/artwork/.DS_Store differ diff --git a/server/server.go b/server/server.go index 97ffd6e..3c752d5 100644 --- a/server/server.go +++ b/server/server.go @@ -239,3 +239,60 @@ func Server() (*gin.Engine, error) { return r, nil } + +func MapPort(port string) (string, error) { + //Get Server port based on the config file + config, err := config.ConfigInit(nil, nil) + if err != nil { + return "", err + } + + // update IPTable with new port and ip address and update ip table + var ProxyIpAddr p2p.IpAddress + var lowestLatencyIpAddress p2p.IpAddress + + clientIPTable.RemoveOfflineNodes() + + table, err := p2p.ReadIpTable() + if err != nil { + return "", err + } + + var lowestLatency int64 + // random large number + lowestLatency = 10000000 + + for i, _ := range table.IpAddress { + // Checks if the ping is the lowest and if the following node is acting as a proxy + //if table.IpAddress[i].Latency.Milliseconds() < lowestLatency && table.IpAddress[i].ProxyPort != "" { + if table.IpAddress[i].Latency.Milliseconds() < lowestLatency && table.IpAddress[i].NAT != "" { + lowestLatency = table.IpAddress[i].Latency.Milliseconds() + lowestLatencyIpAddress = table.IpAddress[i] + } + } + + // If there is an identified node + if lowestLatency != 10000000 { + serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort) + if err != nil { + return "", err + } + // Create 3 second delay to allow FRP server to start + time.Sleep(1 * time.Second) + // Starts FRP as a client with + proxyPort, err := frp.StartFRPClientForServer(lowestLatencyIpAddress.Ipv4, serverPort, port, "") + if err != nil { + return "", err + } + + // updating with the current proxy address + ProxyIpAddr.Ipv4 = lowestLatencyIpAddress.Ipv4 + ProxyIpAddr.ServerPort = proxyPort + ProxyIpAddr.Name = config.MachineName + ProxyIpAddr.NAT = "False" + ProxyIpAddr.EscapeImplementation = "FRP" + //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) + } + + return ProxyIpAddr.Ipv4 + ":" + ProxyIpAddr.ServerPort, nil +}