add option to disable public ip monitoring in config

This commit is contained in:
ghostersk
2025-05-28 07:17:03 +01:00
parent 52d20c8f0b
commit da17aef692
3 changed files with 58 additions and 3 deletions

Binary file not shown.

View File

@@ -37,6 +37,9 @@ const (
// DefaultLogRotationSizeMB is the default log rotation size in MB (5MB)
DefaultLogRotationSizeMB = 5
// DefaultObtainPublicIP is the default setting for whether to obtain public IP
DefaultObtainPublicIP = true
)
// DefaultPublicIPHTTPURLs are the default URLs for HTTP-based public IP detection
@@ -55,6 +58,7 @@ type Config struct {
HealthCheckInterval int // Health check interval in minutes (0 = disabled)
HealthCheckPath string // Health check endpoint path
PublicIPHTTPURLs []string // Custom HTTP URLs for public IP detection
ObtainPublicIP bool // Whether to obtain public IP (true = 1, false = 0)
SessionLogRotationSizeMB int // Session monitor log rotation size in MB (0 = no rotation, max 20MB)
ErrorLogRotationSizeMB int // Error log rotation size in MB (0 = no rotation, max 20MB)
EventLogRotationSizeMB int // Event CSV log rotation size in MB (0 = no rotation, max 20MB)
@@ -79,6 +83,7 @@ func GetConfig() *Config {
HealthCheckInterval: DefaultHealthCheckInterval,
HealthCheckPath: DefaultHealthCheckPath,
PublicIPHTTPURLs: make([]string, len(DefaultPublicIPHTTPURLs)),
ObtainPublicIP: DefaultObtainPublicIP,
SessionLogRotationSizeMB: DefaultLogRotationSizeMB,
ErrorLogRotationSizeMB: DefaultLogRotationSizeMB,
EventLogRotationSizeMB: DefaultLogRotationSizeMB,
@@ -159,6 +164,20 @@ func (c *Config) Load(configFile string) error {
copy(c.PublicIPHTTPURLs, DefaultPublicIPHTTPURLs)
}
// Load obtain public IP setting (supports both boolean and integer values)
obtainPublicIPKey := apiSection.Key("obtain_public_ip")
if obtainPublicIPKey.String() != "" {
// Check if it's a numeric value (0 or 1)
if intVal, err := obtainPublicIPKey.Int(); err == nil {
c.ObtainPublicIP = intVal != 0
} else {
// Fall back to boolean parsing
c.ObtainPublicIP = obtainPublicIPKey.MustBool(DefaultObtainPublicIP)
}
} else {
c.ObtainPublicIP = DefaultObtainPublicIP
}
// Read Logging section
loggingSection := cfg.Section("Logging")
c.SessionLogRotationSizeMB = loggingSection.Key("session_log_rotation_size_mb").MustInt(DefaultLogRotationSizeMB)
@@ -230,6 +249,20 @@ func (c *Config) LoadFromSource(configFile string) error {
copy(c.PublicIPHTTPURLs, DefaultPublicIPHTTPURLs)
}
// Load obtain public IP setting (supports both boolean and integer values)
obtainPublicIPKey := apiSection.Key("obtain_public_ip")
if obtainPublicIPKey.String() != "" {
// Check if it's a numeric value (0 or 1)
if intVal, err := obtainPublicIPKey.Int(); err == nil {
c.ObtainPublicIP = intVal != 0
} else {
// Fall back to boolean parsing
c.ObtainPublicIP = obtainPublicIPKey.MustBool(DefaultObtainPublicIP)
}
} else {
c.ObtainPublicIP = DefaultObtainPublicIP
}
// Read Logging section
loggingSection := cfg.Section("Logging")
c.SessionLogRotationSizeMB = loggingSection.Key("session_log_rotation_size_mb").MustInt(DefaultLogRotationSizeMB)
@@ -277,6 +310,7 @@ func (c *Config) Save(configFile string) error {
apiSection.Key("health_check_interval").SetValue(fmt.Sprintf("%d", c.HealthCheckInterval))
apiSection.Key("health_check_path").SetValue(c.HealthCheckPath)
apiSection.Key("public_ip_http_urls").SetValue(strings.Join(c.PublicIPHTTPURLs, ","))
apiSection.Key("obtain_public_ip").SetValue(fmt.Sprintf("%v", c.ObtainPublicIP))
// Create Logging section
loggingSection, err := cfg.NewSection("Logging")
@@ -360,6 +394,13 @@ func (c *Config) UpdateEventLogRotationSize(sizeMB int) {
c.EventLogRotationSizeMB = sizeMB
}
// UpdateObtainPublicIP updates the obtain public IP setting
func (c *Config) UpdateObtainPublicIP(obtainPublicIP bool) {
c.mu.Lock()
defer c.mu.Unlock()
c.ObtainPublicIP = obtainPublicIP
}
// UpdateInstallDir updates the installation directory
func (c *Config) UpdateInstallDir(installDir string) {
c.mu.Lock()
@@ -385,6 +426,7 @@ func (c *Config) createDefaultConfig(configFile string) error {
apiSection.Key("health_check_interval").SetValue(fmt.Sprintf("%d", c.HealthCheckInterval))
apiSection.Key("health_check_path").SetValue(c.HealthCheckPath)
apiSection.Key("public_ip_http_urls").SetValue(strings.Join(c.PublicIPHTTPURLs, ","))
apiSection.Key("obtain_public_ip").SetValue(fmt.Sprintf("%v", c.ObtainPublicIP))
// Create Logging section
loggingSection, err := cfg.NewSection("Logging")

View File

@@ -360,9 +360,14 @@ func destroyWindow(hwnd HWND) {
// getPublicIP retrieves public IP with HTTP first, then DNS fallback
func getPublicIP() (string, error) {
// Get config to check for custom HTTP URLs
// Get config to check if public IP detection is enabled
cfg := config.GetConfig()
// If public IP detection is disabled, return empty string
if !cfg.ObtainPublicIP {
return "", nil
}
// Use custom URLs if configured, otherwise use defaults
urls := []string{
"https://ipv4.icanhazip.com",
@@ -430,8 +435,16 @@ func (m *Monitor) logEventWithTime(eventType, username, computerName, localIP st
// Get public IP (this can be done in background as it's less time-sensitive)
publicIP, err := getPublicIP()
if err != nil {
m.logger.Warning("Failed to get public IP: %v - using 'Unknown'", err)
publicIP = "Unknown"
// Check if public IP detection is disabled
cfg := config.GetConfig()
if !cfg.ObtainPublicIP {
// If disabled, use empty string without logging warning
publicIP = ""
} else {
// If enabled but failed, log warning and use "Unknown"
m.logger.Warning("Failed to get public IP: %v - using 'Unknown'", err)
publicIP = "Unknown"
}
}
// Format the timestamp using the pre-captured event time