github.com/sagernet/sing-box@v1.9.0-rc.20/cmd/sing-box/cmd_geoip.go (about) 1 package main 2 3 import ( 4 "github.com/sagernet/sing-box/log" 5 E "github.com/sagernet/sing/common/exceptions" 6 7 "github.com/oschwald/maxminddb-golang" 8 "github.com/spf13/cobra" 9 ) 10 11 var ( 12 geoipReader *maxminddb.Reader 13 commandGeoIPFlagFile string 14 ) 15 16 var commandGeoip = &cobra.Command{ 17 Use: "geoip", 18 Short: "GeoIP tools", 19 PersistentPreRun: func(cmd *cobra.Command, args []string) { 20 err := geoipPreRun() 21 if err != nil { 22 log.Fatal(err) 23 } 24 }, 25 } 26 27 func init() { 28 commandGeoip.PersistentFlags().StringVarP(&commandGeoIPFlagFile, "file", "f", "geoip.db", "geoip file") 29 mainCommand.AddCommand(commandGeoip) 30 } 31 32 func geoipPreRun() error { 33 reader, err := maxminddb.Open(commandGeoIPFlagFile) 34 if err != nil { 35 return err 36 } 37 if reader.Metadata.DatabaseType != "sing-geoip" { 38 reader.Close() 39 return E.New("incorrect database type, expected sing-geoip, got ", reader.Metadata.DatabaseType) 40 } 41 geoipReader = reader 42 return nil 43 }