sing-geoip: add rule-set

Update pkgs/data/misc/sing-geoip/default.nix

Co-authored-by: Aleksana <alexander.huang.y@gmail.com>
This commit is contained in:
linsui 2024-04-02 12:53:47 +08:00
parent c392dd7997
commit 3536f16682
2 changed files with 80 additions and 6 deletions

View file

@ -8,26 +8,32 @@
let
generator = buildGoModule rec {
pname = "sing-geoip";
version = "20230512";
version = "20240312";
src = fetchFromGitHub {
owner = "SagerNet";
repo = pname;
rev = "refs/tags/${version}";
hash = "sha256-Zm+5N/37hoHpH/TLNJrHeaBXI8G1jEpM1jz6Um8edNE=";
hash = "sha256-nIrbiECK25GyuPEFqMvPdZUShC2JC1NI60Y10SsoWyY=";
};
vendorHash = "sha256-ejXAdsJwXhqet+Ca+pDLWwu0gex79VcIxW6rmhRnbTQ=";
vendorHash = "sha256-WH0eMg06qCiVcy4H+vBtYrmLMA2KJRCPGXiEnatW+LU=";
postPatch = ''
sed -i -e '/func main()/,/^}/d' main.go
cat ${./main.go} >> main.go
'';
meta = with lib; {
description = "GeoIP data for sing-box";
homepage = "https://github.com/SagerNet/sing-geoip";
license = licenses.gpl3Plus;
maintainers = with maintainers; [ linsui ];
mainProgram = "sing-geoip";
};
};
in
stdenvNoCC.mkDerivation rec {
stdenvNoCC.mkDerivation {
inherit (generator) pname;
inherit (dbip-country-lite) version;
@ -38,8 +44,7 @@ stdenvNoCC.mkDerivation rec {
buildPhase = ''
runHook preBuild
${pname} ${dbip-country-lite.mmdb} geoip.db
${pname} ${dbip-country-lite.mmdb} geoip-cn.db cn
sing-geoip ${dbip-country-lite.mmdb}
runHook postBuild
'';
@ -49,6 +54,7 @@ stdenvNoCC.mkDerivation rec {
install -Dm644 geoip.db $out/share/sing-box/geoip.db
install -Dm644 geoip-cn.db $out/share/sing-box/geoip-cn.db
install -Dm644 rule-set/* -t $out/share/sing-box/rule-set
runHook postInstall
'';

View file

@ -0,0 +1,68 @@
func main() {
var err error
input := os.Args[1]
ruleSetOutput := "rule-set"
binary, err := os.ReadFile(input)
if err != nil {
panic(err)
}
metadata, countryMap, err := parse(binary)
if err != nil {
panic(err)
}
allCodes := make([]string, 0, len(countryMap))
for code := range countryMap {
allCodes = append(allCodes, code)
}
writer, err := newWriter(metadata, allCodes)
if err != nil {
panic(err)
}
err = write(writer, countryMap, "geoip.db", nil)
if err != nil {
panic(err)
}
writer, err = newWriter(metadata, []string{"cn"})
if err != nil {
panic(err)
}
err = write(writer, countryMap, "geoip-cn.db", []string{"cn"})
if err != nil {
panic(err)
}
err = os.MkdirAll(ruleSetOutput, 0o755)
if err != nil {
panic(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 {
panic(err)
}
err = srs.Write(outputRuleSet, plainRuleSet)
if err != nil {
outputRuleSet.Close()
panic(err)
}
outputRuleSet.Close()
}
}