packages/hyprspace: 0.7.1 -> 0.8.0

- config overhaul
- remove unnecessary cli args
- remove down command
This commit is contained in:
Max Headroom 2023-10-27 00:32:18 +02:00
parent 7086f652a8
commit 9954f8eb03
8 changed files with 91 additions and 201 deletions

View file

@ -1,62 +0,0 @@
package cli
import (
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/DataDrake/cli-ng/v2/cmd"
"github.com/hyprspace/hyprspace/tun"
)
// Down brings down a Hyprspace interface and removes it from the system.
var Down = cmd.Sub{
Name: "down",
Alias: "d",
Short: "Bring Down A Hyprspace Interface.",
Args: &DownArgs{},
Run: DownRun,
}
// DownArgs handles the specific arguments for the down command.
type DownArgs struct {
InterfaceName string
}
// DownRun handles the execution of the down command.
func DownRun(r *cmd.Root, c *cmd.Sub) {
// Parse Command Args
args := c.Args.(*DownArgs)
// Parse Global Config Flag for Custom Config Path
configPath := r.Flags.(*GlobalFlags).Config
if configPath == "" {
configPath = "/etc/hyprspace/" + args.InterfaceName + ".json"
}
// Read lock from file system to stop process.
lockPath := filepath.Join(filepath.Dir(configPath), args.InterfaceName+".lock")
out, err := os.ReadFile(lockPath)
checkErr(err)
pid, err := strconv.Atoi(string(out))
checkErr(err)
process, err := os.FindProcess(pid)
checkErr(err)
err0 := process.Signal(os.Interrupt)
err1 := tun.Delete(args.InterfaceName)
// Different types of systems may need the tun devices destroyed first or
// the process to exit first don't worry as long as one of these two has
// succeeded.
if err0 != nil && err1 != nil {
checkErr(err0)
checkErr(err1)
}
fmt.Println("[+] deleted hyprspace " + args.InterfaceName + " daemon")
}

View file

@ -5,11 +5,9 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/DataDrake/cli-ng/v2/cmd" "github.com/DataDrake/cli-ng/v2/cmd"
"github.com/hyprspace/hyprspace/config" "github.com/hyprspace/hyprspace/config"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto" "github.com/libp2p/go-libp2p/core/crypto"
"github.com/multiformats/go-multibase" "github.com/multiformats/go-multibase"
) )
@ -19,41 +17,35 @@ var Init = cmd.Sub{
Name: "init", Name: "init",
Alias: "i", Alias: "i",
Short: "Initialize An Interface Config", Short: "Initialize An Interface Config",
Args: &InitArgs{},
Run: InitRun, Run: InitRun,
} }
// InitArgs handles the specific arguments for the init command.
type InitArgs struct {
InterfaceName string
}
// InitRun handles the execution of the init command. // InitRun handles the execution of the init command.
func InitRun(r *cmd.Root, c *cmd.Sub) { func InitRun(r *cmd.Root, c *cmd.Sub) {
// Parse Command Arguments ifName := r.Flags.(*GlobalFlags).InterfaceName
args := c.Args.(*InitArgs) if ifName == "" {
ifName = "hyprspace"
// Parse Global Config Flag
configPath := r.Flags.(*GlobalFlags).Config
if configPath == "" {
configPath = "/etc/hyprspace/" + args.InterfaceName + ".json"
} }
// Create New Libp2p Node configPath := r.Flags.(*GlobalFlags).Config
host, err := libp2p.New() if configPath == "" {
configPath = "/etc/hyprspace/" + ifName + ".json"
}
privKey, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 256)
checkErr(err) checkErr(err)
// Get Node's Private Key keyBytes, err := crypto.MarshalPrivateKey(privKey)
keyBytes, err := crypto.MarshalPrivateKey(host.Peerstore().PrivKey(host.ID()))
checkErr(err) checkErr(err)
// Setup an initial default command. // Setup an initial default command.
new := config.Config{ new := config.Config{
Interface: config.Interface{ EncodedPrivateKey: multibase.MustNewEncoder(multibase.Base58BTC).Encode(keyBytes),
Name: args.InterfaceName, EncodedListenAddresses: []string{
ListenPort: 8001, "/ip4/0.0.0.0/tcp/8001",
ID: host.ID(), "/ip4/0.0.0.0/udp/8001/quic-v1",
PrivateKey: multibase.MustNewEncoder(multibase.Base58BTC).Encode(keyBytes), "/ip6/::/tcp/8001",
"/ip6/::/udp/8001/quic-v1",
}, },
Peers: make([]config.Peer, 0), Peers: make([]config.Peer, 0),
} }
@ -67,21 +59,11 @@ func InitRun(r *cmd.Root, c *cmd.Sub) {
f, err := os.Create(configPath) f, err := os.Create(configPath)
checkErr(err) checkErr(err)
// Write out config to file.
_, err = f.Write(out) _, err = f.Write(out)
checkErr(err) checkErr(err)
err = f.Close() err = f.Close()
checkErr(err) checkErr(err)
// Print config creation message to user
fmt.Printf("Initialized new config at %s\n", configPath) fmt.Printf("Initialized new config at %s\n", configPath)
fmt.Println("To edit the config run,")
fmt.Println()
if strings.HasPrefix(configPath, "/etc/") {
fmt.Printf(" sudo nano %s\n", configPath)
} else {
fmt.Printf(" nano %s\n", configPath)
}
fmt.Println()
} }

