-
Notifications
You must be signed in to change notification settings - Fork 0
Implement enhanced proxy management and logging in the daemon + restart logic throttling for MacOS and Windows #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 18 commits
f624e9b
c9acad1
d13a4f8
840203e
4d35b38
f8000dd
da9498c
1d22f7d
19aef1f
e20f545
d71929b
da94221
48bfbbc
d0a6b2c
7f3190a
5290060
9a901ef
3dc1df5
a329518
1cbbdda
1f99fb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,12 @@ import ( | |
| "github.com/AikidoSec/safechain-internals/internal/version" | ||
| ) | ||
|
|
||
| const ( | ||
| DaemonStatusLogInterval = 1 * time.Hour | ||
| ProxyStartMaxRetries = 20 | ||
| ProxyStartRetryInterval = 3 * time.Minute | ||
| ) | ||
|
|
||
| type Config struct { | ||
| ConfigPath string | ||
| LogLevel string | ||
|
|
@@ -33,6 +39,11 @@ type Daemon struct { | |
| ingress *ingress.Server | ||
| logRotator *utils.LogRotator | ||
| logReaper *utils.LogReaper | ||
|
|
||
| proxyRetryCount int | ||
| proxyLastRetryTime time.Time | ||
|
|
||
| daemonLastStatusLogTime time.Time | ||
| } | ||
|
|
||
| func New(ctx context.Context, cancel context.CancelFunc, config *Config) (*Daemon, error) { | ||
|
|
@@ -213,19 +224,68 @@ func (d *Daemon) Uninstall(ctx context.Context, removeScanners bool) error { | |
| return nil | ||
| } | ||
|
|
||
| func (d *Daemon) heartbeat() error { | ||
| if !d.proxy.IsProxyRunning() { | ||
| log.Println("Proxy is not running, starting it...") | ||
| if err := d.startProxyAndInstallCA(d.ctx); err != nil { | ||
| return fmt.Errorf("failed to start proxy and install CA: %v", err) | ||
| func (d *Daemon) printDaemonStatus() { | ||
| log.Println("Daemon status:") | ||
| if d.proxy.IsProxyRunning() { | ||
| proxyVersion, _ := d.proxy.Version() | ||
| log.Printf("\t- Proxy: %s", proxyVersion) | ||
| } else { | ||
| log.Println("\t- Proxy: not running!") | ||
| } | ||
|
|
||
| for _, scannerName := range d.registry.List() { | ||
| scanner, err := d.registry.Get(scannerName) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| if d.proxy.IsProxyRunning() { | ||
| log.Println("Proxy started successfully") | ||
| } else { | ||
| log.Println("Failed to start proxy, will try again later") | ||
| if scanner.IsInstalled(d.ctx) { | ||
| log.Printf("\t- %s: %s", scannerName, scanner.Version(d.ctx)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (d *Daemon) handleProxy() error { | ||
|
||
| if d.proxy.IsProxyRunning() { | ||
| return nil | ||
| } | ||
|
|
||
| if d.proxyRetryCount >= ProxyStartMaxRetries { | ||
| // Exit daemon loop if proxy start retry limit is reached | ||
| return fmt.Errorf("proxy start retry limit reached (%d attempts), not retrying", d.proxyRetryCount) | ||
|
||
| } | ||
|
|
||
| if !d.proxyLastRetryTime.IsZero() && time.Since(d.proxyLastRetryTime) < ProxyStartRetryInterval { | ||
| log.Printf("Proxy is not running, waiting for retry interval before next attempt") | ||
|
||
| return nil | ||
| } | ||
|
|
||
| d.proxyRetryCount++ | ||
| d.proxyLastRetryTime = time.Now() | ||
| log.Printf("Proxy is not running, starting it... (attempt %d/%d)", d.proxyRetryCount, ProxyStartMaxRetries) | ||
|
|
||
| if err := d.startProxyAndInstallCA(d.ctx); err != nil { | ||
| log.Printf("Failed to start proxy and install CA: %v", err) | ||
| return nil | ||
tudor-timcu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if d.proxy.IsProxyRunning() { | ||
| log.Println("Proxy started successfully") | ||
| d.proxyRetryCount = 0 | ||
| d.proxyLastRetryTime = time.Time{} | ||
| } else { | ||
| log.Println("Proxy is running") | ||
| log.Printf("Failed to start proxy, will try again later") | ||
bitterpanda63 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (d *Daemon) heartbeat() error { | ||
| if err := d.handleProxy(); err != nil { | ||
| return fmt.Errorf("failed to handle proxy: %v", err) | ||
| } | ||
|
|
||
| if time.Since(d.daemonLastStatusLogTime) >= DaemonStatusLogInterval { | ||
| d.printDaemonStatus() | ||
| d.daemonLastStatusLogTime = time.Now() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.