Skip to content

Commit

Permalink
Merge pull request #115 from Akilan1999/setup-baremetal
Browse files Browse the repository at this point in the history
Setup baremetal
  • Loading branch information
Akilan1999 authored Nov 14, 2024
2 parents ff105bf + 277259a commit 08d14b4
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ server/docker/containers/
export/

# Any testing file
test*
*test

# Ignore public and private keys
p2prc.publicKey
Expand Down
18 changes: 0 additions & 18 deletions cert.pem

This file was deleted.

2 changes: 1 addition & 1 deletion client/clientIPTable/AddCustomInformationToIPTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func AddCustomInformationToIPTable(text string) error {
if found {
table.WriteIpTable()
// update IPTable after modified entry
UpdateIpTableListClient()
go UpdateIpTableListClient()
} else {
return errors.New("start server with p2prc -s as the server is currently not running")
}
Expand Down
9 changes: 5 additions & 4 deletions client/clientIPTable/Iptable.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ func UpdateIpTable(IpAddress string, serverPort string, wg *sync.WaitGroup) erro
}
}

err = ipStruct.WriteIpTable()
if err != nil {
return err
}
// Not required to update IP table as speed test updates the IP Table
//err = ipStruct.WriteIpTable()
//if err != nil {
// return err
//}

wg.Done()

Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type Config struct {
PrivateKeyFile string
PemFile string
KeyFile string
BareMetal string
BareMetal bool
UnsafeMode bool
CustomConfig interface{}
//NetworkInterface string
//NetworkInterfaceIPV6Index int
Expand Down
5 changes: 3 additions & 2 deletions config/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, No
}

// Generate Public and private keys and set path
Defaults.PublicKeyFile = defaultPath + "p2prc.PublicKeyBareMetal"
Defaults.PublicKeyFile = defaultPath + "p2prc.publicKey"
Defaults.PrivateKeyFile = defaultPath + "p2prc.privateKey"
Defaults.BareMetal = "False"
Defaults.BareMetal = false
Defaults.UnsafeMode = false

// Generate certificate files for SSL
err = GenerateCertificate()
Expand Down
28 changes: 0 additions & 28 deletions key.pem

This file was deleted.

52 changes: 37 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"log"
"os"
"log"
"os"
"os/signal"
"syscall"

"github.com/Akilan1999/p2p-rendering-computation/cmd"
"github.com/urfave/cli/v2"
"github.com/Akilan1999/p2p-rendering-computation/cmd"
"github.com/urfave/cli/v2"
)

// VERSION specifies the version of the platform
Expand All @@ -16,16 +18,36 @@ var mode string
var OS, Pull_location, Run_script string
var List_servers, Ip_table bool

// To be implemented later on
func getFireSignalsChannel() chan os.Signal {

c := make(chan os.Signal, 1)
signal.Notify(c,
// https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
syscall.SIGTERM, // "the normal way to politely ask a program to terminate"
syscall.SIGINT, // Ctrl+C
syscall.SIGQUIT, // Ctrl-\
syscall.SIGKILL, // "always fatal", "SIGKILL and SIGSTOP may not be caught by a program"
syscall.SIGHUP, // "terminal is disconnected"
)
return c

}

func exit() {
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
}