View file

@ -31,7 +31,6 @@ func init() {
cmd.Register(&cmd.Help) cmd.Register(&cmd.Help)
cmd.Register(&Init) cmd.Register(&Init)
cmd.Register(&Up) cmd.Register(&Up)
cmd.Register(&Down)
cmd.Register(&Status) cmd.Register(&Status)
cmd.Register(&Peers) cmd.Register(&Peers)
cmd.Register(&Route) cmd.Register(&Route)

View file

@ -11,7 +11,6 @@ import (
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"strconv"
"syscall" "syscall"
"time" "time"
@ -27,7 +26,6 @@ import (
"github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr" "github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multibase"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/yl2chen/cidranger" "github.com/yl2chen/cidranger"
) )
@ -51,34 +49,25 @@ var Up = cmd.Sub{
Name: "up", Name: "up",
Alias: "up", Alias: "up",
Short: "Create and Bring Up a Hyprspace Interface.", Short: "Create and Bring Up a Hyprspace Interface.",
Args: &UpArgs{},
Flags: &UpFlags{},
Run: UpRun, Run: UpRun,
} }
// UpArgs handles the specific arguments for the up command.
type UpArgs struct {
InterfaceName string
}
// UpFlags handles the specific flags for the up command.
type UpFlags struct {
Foreground bool `short:"f" long:"foreground" desc:"Don't Create Background Daemon."`
}
// UpRun handles the execution of the up command. // UpRun handles the execution of the up command.
func UpRun(r *cmd.Root, c *cmd.Sub) { func UpRun(r *cmd.Root, c *cmd.Sub) {
// Parse Command Args ifName := r.Flags.(*GlobalFlags).InterfaceName
args := c.Args.(*UpArgs) if ifName == "" {
ifName = "hyprspace"
}
// Parse Global Config Flag for Custom Config Path // Parse Global Config Flag for Custom Config Path
configPath := r.Flags.(*GlobalFlags).Config configPath := r.Flags.(*GlobalFlags).Config
if configPath == "" { if configPath == "" {
configPath = "/etc/hyprspace/" + args.InterfaceName + ".json" configPath = "/etc/hyprspace/" + ifName + ".json"
} }
// Read in configuration from file. // Read in configuration from file.
cfg2, err := config.Read(configPath) cfg2, err := config.Read(configPath)
cfg2.Interface = ifName
cfg = cfg2 cfg = cfg2
checkErr(err) checkErr(err)
@ -86,8 +75,8 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
// Create new TUN device // Create new TUN device
tunDev, err = tun.New( tunDev, err = tun.New(
cfg.Interface.Name, cfg.Interface,
tun.Address(cfg.Interface.BuiltinAddr.String()+"/32"), tun.Address(cfg.BuiltinAddr.String()+"/32"),
tun.MTU(1420), tun.MTU(1420),
) )
if err != nil { if err != nil {
@ -108,16 +97,11 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
fmt.Println("[+] Creating LibP2P Node") fmt.Println("[+] Creating LibP2P Node")
// Check that the listener port is available.
port, err := verifyPort(cfg.Interface.ListenPort)
checkErr(err)
_, privateKey, err := multibase.Decode(cfg.Interface.PrivateKey)
// Create P2P Node // Create P2P Node
host, dht, err := p2p.CreateNode( host, dht, err := p2p.CreateNode(
ctx, ctx,
privateKey, cfg.PrivateKey,
port, cfg.ListenAddresses,
streamHandler, streamHandler,
p2p.NewClosedCircuitRelayFilter(cfg.Peers), p2p.NewClosedCircuitRelayFilter(cfg.Peers),
cfg.Peers, cfg.Peers,
@ -136,7 +120,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
go p2p.Discover(ctx, host, dht, cfg.Peers) go p2p.Discover(ctx, host, dht, cfg.Peers)
// Configure path for lock // Configure path for lock
lockPath := filepath.Join(filepath.Dir(cfg.Path), cfg.Interface.Name+".lock") lockPath := filepath.Join(filepath.Dir(cfg.Path), cfg.Interface+".lock")
// PeX // PeX
go p2p.PeXService(ctx, host, cfg) go p2p.PeXService(ctx, host, cfg)
@ -151,10 +135,10 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
go eventLogger(ctx, host) go eventLogger(ctx, host)
// RPC server // RPC server
go hsrpc.RpcServer(ctx, multiaddr.StringCast(fmt.Sprintf("/unix/run/hyprspace-rpc.%s.sock", cfg.Interface.Name)), host, *cfg, *tunDev) go hsrpc.RpcServer(ctx, multiaddr.StringCast(fmt.Sprintf("/unix/run/hyprspace-rpc.%s.sock", cfg.Interface)), host, *cfg, *tunDev)
// Magic DNS server // Magic DNS server
go hsdns.MagicDnsServer(ctx, *cfg) go hsdns.MagicDnsServer(ctx, *cfg, node)
// metrics endpoint // metrics endpoint
metricsPort, ok := os.LookupEnv("HYPRSPACE_METRICS_PORT") metricsPort, ok := os.LookupEnv("HYPRSPACE_METRICS_PORT")
@ -201,7 +185,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
} }
dstIP := net.IPv4(packet[16], packet[17], packet[18], packet[19]) dstIP := net.IPv4(packet[16], packet[17], packet[18], packet[19])
if cfg.Interface.BuiltinAddr.Equal(dstIP) { if cfg.BuiltinAddr.Equal(dstIP) {
continue continue
} }
var dst peer.ID var dst peer.ID
@ -354,34 +338,3 @@ func streamHandler(stream network.Stream) {
tunDev.Iface.Write(packet[:size]) tunDev.Iface.Write(packet[:size])
} }
} }
func verifyPort(port int) (int, error) {
var ln net.Listener
var err error
// If a user manually sets a port don't try to automatically
// find an open port.
if port != 8001 {
ln, err = net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {
return port, errors.New("could not create node, listen port already in use by something else")
}
} else {
// Automatically look for an open port when a custom port isn't
// selected by a user.
for {
ln, err = net.Listen("tcp", ":"+strconv.Itoa(port))
if err == nil {
break
}
if port >= 65535 {
return port, errors.New("failed to find open port")
}
port++
}
}
if ln != nil {
ln.Close()
}
return port, nil
}

View file

@ -8,25 +8,24 @@ import (
"os" "os"
"strings" "strings"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multibase"
"github.com/yl2chen/cidranger" "github.com/yl2chen/cidranger"
) )
// Config is the main Configuration Struct for Hyprspace. // Config is the main Configuration Struct for Hyprspace.
type Config struct { type Config struct {
Path string `json:"-"` Path string `json:"-"`
Interface Interface `json:"interface"` Interface string `json:"-"`
Peers []Peer `json:"peers"` EncodedListenAddresses []string `json:"listenAddresses"`
PeerLookup PeerLookup `json:"-"` ListenAddresses []multiaddr.Multiaddr `json:"-"`
} Peers []Peer `json:"peers"`
PeerLookup PeerLookup `json:"-"`
// Interface defines all of the fields that a local node needs to know about itself! EncodedPrivateKey string `json:"privateKey"`
type Interface struct { PrivateKey crypto.PrivKey `json:"-"`
Name string `json:"name"` BuiltinAddr net.IP `json:"-"`
ID peer.ID `json:"id"`
ListenPort int `json:"listen_port"`
BuiltinAddr net.IP `json:"-"`
PrivateKey string `json:"private_key"`
} }
// Peer defines a peer in the configuration. We might add more to this later. // Peer defines a peer in the configuration. We might add more to this later.
@ -64,11 +63,11 @@ func Read(path string) (*Config, error) {
return nil, err return nil, err
} }
result := Config{ result := Config{
Interface: Interface{ EncodedListenAddresses: []string{
Name: "hs0", "/ip4/0.0.0.0/tcp/8001",
ListenPort: 8001, "/ip4/0.0.0.0/udp/8001/quic-v1",
ID: "", "/ip6/::/tcp/8001",
PrivateKey: "", "/ip6/::/udp/8001/quic-v1",
}, },
} }
@ -78,7 +77,35 @@ func Read(path string) (*Config, error) {
return nil, err return nil, err
} }
result.Interface.BuiltinAddr = mkBuiltinAddr(result.Interface.ID) _, keyBytes, err := multibase.Decode(result.EncodedPrivateKey)
if err != nil {
return nil, err
}
pk, err := crypto.UnmarshalPrivateKey(keyBytes)
if err != nil {
return nil, err
}
result.PrivateKey = pk
if err != nil {
return nil, err
}
peerID, err := peer.IDFromPrivateKey(result.PrivateKey)
if err != nil {
return nil, err
}
result.BuiltinAddr = mkBuiltinAddr(peerID)
for _, addrString := range result.EncodedListenAddresses {
addr, err := multiaddr.NewMultiaddr(addrString)
if err != nil {
return nil, err
}
result.ListenAddresses = append(result.ListenAddresses, addr)
}
result.PeerLookup.ByRoute = cidranger.NewPCTrieRanger() result.PeerLookup.ByRoute = cidranger.NewPCTrieRanger()
result.PeerLookup.ByName = make(map[string]Peer) result.PeerLookup.ByName = make(map[string]Peer)

