added python function call add custom information

This commit is contained in:
2025-04-24 23:15:02 +01:00
parent c445ac88c4
commit a6939ec35e
4 changed files with 137 additions and 108 deletions

View File

@@ -2,11 +2,11 @@ package main
import "C"
import (
"encoding/json"
"time"
"encoding/json"
"time"
"github.com/Akilan1999/p2p-rendering-computation/abstractions"
"github.com/Akilan1999/p2p-rendering-computation/p2p/frp"
"github.com/Akilan1999/p2p-rendering-computation/abstractions"
"github.com/Akilan1999/p2p-rendering-computation/p2p/frp"
)
// The Client package where data-types
@@ -18,20 +18,20 @@ import (
//export StartContainer
func StartContainer(IP string) (output *C.char) {
container, err := abstractions.StartContainer(IP)
if err != nil {
return C.CString(err.Error())
}
return ConvertStructToJSONString(container)
container, err := abstractions.StartContainer(IP)
if err != nil {
return C.CString(err.Error())
}
return ConvertStructToJSONString(container)
}
//export RemoveContainer
func RemoveContainer(IP string, ID string) (output *C.char) {
err := abstractions.RemoveContainer(IP, ID)
if err != nil {
return C.CString(err.Error())
}
return C.CString("Success")
err := abstractions.RemoveContainer(IP, ID)
if err != nil {
return C.CString(err.Error())
}
return C.CString("Success")
}
// --------------------------------- Plugin Control ----------------------------------------
@@ -77,20 +77,20 @@ func RemoveContainer(IP string, ID string) (output *C.char) {
//export GetSpecs
func GetSpecs(IP string) (output *C.char) {
specs, err := abstractions.GetSpecs(IP)
if err != nil {
return C.CString(err.Error())
}
return ConvertStructToJSONString(specs)
specs, err := abstractions.GetSpecs(IP)
if err != nil {
return C.CString(err.Error())
}
return ConvertStructToJSONString(specs)
}
//export Init
func Init(customConfig string) (output *C.char) {
init, err := abstractions.Init(customConfig)
if err != nil {
return C.CString(err.Error())
}
return ConvertStructToJSONString(init)
init, err := abstractions.Init(customConfig)
if err != nil {
return C.CString(err.Error())
}
return ConvertStructToJSONString(init)
}
@@ -98,72 +98,81 @@ func Init(customConfig string) (output *C.char) {
//export ViewIPTable
func ViewIPTable() (output *C.char) {
table, err := abstractions.ViewIPTable()
if err != nil {
return C.CString(err.Error())
}
return ConvertStructToJSONString(table)
table, err := abstractions.ViewIPTable()
if err != nil {
return C.CString(err.Error())
}
return ConvertStructToJSONString(table)
}
//export UpdateIPTable
func UpdateIPTable() (output *C.char) {
err := abstractions.UpdateIPTable()
if err != nil {
return C.CString(err.Error())
}
return C.CString("Success")
err := abstractions.UpdateIPTable()
if err != nil {
return C.CString(err.Error())
}
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)
// Get free port from P2PRC server node
serverPort, err := frp.GetFRPServerPort("http://" + HostOutsideNATIP + ":" + HostOutsideNATPort)
if err != nil {
return C.CString(err.Error())
}
if err != nil {
return C.CString(err.Error())
}
time.Sleep(5 * time.Second)
time.Sleep(5 * time.Second)
ExposedPort, err := frp.StartFRPClientForServer(HostOutsideNATIP+":"+HostOutsideNATPort, serverPort, internalPort, "")
if err != nil {
return C.CString(err.Error())
}
ExposedPort, err := frp.StartFRPClientForServer(HostOutsideNATIP+":"+HostOutsideNATPort, serverPort, internalPort, "")
if err != nil {
return C.CString(err.Error())
}
return C.CString(ExposedPort)
return C.CString(ExposedPort)
}
//export MapPort
func MapPort(Port string, DomainName string, ServerAddress string) *C.char {
Address, err := abstractions.MapPort(Port, DomainName, ServerAddress)
if err != nil {
return C.CString(err.Error())
}
return C.CString(Address.EntireAddress)
Address, err := abstractions.MapPort(Port, DomainName, ServerAddress)
if err != nil {
return C.CString(err.Error())
}
return C.CString(Address.EntireAddress)
}
//export CustomInformation
func CustomInformation(CustomInformation string) *C.char {
err := abstractions.AddCustomInformation(CustomInformation)
if err != nil {
return C.CString(err.Error())
}
return C.CString("Success")
}
// --------------------------------- Controlling Server ----------------------------------------
//export Server
func Server() (output *C.char) {
_, err := abstractions.Start()
if err != nil {
return C.CString(err.Error())
}
_, err := abstractions.Start()
if err != nil {
return C.CString(err.Error())
}
return ConvertStructToJSONString("")
return ConvertStructToJSONString("")
}
// --------------------------------- Helper Functions ----------------------------------------
func ConvertStructToJSONString(Struct interface{}) *C.char {
jsonBytes, err := json.Marshal(Struct)
if err != nil {
return C.CString(err.Error())
}
jsonBytes, err := json.Marshal(Struct)
if err != nil {
return C.CString(err.Error())
}
// Convert the JSON bytes to a string
return C.CString(string(jsonBytes))
// Convert the JSON bytes to a string
return C.CString(string(jsonBytes))
}
func main() {}

View File

@@ -97,6 +97,18 @@ def ListNodes():
ipTableObject = json.loads((str(ipTable).strip("b'")))
dat: IPAddress = dacite.from_dict(IPAddress,ipTableObject)
return dat
# python function to pass-through custom
# information to interpret which can be
# interpreted as a DSL.
def AddCustomInformation(message="") -> bool:
message = go_string(c_char_p(message.encode('utf-8')), len(message))
p2prc.CustomInformation.restype = c_char_p
status = p2prc.CustomInformation(message)
if str(status).strip("b'") == "Success":
return True
return False

View File

@@ -5,3 +5,7 @@ if __name__ == "__main__":
P2PRCNodes = ListNodes()
# Print nodes in the network
print(P2PRCNodes)
# Add custom information to the network
if AddCustomInformation("Test"):
print("It worked")

View File

@@ -2,87 +2,91 @@ package abstractions
import "C"
import (
"github.com/Akilan1999/p2p-rendering-computation/client"
"github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable"
Config "github.com/Akilan1999/p2p-rendering-computation/config"
"github.com/Akilan1999/p2p-rendering-computation/config/generate"
"github.com/Akilan1999/p2p-rendering-computation/p2p"
"github.com/Akilan1999/p2p-rendering-computation/server"
"github.com/Akilan1999/p2p-rendering-computation/server/docker"
"github.com/gin-gonic/gin"
"os"
"github.com/Akilan1999/p2p-rendering-computation/client"
"github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable"
Config "github.com/Akilan1999/p2p-rendering-computation/config"
"github.com/Akilan1999/p2p-rendering-computation/config/generate"
"github.com/Akilan1999/p2p-rendering-computation/p2p"
"github.com/Akilan1999/p2p-rendering-computation/server"
"github.com/Akilan1999/p2p-rendering-computation/server/docker"
"github.com/gin-gonic/gin"
"os"
)
// Init Initialises p2prc
func Init(customConfig interface{}) (config *Config.Config, err error) {
// Get config file path
// Checks P2PRC path initially
// - Get PATH if environment varaible
path, err := Config.GetPathP2PRC("P2PRC")
if err != nil {
return
}
// check if the config file exists
if _, err = os.Stat(path + "config.json"); err != nil {
// Initialize with base p2prc config files
// set the config file with default paths
config, err = generate.SetDefaults("P2PRC", false, customConfig, false)
if err != nil {
return
}
} else {
// If the configs are available then use them over generating new ones.
config, err = Config.ConfigInit(nil, nil)
if err != nil {
return
}
}
// Get config file path
// Checks P2PRC path initially
// - Get PATH if environment varaible
path, err := Config.GetPathP2PRC("P2PRC")
if err != nil {
return
}
// check if the config file exists
if _, err = os.Stat(path + "config.json"); err != nil {
// Initialize with base p2prc config files
// set the config file with default paths
config, err = generate.SetDefaults("P2PRC", false, customConfig, false)
if err != nil {
return
}
} else {
// If the configs are available then use them over generating new ones.
config, err = Config.ConfigInit(nil, nil)
if err != nil {
return
}
}
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
engine, err := server.Server()
if err != nil {
return nil, err
}
return engine, nil
}
// MapPort Creates a reverse proxy connection and maps the appropriate port
func MapPort(port string, domainName string, serverAddress string) (response *client.ResponseMAPPort, err error) {
response, err = client.MAPPort(port, domainName, serverAddress)
return
response, err = client.MAPPort(port, domainName, serverAddress)
return
}
// StartContainer Starts docker container on the remote machine
func StartContainer(IP string) (container *docker.DockerVM, err error) {
container, err = client.StartContainer(IP, 0, false, "", "")
return
container, err = client.StartContainer(IP, 0, false, "", "")
return
}
// RemoveContainer Removes docker container based on the IP address and ID
// provided
func RemoveContainer(IP string, ID string) error {
return client.RemoveContianer(IP, ID)
return client.RemoveContianer(IP, ID)
}
// GetSpecs Get spec information about the remote server
func GetSpecs(IP string) (specs *server.SysInfo, err error) {
specs, err = client.GetSpecs(IP)
return
specs, err = client.GetSpecs(IP)
return
}
// ViewIPTable View information of nodes in the network
func ViewIPTable() (table *p2p.IpAddresses, err error) {
table, err = p2p.ReadIpTable()
return
table, err = p2p.ReadIpTable()
return
}
// UpdateIPTable Force updates IP tables based on new
// new nodes discovered in the network
func UpdateIPTable() (err error) {
return clientIPTable.UpdateIpTableListClient()
return clientIPTable.UpdateIpTableListClient()
}
func AddCustomInformation(information string) error {
return clientIPTable.AddCustomInformationToIPTable(information)
}