-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
927865e
commit 733c14d
Showing
57 changed files
with
2,729 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/.idea/ | ||
/vendor/ | ||
/*.json | ||
/*.srs | ||
/*.db | ||
/site/ | ||
/bin/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/sagernet/sing-box/log" | ||
E "github.com/sagernet/sing/common/exceptions" | ||
|
||
"github.com/oschwald/maxminddb-golang" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
geoipReader *maxminddb.Reader | ||
commandGeoIPFlagFile string | ||
) | ||
|
||
var commandGeoip = &cobra.Command{ | ||
Use: "geoip", | ||
Short: "GeoIP tools", | ||
PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||
err := geoipPreRun() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
commandGeoip.PersistentFlags().StringVarP(&commandGeoIPFlagFile, "file", "f", "geoip.db", "geoip file") | ||
mainCommand.AddCommand(commandGeoip) | ||
} | ||
|
||
func geoipPreRun() error { | ||
reader, err := maxminddb.Open(commandGeoIPFlagFile) | ||
if err != nil { | ||
return err | ||
} | ||
if reader.Metadata.DatabaseType != "sing-geoip" { | ||
reader.Close() | ||
return E.New("incorrect database type, expected sing-geoip, got ", reader.Metadata.DatabaseType) | ||
} | ||
geoipReader = reader | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"net" | ||
"os" | ||
"strings" | ||
|
||
"github.com/sagernet/sing-box/common/json" | ||
C "github.com/sagernet/sing-box/constant" | ||
"github.com/sagernet/sing-box/log" | ||
"github.com/sagernet/sing-box/option" | ||
E "github.com/sagernet/sing/common/exceptions" | ||
|
||
"github.com/oschwald/maxminddb-golang" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var flagGeoipExportOutput string | ||
|
||
const flagGeoipExportDefaultOutput = "geoip-<country>.srs" | ||
|
||
var commandGeoipExport = &cobra.Command{ | ||
Use: "export <country>", | ||
Short: "Export geoip country as rule-set", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := geoipExport(args[0]) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
commandGeoipExport.Flags().StringVarP(&flagGeoipExportOutput, "output", "o", flagGeoipExportDefaultOutput, "Output path") | ||
commandGeoip.AddCommand(commandGeoipExport) | ||
} | ||
|
||
func geoipExport(countryCode string) error { | ||
networks := geoipReader.Networks(maxminddb.SkipAliasedNetworks) | ||
countryMap := make(map[string][]*net.IPNet) | ||
var ( | ||
ipNet *net.IPNet | ||
nextCountryCode string | ||
err error | ||
) | ||
for networks.Next() { | ||
ipNet, err = networks.Network(&nextCountryCode) | ||
if err != nil { | ||
return err | ||
} | ||
countryMap[nextCountryCode] = append(countryMap[nextCountryCode], ipNet) | ||
} | ||
ipNets := countryMap[strings.ToLower(countryCode)] | ||
if len(ipNets) == 0 { | ||
return E.New("country code not found: ", countryCode) | ||
} | ||
|
||
var ( | ||
outputFile *os.File | ||
outputWriter io.Writer | ||
) | ||
if flagGeoipExportOutput == "stdout" { | ||
outputWriter = os.Stdout | ||
} else if flagGeoipExportOutput == flagGeoipExportDefaultOutput { | ||
outputFile, err = os.Create("geoip-" + countryCode + ".json") | ||
if err != nil { | ||
return err | ||
} | ||
defer outputFile.Close() | ||
outputWriter = outputFile | ||
} else { | ||
outputFile, err = os.Create(flagGeoipExportOutput) | ||
if err != nil { | ||
return err | ||
} | ||
defer outputFile.Close() | ||
outputWriter = outputFile | ||
} | ||
|
||
encoder := json.NewEncoder(outputWriter) | ||
encoder.SetIndent("", " ") | ||
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.PlainRuleSetCompat | ||
plainRuleSet.Version = C.RuleSetVersion1 | ||
plainRuleSet.Options.Rules = []option.HeadlessRule{ | ||
{ | ||
Type: C.RuleTypeDefault, | ||
DefaultOptions: headlessRule, | ||
}, | ||
} | ||
return encoder.Encode(plainRuleSet) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/sagernet/sing-box/log" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var commandGeoipList = &cobra.Command{ | ||
Use: "list", | ||
Short: "List geoip country codes", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := listGeoip() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
commandGeoip.AddCommand(commandGeoipList) | ||
} | ||
|
||
func listGeoip() error { | ||
for _, code := range geoipReader.Metadata.Languages { | ||
os.Stdout.WriteString(code + "\n") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/sagernet/sing-box/common/geosite" | ||
"github.com/sagernet/sing-box/log" | ||
E "github.com/sagernet/sing/common/exceptions" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
commandGeoSiteFlagFile string | ||
geositeReader *geosite.Reader | ||
geositeCodeList []string | ||
) | ||
|
||
var commandGeoSite = &cobra.Command{ | ||
Use: "geosite", | ||
Short: "Geosite tools", | ||
PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||
err := geositePreRun() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
commandGeoSite.PersistentFlags().StringVarP(&commandGeoSiteFlagFile, "file", "f", "geosite.db", "geosite file") | ||
mainCommand.AddCommand(commandGeoSite) | ||
} | ||
|
||
func geositePreRun() error { | ||
reader, codeList, err := geosite.Open(commandGeoSiteFlagFile) | ||
if err != nil { | ||
return E.Cause(err, "open geosite file") | ||
} | ||
geositeReader = reader | ||
geositeCodeList = codeList | ||
return nil | ||
} |
Oops, something went wrong.