Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #52 from hyprspace/feature/reduced-bandwidth
Browse files Browse the repository at this point in the history
Improve Background Bandwidth Usage by Removing Discover Keys
  • Loading branch information
alecbcs authored Oct 23, 2021
2 parents c81dcf2 + dd88194 commit bba9921
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 112 deletions.
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ brew install iproute2mac
| `init` | `i` | Initialize an interface's configuration. |
| `up` | `up` | Create and Bring Up a Hyprspace Interface |
| `down ` | `d` | Bring Down and Delete A Hyprspace Interface |
| `update` | `upd` | Have Hyprspace update its own binary to the latest release. |
| `update` | `upd` | Have Hyprspace update its own binary to the latest release. |

### Global Flags
| Flag | Alias | Description |
| ------------------- | ------- | -------------------------------------------------------------------------- |
| `--config` | `-c` | Specify the path to a hyprspace config for an interface. |


## Tutorial

Expand Down Expand Up @@ -131,20 +137,6 @@ Notice here we'll have to pick one of our machines to be `10.1.1.1`
and the other to be `10.1.1.2`. Make sure to update the interface's IP
address for the machine who needs to change to be `10.1.1.2`.

### Update our Discover Key

Looking in the interface's configuration you'll also notice a field called
`discover_key` (right above the interface's private key). It doesn't matter
which discovery key you pick but it much be the same for all of the nodes in your little cluster
so that they can find each other.

(*Note you can use different `discover_key`s with different interfaces on the same
host to create different isolated networks.)

```yaml
discover_key: fiftieth-dandelion-wronged-craftwork
```

### Starting Up the Interfaces!
Now that we've got our configs all sorted we can start up the two interfaces!

Expand Down
17 changes: 5 additions & 12 deletions cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"context"
"os"
"path/filepath"
"strings"

"github.com/DataDrake/cli-ng/v2/cmd"
"github.com/hyprspace/hyprspace/config"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/sethvargo/go-diceware/diceware"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -47,19 +45,14 @@ func InitRun(r *cmd.Root, c *cmd.Sub) {
keyBytes, err := crypto.MarshalPrivateKey(host.Peerstore().PrivKey(host.ID()))
checkErr(err)

// Generate a random diceware discovery key
list, err := diceware.Generate(4)
checkErr(err)

// Setup an initial default command.
new := config.Config{
Interface: config.Interface{
Name: args.InterfaceName,
ListenPort: 8001,
Address: "10.1.1.1/24",
ID: host.ID().Pretty(),
PrivateKey: string(keyBytes),
DiscoverKey: strings.Join(list, "-"),
Name: args.InterfaceName,
ListenPort: 8001,
Address: "10.1.1.1/24",
ID: host.ID().Pretty(),
PrivateKey: string(keyBytes),
},
}

Expand Down
40 changes: 19 additions & 21 deletions cli/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ import (
)

var (
// Global is the global interface configuration for the
// application instance.
Global config.Config
// iface is the tun device used to pass packets between
// Hyprspace and the user's machine.
iface *water.Interface
Expand Down Expand Up @@ -73,13 +70,13 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
}

// Read in configuration from file.
Global, err := config.Read(configPath)
cfg, err := config.Read(configPath)
checkErr(err)

if !flags.Foreground {
// Make results chan
out := make(chan error)
go createDaemon(out)
go createDaemon(cfg, out)

select {
case err = <-out:
Expand All @@ -94,21 +91,21 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
}

// Setup reverse lookup hash map for authentication.
RevLookup = make(map[string]bool, len(Global.Peers))
for _, id := range Global.Peers {
RevLookup = make(map[string]bool, len(cfg.Peers))
for _, id := range cfg.Peers {
RevLookup[id.ID] = true
}

fmt.Println("[+] Creating TUN Device")
// Create new TUN device
iface, err = tun.New(Global.Interface.Name)
iface, err = tun.New(cfg.Interface.Name)
if err != nil {
checkErr(errors.New("interface already in use"))
}
// Set TUN MTU
tun.SetMTU(Global.Interface.Name, 1420)
tun.SetMTU(cfg.Interface.Name, 1420)
// Add Address to Interface
tun.SetAddress(Global.Interface.Name, Global.Interface.Address)
tun.SetAddress(cfg.Interface.Name, cfg.Interface.Address)

// Setup System Context
ctx := context.Background()
Expand All @@ -117,7 +114,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {

// Check that the listener port is available.
var ln net.Listener
port := Global.Interface.ListenPort
port := cfg.Interface.ListenPort
if port != 8001 {
ln, err = net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {
Expand All @@ -141,21 +138,21 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {

// Create P2P Node
host, dht, err := p2p.CreateNode(ctx,
Global.Interface.PrivateKey,
cfg.Interface.PrivateKey,
port,
streamHandler)
checkErr(err)

// Setup Peer Table for Quick Packet --> Dest ID lookup
peerTable := make(map[string]peer.ID)
for ip, id := range Global.Peers {
for ip, id := range cfg.Peers {
peerTable[ip], err = peer.Decode(id.ID)
checkErr(err)
}

fmt.Println("[+] Setting Up Node Discovery via DHT")
// Setup P2P Discovery
go p2p.Discover(ctx, host, dht, Global.Interface.DiscoverKey, peerTable)
go p2p.Discover(ctx, host, dht, peerTable)
go prettyDiscovery(ctx, host, peerTable)

go func() {
Expand All @@ -173,7 +170,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
}()

// Bring Up TUN Device
tun.Up(Global.Interface.Name)
tun.Up(cfg.Interface.Name)

fmt.Println("[+] Network Setup Complete...Waiting on Node Discovery")
// Listen For New Packets on TUN Interface
Expand All @@ -185,7 +182,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
plen, err = iface.Read(packet)
checkErr(err)
header, _ = ipv4.ParseHeader(packet)
_, ok := Global.Peers[header.Dst.String()]
_, ok := cfg.Peers[header.Dst.String()]
if ok {
stream, err = host.NewStream(ctx, peerTable[header.Dst.String()], p2p.Protocol)
if err != nil {
Expand All @@ -198,7 +195,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
}
}

func createDaemon(out chan<- error) {
func createDaemon(cfg config.Config, out chan<- error) {
path, err := os.Executable()
checkErr(err)
// Create Pipe to monitor for daemon output.
Expand All @@ -215,14 +212,15 @@ func createDaemon(out chan<- error) {
checkErr(err)
scanner := bufio.NewScanner(r)
count := 0
for scanner.Scan() && count < (4+len(Global.Peers)) {
for count < len(cfg.Peers) && scanner.Scan() {
fmt.Println(scanner.Text())
count++
if strings.HasPrefix(scanner.Text(), "[+] Connection to") {
count++
}
}
fmt.Println(scanner.Text())
err = process.Release()
checkErr(err)
if count < 4 {
if count < len(cfg.Peers) {
out <- errors.New("failed to create daemon")
}
out <- nil
Expand Down
22 changes: 10 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ type Config struct {

// Interface defines all of the fields that a local node needs to know about itself!
type Interface struct {
Name string `yaml:"name"`
ID string `yaml:"id"`
ListenPort int `yaml:"listen_port"`
Address string `yaml:"address"`
DiscoverKey string `yaml:"discover_key"`
PrivateKey string `yaml:"private_key"`
Name string `yaml:"name"`
ID string `yaml:"id"`
ListenPort int `yaml:"listen_port"`
Address string `yaml:"address"`
PrivateKey string `yaml:"private_key"`
}

// Peer defines a peer in the configuration. We might add more to this later.
Expand All @@ -35,12 +34,11 @@ func Read(path string) (result Config, err error) {
}
result = Config{
Interface: Interface{
Name: "hs0",
ListenPort: 8001,
Address: "10.1.1.1",
ID: "",
DiscoverKey: "",
PrivateKey: "",
Name: "hs0",
ListenPort: 8001,
Address: "10.1.1.1",
ID: "",
PrivateKey: "",
},
}
err = yaml.Unmarshal(in, &result)
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/libp2p/go-libp2p v0.14.1
github.com/libp2p/go-libp2p-core v0.8.6
github.com/libp2p/go-libp2p-discovery v0.5.0
github.com/libp2p/go-libp2p-kad-dht v0.12.1
github.com/libp2p/go-libp2p-quic-transport v0.12.0 // indirect
github.com/libp2p/go-tcp-transport v0.2.8 // indirect
github.com/libp2p/go-libp2p-quic-transport v0.12.0
github.com/libp2p/go-tcp-transport v0.2.8
github.com/multiformats/go-multiaddr v0.3.3
github.com/sethvargo/go-diceware v0.2.1
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781
Expand Down
Loading

0 comments on commit bba9921

Please sign in to comment.