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-10-25 00:34:12 +03:00
|
|
|
"strings"
|
2022-09-25 22:04:44 +03:00
|
|
|
|
2023-10-27 01:32:18 +03:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
2023-01-23 01:34:17 +02:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-10-27 01:32:18 +03:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
"github.com/multiformats/go-multibase"
|
2023-10-25 00:34:12 +03:00
|
|
|
"github.com/yl2chen/cidranger"
|
2022-09-25 22:04:44 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the main Configuration Struct for Hyprspace.
|
|
|
|
type Config struct {
|
2023-10-27 01:32:18 +03:00
|
|
|
Path string `json:"-"`
|
|
|
|
Interface string `json:"-"`
|
|
|
|
EncodedListenAddresses []string `json:"listenAddresses"`
|
|
|
|
ListenAddresses []multiaddr.Multiaddr `json:"-"`
|
|
|
|
Peers []Peer `json:"peers"`
|
|
|
|
PeerLookup PeerLookup `json:"-"`
|
|
|
|
EncodedPrivateKey string `json:"privateKey"`
|
|
|
|
PrivateKey crypto.PrivKey `json:"-"`
|
|
|
|
BuiltinAddr net.IP `json:"-"`
|
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-25 00:34:12 +03:00
|
|
|
ID peer.ID `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
BuiltinAddr net.IP `json:"-"`
|
|
|
|
Routes []Route `json:"routes"`
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
|
2022-10-08 12:40:30 +03:00
|
|
|
type Route struct {
|
2023-10-25 00:34:12 +03:00
|
|
|
NetworkStr string `json:"net"`
|
|
|
|
Network net.IPNet `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeerLookup is a helper struct for quickly looking up a peer based on various parameters
|
|
|
|
type PeerLookup struct {
|
|
|
|
ByRoute cidranger.Ranger
|
|
|
|
ByName map[string]Peer
|
|
|
|
}
|
|
|
|
|
|
|
|
type RouteTableEntry struct {
|
2023-10-26 04:31:05 +03:00
|
|
|
Net net.IPNet
|
|
|
|
Target Peer
|
2023-10-25 00:34:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rte RouteTableEntry) Network() net.IPNet {
|
2023-10-26 04:31:05 +03:00
|
|
|
return rte.Net
|
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{
|
2023-10-27 01:32:18 +03:00
|
|
|
EncodedListenAddresses: []string{
|
|
|
|
"/ip4/0.0.0.0/tcp/8001",
|
|
|
|
"/ip4/0.0.0.0/udp/8001/quic-v1",
|
|
|
|
"/ip6/::/tcp/8001",
|
|
|
|
"/ip6/::/udp/8001/quic-v1",
|
2022-09-25 22:04:44 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-10-27 01:32:18 +03:00
|
|
|
_, 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)
|
|
|
|
}
|
2023-10-25 00:34:12 +03:00
|
|
|
|
|
|
|
result.PeerLookup.ByRoute = cidranger.NewPCTrieRanger()
|
|
|
|
result.PeerLookup.ByName = make(map[string]Peer)
|
|
|
|
|
|
|
|
for i, p := range result.Peers {
|
|
|
|
p.BuiltinAddr = mkBuiltinAddr(p.ID)
|
|
|
|
p.Routes = append(p.Routes, Route{
|
|
|
|
Network: net.IPNet{
|
|
|
|
IP: p.BuiltinAddr,
|
|
|
|
Mask: net.IPv4Mask(255, 255, 255, 255),
|
|
|
|
},
|
|
|
|
})
|
2023-01-23 01:34:17 +02:00
|
|
|
for _, r := range p.Routes {
|
2023-10-25 00:34:12 +03:00
|
|
|
if r.NetworkStr != "" {
|
|
|
|
_, n, err := net.ParseCIDR(r.NetworkStr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("[!] Invalid network:", r.NetworkStr)
|
|
|
|
}
|
|
|
|
r.Network = *n
|
2023-01-23 01:34:17 +02:00
|
|
|
}
|
2023-10-25 00:34:12 +03:00
|
|
|
|
|
|
|
result.PeerLookup.ByRoute.Insert(&RouteTableEntry{
|
2023-10-26 04:31:05 +03:00
|
|
|
Net: r.Network,
|
|
|
|
Target: p,
|
2023-10-25 00:34:12 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
fmt.Printf("[+] Route %s via /p2p/%s\n", r.Network.String(), p.ID)
|
|
|
|
}
|
|
|
|
if p.Name != "" {
|
|
|
|
result.PeerLookup.ByName[strings.ToLower(p.Name)] = p
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
2023-10-25 00:34:12 +03:00
|
|
|
result.Peers[i] = p
|
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
|
|
|
|
2023-10-25 00:34:12 +03:00
|
|
|
func mkBuiltinAddr(p peer.ID) net.IP {
|
|
|
|
builtinAddr := []byte{100, 64, 1, 2}
|
|
|
|
for i, b := range []byte(p) {
|
|
|
|
builtinAddr[(i%2)+2] ^= b
|
|
|
|
}
|
|
|
|
return net.IP(builtinAddr)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-25 00:34:12 +03:00
|
|
|
func (cfg Config) FindRoute(needle net.IPNet) (*RouteTableEntry, bool) {
|
|
|
|
networks, err := cfg.PeerLookup.ByRoute.CoveredNetworks(needle)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, false
|
|
|
|
} else if len(networks) == 0 {
|
|
|
|
return nil, false
|
|
|
|
} else if len(networks) > 1 {
|
|
|
|
for _, n := range networks {
|
|
|
|
fmt.Printf("[!] Found duplicate route %s to /p2p/%s for %s\n", n.Network(), n.(RouteTableEntry).Target.ID, needle)
|
2023-01-23 01:34:17 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-25 00:34:12 +03:00
|
|
|
return networks[0].(*RouteTableEntry), true
|
2023-01-23 01:34:17 +02:00
|
|
|
}
|
2023-01-23 18:26:13 +02:00
|
|
|
|
2023-10-25 00:34:12 +03:00
|
|
|
func (cfg Config) FindRouteForIP(needle net.IP) (*RouteTableEntry, bool) {
|
|
|
|
networks, err := cfg.PeerLookup.ByRoute.ContainingNetworks(needle)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, false
|
|
|
|
} else if len(networks) == 0 {
|
|
|
|
return nil, false
|
|
|
|
} else if len(networks) > 1 {
|
|
|
|
for _, n := range networks {
|
|
|
|
fmt.Printf("[!] Found duplicate route %s to /p2p/%s for %s\n", n.Network(), n.(RouteTableEntry).Target.ID, needle)
|
2023-01-23 18:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-25 00:34:12 +03:00
|
|
|
return networks[0].(*RouteTableEntry), true
|
2023-01-23 18:26:13 +02:00
|
|
|
}
|