Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
869969bb78 |
@@ -1,13 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set -e -o pipefail
|
|
||||||
|
|
||||||
cd rule-set
|
|
||||||
git init
|
|
||||||
git config --local user.email "github-action@users.noreply.github.com"
|
|
||||||
git config --local user.name "GitHub Action"
|
|
||||||
git remote add origin https://github-action:$GITHUB_TOKEN@github.com/SagerNet/sing-geoip.git
|
|
||||||
git branch -M rule-set
|
|
||||||
git add .
|
|
||||||
git commit -m "Update rule-set"
|
|
||||||
git push -f origin rule-set
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
PROJECTS=$(dirname "$0")/../..
|
|
||||||
|
|
||||||
go get -x github.com/sagernet/sing-box@$(git -C $PROJECTS/sing-box rev-parse HEAD)
|
|
||||||
go mod tidy
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
name: Build
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
name: Build
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
- name: Get latest go version
|
|
||||||
id: version
|
|
||||||
run: |
|
|
||||||
echo ::set-output name=go_version::$(curl -s https://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json | grep -oE '"version": "[0-9]{1}.[0-9]{1,}(.[0-9]{1,})?"' | head -1 | cut -d':' -f2 | sed 's/ //g; s/"//g')
|
|
||||||
- name: Setup Go
|
|
||||||
uses: actions/setup-go@v2
|
|
||||||
with:
|
|
||||||
go-version: ${{ steps.version.outputs.go_version }}
|
|
||||||
- name: Build geoip
|
|
||||||
id: build
|
|
||||||
env:
|
|
||||||
NO_SKIP: true
|
|
||||||
run: go run -v .
|
|
||||||
- name: Upload artifacts
|
|
||||||
uses: actions/upload-artifact@v2
|
|
||||||
with:
|
|
||||||
name: geoip.db
|
|
||||||
path: geoip.db
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
name: Release
|
|
||||||
on:
|
|
||||||
workflow_dispatch:
|
|
||||||
schedule:
|
|
||||||
- cron: "0 8 12 * *"
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
name: Build
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
- name: Get latest go version
|
|
||||||
id: version
|
|
||||||
run: |
|
|
||||||
echo ::set-output name=go_version::$(curl -s https://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json | grep -oE '"version": "[0-9]{1}.[0-9]{1,}(.[0-9]{1,})?"' | head -1 | cut -d':' -f2 | sed 's/ //g; s/"//g')
|
|
||||||
- name: Setup Go
|
|
||||||
uses: actions/setup-go@v2
|
|
||||||
with:
|
|
||||||
go-version: ${{ steps.version.outputs.go_version }}
|
|
||||||
- name: Build geoip
|
|
||||||
id: build
|
|
||||||
run: go run -v .
|
|
||||||
- name: Release rule sets
|
|
||||||
if: steps.build.outputs.skip != 'true'
|
|
||||||
run: .github/release-rule-set.sh
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
- name: Generate sha256 hash
|
|
||||||
if: steps.build.outputs.skip != 'true'
|
|
||||||
run: |
|
|
||||||
sha256sum geoip.db > geoip.db.sha256sum
|
|
||||||
- uses: dev-drprasad/delete-older-releases@v0.3.2
|
|
||||||
with:
|
|
||||||
keep_latest: 10
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
- name: Release geoip
|
|
||||||
if: steps.build.outputs.skip != 'true'
|
|
||||||
uses: softprops/action-gh-release@v1
|
|
||||||
with:
|
|
||||||
tag_name: ${{ steps.build.outputs.tag }}
|
|
||||||
files: |
|
|
||||||
geoip.db
|
|
||||||
geoip.db.sha256sum
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
/.idea/
|
|
||||||
/vendor/
|
|
||||||
/*.db
|
|
||||||
*.mmdb
|
|
||||||
/rule-set/
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
linters:
|
|
||||||
disable-all: true
|
|
||||||
enable:
|
|
||||||
- gofumpt
|
|
||||||
- govet
|
|
||||||
- gci
|
|
||||||
- staticcheck
|
|
||||||
|
|
||||||
linters-settings:
|
|
||||||
gci:
|
|
||||||
custom-order: true
|
|
||||||
sections:
|
|
||||||
- standard
|
|
||||||
- prefix(github.com/sagernet/)
|
|
||||||
- default
|
|
||||||
staticcheck:
|
|
||||||
go: '1.20'
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
Copyright (C) 2022 by nekohasekai <contact-sagernet@sekai.icu>
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
fmt:
|
|
||||||
@gofumpt -l -w .
|
|
||||||
@gofmt -s -w .
|
|
||||||
@gci write --custom-order -s standard -s "prefix(github.com/sagernet/)" -s "default" .
|
|
||||||
|
|
||||||
fmt_install:
|
|
||||||
go install -v mvdan.cc/gofumpt@latest
|
|
||||||
go install -v github.com/daixiang0/gci@latest
|
|
||||||
|
|
||||||
lint:
|
|
||||||
golangci-lint run ./...
|
|
||||||
|
|
||||||
lint_install:
|
|
||||||
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
|
||||||
|
|
||||||
test:
|
|
||||||
go test -v ./...
|
|
||||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 207 KiB |
@@ -0,0 +1 @@
|
|||||||
|
464da4039de5c8622d21e9f2938a36d80bfed33fcfbe1293ba74eef94b9fb49e geoip-cn.db
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
71484cf35bb48453e26bcc3373a0988a2536588f8e3ca96cda59ff742af6c392 geoip.db
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
module github.com/sagernet/sing-geoip
|
|
||||||
|
|
||||||
go 1.18
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/google/go-github/v45 v45.2.0
|
|
||||||
github.com/maxmind/mmdbwriter v1.0.0
|
|
||||||
github.com/oschwald/geoip2-golang v1.9.0
|
|
||||||
github.com/oschwald/maxminddb-golang v1.12.0
|
|
||||||
github.com/sagernet/sing v0.2.18-0.20231129075305-eb56a60214be
|
|
||||||
github.com/sagernet/sing-box v1.6.8-0.20231129123339-5a56487cf544
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/google/go-cmp v0.5.9 // indirect
|
|
||||||
github.com/google/go-querystring v1.1.0 // indirect
|
|
||||||
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
|
|
||||||
github.com/miekg/dns v1.1.57 // indirect
|
|
||||||
github.com/sagernet/sing-dns v0.1.11 // indirect
|
|
||||||
go4.org/netipx v0.0.0-20230824141953-6213f710f925 // indirect
|
|
||||||
golang.org/x/crypto v0.15.0 // indirect
|
|
||||||
golang.org/x/mod v0.14.0 // indirect
|
|
||||||
golang.org/x/net v0.18.0 // indirect
|
|
||||||
golang.org/x/sys v0.14.0 // indirect
|
|
||||||
golang.org/x/tools v0.15.0 // indirect
|
|
||||||
)
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
|
||||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
|
||||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
|
||||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
|
||||||
github.com/google/go-github/v45 v45.2.0 h1:5oRLszbrkvxDDqBCNj2hjDZMKmvexaZ1xw/FCD+K3FI=
|
|
||||||
github.com/google/go-github/v45 v45.2.0/go.mod h1:FObaZJEDSTa/WGCzZ2Z3eoCDXWJKMenWWTrd8jrta28=
|
|
||||||
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
|
|
||||||
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
|
|
||||||
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
|
|
||||||
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
|
|
||||||
github.com/maxmind/mmdbwriter v1.0.0 h1:bieL4P6yaYaHvbtLSwnKtEvScUKKD6jcKaLiTM3WSMw=
|
|
||||||
github.com/maxmind/mmdbwriter v1.0.0/go.mod h1:noBMCUtyN5PUQ4H8ikkOvGSHhzhLok51fON2hcrpKj8=
|
|
||||||
github.com/miekg/dns v1.1.57 h1:Jzi7ApEIzwEPLHWRcafCN9LZSBbqQpxjt/wpgvg7wcM=
|
|
||||||
github.com/miekg/dns v1.1.57/go.mod h1:uqRjCRUuEAA6qsOiJvDd+CFo/vW+y5WR6SNmHE55hZk=
|
|
||||||
github.com/oschwald/geoip2-golang v1.9.0 h1:uvD3O6fXAXs+usU+UGExshpdP13GAqp4GBrzN7IgKZc=
|
|
||||||
github.com/oschwald/geoip2-golang v1.9.0/go.mod h1:BHK6TvDyATVQhKNbQBdrj9eAvuwOMi2zSFXizL3K81Y=
|
|
||||||
github.com/oschwald/maxminddb-golang v1.12.0 h1:9FnTOD0YOhP7DGxGsq4glzpGy5+w7pq50AS6wALUMYs=
|
|
||||||
github.com/oschwald/maxminddb-golang v1.12.0/go.mod h1:q0Nob5lTCqyQ8WT6FYgS1L7PXKVVbgiymefNwIjPzgY=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
|
||||||
github.com/sagernet/sing v0.2.18-0.20231129075305-eb56a60214be h1:FigAM9kq7RRXmHvgn8w2a8tqCY5CMV5GIk0id84dI0o=
|
|
||||||
github.com/sagernet/sing v0.2.18-0.20231129075305-eb56a60214be/go.mod h1:OL6k2F0vHmEzXz2KW19qQzu172FDgSbUSODylighuVo=
|
|
||||||
github.com/sagernet/sing-box v1.6.8-0.20231129123339-5a56487cf544 h1:kHe9kQpKMEmGJbTdDntXA0rl1BdQWQZtkDaxmZkAPC4=
|
|
||||||
github.com/sagernet/sing-box v1.6.8-0.20231129123339-5a56487cf544/go.mod h1:C5Gcyr9BsHs1Iq7eAY53hAlh/j+5fjJVsLA18z/7ZL4=
|
|
||||||
github.com/sagernet/sing-dns v0.1.11 h1:PPrMCVVrAeR3f5X23I+cmvacXJ+kzuyAsBiWyUKhGSE=
|
|
||||||
github.com/sagernet/sing-dns v0.1.11/go.mod h1:zJ/YjnYB61SYE+ubMcMqVdpaSvsyQ2iShQGO3vuLvvE=
|
|
||||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
|
||||||
go4.org/netipx v0.0.0-20230824141953-6213f710f925 h1:eeQDDVKFkx0g4Hyy8pHgmZaK0EqB4SD6rvKbUdN3ziQ=
|
|
||||||
go4.org/netipx v0.0.0-20230824141953-6213f710f925/go.mod h1:PLyyIXexvUFg3Owu6p/WfdlivPbZJsZdgWZlrGope/Y=
|
|
||||||
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
|
|
||||||
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
|
|
||||||
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
|
|
||||||
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
|
||||||
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
|
|
||||||
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
|
|
||||||
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
|
|
||||||
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
|
|
||||||
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|
||||||
golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8=
|
|
||||||
golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
||||||
@@ -1,245 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/google/go-github/v45/github"
|
|
||||||
"github.com/maxmind/mmdbwriter"
|
|
||||||
"github.com/maxmind/mmdbwriter/inserter"
|
|
||||||
"github.com/maxmind/mmdbwriter/mmdbtype"
|
|
||||||
"github.com/oschwald/geoip2-golang"
|
|
||||||
"github.com/oschwald/maxminddb-golang"
|
|
||||||
"github.com/sagernet/sing-box/common/srs"
|
|
||||||
C "github.com/sagernet/sing-box/constant"
|
|
||||||
"github.com/sagernet/sing-box/log"
|
|
||||||
"github.com/sagernet/sing-box/option"
|
|
||||||
"github.com/sagernet/sing/common"
|
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
|
||||||
)
|
|
||||||
|
|
||||||
var githubClient *github.Client
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
accessToken, loaded := os.LookupEnv("ACCESS_TOKEN")
|
|
||||||
if !loaded {
|
|
||||||
githubClient = github.NewClient(nil)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
transport := &github.BasicAuthTransport{
|
|
||||||
Username: accessToken,
|
|
||||||
}
|
|
||||||
githubClient = github.NewClient(transport.Client())
|
|
||||||
}
|
|
||||||
|
|
||||||
func fetch(from string) (*github.RepositoryRelease, error) {
|
|
||||||
names := strings.SplitN(from, "/", 2)
|
|
||||||
latestRelease, _, err := githubClient.Repositories.GetLatestRelease(context.Background(), names[0], names[1])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return latestRelease, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func get(downloadURL *string) ([]byte, error) {
|
|
||||||
log.Info("download ", *downloadURL)
|
|
||||||
response, err := http.Get(*downloadURL)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer response.Body.Close()
|
|
||||||
return io.ReadAll(response.Body)
|
|
||||||
}
|
|
||||||
|
|
||||||
func download(release *github.RepositoryRelease) ([]byte, error) {
|
|
||||||
geoipAsset := common.Find(release.Assets, func(it *github.ReleaseAsset) bool {
|
|
||||||
return *it.Name == "Country.mmdb"
|
|
||||||
})
|
|
||||||
if geoipAsset == nil {
|
|
||||||
return nil, E.New("Country.mmdb not found in upstream release ", release.Name)
|
|
||||||
}
|
|
||||||
return get(geoipAsset.BrowserDownloadURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
func parse(binary []byte) (metadata maxminddb.Metadata, countryMap map[string][]*net.IPNet, err error) {
|
|
||||||
database, err := maxminddb.FromBytes(binary)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
metadata = database.Metadata
|
|
||||||
networks := database.Networks(maxminddb.SkipAliasedNetworks)
|
|
||||||
countryMap = make(map[string][]*net.IPNet)
|
|
||||||
var country geoip2.Enterprise
|
|
||||||
var ipNet *net.IPNet
|
|
||||||
for networks.Next() {
|
|
||||||
ipNet, err = networks.Network(&country)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var code string
|
|
||||||
if country.Country.IsoCode != "" {
|
|
||||||
code = strings.ToLower(country.Country.IsoCode)
|
|
||||||
} else if country.RegisteredCountry.IsoCode != "" {
|
|
||||||
code = strings.ToLower(country.RegisteredCountry.IsoCode)
|
|
||||||
} else if country.RepresentedCountry.IsoCode != "" {
|
|
||||||
code = strings.ToLower(country.RepresentedCountry.IsoCode)
|
|
||||||
} else if country.Continent.Code != "" {
|
|
||||||
code = strings.ToLower(country.Continent.Code)
|
|
||||||
} else {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
countryMap[code] = append(countryMap[code], ipNet)
|
|
||||||
}
|
|
||||||
err = networks.Err()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func newWriter(metadata maxminddb.Metadata, codes []string) (*mmdbwriter.Tree, error) {
|
|
||||||
return mmdbwriter.New(mmdbwriter.Options{
|
|
||||||
DatabaseType: "sing-geoip",
|
|
||||||
Languages: codes,
|
|
||||||
IPVersion: int(metadata.IPVersion),
|
|
||||||
RecordSize: int(metadata.RecordSize),
|
|
||||||
Inserter: inserter.ReplaceWith,
|
|
||||||
DisableIPv4Aliasing: true,
|
|
||||||
IncludeReservedNetworks: true,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func open(path string, codes []string) (*mmdbwriter.Tree, error) {
|
|
||||||
reader, err := maxminddb.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if reader.Metadata.DatabaseType != "sing-geoip" {
|
|
||||||
return nil, E.New("invalid sing-geoip database")
|
|
||||||
}
|
|
||||||
reader.Close()
|
|
||||||
|
|
||||||
return mmdbwriter.Load(path, mmdbwriter.Options{
|
|
||||||
Languages: append(reader.Metadata.Languages, common.Filter(codes, func(it string) bool {
|
|
||||||
return !common.Contains(reader.Metadata.Languages, it)
|
|
||||||
})...),
|
|
||||||
Inserter: inserter.ReplaceWith,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func write(writer *mmdbwriter.Tree, dataMap map[string][]*net.IPNet, output string, codes []string) error {
|
|
||||||
if len(codes) == 0 {
|
|
||||||
codes = make([]string, 0, len(dataMap))
|
|
||||||
for code := range dataMap {
|
|
||||||
codes = append(codes, code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Strings(codes)
|
|
||||||
codeMap := make(map[string]bool)
|
|
||||||
for _, code := range codes {
|
|
||||||
codeMap[code] = true
|
|
||||||
}
|
|
||||||
for code, data := range dataMap {
|
|
||||||
if !codeMap[code] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for _, item := range data {
|
|
||||||
err := writer.Insert(item, mmdbtype.String(code))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
outputFile, err := os.Create(output)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer outputFile.Close()
|
|
||||||
_, err = writer.WriteTo(outputFile)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func release(source string, destination string, output string, ruleSetOutput string) error {
|
|
||||||
sourceRelease, err := fetch(source)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
destinationRelease, err := fetch(destination)
|
|
||||||
if err != nil {
|
|
||||||
log.Warn("missing destination latest release")
|
|
||||||
} else {
|
|
||||||
if os.Getenv("NO_SKIP") != "true" && strings.Contains(*destinationRelease.Name, *sourceRelease.Name) {
|
|
||||||
log.Info("already latest")
|
|
||||||
setActionOutput("skip", "true")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
binary, err := download(sourceRelease)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
metadata, countryMap, err := parse(binary)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
allCodes := make([]string, 0, len(countryMap))
|
|
||||||
for code := range countryMap {
|
|
||||||
allCodes = append(allCodes, code)
|
|
||||||
}
|
|
||||||
writer, err := newWriter(metadata, allCodes)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = write(writer, countryMap, output, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
os.RemoveAll(ruleSetOutput)
|
|
||||||
err = os.MkdirAll(ruleSetOutput, 0o755)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for countryCode, ipNets := range countryMap {
|
|
||||||
var headlessRule option.DefaultHeadlessRule
|
|
||||||
headlessRule.IPCIDR = make([]string, 0, len(ipNets))
|
|
||||||
for _, cidr := range ipNets {
|
|
||||||
headlessRule.IPCIDR = append(headlessRule.IPCIDR, cidr.String())
|
|
||||||
}
|
|
||||||
var plainRuleSet option.PlainRuleSet
|
|
||||||
plainRuleSet.Rules = []option.HeadlessRule{
|
|
||||||
{
|
|
||||||
Type: C.RuleTypeDefault,
|
|
||||||
DefaultOptions: headlessRule,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
srsPath, _ := filepath.Abs(filepath.Join(ruleSetOutput, "geoip-"+countryCode+".srs"))
|
|
||||||
os.Stderr.WriteString("write " + srsPath + "\n")
|
|
||||||
outputRuleSet, err := os.Create(srsPath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = srs.Write(outputRuleSet, plainRuleSet)
|
|
||||||
if err != nil {
|
|
||||||
outputRuleSet.Close()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
outputRuleSet.Close()
|
|
||||||
}
|
|
||||||
setActionOutput("tag", *sourceRelease.Name)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func setActionOutput(name string, content string) {
|
|
||||||
os.Stdout.WriteString("::set-output name=" + name + "::" + content + "\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
err := release("Dreamacro/maxmind-geoip", "sagernet/sing-geoip", "geoip.db", "rule-set")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user