feat: add option to log the n most active IPs each hour (#496)

Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/496
Co-authored-by: crapStone <me@crapstone.dev>
Co-committed-by: crapStone <me@crapstone.dev>
This commit is contained in:
crapStone 2025-06-25 22:12:33 +02:00 committed by crapStone
parent d27c594c28
commit 5477ba2c46
8 changed files with 97 additions and 5 deletions

View file

@ -14,6 +14,8 @@ type ServerConfig struct {
HttpPort uint16 `default:"80"`
HttpServerEnabled bool `default:"true"`
UseProxyProtocol bool `default:"false"`
LogMostActiveIps bool `default:"false"`
MostActiveIpCount uint `default:"10"`
MainDomain string
RawDomain string
PagesBranches []string

View file

@ -72,6 +72,10 @@ func mergeServerConfig(ctx *cli.Context, config *ServerConfig) {
if ctx.IsSet("use-proxy-protocol") {
config.UseProxyProtocol = ctx.Bool("use-proxy-protocol")
}
if ctx.IsSet("log-most-active-ips") {
config.LogMostActiveIps = true
config.MostActiveIpCount = ctx.Uint("log-most-active-ips")
}
if ctx.IsSet("pages-domain") {
config.MainDomain = ctx.String("pages-domain")

View file

@ -140,6 +140,9 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T
Port: 8080,
HttpPort: 80,
HttpServerEnabled: false,
UseProxyProtocol: false,
LogMostActiveIps: false,
MostActiveIpCount: 10,
MainDomain: "original",
RawDomain: "original",
PagesBranches: []string{"original"},
@ -180,6 +183,9 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T
Port: 8443,
HttpPort: 443,
HttpServerEnabled: true,
UseProxyProtocol: true,
LogMostActiveIps: true,
MostActiveIpCount: 42,
MainDomain: "changed",
RawDomain: "changed",
PagesBranches: []string{"changed"},
@ -227,6 +233,8 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T
"--port", "8443",
"--http-port", "443",
"--enable-http-server",
"--use-proxy-protocol",
"--log-most-active-ips", "42",
// Forge
"--forge-root", "changed",
"--forge-api-token", "changed",
@ -277,6 +285,9 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
Port: 8080,
HttpPort: 80,
HttpServerEnabled: false,
UseProxyProtocol: false,
LogMostActiveIps: false,
MostActiveIpCount: 10,
MainDomain: "original",
RawDomain: "original",
AllowedCorsDomains: []string{"original"},
@ -290,6 +301,9 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
Port: 8443,
HttpPort: 443,
HttpServerEnabled: true,
UseProxyProtocol: true,
LogMostActiveIps: true,
MostActiveIpCount: 21,
MainDomain: "changed",
RawDomain: "changed",
AllowedCorsDomains: fixArrayFromCtx(ctx, "allowed-cors-domains", []string{"changed"}),
@ -309,6 +323,8 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
"--port", "8443",
"--http-port", "443",
"--enable-http-server",
"--use-proxy-protocol",
"--log-most-active-ips", "21",
},
)
}
@ -329,6 +345,11 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE
{args: []string{"--pages-branch", "changed"}, callback: func(sc *ServerConfig) { sc.PagesBranches = []string{"changed"} }},
{args: []string{"--allowed-cors-domains", "changed"}, callback: func(sc *ServerConfig) { sc.AllowedCorsDomains = []string{"changed"} }},
{args: []string{"--blacklisted-paths", "changed"}, callback: func(sc *ServerConfig) { sc.BlacklistedPaths = []string{"changed"} }},
{args: []string{"--use-proxy-protocol"}, callback: func(sc *ServerConfig) { sc.UseProxyProtocol = true }},
{args: []string{"--log-most-active-ips", "42"}, callback: func(sc *ServerConfig) {
sc.LogMostActiveIps = true
sc.MostActiveIpCount = 42
}},
}
for _, pair := range testValuePairs {
@ -345,6 +366,9 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE
PagesBranches: []string{"original"},
AllowedCorsDomains: []string{"original"},
BlacklistedPaths: []string{"original"},
UseProxyProtocol: false,
LogMostActiveIps: false,
MostActiveIpCount: 10,
}
expectedConfig := cfg