2022-09-25 22:04:44 +03:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-01-23 01:34:17 +02:00
|
|
|
"log"
|
2022-09-25 22:04:44 +03:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
2023-01-23 01:34:17 +02:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-09-25 22:04:44 +03:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the main Configuration Struct for Hyprspace.
|
|
|
|
type Config struct {
|
2023-01-23 01:34:17 +02:00
|
|
|
Path string `yaml:"path,omitempty"`
|
|
|
|
Interface Interface `yaml:"interface"`
|
|
|
|
Peers []Peer `yaml:"peers"`
|
|
|
|
Routes []Route
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Interface defines all of the fields that a local node needs to know about itself!
|
|
|
|
type Interface struct {
|
2023-01-23 01:34:17 +02:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
ID peer.ID `yaml:"id"`
|
|
|
|
ListenPort int `yaml:"listen_port"`
|
|
|
|
Address string `yaml:"address"`
|
|
|
|
PrivateKey string `yaml:"private_key"`
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Peer defines a peer in the configuration. We might add more to this later.
|
|
|
|
type Peer struct {
|
2023-01-23 01:34:17 +02:00
|
|
|
ID peer.ID `yaml:"id"`
|
|
|
|
Routes []Route `yaml:"routes"`
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
|
2022-10-08 12:40:30 +03:00
|
|
|
type Route struct {
|
2023-01-23 01:34:17 +02:00
|
|
|
Target Peer
|
|
|
|
NetworkStr string `yaml:"net"`
|
|
|
|
Network net.IPNet
|
2022-10-08 12:40:30 +03:00
|
|
|
}
|
|
|
|
|
2022-09-25 22:04:44 +03:00
|
|
|
// Read initializes a config from a file.
|
|
|
|
func Read(path string) (*Config, error) {
|
|
|
|
in, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := Config{
|
|
|
|
Interface: Interface{
|
|
|
|
Name: "hs0",
|
|
|
|
ListenPort: 8001,
|
|
|
|
Address: "10.1.1.1/24",
|
|
|
|
ID: "",
|
|
|
|
PrivateKey: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read in config settings from file.
|
|
|
|
err = yaml.Unmarshal(in, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-23 01:34:17 +02:00
|
|
|
for _, p := range result.Peers {
|
|
|
|
for _, r := range p.Routes {
|
|
|
|
r.Target = p
|
|
|
|
_, n, err := net.ParseCIDR(r.NetworkStr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("[!] Invalid network:", r.NetworkStr)
|
|
|
|
}
|
|
|
|
r.Network = *n
|
|
|
|
result.Routes = append(result.Routes, r)
|
|
|
|
fmt.Printf("[+] Route %s via %s\n", r.Network.String(), p.ID.String())
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite path of config to input.
|
|
|
|
result.Path = path
|
|
|
|
return &result, nil
|
|
|
|
}
|
2023-01-23 01:34:17 +02:00
|
|
|
|
|
|
|
func FindPeer(peers []Peer, needle peer.ID) (*Peer, bool) {
|
|
|
|
for _, p := range peers {
|
|
|
|
if p.ID == needle {
|
|
|
|
return &p, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func FindRoute(routes []Route, needle net.IPNet) (*Route, bool) {
|
|
|
|
for _, r := range routes {
|
|
|
|
bits1, _ := r.Network.Mask.Size()
|
|
|
|
bits2, _ := needle.Mask.Size()
|
|
|
|
if r.Network.IP.Equal(needle.IP) && bits1 == bits2 {
|
|
|
|
return &r, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|