Skip to content
This repository was archived by the owner on Feb 24, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions cli/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cli

import (
"fmt"
"os"

"github.com/DataDrake/cli-ng/v2/cmd"
"github.com/hyprspace/hyprspace/daemon"
)

// Daemon brings the daemon up or down
var Daemon = cmd.Sub{
Name: "daemon",
Alias: "b",
Short: "Control the daemon.",
Args: &DaemonArgs{},
Run: DaemonRun,
}

type DaemonArgs struct {
UpDown string
}

// Brings the daemon up or down
func DaemonRun(r *cmd.Root, c *cmd.Sub) {
// Parse Command Args
args := c.Args.(*DaemonArgs)

if args.UpDown == "up" {
fmt.Println("Starting hyprspace daemon on port", daemon.PORT)
out := make(chan error)
go daemon.Run(out)

select {
case err := <-out:
if err == nil {
fmt.Println("Daemon shutting down")
os.Exit(0)
}
fmt.Println(err)
os.Exit(1)
}
// At this point the daemon has shut down
} else if args.UpDown == "down" {
daemon.Shutdown()
}
}
11 changes: 7 additions & 4 deletions cli/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/DataDrake/cli-ng/v2/cmd"
"github.com/hyprspace/hyprspace/tun"
"github.com/hyprspace/hyprspace/daemon"
)

// Down brings down a Hyprspace interface and removes it from the system.
Expand All @@ -26,7 +26,10 @@ func DownRun(r *cmd.Root, c *cmd.Sub) {
// Parse Command Args
args := c.Args.(*DownArgs)

fmt.Println("[+] ip link delete dev " + args.InterfaceName)
err := tun.Delete(args.InterfaceName)
checkErr(err)
err := daemon.DownInterface(args.InterfaceName)
if err != nil && err.Error() != "" {
fmt.Println("Failed to bring down interface:", err)
} else {
fmt.Println("[-] Shutdown hyprspace interface", args.InterfaceName)
}
}
5 changes: 3 additions & 2 deletions cli/init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"context"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -40,7 +39,7 @@ func InitRun(r *cmd.Root, c *cmd.Sub) {
}

// Create New Libp2p Node
host, err := libp2p.New(context.Background())
host, err := libp2p.New()
checkErr(err)

// Get Node's Private Key
Expand All @@ -57,6 +56,8 @@ func InitRun(r *cmd.Root, c *cmd.Sub) {
Name: args.InterfaceName,
ListenPort: 8001,
Address: "10.1.1.1/24",
Transports: []string{"quic", "tcp"},
AutoRelay: false,
ID: host.ID().Pretty(),
PrivateKey: string(keyBytes),
DiscoverKey: strings.Join(list, "-"),
Expand Down
39 changes: 39 additions & 0 deletions cli/peers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cli

import (
"fmt"

"github.com/DataDrake/cli-ng/v2/cmd"
"github.com/hyprspace/hyprspace/daemon"
)

// Peers creates and brings up a Hyprspace Interface.
var Peers = cmd.Sub{
Name: "peers",
Alias: "p",
Short: "List peers currently connected to an interface.",
Args: &PeersArgs{},
Run: PeersRun,
}

// PeersArgs handles the specific arguments for the peers command.
type PeersArgs struct {
InterfaceName string
}

// PeersRun handles the execution of the up command.
func PeersRun(r *cmd.Root, c *cmd.Sub) {
// Parse Command Args
args := c.Args.(*PeersArgs)

peers, err := daemon.GetConnectedPeers(args.InterfaceName)

if err != nil && err.Error() != "" {
fmt.Println("Failed to get peers:", err)
} else {
fmt.Printf("Peers connected on %s: %d\n", args.InterfaceName, len(peers))
for _, p := range peers {
fmt.Println(p)
}
}
}
2 changes: 2 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func init() {
cmd.Register(&Down)
cmd.Register(&Update)
cmd.Register(&cmd.Version)
cmd.Register(&Daemon)
cmd.Register(&Peers)
}

func checkErr(err error) {
Expand Down
Loading