mirror of
https://github.com/wlynxg/NetHive.git
synced 2025-09-26 19:11:14 +08:00
fix: save the configuration locally
This commit is contained in:
60
cmd/main.go
60
cmd/main.go
@@ -2,37 +2,53 @@ package main
|
||||
|
||||
import (
|
||||
"NetHive/core/engine"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"os"
|
||||
"flag"
|
||||
"log"
|
||||
"path"
|
||||
|
||||
"github.com/libp2p/go-libp2p/core/crypto"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
)
|
||||
|
||||
func main() {
|
||||
file, err := os.ReadFile("private.key")
|
||||
if err != nil {
|
||||
dir := flag.String("config-dir", "", `configuration file path`)
|
||||
if dir == nil || *dir == "" || !gfile.Exists(*dir) || !gfile.IsDir(*dir) {
|
||||
log.Fatal("config dir must be a valid folder path")
|
||||
return
|
||||
}
|
||||
key, err := crypto.UnmarshalPrivateKey(file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println(peer.IDFromPrivateKey(key))
|
||||
|
||||
opt := engine.Option{
|
||||
TUNName: "hive0",
|
||||
MTU: 1500,
|
||||
LocalAddr: netip.MustParsePrefix("192.168.199.1/32"),
|
||||
PrivateKey: key,
|
||||
PeersRouteTable: map[peer.ID][]netip.Prefix{
|
||||
"12D3KooWFDbzFGc89W8ZbaedTVcD4YgGMaKzTa3kw9hcZrBUrdZt": {netip.MustParsePrefix("192.168.199.1/32")},
|
||||
"12D3KooWLYUNghjWRUXBrdLqP2E7qky8r6GzJ74sJS4ZXn7cddJF": {netip.MustParsePrefix("192.168.199.2/32")},
|
||||
},
|
||||
var opt *engine.Option
|
||||
cfg := path.Join(*dir, "config.json")
|
||||
if gfile.Exists(cfg) {
|
||||
load, err := gjson.Load(cfg)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := load.Scan(&opt); err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
key, err := engine.NewPrivateKey()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
opt = &engine.Option{
|
||||
TUNName: "hive0",
|
||||
MTU: 1500,
|
||||
PrivateKey: key,
|
||||
}
|
||||
|
||||
if err := gfile.PutBytes(cfg, gjson.New(opt).MustToJson()); err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
e, err := engine.New(&opt)
|
||||
e, err := engine.New(opt)
|
||||
if err != nil {
|
||||
panic(e)
|
||||
}
|
||||
|
@@ -106,8 +106,12 @@ func New(opt *Option) (*Engine, error) {
|
||||
e.routeTable.set.Store(id, set)
|
||||
}
|
||||
|
||||
pk, err := opt.PrivateKey.PrivKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err := libp2p.New(
|
||||
libp2p.Identity(opt.PrivateKey),
|
||||
libp2p.Identity(pk),
|
||||
libp2p.EnableAutoRelayWithPeerSource(func(ctx context.Context, num int) <-chan peer.AddrInfo { return e.relayChan }),
|
||||
)
|
||||
if err != nil {
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"net/netip"
|
||||
|
||||
"github.com/libp2p/go-libp2p/core/crypto"
|
||||
@@ -8,10 +10,47 @@ import (
|
||||
)
|
||||
|
||||
type Option struct {
|
||||
TUNName string
|
||||
MTU int
|
||||
PrivateKey crypto.PrivKey
|
||||
PeersRouteTable map[peer.ID][]netip.Prefix
|
||||
LocalRoute []netip.Prefix
|
||||
LocalAddr netip.Prefix
|
||||
TUNName string `json:"tun_name"`
|
||||
MTU int `json:"mtu"`
|
||||
PrivateKey *PrivateKey `json:"private_key"`
|
||||
PeersRouteTable map[peer.ID][]netip.Prefix `json:"peers_route_table"`
|
||||
LocalRoute []netip.Prefix `json:"local_route"`
|
||||
LocalAddr netip.Prefix `json:"local_addr"`
|
||||
}
|
||||
|
||||
type PrivateKey struct {
|
||||
key []byte
|
||||
}
|
||||
|
||||
func NewPrivateKey() (*PrivateKey, error) {
|
||||
key, _, err := crypto.GenerateEd25519Key(rand.Reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
privateKey, err := crypto.MarshalPrivateKey(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PrivateKey{key: privateKey}, nil
|
||||
}
|
||||
|
||||
func (p *PrivateKey) MarshalJSON() ([]byte, error) {
|
||||
return []byte(hex.EncodeToString(p.key)), nil
|
||||
}
|
||||
|
||||
func (p *PrivateKey) UnmarshalJSON(data []byte) error {
|
||||
decodeString, err := hex.DecodeString(string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.key = decodeString
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PrivateKey) PrivKey() (crypto.PrivKey, error) {
|
||||
key, err := crypto.UnmarshalPrivateKey(p.key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
@@ -27,7 +27,7 @@ import (
|
||||
// The zero Map is empty and ready for use. A Map must not be copied after first use.
|
||||
//
|
||||
// In the terminology of the Go memory model, Map arranges that a write operation
|
||||
// “synchronizes before” any read operation that observes the effect of the write, where
|
||||
// “synchronizes before” any read operation that observes the effect of to write, where
|
||||
// read and write operations are defined as follows.
|
||||
// Load, LoadAndDelete, LoadOrStore, Swap, CompareAndSwap, and CompareAndDelete
|
||||
// are read operations; Delete, LoadAndDelete, Store, and Swap are write operations;
|
||||
|
Reference in New Issue
Block a user