added flag to add custom metadata

This commit is contained in:
2023-09-27 17:27:52 +01:00
parent 7ed25ef4b3
commit 96e755bc19
8 changed files with 115 additions and 2 deletions

View File

@@ -208,6 +208,11 @@ p2prc --pp <repo link>
p2prc --rp <plugin name>
```
### Added custom metadata about the current node
```
p2prc --amd "custom metadata"
```
<br>
--------------

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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,
},
}

View File

@@ -30,6 +30,7 @@ type Config struct {
GroupTrackContainersPath string
FRPServerPort string
BehindNAT string
IPTableKey string
CustomConfig interface{}
//NetworkInterface string
//NetworkInterfaceIPV6Index int

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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)