Skip to content

Commit

Permalink
Feat: support MaxMind GeoLite2 ASN CSV data as input
Browse files Browse the repository at this point in the history
  • Loading branch information
Loyalsoldier committed Aug 9, 2024
1 parent 67bcec8 commit cdc2a64
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ These two concepts are notable: `input` and `output`. The `input` is the data so
- **cutter**:用于裁剪前置步骤中的数据
- **v2rayGeoIPDat**:V2Ray GeoIP dat 格式(`geoip.dat`)
- **maxmindMMDB**:MaxMind mmdb 数据格式(`GeoLite2-Country.mmdb`)
- **maxmindGeoLite2ASNCSV**:MaxMind GeoLite2 ASN CSV 数据(`GeoLite2-ASN-CSV.zip`)
- **maxmindGeoLite2CountryCSV**:MaxMind GeoLite2 country CSV 数据(`GeoLite2-Country-CSV.zip`)
- **singboxSRS**:sing-box SRS 格式(`geoip-cn.srs`)
- **clashRuleSetClassical**:[classical 类型的 Clash RuleSet](https://github.com/Dreamacro/clash/wiki/premium-core-features#classical)
Expand Down Expand Up @@ -253,6 +254,7 @@ All available input formats:
- clashRuleSet (Convert ipcidr type of Clash RuleSet to other formats)
- clashRuleSetClassical (Convert classical type of Clash RuleSet to other formats (just processing IP & CIDR lines))
- cutter (Remove data from previous steps)
- maxmindGeoLite2ASNCSV (Convert MaxMind GeoLite2 ASN CSV data to other formats)
- maxmindGeoLite2CountryCSV (Convert MaxMind GeoLite2 country CSV data to other formats)
- maxmindMMDB (Convert MaxMind mmdb database to other formats)
- private (Convert LAN and private network CIDR to other formats)
Expand Down
25 changes: 25 additions & 0 deletions config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@
"wantedList": ["cn", "us"]
}
},
{
"type": "maxmindGeoLite2ASNCSV",
"action": "add",
"args": {
"ipv4": "./geolite2/GeoLite2-ASN-Blocks-IPv4.csv",
"ipv6": "./geolite2/GeoLite2-ASN-Blocks-IPv6.csv",
"wantedList": {
"facebook": ["AS63293", "AS54115", "AS32934"],
"fastly": ["AS54113", "AS394192"]
}
}
},
{
"type": "maxmindGeoLite2ASNCSV",
"action": "add",
"args": {
"ipv4": "./geolite2/GeoLite2-ASN-Blocks-IPv4.csv",
"ipv6": "./geolite2/GeoLite2-ASN-Blocks-IPv6.csv",
"wantedList": {
"facebook": ["AS63293", "AS54115", "AS32934"],
"fastly": ["AS54113", "AS394192"]
},
"onlyIPType": "ipv4"
}
},
{
"type": "maxmindMMDB",
"action": "add"
Expand Down
203 changes: 203 additions & 0 deletions plugin/maxmind/asn_csv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package maxmind

import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/Loyalsoldier/geoip/lib"
)

const (
typeASNCSV = "maxmindGeoLite2ASNCSV"
descASNCSV = "Convert MaxMind GeoLite2 ASN CSV data to other formats"
)

var (
defaultASNIPv4File = filepath.Join("./", "geolite2", "GeoLite2-ASN-Blocks-IPv4.csv")
defaultASNIPv6File = filepath.Join("./", "geolite2", "GeoLite2-ASN-Blocks-IPv6.csv")
)

func init() {
lib.RegisterInputConfigCreator(typeASNCSV, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
return newGeoLite2ASNCSV(action, data)
})
lib.RegisterInputConverter(typeASNCSV, &geoLite2ASNCSV{
Description: descASNCSV,
})
}

