From 023ce59d4444b368092a87493266b1d083bbe222 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Wed, 21 Aug 2024 18:23:51 +0200 Subject: [PATCH] feat(p2p): allow to set intervals (#3353) Signed-off-by: Ettore Di Giacinto --- core/cli/run.go | 4 +++- core/p2p/p2p.go | 16 +++++++++++----- core/p2p/p2p_disabled.go | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/core/cli/run.go b/core/cli/run.go index 4f24182e8c3..55ae0fd56b7 100644 --- a/core/cli/run.go +++ b/core/cli/run.go @@ -52,6 +52,8 @@ type RunCMD struct { DisablePredownloadScan bool `env:"LOCALAI_DISABLE_PREDOWNLOAD_SCAN" help:"If true, disables the best-effort security scanner before downloading any files." group:"hardening" default:"false"` OpaqueErrors bool `env:"LOCALAI_OPAQUE_ERRORS" default:"false" help:"If true, all error responses are replaced with blank 500 errors. This is intended only for hardening against information leaks and is normally not recommended." group:"hardening"` Peer2Peer bool `env:"LOCALAI_P2P,P2P" name:"p2p" default:"false" help:"Enable P2P mode" group:"p2p"` + Peer2PeerDHTInterval int `env:"LOCALAI_P2P_DHT_INTERVAL,P2P_DHT_INTERVAL" default:"360" name:"p2p-dht-interval" help:"Interval for DHT refresh (used during token generation)" group:"p2p"` + Peer2PeerOTPInterval int `env:"LOCALAI_P2P_OTP_INTERVAL,P2P_OTP_INTERVAL" default:"9000" name:"p2p-otp-interval" help:"Interval for OTP refresh (used during token generation)" group:"p2p"` Peer2PeerToken string `env:"LOCALAI_P2P_TOKEN,P2P_TOKEN,TOKEN" name:"p2ptoken" help:"Token for P2P mode (optional)" group:"p2p"` Peer2PeerNetworkID string `env:"LOCALAI_P2P_NETWORK_ID,P2P_NETWORK_ID" help:"Network ID for P2P mode, can be set arbitrarly by the user for grouping a set of instances" group:"p2p"` ParallelRequests bool `env:"LOCALAI_PARALLEL_REQUESTS,PARALLEL_REQUESTS" help:"Enable backends to handle multiple requests in parallel if they support it (e.g.: llama.cpp or vllm)" group:"backends"` @@ -106,7 +108,7 @@ func (r *RunCMD) Run(ctx *cliContext.Context) error { // IF no token is provided, and p2p is enabled, // we generate one and wait for the user to pick up the token (this is for interactive) log.Info().Msg("No token provided, generating one") - token = p2p.GenerateToken() + token = p2p.GenerateToken(r.Peer2PeerDHTInterval, r.Peer2PeerOTPInterval) log.Info().Msg("Generated Token:") fmt.Println(token) diff --git a/core/p2p/p2p.go b/core/p2p/p2p.go index 53ae63b5159..15e1dc37c81 100644 --- a/core/p2p/p2p.go +++ b/core/p2p/p2p.go @@ -28,9 +28,15 @@ import ( "github.com/mudler/edgevpn/pkg/logger" ) -func generateNewConnectionData() *node.YAMLConnectionConfig { +func generateNewConnectionData(DHTInterval, OTPInterval int) *node.YAMLConnectionConfig { maxMessSize := 20 << 20 // 20MB keyLength := 43 + if DHTInterval == 0 { + DHTInterval = 360 + } + if OTPInterval == 0 { + OTPInterval = 9000 + } return &node.YAMLConnectionConfig{ MaxMessageSize: maxMessSize, @@ -40,21 +46,21 @@ func generateNewConnectionData() *node.YAMLConnectionConfig { OTP: node.OTP{ DHT: node.OTPConfig{ Key: eutils.RandStringRunes(keyLength), - Interval: 120, + Interval: DHTInterval, Length: keyLength, }, Crypto: node.OTPConfig{ Key: eutils.RandStringRunes(keyLength), - Interval: 9000, + Interval: OTPInterval, Length: keyLength, }, }, } } -func GenerateToken() string { +func GenerateToken(DHTInterval, OTPInterval int) string { // Generates a new config and exit - return generateNewConnectionData().Base64() + return generateNewConnectionData(DHTInterval, OTPInterval).Base64() } func IsP2PEnabled() bool { diff --git a/core/p2p/p2p_disabled.go b/core/p2p/p2p_disabled.go index f0d331dfb80..c5ba98fdab5 100644 --- a/core/p2p/p2p_disabled.go +++ b/core/p2p/p2p_disabled.go @@ -10,7 +10,7 @@ import ( "github.com/mudler/edgevpn/pkg/node" ) -func GenerateToken() string { +func GenerateToken(DHTInterval, OTPInterval int) string { return "not implemented" }