Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if geo is configured before making calls #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/api/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,16 +333,21 @@ func NewApi(ctx context.Context, WorkerManager *worker.Manager, HostManager *hos
return nil, err
}

log.Infof("Checking IP %s", args[0])
if !a.GeoDatabase.IsValid() {
return "", nil
}

log.Tracef("Checking geo:iso IP %s", args[0])
val := net.ParseIP(args[0])

if val == nil {
return nil, fmt.Errorf("invalid IP")
}

record, err := a.GeoDatabase.GetCity(&val)

if err != nil && !errors.Is(err, geo.NotValidConfig) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most likely can remove the error check clause since it doesnt return this case anymore.

return nil, err
return "", err
}

return geo.GetIsoCodeFromRecord(record), nil
Expand Down
10 changes: 0 additions & 10 deletions internal/geo/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ func (g *GeoDatabase) IsValid() bool {
}

func (g *GeoDatabase) GetASN(ip *net.IP) (*geoip2.ASN, error) {

if !g.IsValid() {
return nil, NotValidConfig
}

g.RLock()
defer g.RUnlock()
record := &geoip2.ASN{}
Expand All @@ -104,11 +99,6 @@ func (g *GeoDatabase) GetASN(ip *net.IP) (*geoip2.ASN, error) {
}

func (g *GeoDatabase) GetCity(ip *net.IP) (*geoip2.City, error) {

if !g.IsValid() {
return nil, NotValidConfig
}

g.RLock()
defer g.RUnlock()
record := &geoip2.City{}
Expand Down