mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-17 05:44:57 +00:00
Compare commits
8 Commits
Xe/fix-ngi
...
Xe/iplist2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e05d8bd65b | ||
|
|
c84a414570 | ||
|
|
ad41bc7a25 | ||
|
|
c5815f3be7 | ||
|
|
2dc0719556 | ||
|
|
9ccfdf37ef | ||
|
|
b592aaf656 | ||
|
|
1d8e98c5ec |
5
.github/actions/spelling/allow.txt
vendored
5
.github/actions/spelling/allow.txt
vendored
@@ -18,3 +18,8 @@ clampip
|
|||||||
pseudoprofound
|
pseudoprofound
|
||||||
reimagining
|
reimagining
|
||||||
iocaine
|
iocaine
|
||||||
|
admins
|
||||||
|
fout
|
||||||
|
iplist
|
||||||
|
NArg
|
||||||
|
blocklists
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
- Add iplist2rule tool that lets admins turn an IP address blocklist into an Anubis ruleset.
|
||||||
- Add Polish locale ([#1292](https://github.com/TecharoHQ/anubis/pull/1309))
|
- Add Polish locale ([#1292](https://github.com/TecharoHQ/anubis/pull/1309))
|
||||||
|
|
||||||
<!-- This changes the project to: -->
|
<!-- This changes the project to: -->
|
||||||
|
|||||||
50
docs/docs/admin/iplist2rule.mdx
Normal file
50
docs/docs/admin/iplist2rule.mdx
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
---
|
||||||
|
title: iplist2rule CLI tool
|
||||||
|
---
|
||||||
|
|
||||||
|
The `iplist2rule` tool converts IP blocklists into Anubis challenge policies. It reads common IP block list formats and generates the appropriate Anubis policy file for IP address filtering.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Install directly with Go
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go install github.com/TecharoHQ/anubis/utils/cmd/iplist2rule@latest
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Basic conversion from URL:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
iplist2rule https://raw.githubusercontent.com/7c/torfilter/refs/heads/main/lists/txt/torfilter-1m-flat.txt filter-tor.yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
Explicitly allow every IP address on a list:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
iplist2rule --action ALLOW https://raw.githubusercontent.com/7c/torfilter/refs/heads/main/lists/txt/torfilter-1m-flat.txt filter-tor.yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
Add weight to requests matching IP addresses on a list:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
iplist2rule --action WEIGH --weight 20 https://raw.githubusercontent.com/7c/torfilter/refs/heads/main/lists/txt/torfilter-1m-flat.txt filter-tor.yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
## Options
|
||||||
|
|
||||||
|
| Flag | Description | Default |
|
||||||
|
| :------------ | :----------------------------------------------------------------------------------------------- | :-------------------------------- |
|
||||||
|
| `--action` | The Anubis action to take for the IP address in question, must be in ALL CAPS. | `DENY` (forbids traffic) |
|
||||||
|
| `--rule-name` | The name for the generated Anubis rule, should be in kebab-case. | (not set, inferred from filename) |
|
||||||
|
| `--weight` | When `--action=WEIGH`, how many weight points should be added or removed from matching requests? | 0 (not set) |
|
||||||
|
|
||||||
|
## Using the Generated Policy
|
||||||
|
|
||||||
|
Save the output and import it in your main policy file:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
bots:
|
||||||
|
- import: "./filter-tor.yaml"
|
||||||
|
```
|
||||||
@@ -12,6 +12,7 @@ Install directly with Go:
|
|||||||
```bash
|
```bash
|
||||||
go install github.com/TecharoHQ/anubis/cmd/robots2policy@latest
|
go install github.com/TecharoHQ/anubis/cmd/robots2policy@latest
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Basic conversion from URL:
|
Basic conversion from URL:
|
||||||
@@ -35,8 +36,8 @@ robots2policy -input robots.txt -action DENY -format json
|
|||||||
## Options
|
## Options
|
||||||
|
|
||||||
| Flag | Description | Default |
|
| Flag | Description | Default |
|
||||||
|-----------------------|--------------------------------------------------------------------|---------------------|
|
| --------------------- | ------------------------------------------------------------------ | ------------------- |
|
||||||
| `-input` | robots.txt file path or URL (use `-` for stdin) | *required* |
|
| `-input` | robots.txt file path or URL (use `-` for stdin) | _required_ |
|
||||||
| `-output` | Output file (use `-` for stdout) | stdout |
|
| `-output` | Output file (use `-` for stdout) | stdout |
|
||||||
| `-format` | Output format: `yaml` or `json` | `yaml` |
|
| `-format` | Output format: `yaml` or `json` | `yaml` |
|
||||||
| `-action` | Action for disallowed paths: `ALLOW`, `DENY`, `CHALLENGE`, `WEIGH` | `CHALLENGE` |
|
| `-action` | Action for disallowed paths: `ALLOW`, `DENY`, `CHALLENGE`, `WEIGH` | `CHALLENGE` |
|
||||||
@@ -47,6 +48,7 @@ robots2policy -input robots.txt -action DENY -format json
|
|||||||
## Example
|
## Example
|
||||||
|
|
||||||
Input robots.txt:
|
Input robots.txt:
|
||||||
|
|
||||||
```txt
|
```txt
|
||||||
User-agent: *
|
User-agent: *
|
||||||
Disallow: /admin/
|
Disallow: /admin/
|
||||||
@@ -57,6 +59,7 @@ Disallow: /
|
|||||||
```
|
```
|
||||||
|
|
||||||
Generated policy:
|
Generated policy:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- name: robots-txt-policy-disallow-1
|
- name: robots-txt-policy-disallow-1
|
||||||
action: CHALLENGE
|
action: CHALLENGE
|
||||||
@@ -77,8 +80,8 @@ Generated policy:
|
|||||||
Save the output and import it in your main policy file:
|
Save the output and import it in your main policy file:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
import:
|
bots:
|
||||||
- path: "./robots-policy.yaml"
|
- import: "./robots-policy.yaml"
|
||||||
```
|
```
|
||||||
|
|
||||||
The tool handles wildcard patterns, user-agent specific rules, and blacklisted bots automatically.
|
The tool handles wildcard patterns, user-agent specific rules, and blacklisted bots automatically.
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- 3004:3004
|
- 3004:3004
|
||||||
volumes:
|
volumes:
|
||||||
- ../pki/registry.local.cetacean.club:/etc/techaro/pki/registry.local.cetacean.club
|
- ./pki/registry.local.cetacean.club:/etc/techaro/pki/registry.local.cetacean.club
|
||||||
|
|
||||||
anubis:
|
anubis:
|
||||||
image: ko.local/anubis
|
image: ko.local/anubis
|
||||||
|
|||||||
@@ -1,53 +1,56 @@
|
|||||||
REPO_ROOT=$(git rev-parse --show-toplevel)
|
REPO_ROOT=$(git rev-parse --show-toplevel)
|
||||||
(cd $REPO_ROOT && go install ./utils/cmd/...)
|
(cd $REPO_ROOT && go install ./utils/cmd/...)
|
||||||
|
|
||||||
|
mkdir -p pki
|
||||||
|
echo '*' >>./pki/.gitignore
|
||||||
|
|
||||||
function cleanup() {
|
function cleanup() {
|
||||||
set +e
|
set +e
|
||||||
|
|
||||||
pkill -P $$
|
pkill -P $$
|
||||||
|
|
||||||
if [ -f "docker-compose.yaml" ]; then
|
if [ -f "docker-compose.yaml" ]; then
|
||||||
docker compose down -t 1 || :
|
docker compose down -t 1 || :
|
||||||
docker compose rm -f || :
|
docker compose rm -f || :
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
trap cleanup EXIT SIGINT
|
trap cleanup EXIT SIGINT
|
||||||
|
|
||||||
function build_anubis_ko() {
|
function build_anubis_ko() {
|
||||||
(
|
(
|
||||||
cd $REPO_ROOT && npm ci && npm run assets
|
cd $REPO_ROOT && npm ci && npm run assets
|
||||||
)
|
)
|
||||||
(
|
(
|
||||||
cd $REPO_ROOT &&
|
cd $REPO_ROOT &&
|
||||||
VERSION=devel ko build \
|
VERSION=devel ko build \
|
||||||
--platform=all \
|
--platform=all \
|
||||||
--base-import-paths \
|
--base-import-paths \
|
||||||
--tags="latest" \
|
--tags="latest" \
|
||||||
--image-user=1000 \
|
--image-user=1000 \
|
||||||
--image-annotation="" \
|
--image-annotation="" \
|
||||||
--image-label="" \
|
--image-label="" \
|
||||||
./cmd/anubis \
|
./cmd/anubis \
|
||||||
--local
|
--local
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function mint_cert() {
|
function mint_cert() {
|
||||||
if [ "$#" -ne 1 ]; then
|
if [ "$#" -ne 1 ]; then
|
||||||
echo "Usage: mint_cert <domain.name>"
|
echo "Usage: mint_cert <domain.name>"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
domainName="$1"
|
domainName="$1"
|
||||||
|
|
||||||
# If the transient local TLS certificate doesn't exist, mint a new one
|
# If the transient local TLS certificate doesn't exist, mint a new one
|
||||||
if [ ! -f "${REPO_ROOT}/test/pki/${domainName}/cert.pem" ]; then
|
if [ ! -f "./pki/${domainName}/cert.pem" ]; then
|
||||||
# Subshell to contain the directory change
|
# Subshell to contain the directory change
|
||||||
(
|
(
|
||||||
cd ${REPO_ROOT}/test/pki &&
|
cd ./pki &&
|
||||||
mkdir -p "${domainName}" &&
|
mkdir -p "${domainName}" &&
|
||||||
go tool minica -domains "${domainName}" &&
|
go tool minica -domains "${domainName}" &&
|
||||||
cd "${domainName}" &&
|
cd "${domainName}" &&
|
||||||
chmod 666 *
|
chmod 666 *
|
||||||
)
|
)
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,17 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
export VERSION=$GITHUB_COMMIT-test
|
|
||||||
export KO_DOCKER_REPO=ko.local
|
|
||||||
|
|
||||||
source ../lib/lib.sh
|
source ../lib/lib.sh
|
||||||
|
|
||||||
|
export KO_DOCKER_REPO=ko.local
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
build_anubis_ko
|
|
||||||
mint_cert mimi.techaro.lol
|
mint_cert mimi.techaro.lol
|
||||||
|
|
||||||
docker run --rm \
|
docker run --rm \
|
||||||
-v ./conf/nginx:/etc/nginx:ro \
|
-v $PWD/conf/nginx:/etc/nginx:ro \
|
||||||
-v ../pki:/techaro/pki:ro \
|
-v $PWD/pki:/techaro/pki:ro \
|
||||||
nginx \
|
nginx \
|
||||||
nginx -t
|
nginx -t
|
||||||
|
|
||||||
docker compose up -d
|
|
||||||
|
|
||||||
docker compose down -t 1 || :
|
|
||||||
docker compose rm -f || :
|
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ services:
|
|||||||
KEY_FNAME: key.pem
|
KEY_FNAME: key.pem
|
||||||
PROXY_TO: http://anubis:3000
|
PROXY_TO: http://anubis:3000
|
||||||
volumes:
|
volumes:
|
||||||
- ../../pki/relayd:/techaro/pki:ro
|
- ./pki/relayd:/techaro/pki:ro
|
||||||
|
|
||||||
# novnc:
|
# novnc:
|
||||||
# image: geek1011/easy-novnc
|
# image: geek1011/easy-novnc
|
||||||
@@ -42,7 +42,7 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
DISPLAY: display:0
|
DISPLAY: display:0
|
||||||
volumes:
|
volumes:
|
||||||
- ../../pki:/usr/local/share/ca-certificates/minica:ro
|
- ./pki:/usr/local/share/ca-certificates/minica:ro
|
||||||
- ../scripts:/hack/scripts:ro
|
- ../scripts:/hack/scripts:ro
|
||||||
depends_on:
|
depends_on:
|
||||||
- anubis
|
- anubis
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ services:
|
|||||||
KEY_FNAME: key.pem
|
KEY_FNAME: key.pem
|
||||||
PROXY_TO: http://anubis:3000
|
PROXY_TO: http://anubis:3000
|
||||||
volumes:
|
volumes:
|
||||||
- ../../pki/relayd:/techaro/pki:ro
|
- ./pki/relayd:/techaro/pki:ro
|
||||||
|
|
||||||
# novnc:
|
# novnc:
|
||||||
# image: geek1011/easy-novnc
|
# image: geek1011/easy-novnc
|
||||||
@@ -40,5 +40,5 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
DISPLAY: display:0
|
DISPLAY: display:0
|
||||||
volumes:
|
volumes:
|
||||||
- ../../pki:/usr/local/share/ca-certificates/minica:ro
|
- ./pki:/usr/local/share/ca-certificates/minica:ro
|
||||||
- ../scripts:/hack/scripts:ro
|
- ../scripts:/hack/scripts:ro
|
||||||
|
|||||||
57
utils/cmd/iplist2rule/blocklist.go
Normal file
57
utils/cmd/iplist2rule/blocklist.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/netip"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FetchBlocklist reads the blocklist over HTTP and returns every non-commented
|
||||||
|
// line parsed as an IP address in CIDR notation. IPv4 addresses are returned as
|
||||||
|
// /32, IPv6 addresses as /128.
|
||||||
|
//
|
||||||
|
// This function was generated with GLM 4.7.
|
||||||
|
func FetchBlocklist(url string) ([]string, error) {
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("HTTP request failed with status: %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
var lines []string
|
||||||
|
scanner := bufio.NewScanner(resp.Body)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
// Skip empty lines and comments (lines starting with #)
|
||||||
|
if line == "" || strings.HasPrefix(line, "#") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
addr, err := netip.ParseAddr(line)
|
||||||
|
if err != nil {
|
||||||
|
// Skip lines that aren't valid IP addresses
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var cidr string
|
||||||
|
if addr.Is4() {
|
||||||
|
cidr = fmt.Sprintf("%s/32", addr.String())
|
||||||
|
} else {
|
||||||
|
cidr = fmt.Sprintf("%s/128", addr.String())
|
||||||
|
}
|
||||||
|
lines = append(lines, cidr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil && err != io.EOF {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines, nil
|
||||||
|
}
|
||||||
103
utils/cmd/iplist2rule/main.go
Normal file
103
utils/cmd/iplist2rule/main.go
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/TecharoHQ/anubis/lib/config"
|
||||||
|
"github.com/facebookgo/flagenv"
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Rule struct {
|
||||||
|
Name string `yaml:"name" json:"name"`
|
||||||
|
Action config.Rule `yaml:"action" json:"action"`
|
||||||
|
RemoteAddr []string `json:"remote_addresses,omitempty" yaml:"remote_addresses,omitempty"`
|
||||||
|
Weight *config.Weight `json:"weight,omitempty" yaml:"weight,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.Usage = func() {
|
||||||
|
fmt.Printf(`Usage of %[1]s:
|
||||||
|
|
||||||
|
%[1]s [flags] <blocklist-url> <filename>
|
||||||
|
|
||||||
|
Grabs the contents of the blocklist, converts it to an Anubis ruleset, and writes it to filename.
|
||||||
|
|
||||||
|
Flags:
|
||||||
|
`, filepath.Base(os.Args[0]))
|
||||||
|
|
||||||
|
flag.PrintDefaults()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
action = flag.String("action", "DENY", "Anubis action to take (ALLOW / DENY / WEIGH)")
|
||||||
|
manualRuleName = flag.String("rule-name", "", "If set, prefer this name over inferring from filename")
|
||||||
|
weight = flag.Int("weight", 0, "If set to any number, add/subtract this many weight points when --action=WEIGH")
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flagenv.Parse()
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if flag.NArg() != 2 {
|
||||||
|
flag.Usage()
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
blocklistURL := flag.Arg(0)
|
||||||
|
foutName := flag.Arg(1)
|
||||||
|
ruleName := strings.TrimSuffix(foutName, filepath.Ext(foutName))
|
||||||
|
|
||||||
|
if *manualRuleName != "" {
|
||||||
|
ruleName = *manualRuleName
|
||||||
|
}
|
||||||
|
|
||||||
|
ruleAction := config.Rule(*action)
|
||||||
|
if err := ruleAction.Valid(); err != nil {
|
||||||
|
log.Fatalf("--action=%q is invalid: %v", *action, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result := &Rule{
|
||||||
|
Name: ruleName,
|
||||||
|
Action: ruleAction,
|
||||||
|
}
|
||||||
|
|
||||||
|
if *weight != 0 {
|
||||||
|
if ruleAction != config.RuleWeigh {
|
||||||
|
log.Fatalf("used --weight=%d but --action=%s", *weight, *action)
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Weight = &config.Weight{
|
||||||
|
Adjust: *weight,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ips, err := FetchBlocklist(blocklistURL)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("can't fetch blocklist %s: %v", blocklistURL, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result.RemoteAddr = ips
|
||||||
|
|
||||||
|
fout, err := os.Create(foutName)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("can't create output file %q: %v", foutName, err)
|
||||||
|
}
|
||||||
|
defer fout.Close()
|
||||||
|
|
||||||
|
fmt.Fprintf(fout, "# Generated by %s on %s from %s\n\n", filepath.Base(os.Args[0]), time.Now().Format(time.RFC3339), blocklistURL)
|
||||||
|
|
||||||
|
data, err := yaml.Marshal([]*Rule{result})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("can't marshal yaml")
|
||||||
|
}
|
||||||
|
|
||||||
|
fout.Write(data)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user