Import Anubis v1.28.2
Replace the tree with upstream TecharoHQ/anubis v1.28.2 (a1384fbf42e3). Masyunis customizations are re-applied in the following commits.
This commit is contained in:
1 parent
eb3e46d8d0
commit
e07de898cd
573 files changed
+35846
-6807
No files matched your search
@@ -7,26 +7,19 @@ import (
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"strings"
|
||||
|
||||
"github.com/TecharoHQ/anubis/internal/iplist"
|
||||
)
|
||||
|
||||
// FetchBlocklist reads the blocklist over HTTP and returns every non-commented
|
||||
// ParseBlocklist reads a plain-text blocklist 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()
|
||||
func ParseBlocklist(list io.Reader) ([]string, error) {
|
||||
var addrs []string
|
||||
|
||||
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)
|
||||
scanner := bufio.NewScanner(list)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
// Skip empty lines and comments (lines starting with #)
|
||||
@@ -46,12 +39,52 @@ func FetchBlocklist(url string) ([]string, error) {
|
||||
} else {
|
||||
cidr = fmt.Sprintf("%s/128", addr.String())
|
||||
}
|
||||
lines = append(lines, cidr)
|
||||
addrs = append(addrs, cidr)
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil && err != io.EOF {
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return lines, nil
|
||||
return addrs, nil
|
||||
}
|
||||
|
||||
// FetchBlocklist fetches url over HTTP and parses the response body as a
|
||||
// blocklist. JSON responses (detected via the Content-Type header or a
|
||||
// ".json" URL suffix) are parsed with ParsePrefixList; everything else is
|
||||
// treated as a plain-text list and parsed with ParseBlocklist.
|
||||
func FetchBlocklist(url string) ([]string, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() //nolint:errcheck
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("HTTP request failed with status: %s", resp.Status)
|
||||
}
|
||||
|
||||
if isJSONBlocklist(url, resp.Header.Get("Content-Type")) {
|
||||
return ParsePrefixList(resp.Body)
|
||||
}
|
||||
|
||||
return ParseBlocklist(resp.Body)
|
||||
}
|
||||
|
||||
func isJSONBlocklist(url, contentType string) bool {
|
||||
if contentType == "json" {
|
||||
return true
|
||||
}
|
||||
|
||||
return strings.HasSuffix(strings.ToLower(url), ".json")
|
||||
}
|
||||
|
||||
// ParsePrefixList decodes a JSON document read from list in the Google/OpenAI
|
||||
// bot IP range format and returns every IPv4 and IPv6 prefix it contains.
|
||||
func ParsePrefixList(list io.Reader) ([]string, error) {
|
||||
pl, err := iplist.Parse(list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pl.CIDRs(), nil
|
||||
}
|
||||
@@ -90,14 +90,21 @@ func main() {
|
||||
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)
|
||||
if _, err := fmt.Fprintf(fout, "# Generated by %s on %s from %s\n\n", filepath.Base(os.Args[0]), time.Now().Format(time.RFC3339), blocklistURL); err != nil {
|
||||
log.Fatalf("can't write signature line: %v", err)
|
||||
}
|
||||
|
||||
data, err := yaml.Marshal([]*Rule{result})
|
||||
if err != nil {
|
||||
log.Fatalf("can't marshal yaml")
|
||||
log.Fatalf("can't marshal yaml: %v", err)
|
||||
}
|
||||
|
||||
fout.Write(data)
|
||||
if _, err := fout.Write(data); err != nil {
|
||||
log.Fatalf("can't write data: %v", err)
|
||||
}
|
||||
|
||||
if err := fout.Close(); err != nil {
|
||||
log.Fatalf("can't close file: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user