diff --git a/Docs/Installation.md b/Docs/Installation.md index a0e568f..795ef64 100644 --- a/Docs/Installation.md +++ b/Docs/Installation.md @@ -208,6 +208,11 @@ p2prc --pp p2prc --rp ``` +### Added custom metadata about the current node +``` +p2prc --amd "custom metadata" +``` +
-------------- diff --git a/client/clientIPTable/AddCustomInformationToIPTable.go b/client/clientIPTable/AddCustomInformationToIPTable.go new file mode 100644 index 0000000..56763b3 --- /dev/null +++ b/client/clientIPTable/AddCustomInformationToIPTable.go @@ -0,0 +1,40 @@ +package clientIPTable + +import ( + "errors" + "github.com/Akilan1999/p2p-rendering-computation/config" + "github.com/Akilan1999/p2p-rendering-computation/p2p" +) + +func AddCustomInformationToIPTable(text string) error { + // Get config information + Config, err := config.ConfigInit(nil, nil) + if err != nil { + return err + } + + // Get IPTable information + table, err := p2p.ReadIpTable() + if err != nil { + return err + } + + found := false + + for i, _ := range table.IpAddress { + if table.IpAddress[i].Name == Config.MachineName { + table.IpAddress[i].CustomInformation = text + found = true + } + } + + if found { + table.WriteIpTable() + // update IPTable after modified entry + UpdateIpTableListClient() + } else { + return errors.New("start server with p2prc -s as the server is currently not running") + } + + return nil +} diff --git a/cmd/action.go b/cmd/action.go index 5af1566..c66b7ca 100644 --- a/cmd/action.go +++ b/cmd/action.go @@ -273,5 +273,14 @@ var CliAction = func(ctx *cli.Context) error { } } + if AddMetaData != "" { + err := clientIPTable.AddCustomInformationToIPTable(AddMetaData) + if err != nil { + fmt.Println(err) + } else { + fmt.Println("Success") + } + } + return nil } diff --git a/cmd/flags.go b/cmd/flags.go index b031c4b..4a6a16f 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -37,6 +37,7 @@ var ( //-------------------------------- PullPlugin string RemovePlugin string + AddMetaData string ) var AppConfigFlags = []cli.Flag{ @@ -241,4 +242,11 @@ var AppConfigFlags = []cli.Flag{ EnvVars: []string{"REMOVEPLUGIN"}, Destination: &RemovePlugin, }, + &cli.StringFlag{ + Name: "AddMetaData", + Aliases: []string{"amd"}, + Usage: "Adds metadata about the current node in the p2p network which is then propagated through the network", + EnvVars: []string{"ADDMETADATA"}, + Destination: &AddMetaData, + }, } diff --git a/config/config.go b/config/config.go index dc1749c..374d963 100644 --- a/config/config.go +++ b/config/config.go @@ -30,6 +30,7 @@ type Config struct { GroupTrackContainersPath string FRPServerPort string BehindNAT string + IPTableKey string CustomConfig interface{} //NetworkInterface string //NetworkInterfaceIPV6Index int diff --git a/config/generate/generate.go b/config/generate/generate.go index 06a6131..571618f 100644 --- a/config/generate/generate.go +++ b/config/generate/generate.go @@ -2,7 +2,9 @@ package generate import ( "github.com/Akilan1999/p2p-rendering-computation/config" + "math/rand" "os" + "time" ) var ( @@ -85,13 +87,15 @@ func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, No Defaults.CustomConfig = CustomConfig Defaults.BehindNAT = "True" Defaults.DockerRunLogs = "/tmp/" + // Generate a random key to be added to IPTable + Defaults.IPTableKey = String(12) // Random name generator hostname, err := os.Hostname() if err != nil { return nil, err } - Defaults.MachineName = hostname + Defaults.MachineName = hostname + "-" + String(7) } else { Defaults = *ConfigUpdate[0] } @@ -130,3 +134,22 @@ func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, No return Config, nil } + +// Generating a random string +const charset = "abcdefghijklmnopqrstuvwxyz" + + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + +var seededRand *rand.Rand = rand.New( + rand.NewSource(time.Now().UnixNano())) + +func StringWithCharset(length int, charset string) string { + b := make([]byte, length) + for i := range b { + b[i] = charset[seededRand.Intn(len(charset))] + } + return string(b) +} + +func String(length int) string { + return StringWithCharset(length, charset) +} diff --git a/p2p/iptable.go b/p2p/iptable.go index a284cde..e185e14 100644 --- a/p2p/iptable.go +++ b/p2p/iptable.go @@ -1,6 +1,8 @@ package p2p import ( + "bytes" + "crypto/sha256" "encoding/json" "fmt" "github.com/Akilan1999/p2p-rendering-computation/config" @@ -27,7 +29,8 @@ type IpAddress struct { ServerPort string `json:"ServerPort"` NAT string `json:"NAT"` EscapeImplementation string `json:"EscapeImplementation"` - CustomInformation []byte + CustomInformation string `json:"CustomInformation"` + //CustomInformationKey []byte `json:"CustomInformationKey"` } type IP struct { @@ -265,3 +268,26 @@ func PrettyPrint(data interface{}) { } fmt.Printf("%s \n", p) } + +func GenerateHashSHA256(text string) []byte { + h := sha256.New() + h.Write([]byte(text)) + + return h.Sum(nil) +} + +// ValidateHashSHA256 CustomInformationKey the text and check if the text and +// the hash are the same, if they are +// then return true +// SHA256 is the current hashing algorthm +// used. +func ValidateHashSHA256(text string, Hash []byte) bool { + h := sha256.New() + h.Write([]byte(text)) + + textHash := h.Sum(nil) + if bytes.Equal(textHash, Hash) { + return true + } + return false +} diff --git a/server/server.go b/server/server.go index 0dce4f2..97ffd6e 100644 --- a/server/server.go +++ b/server/server.go @@ -212,6 +212,7 @@ func Server() (*gin.Engine, error) { ProxyIpAddr.Name = config.MachineName ProxyIpAddr.NAT = "False" ProxyIpAddr.EscapeImplementation = "FRP" + //ProxyIpAddr.CustomInformationKey = p2p.GenerateHashSHA256(config.IPTableKey) // append the following to the ip table table.IpAddress = append(table.IpAddress, ProxyIpAddr)