func newGeoLite2ASNCSV(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
var tmp struct {
IPv4File string `json:"ipv4"`
IPv6File string `json:"ipv6"`
Want map[string][]string `json:"wantedList"`
OnlyIPType lib.IPType `json:"onlyIPType"`
}

if len(data) > 0 {
if err := json.Unmarshal(data, &tmp); err != nil {
return nil, err
}
}

if tmp.IPv4File == "" {
tmp.IPv4File = defaultASNIPv4File
}

if tmp.IPv6File == "" {
tmp.IPv6File = defaultASNIPv6File
}

// Filter want list
wantList := make(map[string][]string) // map[asn][]listname
for list, asnList := range tmp.Want {
list = strings.ToUpper(strings.TrimSpace(list))
if list == "" {
continue
}

for _, asn := range asnList {
asn = strings.TrimPrefix(strings.ToLower(strings.TrimSpace(asn)), "as")
if asn == "" {
continue
}

if listArr, found := wantList[asn]; found {
listArr = append(listArr, list)
wantList[asn] = listArr
} else {
wantList[asn] = []string{list}
}
}
}

if len(wantList) == 0 {
return nil, fmt.Errorf("❌ [type %s | action %s] wantedList must be specified in config", typeASNCSV, action)
}

return &geoLite2ASNCSV{
Type: typeASNCSV,
Action: action,
Description: descASNCSV,
IPv4File: tmp.IPv4File,
IPv6File: tmp.IPv6File,
Want: wantList,
OnlyIPType: tmp.OnlyIPType,
}, nil
}

type geoLite2ASNCSV struct {
Type string
Action lib.Action
Description string
IPv4File string
IPv6File string
Want map[string][]string
OnlyIPType lib.IPType
}

func (g *geoLite2ASNCSV) GetType() string {
return g.Type
}

func (g *geoLite2ASNCSV) GetAction() lib.Action {
return g.Action
}

func (g *geoLite2ASNCSV) GetDescription() string {
return g.Description
}

func (g *geoLite2ASNCSV) Input(container lib.Container) (lib.Container, error) {
entries := make(map[string]*lib.Entry)

if g.IPv4File != "" {
if err := g.process(g.IPv4File, entries); err != nil {
return nil, err
}
}

if g.IPv6File != "" {
if err := g.process(g.IPv6File, entries); err != nil {
return nil, err
}
}

if len(entries) == 0 {
return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", typeASNCSV, g.Action)
}

var ignoreIPType lib.IgnoreIPOption
switch g.OnlyIPType {
case lib.IPv4:
ignoreIPType = lib.IgnoreIPv6
case lib.IPv6:
ignoreIPType = lib.IgnoreIPv4
}

for _, entry := range entries {
switch g.Action {
case lib.ActionAdd:
if err := container.Add(entry, ignoreIPType); err != nil {
return nil, err
}
case lib.ActionRemove:
if err := container.Remove(entry, lib.CaseRemovePrefix, ignoreIPType); err != nil {
return nil, err
}
default:
return nil, lib.ErrUnknownAction
}
}

return container, nil
}

func (g *geoLite2ASNCSV) process(file string, entries map[string]*lib.Entry) error {
if entries == nil {
entries = make(map[string]*lib.Entry)
}

fReader, err := os.Open(file)
if err != nil {
return err
}
defer fReader.Close()

reader := csv.NewReader(fReader)
reader.Read() // skip header

for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return err
}

if len(record) < 2 {
return fmt.Errorf("❌ [type %s | action %s] invalid record: %v", typeASNCSV, g.Action, record)
}

if listArr, found := g.Want[strings.TrimSpace(record[1])]; found {
for _, listName := range listArr {
entry, got := entries[listName]
if !got {
entry = lib.NewEntry(listName)
}
if err := entry.AddPrefix(strings.TrimSpace(record[0])); err != nil {
return err
}
entries[listName] = entry
}
}
}

return nil
}
10 changes: 5 additions & 5 deletions plugin/maxmind/country_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const (
)

var (
defaultCCFile = filepath.Join("./", "geolite2", "GeoLite2-Country-Locations-en.csv")
defaultIPv4File = filepath.Join("./", "geolite2", "GeoLite2-Country-Blocks-IPv4.csv")
defaultIPv6File = filepath.Join("./", "geolite2", "GeoLite2-Country-Blocks-IPv6.csv")
defaultCCFile = filepath.Join("./", "geolite2", "GeoLite2-Country-Locations-en.csv")
defaultCountryIPv4File = filepath.Join("./", "geolite2", "GeoLite2-Country-Blocks-IPv4.csv")
defaultCountryIPv6File = filepath.Join("./", "geolite2", "GeoLite2-Country-Blocks-IPv6.csv")
)

func init() {
Expand Down Expand Up @@ -52,11 +52,11 @@ func newGeoLite2CountryCSV(action lib.Action, data json.RawMessage) (lib.InputCo
}

if tmp.IPv4File == "" {
tmp.IPv4File = defaultIPv4File
tmp.IPv4File = defaultCountryIPv4File
}

if tmp.IPv6File == "" {
tmp.IPv6File = defaultIPv6File
tmp.IPv6File = defaultCountryIPv6File
}

return &geoLite2CountryCSV{
Expand Down

0 comments on commit cdc2a64

Please sign in to comment.