View file

@ -10,6 +10,7 @@ import (
"github.com/hyprspace/hyprspace/config" "github.com/hyprspace/hyprspace/config"
"github.com/iguanesolutions/go-systemd/v5/resolved" "github.com/iguanesolutions/go-systemd/v5/resolved"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/peer"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/multiformats/go-multibase" "github.com/multiformats/go-multibase"
@ -66,7 +67,7 @@ func writeResponse(msg *dns.Msg, q dns.Question, p peer.ID, addr net.IP) {
}) })
} }
func MagicDnsServer(ctx context.Context, config config.Config) { func MagicDnsServer(ctx context.Context, config config.Config, node host.Host) {
dns.HandleFunc(domainSuffix, func(w dns.ResponseWriter, r *dns.Msg) { dns.HandleFunc(domainSuffix, func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(r) m.SetReply(r)
@ -75,8 +76,8 @@ func MagicDnsServer(ctx context.Context, config config.Config) {
switch q.Qtype { switch q.Qtype {
case dns.TypeA: case dns.TypeA:
if qpeer, err := peer.Decode(strings.TrimSuffix(q.Name, "."+domainSuffix)); err == nil { if qpeer, err := peer.Decode(strings.TrimSuffix(q.Name, "."+domainSuffix)); err == nil {
if qpeer == config.Interface.ID { if qpeer == node.ID() {
m.Answer = append(m.Answer, mkIDRecord(config.Interface.ID, config.Interface.BuiltinAddr)) m.Answer = append(m.Answer, mkIDRecord(node.ID(), config.BuiltinAddr))
} else { } else {
for _, p := range config.Peers { for _, p := range config.Peers {
if p.ID == qpeer { if p.ID == qpeer {
@ -94,8 +95,8 @@ func MagicDnsServer(ctx context.Context, config config.Config) {
qName := strings.ToLower(strings.TrimSuffix(q.Name, "."+domainSuffix)) qName := strings.ToLower(strings.TrimSuffix(q.Name, "."+domainSuffix))
if qName == strings.ToLower(hostname) { if qName == strings.ToLower(hostname) {
m.Answer = append(m.Answer, mkAliasRecord(qName, config.Interface.ID)) m.Answer = append(m.Answer, mkAliasRecord(qName, node.ID()))
m.Answer = append(m.Answer, mkIDRecord(config.Interface.ID, config.Interface.BuiltinAddr)) m.Answer = append(m.Answer, mkIDRecord(node.ID(), config.BuiltinAddr))
} else if p, found := config.PeerLookup.ByName[qName]; found { } else if p, found := config.PeerLookup.ByName[qName]; found {
m.Answer = append(m.Answer, mkAliasRecord(qName, p.ID)) m.Answer = append(m.Answer, mkAliasRecord(qName, p.ID))
m.Answer = append(m.Answer, mkIDRecord(p.ID, p.BuiltinAddr)) m.Answer = append(m.Answer, mkIDRecord(p.ID, p.BuiltinAddr))
@ -128,7 +129,7 @@ func MagicDnsServer(ctx context.Context, config config.Config) {
} }
defer conn.Close() defer conn.Close()
link, err := netlink.LinkByName(config.Interface.Name) link, err := netlink.LinkByName(config.Interface)
if err != nil { if err != nil {
fmt.Println("[!] [dns] Failed to get link ID:", err) fmt.Println("[!] [dns] Failed to get link ID:", err)
return return
@ -137,14 +138,14 @@ func MagicDnsServer(ctx context.Context, config config.Config) {
for _, f := range [](func() error){ for _, f := range [](func() error){
func() error { func() error {
return conn.SetLinkDNSEx(ctx, linkID, []resolved.LinkDNSEx{resolved.LinkDNSEx{ return conn.SetLinkDNSEx(ctx, linkID, []resolved.LinkDNSEx{{
Family: syscall.AF_INET, Family: syscall.AF_INET,
Address: []byte{127, 80, 1, 53}, Address: []byte{127, 80, 1, 53},
Port: 5380, Port: 5380,
}}) }})
}, },
func() error { func() error {
return conn.SetLinkDomains(ctx, linkID, []resolved.LinkDomain{resolved.LinkDomain{ return conn.SetLinkDomains(ctx, linkID, []resolved.LinkDomain{{
Domain: domainSuffix, Domain: domainSuffix,
RoutingDomain: false, RoutingDomain: false,
}}) }})

View file

@ -91,13 +91,7 @@ func getExtraBootstrapNodes(addr ma.Multiaddr) (nodesList []string) {
} }
// CreateNode creates an internal Libp2p nodes and returns it and it's DHT Discovery service. // CreateNode creates an internal Libp2p nodes and returns it and it's DHT Discovery service.
func CreateNode(ctx context.Context, inputKey []byte, port int, handler network.StreamHandler, acl relay.ACLFilter, vpnPeers []config.Peer) (node host.Host, dhtOut *dht.IpfsDHT, err error) { func CreateNode(ctx context.Context, privateKey crypto.PrivKey, listenAddreses []ma.Multiaddr, handler network.StreamHandler, acl relay.ACLFilter, vpnPeers []config.Peer) (node host.Host, dhtOut *dht.IpfsDHT, err error) {
// Unmarshal Private Key
privateKey, err := crypto.UnmarshalPrivateKey(inputKey)
if err != nil {
return
}
maybePrivateNet := libp2p.ChainOptions() maybePrivateNet := libp2p.ChainOptions()
swarmKeyFile, ok := os.LookupEnv("HYPRSPACE_SWARM_KEY") swarmKeyFile, ok := os.LookupEnv("HYPRSPACE_SWARM_KEY")
if ok { if ok {
@ -112,18 +106,12 @@ func CreateNode(ctx context.Context, inputKey []byte, port int, handler network.
maybePrivateNet = libp2p.PrivateNetwork(key) maybePrivateNet = libp2p.PrivateNetwork(key)
} }
ip6quic := fmt.Sprintf("/ip6/::/udp/%d/quic-v1", port)
ip4quic := fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic-v1", port)
ip6tcp := fmt.Sprintf("/ip6/::/tcp/%d", port)
ip4tcp := fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port)
peerChan := make(chan peer.AddrInfo) peerChan := make(chan peer.AddrInfo)
// Create libp2p node // Create libp2p node
basicHost, err := libp2p.New( basicHost, err := libp2p.New(
maybePrivateNet, maybePrivateNet,
libp2p.ListenAddrStrings(ip6tcp, ip4tcp, ip4quic, ip6quic), libp2p.ListenAddrs(listenAddreses...),
libp2p.Identity(privateKey), libp2p.Identity(privateKey),
libp2p.DefaultSecurity, libp2p.DefaultSecurity,
libp2p.NATPortMap(), libp2p.NATPortMap(),

View file

@ -8,9 +8,9 @@
]; ];
env.GOPATH.eval = "$REPO_DATA_DIR/go"; env.GOPATH.eval = "$REPO_DATA_DIR/go";
}; };
packages.hyprspace = with pkgs; buildGo120Module { packages.hyprspace = with pkgs; buildGo120Module rec {
pname = "hyprspace"; pname = "hyprspace";
version = "0.7.1"; version = "0.8.0";
src = with inputs.nix-filter.lib; let src = with inputs.nix-filter.lib; let
dirs = map inDirectory; dirs = map inDirectory;
@ -32,6 +32,8 @@
vendorSha256 = "sha256-cP93ndADvQ9gFvPdNgi3aY4kAaKAuL0wq6P6PcUwIX4="; vendorSha256 = "sha256-cP93ndADvQ9gFvPdNgi3aY4kAaKAuL0wq6P6PcUwIX4=";
ldflags = [ "-s" "-w" "-X github.com/hyprspace/hyprspace/cli.appVersion=${version}" ];
meta = with lib; { meta = with lib; {
description = "A Lightweight VPN Built on top of Libp2p for Truly Distributed Networks."; description = "A Lightweight VPN Built on top of Libp2p for Truly Distributed Networks.";
homepage = "https://github.com/hyprspace/hyprspace"; homepage = "https://github.com/hyprspace/hyprspace";