mondern slopify hand crafted code for the greater good.

(move from user specified blocklist download URLs to simply country codes with multiple providers available.)
This commit is contained in:
2026-05-01 17:18:50 -06:00
parent 746bb9c8b4
commit b1ed8f75ec
10 changed files with 576 additions and 105 deletions

171
main.go
View File

@@ -1,109 +1,122 @@
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
"sync"
"time"
)
type Country struct {
Name string `json:"name"`
Url string `json:"url"`
v4Addresses []string
}
type JsonListFile struct {
Countries []Country `json:"countries"`
}
func main() {
// Validate arguments
if len(os.Args) < 3 {
log.Fatalf("usage: %s <blocklistname> <jsonlistfile>", os.Args[0])
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s <config.json>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nGenerates a RouterOS address list (.rsc) from GeoIP data.\n")
fmt.Fprintf(os.Stderr, "\nExample:\n %s blocklist.json\n", os.Args[0])
os.Exit(1)
}
blockListName := os.Args[1]
jsonListFile := os.Args[2]
// Load blocklist config file
countries, err := readJsonListFile(jsonListFile)
configFile := os.Args[1]
// Load configuration
cfg, err := LoadConfig(configFile)
if err != nil {
log.Fatalf("failed to read jsonlistfile '%s': %v", jsonListFile, err)
log.Fatalf("Error loading config: %v", err)
}
// Download up to date geoip CIDR data
for i := range countries {
fmt.Println("downloading cidr list for country:", countries[i].Name)
countries[i].v4Addresses, err = downloadAddressList(countries[i].Url)
if err != nil {
log.Fatalf("failed to download address list for county'%s': %v", countries[i].Name, err)
}
}
// Generate mikrotik block list
fmt.Printf("generating blocklist %s.rsc\n", blockListName)
err = generateOutput(countries, blockListName)
// Initialize provider
provider, err := NewProvider(cfg.Provider)
if err != nil {
log.Fatalf("failed to generate output file: %v", err)
}
fmt.Printf("\n\nCopy the file the router, then import the address list\n\n\t/import %s.rsc\n", blockListName)
}
func generateOutput(countries []Country, blockListName string) error {
output := "/ip firewall address-list\n"
for _, country := range countries {
for _, v4Address := range country.v4Addresses {
output += fmt.Sprintf("add address=%s comment=\"%s\" list=%s\n", v4Address, country.Name, blockListName)
}
log.Fatalf("Error initializing provider: %v", err)
}
err := os.WriteFile(fmt.Sprintf("%s.rsc", blockListName), []byte(output), os.ModePerm)
if err != nil {
return fmt.Errorf("failed to write file '%s.rsc': %v", blockListName, err)
}
return nil
}
fmt.Printf("╔══════════════════════════════════════════╗\n")
fmt.Printf("║ routeros-geoip generator ║\n")
fmt.Printf("╚══════════════════════════════════════════╝\n")
fmt.Printf(" Provider: %s\n", provider.Name())
fmt.Printf(" List name: %s\n", cfg.ListName)
fmt.Printf(" Countries: %d\n\n", len(cfg.Countries))
func downloadAddressList(url string) ([]string, error) {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("http get error on url '%s': %v", url, err)
// Download CIDR data for all countries concurrently
type result struct {
data CountryData
err error
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected http status - expected '%d' but got '%d'", http.StatusOK, resp.StatusCode)
results := make([]result, len(cfg.Countries))
var wg sync.WaitGroup
start := time.Now()
for i, code := range cfg.Countries {
wg.Add(1)
go func(idx int, countryCode string) {
defer wg.Done()
name := CountryName(countryCode)
fmt.Printf(" ↓ downloading %s (%s)...\n", name, countryCode)
cidrs, err := provider.FetchCIDRs(countryCode)
if err != nil {
results[idx] = result{err: fmt.Errorf("%s (%s): %v", name, countryCode, err)}
return
}
results[idx] = result{
data: CountryData{
Code: countryCode,
Name: name,
CIDRs: cidrs,
},
}
fmt.Printf(" ✓ %s: %d CIDRs\n", name, len(cidrs))
}(i, code)
}
scanner := bufio.NewScanner(resp.Body)
scanner.Split(bufio.ScanLines)
v4Addresses := make([]string, 0)
for scanner.Scan() {
line := scanner.Text()
_, ipnet, err := net.ParseCIDR(line)
if err != nil {
log.Printf("skipping line: failed to parse line '%s' to cidr: %v", line, err)
wg.Wait()
elapsed := time.Since(start)
// Collect results and check for errors
var countries []CountryData
var errors []error
for _, r := range results {
if r.err != nil {
errors = append(errors, r.err)
continue
}
v4Addresses = append(v4Addresses, ipnet.String())
countries = append(countries, r.data)
}
return v4Addresses, nil
}
// reads the json config file containing the lists of countries you want to block
func readJsonListFile(filename string) ([]Country, error) {
b, err := os.ReadFile(filename)
if len(errors) > 0 {
fmt.Printf("\n⚠ Errors occurred:\n")
for _, err := range errors {
fmt.Printf(" ✗ %v\n", err)
}
if len(countries) == 0 {
log.Fatalf("All downloads failed, cannot generate output")
}
fmt.Printf("\nContinuing with %d/%d countries...\n", len(countries), len(cfg.Countries))
}
// Generate output
fmt.Printf("\n Generating output...\n")
outputFile, err := GenerateRSC(countries, cfg.ListName)
if err != nil {
return nil, fmt.Errorf("failed to open file %s: %v", filename, err)
log.Fatalf("Error generating output: %v", err)
}
jsonListFile := JsonListFile{}
err = json.Unmarshal(b, &jsonListFile)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal json file to jsonListFile struct: %v", err)
totalCIDRs := 0
for _, c := range countries {
totalCIDRs += len(c.CIDRs)
}
return jsonListFile.Countries, nil
fmt.Printf("\n╔══════════════════════════════════════════════════════╗\n")
fmt.Printf("║ Done! %-46s║\n", fmt.Sprintf("(%s)", elapsed.Round(time.Millisecond)))
fmt.Printf("╠══════════════════════════════════════════════════════╣\n")
fmt.Printf("║ Output: %-43s║\n", outputFile)
fmt.Printf("║ Entries: %-43s║\n", fmt.Sprintf("%d CIDRs across %d countries", totalCIDRs, len(countries)))
fmt.Printf("╚══════════════════════════════════════════════════════╝\n")
fmt.Printf("\nImport on your MikroTik router:\n")
fmt.Printf(" /import %s\n\n", outputFile)
}