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

Commit f4a7b5a

Browse files
authored
Merge pull request #11 from hyprspace/feature/experience-improvements
Improve Error Handling & Allow Multiple Interfaces
2 parents 0c59a57 + 5376827 commit f4a7b5a

8 files changed

Lines changed: 73 additions & 19 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
hyprspace
1+
hyprspace
2+
.DS_Store

cli/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func InitRun(r *cmd.Root, c *cmd.Sub) {
5555
new := config.Config{
5656
Interface: config.Interface{
5757
Name: args.InterfaceName,
58+
ListenPort: 8001,
5859
Address: "10.1.1.1/24",
5960
ID: host.ID().Pretty(),
6061
PrivateKey: string(keyBytes),

cli/root.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/DataDrake/cli-ng/v2/cmd"
1010
)
1111

12+
var appVersion string = "develop"
13+
1214
//GlobalFlags contains the flags for commands.
1315
type GlobalFlags struct {
1416
Config string `short:"c" long:"config" desc:"Specify a custom config path."`
@@ -19,15 +21,18 @@ var Root *cmd.Root
1921

2022
func init() {
2123
Root = &cmd.Root{
22-
Name: "hyprspace",
23-
Short: "Hyprspace Distributed Network",
24-
Flags: &GlobalFlags{},
24+
Name: "hyprspace",
25+
Short: "Hyprspace Distributed Network",
26+
Version: appVersion,
27+
Flags: &GlobalFlags{},
2528
}
29+
2630
cmd.Register(&cmd.Help)
2731
cmd.Register(&Init)
2832
cmd.Register(&Up)
2933
cmd.Register(&Down)
3034
cmd.Register(&Update)
35+
cmd.Register(&cmd.Version)
3136
}
3237

3338
func checkErr(err error) {

cli/up.go

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package cli
33
import (
44
"bufio"
55
"context"
6+
"errors"
67
"fmt"
78
"io"
89
"log"
10+
"net"
911
"os"
1012
"os/signal"
13+
"strconv"
1114
"strings"
1215
"syscall"
1316
"time"
@@ -75,15 +78,18 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
7578

7679
if !flags.Foreground {
7780
// Make results chan
78-
out := make(chan bool)
81+
out := make(chan error)
7982
go createDaemon(out)
8083

8184
select {
82-
case <-out:
85+
case err = <-out:
8386
case <-time.After(30 * time.Second):
8487
}
85-
checkErr(err)
86-
fmt.Println("[+] Successfully Created Hyprspace Daemon")
88+
if err != nil {
89+
fmt.Println("[+] Failed to Create Hyprspace Daemon")
90+
} else {
91+
fmt.Println("[+] Successfully Created Hyprspace Daemon")
92+
}
8793
return
8894
}
8995

@@ -96,7 +102,9 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
96102
fmt.Println("[+] Creating TUN Device")
97103
// Create new TUN device
98104
iface, err = tun.New(Global.Interface.Name)
99-
checkErr(err)
105+
if err != nil {
106+
checkErr(errors.New("interface already in use"))
107+
}
100108
// Set TUN MTU
101109
tun.SetMTU(Global.Interface.Name, 1420)
102110
// Add Address to Interface
@@ -106,8 +114,35 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
106114
ctx := context.Background()
107115

108116
fmt.Println("[+] Creating LibP2P Node")
117+
118+
// Check that the listener port is available.
119+
var ln net.Listener
120+
port := Global.Interface.ListenPort
121+
if port != 8001 {
122+
ln, err = net.Listen("tcp", ":"+strconv.Itoa(port))
123+
if err != nil {
124+
checkErr(errors.New("could not create node, listen port already in use by something else"))
125+
}
126+
} else {
127+
for {
128+
ln, err = net.Listen("tcp", ":"+strconv.Itoa(port))
129+
if err == nil {
130+
break
131+
}
132+
if port >= 65535 {
133+
checkErr(errors.New("failed to find open port"))
134+
}
135+
port++
136+
}
137+
}
138+
if ln != nil {
139+
ln.Close()
140+
}
109141
// Create P2P Node
110-
host, dht, err := p2p.CreateNode(ctx, Global.Interface.PrivateKey, streamHandler)
142+
host, dht, err := p2p.CreateNode(ctx,
143+
Global.Interface.PrivateKey,
144+
port,
145+
streamHandler)
111146
checkErr(err)
112147

113148
// Setup Peer Table for Quick Packet --> Dest ID lookup
@@ -162,7 +197,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
162197
}
163198
}
164199

165-
func createDaemon(out chan<- bool) {
200+
func createDaemon(out chan<- error) {
166201
path, err := os.Executable()
167202
checkErr(err)
168203
// Create Pipe to monitor for daemon output.
@@ -173,7 +208,7 @@ func createDaemon(out chan<- bool) {
173208
path,
174209
append(os.Args, "--foreground"),
175210
&os.ProcAttr{
176-
Files: []*os.File{nil, w, nil},
211+
Files: []*os.File{nil, w, w},
177212
},
178213
)
179214
checkErr(err)
@@ -186,7 +221,10 @@ func createDaemon(out chan<- bool) {
186221
fmt.Println(scanner.Text())
187222
err = process.Release()
188223
checkErr(err)
189-
out <- true
224+
if count < 4 {
225+
out <- errors.New("failed to create daemon")
226+
}
227+
out <- nil
190228
}
191229

192230
func streamHandler(stream network.Stream) {

cli/update.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
"github.com/tcnksm/go-latest"
1515
)
1616

17-
var appVersion string
18-
1917
// Update checks for a new version of the Hyprspace program and updates itself
2018
// if a newer version is found and the user agrees to update.
2119
var Update = cmd.Sub{

config/config.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ type Config struct {
1515
// Interface defines all of the fields that a local node needs to know about itself!
1616
type Interface struct {
1717
Name string `yaml:"name"`
18-
Address string `yaml:"address"`
1918
ID string `yaml:"id"`
19+
ListenPort int `yaml:"listen_port"`
20+
Address string `yaml:"address"`
2021
DiscoverKey string `yaml:"discover_key"`
2122
PrivateKey string `yaml:"private_key"`
2223
}
@@ -32,6 +33,16 @@ func Read(path string) (result Config, err error) {
3233
if err != nil {
3334
return
3435
}
36+
result = Config{
37+
Interface: Interface{
38+
Name: "hs0",
39+
ListenPort: 8001,
40+
Address: "10.1.1.1",
41+
ID: "",
42+
DiscoverKey: "",
43+
PrivateKey: "",
44+
},
45+
}
3546
err = yaml.Unmarshal(in, &result)
3647
return
3748
}

p2p/discover.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func Discover(ctx context.Context, h host.Host, dht *dht.IpfsDHT, rendezvous str
1818
var routingDiscovery = discovery.NewRoutingDiscovery(dht)
1919
discovery.Advertise(ctx, routingDiscovery, rendezvous)
2020

21-
ticker := time.NewTicker(time.Second * 1)
21+
ticker := time.NewTicker(time.Second * 5)
2222
defer ticker.Stop()
2323

2424
for {

p2p/node.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
const Protocol = "/hyprspace/0.0.1"
1919

2020
// CreateNode creates an internal Libp2p nodes and returns it and it's DHT Discovery service.
21-
func CreateNode(ctx context.Context, inputKey string, handler network.StreamHandler) (node host.Host, dhtOut *dht.IpfsDHT, err error) {
21+
func CreateNode(ctx context.Context, inputKey string, port int, handler network.StreamHandler) (node host.Host, dhtOut *dht.IpfsDHT, err error) {
2222
// Unmarshal Private Key
2323
privateKey, err := crypto.UnmarshalPrivateKey([]byte(inputKey))
2424
if err != nil {
@@ -27,7 +27,7 @@ func CreateNode(ctx context.Context, inputKey string, handler network.StreamHand
2727

2828
// Create libp2p node
2929
node, err = libp2p.New(ctx,
30-
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", 8001)),
30+
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port)),
3131
libp2p.Identity(privateKey),
3232
)
3333
if err != nil {

0 commit comments

Comments
 (0)