psk
This commit is contained in:
@@ -5,6 +5,7 @@ import "sync"
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
Name string
|
Name string
|
||||||
Hostname string
|
Hostname string
|
||||||
|
PSKPath string
|
||||||
PublicKeyPath string
|
PublicKeyPath string
|
||||||
PrivateKeyPath string
|
PrivateKeyPath string
|
||||||
DHTEndpointPort int64
|
DHTEndpointPort int64
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
package infrastructure
|
package infrastructure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"oc-peer/conf"
|
"oc-peer/conf"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p/core/crypto"
|
"github.com/libp2p/go-libp2p/core/crypto"
|
||||||
|
"github.com/libp2p/go-libp2p/core/pnet"
|
||||||
)
|
)
|
||||||
|
|
||||||
func sign(priv crypto.PrivKey, data []byte) ([]byte, error) {
|
func sign(priv crypto.PrivKey, data []byte) ([]byte, error) {
|
||||||
@@ -47,3 +49,18 @@ func VerifyPubWithPriv() bool {
|
|||||||
}
|
}
|
||||||
return priv.GetPublic().Equals(pub)
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -55,7 +55,12 @@ func Init(ctx context.Context) (*DHTService, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
psk, err := LoadPSKFromFile()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
h, err := libp2p.New(
|
h, err := libp2p.New(
|
||||||
|
libp2p.PrivateNetwork(psk),
|
||||||
libp2p.Identity(priv),
|
libp2p.Identity(priv),
|
||||||
libp2p.ListenAddrStrings(
|
libp2p.ListenAddrStrings(
|
||||||
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", conf.GetConfig().DHTEndpointPort),
|
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", conf.GetConfig().DHTEndpointPort),
|
||||||
|
|||||||
8
main.go
8
main.go
@@ -29,12 +29,12 @@ func main() {
|
|||||||
o.GetStringDefault("LOKI_URL", ""),
|
o.GetStringDefault("LOKI_URL", ""),
|
||||||
o.GetStringDefault("LOG_LEVEL", "info"),
|
o.GetStringDefault("LOG_LEVEL", "info"),
|
||||||
)
|
)
|
||||||
|
|
||||||
conf.GetConfig().Name = o.GetStringDefault("NAME", "local")
|
conf.GetConfig().Name = o.GetStringDefault("NAME", "local")
|
||||||
conf.GetConfig().Hostname = o.GetStringDefault("HOSTNAME", "http://localhost")
|
conf.GetConfig().Hostname = o.GetStringDefault("HOSTNAME", "http://localhost")
|
||||||
conf.GetConfig().PublicKeyPath = o.GetStringDefault("PUBLIC_KEY_PATH", "./pem/public.pem")
|
conf.GetConfig().PSKPath = o.GetStringDefault("PSK_PATH", "./psk/psk")
|
||||||
conf.GetConfig().PrivateKeyPath = o.GetStringDefault("PRIVATE_KEY_PATH", "./pem/private.pem")
|
conf.GetConfig().PublicKeyPath = o.GetStringDefault("PEER_PUBLIC_KEY_PATH", "./pem/public.pem")
|
||||||
conf.GetConfig().DHTEndpointPort = o.GetInt64Default("DHT_ENDPOINT_PORT", 80)
|
conf.GetConfig().PrivateKeyPath = o.GetStringDefault("PEER_PRIVATE_KEY_PATH", "./pem/private.pem")
|
||||||
|
conf.GetConfig().DHTEndpointPort = o.GetInt64Default("DHT_ENDPOINT_PORT", 4001)
|
||||||
// Beego init
|
// Beego init
|
||||||
beego.BConfig.AppName = appname
|
beego.BConfig.AppName = appname
|
||||||
beego.BConfig.Listen.HTTPPort = o.GetIntDefault("port", 8080)
|
beego.BConfig.Listen.HTTPPort = o.GetIntDefault("port", 8080)
|
||||||
|
|||||||
Reference in New Issue
Block a user