55 "encoding/json"
66 "errors"
77 "fmt"
8+ "io"
9+ "log"
810 "net"
911 "net/http"
1012 "os"
@@ -273,11 +275,17 @@ func New(RefreshPeroidH time.Duration) *IPGeolocation {
273275 }
274276
275277 go func () {
276- ipGeolocation .Load (filename )
278+ err := ipGeolocation .Load (filename )
279+ if err != nil {
280+ log .Fatal (err )
281+ }
277282
278283 for {
279284 if ! ipGeolocation .Ready || ipGeolocation .RefreshTime .Add (time .Hour * RefreshPeroidH ).Before (time .Now ()) {
280- ipGeolocation .RefreshData ()
285+ err := ipGeolocation .RefreshData ()
286+ if err != nil {
287+ fmt .Println ("err in ipGeolocation.RefreshData" , err )
288+ }
281289 fmt .Println ("\n CIDR is READY." )
282290 time .Sleep (time .Hour * RefreshPeroidH )
283291 } else {
@@ -313,30 +321,39 @@ func (geo *IPGeolocation) IsDataFresh() bool {
313321 return time .Since (geo .RefreshTime ).Minutes () < 60
314322}
315323
316- func (ig * IPGeolocation ) RefreshData () error {
324+ func (geo * IPGeolocation ) RefreshData () error {
317325 newCIDRListV4 := make (map [string ][]* net.IPNet )
318326 newCIDRListV6 := make (map [string ][]* net.IPNet )
319327
320328 lenCountriesCodes := len (CountriesCodes )
321329
322- for indx , code := range CountriesCodes {
323- ig .downloadCIDRContent ("https://raw.githubusercontent.com/onionj/country-ip-blocks-alternative/master/ipv4/" + code + ".netset" , code , newCIDRListV4 )
324- ig .downloadCIDRContent ("https://raw.githubusercontent.com/onionj/country-ip-blocks-alternative/master/ipv6/" + code + ".netset" , code , newCIDRListV6 )
325- fmt .Println ("Down" , code , "CIDR" , int32 ((float32 (indx + 1 ))/ float32 (lenCountriesCodes )* 100 ), "% " )
330+ for index , code := range CountriesCodes {
331+ err := geo .downloadCIDRContent ("https://raw.githubusercontent.com/onionj/country-ip-blocks-alternative/master/ipv4/" + code + ".netset" , code , newCIDRListV4 )
332+ if err != nil {
333+ return err
334+ }
335+ err = geo .downloadCIDRContent ("https://raw.githubusercontent.com/onionj/country-ip-blocks-alternative/master/ipv6/" + code + ".netset" , code , newCIDRListV6 )
336+ if err != nil {
337+ return err
338+ }
339+ fmt .Println ("Down" , code , "CIDR" , int32 ((float32 (index + 1 ))/ float32 (lenCountriesCodes )* 100 ), "% " )
326340 fmt .Print ("\x0D \u001b [1A" )
327341 }
328342
329- ig .Ready = true
330- ig .RefreshTime = time .Now ()
331- ig .CIDRListV4 = newCIDRListV4
332- ig .CIDRListV6 = newCIDRListV6
343+ geo .Ready = true
344+ geo .RefreshTime = time .Now ()
345+ geo .CIDRListV4 = newCIDRListV4
346+ geo .CIDRListV6 = newCIDRListV6
333347
334- ig .Save (filename )
348+ err := geo .Save (filename )
349+ if err != nil {
350+ return err
351+ }
335352
336353 return nil
337354}
338355
339- func (ig * IPGeolocation ) downloadCIDRContent (url string , code string , newCIDRList map [string ][]* net.IPNet ) error {
356+ func (geo * IPGeolocation ) downloadCIDRContent (url string , code string , newCIDRList map [string ][]* net.IPNet ) error {
340357 response , err := http .Get (url )
341358
342359 if response .StatusCode != 200 {
@@ -345,7 +362,9 @@ func (ig *IPGeolocation) downloadCIDRContent(url string, code string, newCIDRLis
345362 if err != nil {
346363 return fmt .Errorf ("failed to download CIDR for country code %s: %w" , code , err )
347364 }
348- defer response .Body .Close ()
365+ defer func (Body io.ReadCloser ) {
366+ _ = Body .Close ()
367+ }(response .Body )
349368
350369 scanner := bufio .NewScanner (response .Body )
351370 for scanner .Scan () {
@@ -357,31 +376,31 @@ func (ig *IPGeolocation) downloadCIDRContent(url string, code string, newCIDRLis
357376 return fmt .Errorf ("failed to read CIDR for country code %s: %w" , code , err )
358377 }
359378
360- ig .Ready = true
361- ig .RefreshTime = time .Now ()
379+ geo .Ready = true
380+ geo .RefreshTime = time .Now ()
362381 return nil
363382}
364383
365- func (ig IPGeolocation ) Query (ip net.IP ) (string , error ) {
384+ func (geo * IPGeolocation ) Query (ip net.IP ) (string , error ) {
366385 if ip .IsPrivate () || ip .IsLoopback () {
367386 return "" , errors .New ("IP is private" )
368387 }
369388
370- if ! ig .Ready {
389+ if ! geo .Ready {
371390 return "" , errors .New ("IPGeolocation is not ready" )
372391 }
373392
374393 if ip .To4 () != nil {
375- for country := range ig .CIDRListV4 {
376- for _ , cidr := range ig .CIDRListV4 [country ] {
394+ for country := range geo .CIDRListV4 {
395+ for _ , cidr := range geo .CIDRListV4 [country ] {
377396 if cidr .Contains (ip ) {
378397 return country , nil
379398 }
380399 }
381400 }
382401 } else {
383- for country := range ig .CIDRListV6 {
384- for _ , cidr := range ig .CIDRListV6 [country ] {
402+ for country := range geo .CIDRListV6 {
403+ for _ , cidr := range geo .CIDRListV6 [country ] {
385404 if cidr .Contains (ip ) {
386405 return country , nil
387406 }
0 commit comments