github.com/sagernet/sing-box@v1.9.0-rc.20/cmd/sing-box/cmd_geoip_lookup.go (about) 1 package main 2 3 import ( 4 "net/netip" 5 "os" 6 7 "github.com/sagernet/sing-box/log" 8 E "github.com/sagernet/sing/common/exceptions" 9 N "github.com/sagernet/sing/common/network" 10 11 "github.com/spf13/cobra" 12 ) 13 14 var commandGeoipLookup = &cobra.Command{ 15 Use: "lookup <address>", 16 Short: "Lookup if an IP address is contained in the GeoIP database", 17 Args: cobra.ExactArgs(1), 18 Run: func(cmd *cobra.Command, args []string) { 19 err := geoipLookup(args[0]) 20 if err != nil { 21 log.Fatal(err) 22 } 23 }, 24 } 25 26 func init() { 27 commandGeoip.AddCommand(commandGeoipLookup) 28 } 29 30 func geoipLookup(address string) error { 31 addr, err := netip.ParseAddr(address) 32 if err != nil { 33 return E.Cause(err, "parse address") 34 } 35 if !N.IsPublicAddr(addr) { 36 os.Stdout.WriteString("private\n") 37 return nil 38 } 39 var code string 40 _ = geoipReader.Lookup(addr.AsSlice(), &code) 41 if code != "" { 42 os.Stdout.WriteString(code + "\n") 43 return nil 44 } 45 os.Stdout.WriteString("unknown\n") 46 return nil 47 }