func main() {
app := cli.NewApp()
app.Name = "p2p-rendering-computation"
app.Usage = "p2p cli application to create and access VMs in other servers"
app.Version = VERSION
app.Flags = cmd.AppConfigFlags
app.Action = cmd.CliAction

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
app := cli.NewApp()
app.Name = "p2p-rendering-computation"
app.Usage = "p2p cli application to create and access VMs in other servers"
app.Version = VERSION
app.Flags = cmd.AppConfigFlags
app.Action = cmd.CliAction

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
3 changes: 3 additions & 0 deletions p2p/iptable.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type IpAddresses struct {

type IpAddress struct {
Name string `json:"Name"`
MachineUsername string `json:"MachineUsername"`
Ipv4 string `json:"IPV4"`
Ipv6 string `json:"IPV6"`
Latency time.Duration `json:"Latency"`
Expand All @@ -31,6 +32,8 @@ type IpAddress struct {
NAT string `json:"NAT"`
EscapeImplementation string `json:"EscapeImplementation"`
ProxyServer string `json:"ProxyServer"`
UnSafeMode bool `json:"UnSafeMode"`
PublicKey string `json:"PublicKey"`
CustomInformation string `json:"CustomInformation"`
//CustomInformationKey []byte `json:"CustomInformationKey"`
}
Expand Down
29 changes: 29 additions & 0 deletions p2p/speedtest.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package p2p

import (
"github.com/Akilan1999/p2p-rendering-computation/config"
)

// SpeedTest Runs a speed test and does updates IP tables accordingly
func (ip *IpAddresses) SpeedTest() error {

Expand Down Expand Up @@ -50,11 +54,26 @@ func (ip *IpAddresses) SpeedTest() error {

// SpeedTestUpdatedIPTable Called when ip tables from httpclient/server is also passed on
func (ip *IpAddresses) SpeedTestUpdatedIPTable() error {

Config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}

targets, err := ReadIpTable()
if err != nil {
return err
}

// Checks if baremetal mode and unsafe mode
// is enabled. If it is enabled it adds the
// the propagated public key to the list.

AddPublicKey := false
if Config.BareMetal && Config.UnsafeMode {
AddPublicKey = true
}

// To ensure struct has no duplicates IP addresses
//DoNotRead := targets

Expand All @@ -64,6 +83,16 @@ func (ip *IpAddresses) SpeedTestUpdatedIPTable() error {
//To ensure that there are no duplicate IP addresses
Exists := false
for k := range ip.IpAddress {
if AddPublicKey && ip.IpAddress[k].PublicKey != "" {
// This function call (AddAuthorisationKey) is inefficient but to be optimised later on.
// This is because when if the user is running on as unsafe mode the authorization file
// is opened from the SSH directory and then iterates through every single SSH entry
// to find out if the SSH entry exists or not. This will incur multiple CPU cycles
// for no reason. A better approach would be to have been to store the states on memory and only
// add when needed based on the memory location. This is something is to be discussed
// and look upon later on.
AddAuthorisationKey(ip.IpAddress[k].PublicKey)
}
// Checks if both the IPV4 addresses are the same or the IPV6 address is not
// an empty string and IPV6 address are the same
if (ip.IpAddress[k].Ipv4 == targets.IpAddress[i].Ipv4 && targets.IpAddress[i].NAT == "True") || (targets.IpAddress[i].Ipv6 != "" && ip.IpAddress[k].Ipv6 == targets.IpAddress[i].Ipv6) {
Expand Down
123 changes: 123 additions & 0 deletions p2p/ssh_autorisation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// NOTE: Most of the code snippet was generated using ChatGPT
// Prompt used: "generate go program to read and populate ssh authorization file"

package p2p

import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)

// GetAuthorizedKeysPath returns the path to the authorized_keys file
func GetAuthorizedKeysPath() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("could not find home directory: %v", err)
}
return filepath.Join(homeDir, ".ssh", "authorized_keys"), nil
}

// ReadAuthorizedKeys reads and returns the current contents of the authorized_keys file as a map
func ReadAuthorizedKeys(path string) (map[string]bool, error) {
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("could not open authorized_keys file: %v", err)
}
defer file.Close()

keys := make(map[string]bool)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// Skip empty lines and comments
if line != "" && !strings.HasPrefix(line, "#") {
keys[line] = true
}
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading authorized_keys file: %v", err)
}
return keys, nil
}

// AddKeyToAuthorizedKeys adds a new key to the authorized_keys file if it doesn’t already exist
func AddKeyToAuthorizedKeys(path, newKey string) error {
keys, err := ReadAuthorizedKeys(path)
if err != nil {
return err
}

// Check if the key already exists in the map
if keys[newKey] {
return errors.New("key already exists in authorized_keys")
}

// Append the new key
file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return fmt.Errorf("could not open authorized_keys file for writing: %v", err)
}
defer file.Close()

if _, err := file.WriteString(newKey + "\n"); err != nil {
return fmt.Errorf("could not write to authorized_keys file: %v", err)
}
return nil
}

func RemoveKeyFromAuthorizedKeys(path, keyToRemove string) error {
keys, err := ReadAuthorizedKeys(path)
if err != nil {
return err
}

// Check if the key exists in the map
if !keys[keyToRemove] {
return errors.New("key not found in authorized_keys")
}

// Delete the key from the map
delete(keys, keyToRemove)

// Write updated keys back to the authorized_keys file
file, err := os.OpenFile(path, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return fmt.Errorf("could not open authorized_keys file for writing: %v", err)
}
defer file.Close()

for key := range keys {
if _, err := file.WriteString(key + "\n"); err != nil {
return fmt.Errorf("could not write to authorized_keys file: %v", err)
}
}

return nil
}

// AddAuthorisationKey Adds public key provided to the
// authorization file so that nodes can SSH into
// the
func AddAuthorisationKey(PublicKey string) error {
path, err := GetAuthorizedKeysPath()
if err != nil {
return err
}

// Display existing keys
_, err = ReadAuthorizedKeys(path)
if err != nil {
return err
}

err = AddKeyToAuthorizedKeys(path, PublicKey)
if err != nil {
return err
}

return nil
}
Loading

0 comments on commit 08d14b4

Please sign in to comment.