Skip to content

Commit 023ce59

Browse files
authored
feat(p2p): allow to set intervals (#3353)
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 7822d94 commit 023ce59

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

core/cli/run.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type RunCMD struct {
5252
DisablePredownloadScan bool `env:"LOCALAI_DISABLE_PREDOWNLOAD_SCAN" help:"If true, disables the best-effort security scanner before downloading any files." group:"hardening" default:"false"`
5353
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"`
5454
Peer2Peer bool `env:"LOCALAI_P2P,P2P" name:"p2p" default:"false" help:"Enable P2P mode" group:"p2p"`
55+
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"`
56+
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"`
5557
Peer2PeerToken string `env:"LOCALAI_P2P_TOKEN,P2P_TOKEN,TOKEN" name:"p2ptoken" help:"Token for P2P mode (optional)" group:"p2p"`
5658
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"`
5759
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 {
106108
// IF no token is provided, and p2p is enabled,
107109
// we generate one and wait for the user to pick up the token (this is for interactive)
108110
log.Info().Msg("No token provided, generating one")
109-
token = p2p.GenerateToken()
111+
token = p2p.GenerateToken(r.Peer2PeerDHTInterval, r.Peer2PeerOTPInterval)
110112
log.Info().Msg("Generated Token:")
111113
fmt.Println(token)
112114

core/p2p/p2p.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ import (
2828
"github.com/mudler/edgevpn/pkg/logger"
2929
)
3030

31-
func generateNewConnectionData() *node.YAMLConnectionConfig {
31+
func generateNewConnectionData(DHTInterval, OTPInterval int) *node.YAMLConnectionConfig {
3232
maxMessSize := 20 << 20 // 20MB
3333
keyLength := 43
34+
if DHTInterval == 0 {
35+
DHTInterval = 360
36+
}
37+
if OTPInterval == 0 {
38+
OTPInterval = 9000
39+
}
3440

3541
return &node.YAMLConnectionConfig{
3642
MaxMessageSize: maxMessSize,
@@ -40,21 +46,21 @@ func generateNewConnectionData() *node.YAMLConnectionConfig {
4046
OTP: node.OTP{
4147
DHT: node.OTPConfig{
4248
Key: eutils.RandStringRunes(keyLength),
43-
Interval: 120,
49+
Interval: DHTInterval,
4450
Length: keyLength,
4551
},
4652
Crypto: node.OTPConfig{
4753
Key: eutils.RandStringRunes(keyLength),
48-
Interval: 9000,
54+
Interval: OTPInterval,
4955
Length: keyLength,
5056
},
5157
},
5258
}
5359
}
5460

55-
func GenerateToken() string {
61+
func GenerateToken(DHTInterval, OTPInterval int) string {
5662
// Generates a new config and exit
57-
return generateNewConnectionData().Base64()
63+
return generateNewConnectionData(DHTInterval, OTPInterval).Base64()
5864
}
5965

6066
func IsP2PEnabled() bool {

core/p2p/p2p_disabled.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/mudler/edgevpn/pkg/node"
1111
)
1212

13-
func GenerateToken() string {
13+
func GenerateToken(DHTInterval, OTPInterval int) string {
1414
return "not implemented"
1515
}
1616

0 commit comments

Comments
 (0)