lib: fix default difficulty (#96)
Before this did not respect the difficulty flag and instead used difficulty 4. This has been fixed. Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
parent
f462209b02
commit
725e11d3a6
3 changed files with 84 additions and 2 deletions
|
@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- Introduced integration tests using Playwright
|
- Introduced integration tests using Playwright
|
||||||
- Refactor & Split up Anubis into cmd and lib.go
|
- Refactor & Split up Anubis into cmd and lib.go
|
||||||
- Fixed bot check to only apply if address range matches
|
- Fixed bot check to only apply if address range matches
|
||||||
|
- Fix default difficulty setting that was broken in a refactor
|
||||||
|
|
||||||
## v1.14.2
|
## v1.14.2
|
||||||
|
|
||||||
|
|
|
@ -498,8 +498,8 @@ func (s *Server) check(r *http.Request) (CheckResult, *policy.Bot, error) {
|
||||||
|
|
||||||
return cr("default/allow", config.RuleAllow), &policy.Bot{
|
return cr("default/allow", config.RuleAllow), &policy.Bot{
|
||||||
Challenge: &config.ChallengeRules{
|
Challenge: &config.ChallengeRules{
|
||||||
Difficulty: anubis.DefaultDifficulty,
|
Difficulty: s.policy.DefaultDifficulty,
|
||||||
ReportAs: anubis.DefaultDifficulty,
|
ReportAs: s.policy.DefaultDifficulty,
|
||||||
Algorithm: config.AlgorithmFast,
|
Algorithm: config.AlgorithmFast,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
|
|
81
lib/anubis_test.go
Normal file
81
lib/anubis_test.go
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
package lib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/TecharoHQ/anubis"
|
||||||
|
)
|
||||||
|
|
||||||
|
func spawnAnubis(t *testing.T, h http.Handler) string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
policy, err := LoadPoliciesOrDefault("", anubis.DefaultDifficulty)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := New(Options{
|
||||||
|
Next: h,
|
||||||
|
Policy: policy,
|
||||||
|
ServeRobotsTXT: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("can't construct libanubis.Server: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ts := httptest.NewServer(s)
|
||||||
|
t.Log(ts.URL)
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
|
ts.Close()
|
||||||
|
})
|
||||||
|
|
||||||
|
return ts.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCheckDefaultDifficultyMatchesPolicy(t *testing.T) {
|
||||||
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintln(w, "OK")
|
||||||
|
})
|
||||||
|
|
||||||
|
for i := 1; i < 10; i++ {
|
||||||
|
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
||||||
|
policy, err := LoadPoliciesOrDefault("", i)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := New(Options{
|
||||||
|
Next: h,
|
||||||
|
Policy: policy,
|
||||||
|
ServeRobotsTXT: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("can't construct libanubis.Server: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodGet, "/", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Add("X-Real-Ip", "127.0.0.1")
|
||||||
|
|
||||||
|
_, bot, err := s.check(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if bot.Challenge.Difficulty != i {
|
||||||
|
t.Errorf("Challenge.Difficulty is wrong, wanted %d, got: %d", i, bot.Challenge.Difficulty)
|
||||||
|
}
|
||||||
|
|
||||||
|
if bot.Challenge.ReportAs != i {
|
||||||
|
t.Errorf("Challenge.ReportAs is wrong, wanted %d, got: %d", i, bot.Challenge.ReportAs)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue