packages/hyprspace: ban yaml from existence

This commit is contained in:
Max Headroom 2023-10-23 23:12:48 +02:00
parent 16384da670
commit dc51c4250c
8 changed files with 36 additions and 74 deletions

View file

@ -108,19 +108,19 @@ sudo hyprspace init hs1
Now that we've got a set of configurations we'll want to Now that we've got a set of configurations we'll want to
tell the machines about each other. By default Hyprspace will tell the machines about each other. By default Hyprspace will
put the interface configurations in `/etc/hyprspace/interface-name.yaml`. put the interface configurations in `/etc/hyprspace/interface-name.json`.
So for our example we'll run So for our example we'll run
###### Local Machine ###### Local Machine
```bash ```bash
sudo nano /etc/hyprspace/hs0.yaml sudo nano /etc/hyprspace/hs0.json
``` ```
and and
###### Remote Machine ###### Remote Machine
```bash ```bash
sudo nano /etc/hyprspace/hs1.yaml sudo nano /etc/hyprspace/hs1.json
``` ```
### Update Peer Configs ### Update Peer Configs
@ -129,14 +129,20 @@ Now in each config we'll add the other machine's ID as a peer.
You can find each machine's ID at the top of their configuration file. You can find each machine's ID at the top of their configuration file.
Update, Update,
```yaml ```json
peers: {} {
"peers": {}
}
``` ```
to to
```yaml ```json
peers: {
10.1.1.2: "peers": {
id: YOUR-OTHER-PEER-ID "10.1.1.2": {
"id": "YOUR-OTHER-PEER-ID"
}
}
}
``` ```
Notice here we'll have to pick one of our machines to be `10.1.1.1` Notice here we'll have to pick one of our machines to be `10.1.1.1`
@ -186,49 +192,6 @@ sudo hyprspace down hs1
WireGuard is a registered trademark of Jason A. Donenfeld. WireGuard is a registered trademark of Jason A. Donenfeld.
## Routes
### Prepare each route node:
```
# sysctl -n net.ipv4.ip_forward
0
# sysctl -w net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -s <YOUR_TUN_NET>/24 -o eth0 -j MASQUERADE
iptables -A FORWARD 1 -i <HS_TUN> -o <DEV_GATEWAY> -j ACCEPT
iptables -A FORWARD 1 -i <DEV_GATEWAY> -o <HS_TUN> -j ACCEPT
```
Determine gateway router:
```
# curl ifconfg.me
<GATEWAY_ROUTER>
```
### Configure client:
Config hyprspace yaml configuration file:
```
interface:
...
peers:
ID: ...
...
routes:
192.168.3.0/24:
ip: 10.0.0.3
0.0.0.0/0:
ip: 10.0.0.1
```
Prepare routes
```
One for each route:
# ip route add <GATEWAY_ROUTER> via <YOUR_GATEWAY>
And all traffic for hyprspace tun
# ip route add default dev <HS_TUN> metric 1
```
## License ## License
Copyright 2021-2022 Alec Scott <hi@alecbcs.com> Copyright 2021-2022 Alec Scott <hi@alecbcs.com>

View file

@ -32,7 +32,7 @@ func DownRun(r *cmd.Root, c *cmd.Sub) {
// 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 + ".yaml" configPath = "/etc/hyprspace/" + args.InterfaceName + ".json"
} }
// Read lock from file system to stop process. // Read lock from file system to stop process.

View file

@ -1,6 +1,7 @@
package cli package cli
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -11,7 +12,6 @@ import (
"github.com/libp2p/go-libp2p" "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"
"gopkg.in/yaml.v2"
) )
// Init creates a configuration for a Hyprspace Interface. // Init creates a configuration for a Hyprspace Interface.
@ -36,7 +36,7 @@ func InitRun(r *cmd.Root, c *cmd.Sub) {
// Parse Global Config Flag // Parse Global Config Flag
configPath := r.Flags.(*GlobalFlags).Config configPath := r.Flags.(*GlobalFlags).Config
if configPath == "" { if configPath == "" {
configPath = "/etc/hyprspace/" + args.InterfaceName + ".yaml" configPath = "/etc/hyprspace/" + args.InterfaceName + ".json"
} }
// Create New Libp2p Node // Create New Libp2p Node
@ -56,9 +56,10 @@ func InitRun(r *cmd.Root, c *cmd.Sub) {
ID: host.ID(), ID: host.ID(),
PrivateKey: multibase.MustNewEncoder(multibase.Base58BTC).Encode(keyBytes), PrivateKey: multibase.MustNewEncoder(multibase.Base58BTC).Encode(keyBytes),
}, },
Peers: make([]config.Peer, 0),
} }
out, err := yaml.Marshal(&new) out, err := json.MarshalIndent(&new, "", " ")
checkErr(err) checkErr(err)
err = os.MkdirAll(filepath.Dir(configPath), os.ModePerm) err = os.MkdirAll(filepath.Dir(configPath), os.ModePerm)

View file

@ -72,7 +72,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
// 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 + ".yaml" configPath = "/etc/hyprspace/" + args.InterfaceName + ".json"
} }
// Read in configuration from file. // Read in configuration from file.

View file

@ -1,41 +1,41 @@
package config package config
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"net" "net"
"os" "os"
"github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/peer"
"gopkg.in/yaml.v2"
) )
// Config is the main Configuration Struct for Hyprspace. // Config is the main Configuration Struct for Hyprspace.
type Config struct { type Config struct {
Path string `yaml:"path,omitempty"` Path string `json:"-"`
Interface Interface `yaml:"interface"` Interface Interface `json:"interface"`
Peers []Peer `yaml:"peers"` Peers []Peer `json:"peers"`
Routes []Route Routes []Route `json:"-"`
} }
// Interface defines all of the fields that a local node needs to know about itself! // Interface defines all of the fields that a local node needs to know about itself!
type Interface struct { type Interface struct {
Name string `yaml:"name"` Name string `json:"name"`
ID peer.ID `yaml:"id"` ID peer.ID `json:"id"`
ListenPort int `yaml:"listen_port"` ListenPort int `json:"listen_port"`
Address string `yaml:"address"` Address string `json:"address"`
PrivateKey string `yaml:"private_key"` 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.
type Peer struct { type Peer struct {
ID peer.ID `yaml:"id"` ID peer.ID `json:"id"`
Routes []Route `yaml:"routes"` Routes []Route `json:"routes"`
} }
type Route struct { type Route struct {
Target Peer Target Peer
NetworkStr string `yaml:"net"` NetworkStr string `json:"net"`
Network net.IPNet Network net.IPNet
} }
@ -56,7 +56,7 @@ func Read(path string) (*Config, error) {
} }
// Read in config settings from file. // Read in config settings from file.
err = yaml.Unmarshal(in, &result) err = json.Unmarshal(in, &result)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -11,7 +11,6 @@ require (
github.com/prometheus/client_golang v1.16.0 github.com/prometheus/client_golang v1.16.0
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
github.com/vishvananda/netlink v1.1.0 github.com/vishvananda/netlink v1.1.0
gopkg.in/yaml.v2 v2.4.0
) )
require ( require (

View file

@ -569,7 +569,6 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View file

@ -29,7 +29,7 @@
]); ]);
}; };
vendorSha256 = "sha256-6Hg1XdDIs6rXmag0oihCDqgNRDTQRwgPDj40q/b4+mo="; vendorSha256 = "sha256-zr9gRYA979VYaD8jvK1MMEDhbcpHvaJccR91wp5qClU=";
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.";