This commit is contained in:
mr
2026-01-27 15:49:57 +01:00
parent 5a94504abb
commit f7f9d722bd
4 changed files with 27 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ import "sync"
type Config struct {
Name string
Hostname string
PSKPath string
PublicKeyPath string
PrivateKeyPath string
DHTEndpointPort int64

View File

@@ -1,11 +1,13 @@
package infrastructure
import (
"bytes"
"fmt"
"oc-peer/conf"
"os"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/pnet"
)
func sign(priv crypto.PrivKey, data []byte) ([]byte, error) {
@@ -47,3 +49,18 @@ func VerifyPubWithPriv() bool {
}
return priv.GetPublic().Equals(pub)
}
func LoadPSKFromFile() (pnet.PSK, error) {
path := conf.GetConfig().PSKPath
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
// Try to unmarshal as libp2p private key (supports ed25519, rsa, etc.)
psk, err := pnet.DecodeV1PSK(bytes.NewReader(data))
if err != nil {
return nil, err
}
return psk, nil
}

View File

@@ -55,7 +55,12 @@ func Init(ctx context.Context) (*DHTService, error) {
if err != nil {
return nil, err
}
psk, err := LoadPSKFromFile()
if err != nil {
return nil, err
}
h, err := libp2p.New(
libp2p.PrivateNetwork(psk),
libp2p.Identity(priv),
libp2p.ListenAddrStrings(
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", conf.GetConfig().DHTEndpointPort),

View File

@@ -29,12 +29,12 @@ func main() {
o.GetStringDefault("LOKI_URL", ""),
o.GetStringDefault("LOG_LEVEL", "info"),
)
conf.GetConfig().Name = o.GetStringDefault("NAME", "local")
conf.GetConfig().Hostname = o.GetStringDefault("HOSTNAME", "http://localhost")
conf.GetConfig().PublicKeyPath = o.GetStringDefault("PUBLIC_KEY_PATH", "./pem/public.pem")
conf.GetConfig().PrivateKeyPath = o.GetStringDefault("PRIVATE_KEY_PATH", "./pem/private.pem")
conf.GetConfig().DHTEndpointPort = o.GetInt64Default("DHT_ENDPOINT_PORT", 80)
conf.GetConfig().PSKPath = o.GetStringDefault("PSK_PATH", "./psk/psk")
conf.GetConfig().PublicKeyPath = o.GetStringDefault("PEER_PUBLIC_KEY_PATH", "./pem/public.pem")
conf.GetConfig().PrivateKeyPath = o.GetStringDefault("PEER_PRIVATE_KEY_PATH", "./pem/private.pem")
conf.GetConfig().DHTEndpointPort = o.GetInt64Default("DHT_ENDPOINT_PORT", 4001)
// Beego init
beego.BConfig.AppName = appname
beego.BConfig.Listen.HTTPPort = o.GetIntDefault("port", 8080)