From 21e42bf0305d60bbb13c44cbcf70d18367bd0db7 Mon Sep 17 00:00:00 2001 From: eyedeekay Date: Tue, 27 May 2025 20:12:40 -0400 Subject: [PATCH] Simplify and move stuff --- common/config.go | 62 ++++++---------------------------------------- common/util.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 55 deletions(-) diff --git a/common/config.go b/common/config.go index 02d4fa3f..8a424bcf 100644 --- a/common/config.go +++ b/common/config.go @@ -409,66 +409,18 @@ func (f *I2PConfig) Accesslist() string { // If no encryption type is set, returns default value "4,0". // Validates that all encryption types are valid integers. func (f *I2PConfig) LeaseSetEncryptionType() string { - // Use default encryption type if none specified + // Use default if not specified if f.LeaseSetEncryption == "" { - log.Debug("Using default lease set encryption type: 4,0") - return "i2cp.leaseSetEncType=4,0" + return f.formatLeaseSetEncryptionType("4,0") } - // Validate each encryption type is a valid integer - for _, s := range strings.Split(f.LeaseSetEncryption, ",") { - if _, err := strconv.Atoi(s); err != nil { - log.WithField("invalidType", s).Panic("Invalid encrypted leaseSet type") - // panic("Invalid encrypted leaseSet type: " + s) - } + // Validate all encryption types are integers + if err := f.validateEncryptionTypes(f.LeaseSetEncryption); err != nil { + log.WithError(err).Warn("Invalid encryption types, using default") + return f.formatLeaseSetEncryptionType("4,0") } - // Log and return the configured encryption type - log.WithField("leaseSetEncType", f.LeaseSetEncryption).Debug("Lease set encryption type set") - return fmt.Sprintf("i2cp.leaseSetEncType=%s", f.LeaseSetEncryption) -} - -// collectTunnelSettings returns all tunnel-related configuration strings -func (f *I2PConfig) collectTunnelSettings() []string { - return []string{ - f.InboundLength(), - f.OutboundLength(), - f.InboundLengthVariance(), - f.OutboundLengthVariance(), - f.InboundBackupQuantity(), - f.OutboundBackupQuantity(), - f.InboundQuantity(), - f.OutboundQuantity(), - } -} - -// collectConnectionSettings returns all connection behavior configuration strings -func (f *I2PConfig) collectConnectionSettings() []string { - return []string{ - f.UsingCompression(), - f.DoZero(), // Zero hop settings - f.Reduce(), // Reduce idle settings - f.Close(), // Close idle settings - f.Reliability(), // Message reliability - } -} - -// collectLeaseSetSettings returns all lease set configuration strings -func (f *I2PConfig) collectLeaseSetSettings() []string { - lsk, lspk, lspsk := f.LeaseSetSettings() - return []string{ - f.EncryptLease(), // Lease encryption - lsk, lspk, lspsk, // Lease set keys - f.LeaseSetEncryptionType(), // Lease set encryption type - } -} - -// collectAccessSettings returns all access control configuration strings -func (f *I2PConfig) collectAccessSettings() []string { - return []string{ - f.Accesslisttype(), // Access list type - f.Accesslist(), // Access list - } + return f.formatLeaseSetEncryptionType(f.LeaseSetEncryption) } func NewConfig(opts ...func(*I2PConfig) error) (*I2PConfig, error) { diff --git a/common/util.go b/common/util.go index e8dc5b5d..91137ae4 100644 --- a/common/util.go +++ b/common/util.go @@ -1,6 +1,7 @@ package common import ( + "fmt" "math/rand" "net" "strconv" @@ -115,3 +116,66 @@ func (f *I2PConfig) generateRandomTunnelName() string { return string(name) } + +// validateEncryptionTypes checks that all comma-separated values are valid integers +func (f *I2PConfig) validateEncryptionTypes(encTypes string) error { + for _, s := range strings.Split(encTypes, ",") { + trimmed := strings.TrimSpace(s) + if trimmed == "" { + return fmt.Errorf("empty encryption type") + } + if _, err := strconv.Atoi(trimmed); err != nil { + return fmt.Errorf("invalid encryption type '%s': %w", trimmed, err) + } + } + return nil +} + +// formatLeaseSetEncryptionType creates the formatted configuration string +func (f *I2PConfig) formatLeaseSetEncryptionType(encType string) string { + log.WithField("leaseSetEncType", encType).Debug("Lease set encryption type set") + return fmt.Sprintf("i2cp.leaseSetEncType=%s", encType) +} + +// collectTunnelSettings returns all tunnel-related configuration strings +func (f *I2PConfig) collectTunnelSettings() []string { + return []string{ + f.InboundLength(), + f.OutboundLength(), + f.InboundLengthVariance(), + f.OutboundLengthVariance(), + f.InboundBackupQuantity(), + f.OutboundBackupQuantity(), + f.InboundQuantity(), + f.OutboundQuantity(), + } +} + +// collectConnectionSettings returns all connection behavior configuration strings +func (f *I2PConfig) collectConnectionSettings() []string { + return []string{ + f.UsingCompression(), + f.DoZero(), // Zero hop settings + f.Reduce(), // Reduce idle settings + f.Close(), // Close idle settings + f.Reliability(), // Message reliability + } +} + +// collectLeaseSetSettings returns all lease set configuration strings +func (f *I2PConfig) collectLeaseSetSettings() []string { + lsk, lspk, lspsk := f.LeaseSetSettings() + return []string{ + f.EncryptLease(), // Lease encryption + lsk, lspk, lspsk, // Lease set keys + f.LeaseSetEncryptionType(), // Lease set encryption type + } +} + +// collectAccessSettings returns all access control configuration strings +func (f *I2PConfig) collectAccessSettings() []string { + return []string{ + f.Accesslisttype(), // Access list type + f.Accesslist(), // Access list + } +}