Added a golang code example and also domans.list for archive

This commit is contained in:
Eliezer Croitoru 2024-07-08 10:55:29 +03:00
parent 4bb5e83edb
commit fc0e0045b5
4 changed files with 146 additions and 0 deletions

28
convert-to-mt-address-list.rb Executable file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env ruby
filename = ARGV[0]
addressList = ARGV[1]
if filename.nil? or filename.empty?
STDERR.puts("Missing list filename")
exit 1
end
if addressList.nil? or addressList !~ /^[a-zA-Z\-\_0-9]+$/
STDERR.puts("Missing or wrong address list name")
exit 2
end
lines = File.open(filename).readlines
template = "/ip firewall address-list"
lines.each do |cidr|
l = cidr.strip
next if l.empty?
next if l[0] == "#"
template = "#{template}\nadd address=#{l} list=#{addressList}"
end
puts template

3
domains.list Normal file
View File

@ -0,0 +1,3 @@
gmail.com
outlook.com
monday.com

BIN
spf-to-ip4-list Executable file

Binary file not shown.

115
spf-to-ip4-list.go Normal file
View File

@ -0,0 +1,115 @@
package main
import (
"fmt"
"net"
"os"
"regexp"
"strings"
)
func getSPFRecord(domain string) (string, error) {
txtRecords, err := net.LookupTXT(domain)
if err != nil {
return "", fmt.Errorf("error looking up TXT records for %s: %v", domain, err)
}
for _, record := range txtRecords {
if strings.HasPrefix(record, "v=spf1") {
return record, nil
}
}
return "", fmt.Errorf("no SPF record found for %s", domain)
}
func collectSpfRecordDetails(domain string) (map[string][]string, error) {
spfRecord, err := getSPFRecord(domain)
if err != nil {
return nil, err
}
result := make(map[string][]string)
for key, regexStr := range map[string]string{
"ipv4": `ip4:([\d\./]+)`,
"ipv6": `ip6:([a-f0-9\:\/]+)`,
"includes": `include\:([a-z\.\-\_0-9]+)`,
"redirects": `redirect\=([a-z\.\-\_0-9]+)`,
"a": `a\:([a-z\.\-\_0-9]+)`,
} {
re, err := regexp.Compile(regexStr)
if err != nil {
return nil, fmt.Errorf("error compiling regex for %s: %v", key, err)
}
matches := re.FindAllStringSubmatch(spfRecord, -1)
for _, match := range matches {
result[key] = append(result[key], match[1]) // Extract the captured group
}
}
return result, nil
}
func collectAllItemsRecursively(domain, itemType string) ([]string, error) {
items, err := collectSpfRecordDetails(domain)
if err != nil {
return nil, err
}
allItems := items[itemType]
for _, item := range items[itemType] {
subItems, err := collectAllItemsRecursively(item, itemType)
if err != nil {
return nil, err
}
allItems = append(allItems, subItems...)
}
return allItems, nil
}
func getAllSpfIPs(domain, ipVersion string) ([]string, error) {
redirects, err := collectAllItemsRecursively(domain, "redirects")
if err != nil {
return nil, err
}
includes, err := collectAllItemsRecursively(domain, "includes")
if err != nil {
return nil, err
}
items, err := collectSpfRecordDetails(domain)
if err != nil {
return nil, err
}
allIPs := items[ipVersion]
for _, item := range append(redirects, includes...) {
subIPs, err := getAllSpfIPs(item, ipVersion)
if err != nil {
return nil, err
}
allIPs = append(allIPs, subIPs...)
}
return allIPs, nil
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: spf_ips <domain>")
return
}
domain := os.Args[1]
ipv4s, err := getAllSpfIPs(domain, "ipv4")
if err != nil {
fmt.Println("Error:", err)
return
}
for _, ip := range ipv4s {
fmt.Println(ip)
}
}