2022-09-25 22:04:44 +03:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-10-24 00:12:48 +03:00
|
|
|
"encoding/json"
|
2022-09-25 22:04:44 +03:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the main Configuration Struct for Hyprspace.
|
|
|
|
type Config struct {
|
2023-10-24 00:12:48 +03:00
|
|
|
Path string `json:"-"`
|
|
|
|
Interface Interface `json:"interface"`
|
|
|
|
Peers []Peer `json:"peers"`
|
|
|
|
Routes []Route `json:"-"`
|
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-10-24 00:12:48 +03:00
|
|
|
Name string `json:"name"`
|
|
|
|
ID peer.ID `json:"id"`
|
|
|
|
ListenPort int `json:"listen_port"`
|
|
|
|
Address string `json:"address"`
|
|
|
|
PrivateKey string `json:"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-10-24 00:12:48 +03:00
|
|
|
ID peer.ID `json:"id"`
|
|
|
|
Routes []Route `json:"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
|
2023-10-24 00:12:48 +03:00
|
|
|
NetworkStr string `json:"net"`
|
2023-01-23 01:34:17 +02:00
|
|
|
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.
|
2023-10-24 00:12:48 +03:00
|
|
|
err = json.Unmarshal(in, &result)
|
2022-09-25 22:04:44 +03:00
|
|
|
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
|
|
|
|
}
|
2023-01-23 18:26:13 +02:00
|
|
|
|
|
|
|
func FindRouteForIP(routes []Route, needle net.IP) (*Route, bool) {
|
|
|
|
for _, r := range routes {
|
|
|
|
if r.Network.Contains(needle) {
|
|
|
|
return &r, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|