Skip to content

Commit

Permalink
Merge branch 'release/0.2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
axllent committed Jan 3, 2020
2 parents 5a9b2e1 + 9bada88 commit aa573f7
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 247 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.2.2]

- Add support for MaxMind (free) license key requirement to update GeoLite2 databases
- Switch to [ghru](https://github.com/axllent/ghru) for binary release updates


## [0.2.1]

- Fix dataDir flag parsing
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The MIT License (MIT)
Copyright (c) 2019 Ralph Slooten
Copyright (c) 2020 Ralph Slooten

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

TAG=`git describe --tags`
VERSION ?= `git describe --tags`
LDFLAGS=-ldflags "-s -extldflags \"--static\" -w -X main.version=${VERSION}"
LDFLAGS=-ldflags "-s -extldflags \"--static\" -w -X main.version=${VERSION} -X main.licenseKey=${LICENSEKEY}"

build = echo "\n\nBuilding $(1)-$(2)" && CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) go build ${LDFLAGS} -o dist/goiplookup_${VERSION}_$(1)_$(2) \
&& bzip2 dist/goiplookup_${VERSION}_$(1)_$(2)
Expand Down
70 changes: 50 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Go Report Card](https://goreportcard.com/badge/github.com/axllent/goiplookup)](https://goreportcard.com/report/github.com/axllent/goiplookup)

GoipLookup is a geoiplookup replacement for the [free GeoLite2-Country](https://dev.maxmind.com/geoip/geoip2/geolite2/),
GoipLookup is a geoiplookup replacement for the [free MaxMind GeoLite2-Country](https://dev.maxmind.com/geoip/geoip2/geolite2/),
written in [Go](https://golang.org/).

It currently only supports the free GeoLite2-Country database, and there is no planned support for the other types.
Expand All @@ -11,10 +11,10 @@ It currently only supports the free GeoLite2-Country database, and there is no p
## Features

- Drop-in replacement for the now defunt `geoiplookup` utility, simply rename it
- Works with the current Maxmind database format (mmdd)
- Works with the current MaxMind database format (mmdd)
- IPv4, IPv6 and fully qualified domain name (FQDN) support
- Options to return just the country iso (`NZ`) or country name (`New Zealand`), rather than the full `GeoIP Country Edition: NZ, New Zealand`
- Built-in database update support
- Built-in database update support (see [Database updates](#database-updates))
- Built-in self updater (if new release is available)


Expand All @@ -25,23 +25,6 @@ Multiple OS/Architecture binaries are supplied with releases. Extract the binary
If you wish to replace an existing defunct implementation of geoiplookup, then simply name the file `geoiplookup`.


## Updating

GoipLookup comes with a built-in self-updater:

```
goiplookup self-update
```


## Compiling from source

Go >= 1.11 required:

```
go get github.com:axllent/goiplookup.git
```

## Basic usage

```
Expand All @@ -64,3 +47,50 @@ goiplookup -c 8.8.8.8 Return just the country name
goiplookup db-update Update the GeoLite2-Country database (do not run more than once a month)
goiplookup self-update Update the GoIpLookup binary with the latest release
```


## GoipLookup updates

GoipLookup comes with a built-in self-updater:

```
goiplookup self-update
```

Version checked (`goiplookup -V`) will tell you if your version is out of date.


## Database updates

GoipLookup is able to update your GeoLite2 Country database. As of 01/01/2020 MaxMind require a (free) License Key in order to download these updates. The release (binary) versions of goiplookup (>= 0.2.2) already contain a key for this, however if you are compiling from source you will need to set your own licence key in your environment (see below).


### Binary release database updates

```
goiplookup db-update
```


### Self-compiled database updates

If you wish to use your own MaxMind license key, or you are compiling from source, then you must provide a key in your environment.
To generate your own license key from MaxMind you must first [register a free account](https://www.maxmind.com/en/geolite2/signup) and follow the instructions.

```
LICENSEKEY="xxxxxxxx" goiplookup db-update
```
or
```
export LICENSEKEY="xxxxxxxx"
goiplookup db-update
```


## Compiling from source

Go >= 1.11 required:

```
go get github.com/axllent/goiplookup
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/axllent/goiplookup
go 1.13

require (
github.com/axllent/ghru v1.1.3
github.com/oschwald/geoip2-golang v1.3.0
github.com/oschwald/maxminddb-golang v1.5.0 // indirect
github.com/spf13/pflag v1.0.5
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/axllent/ghru v1.1.3 h1:n0jYsuqCYaHHAR6DraXZl8hpBY4j0XV47y5Lyym/jGo=
github.com/axllent/ghru v1.1.3/go.mod h1:rFvMhcO1UAv2Cv6bXscS8EOc7qqNpfe8ZLp23utzs88=
github.com/axllent/semver v0.0.0-20191103011746-394cefa91ee9 h1:LHNcCfePzgC/agAJs5a/5K3hFo8uW04bEdDKDkoX4do=
github.com/axllent/semver v0.0.0-20191103011746-394cefa91ee9/go.mod h1:2xSPzvG8n9mRfdtxSvWvfTfQGWfHsMsHO1iZnKATMSc=
github.com/oschwald/geoip2-golang v1.3.0 h1:D+Hsdos1NARPbzZ2aInUHZL+dApIzo8E0ErJVsWcku8=
github.com/oschwald/geoip2-golang v1.3.0/go.mod h1:0LTTzix/Ao1uMvOhAV4iLU0Lz7eCrP94qZWBTDKf0iE=
github.com/oschwald/maxminddb-golang v1.5.0 h1:rmyoIV6z2/s9TCJedUuDiKht2RN12LWJ1L7iRGtWY64=
Expand Down
29 changes: 14 additions & 15 deletions goiplookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"runtime"

"github.com/axllent/ghru"
flag "github.com/spf13/pflag"
)

Expand All @@ -16,16 +17,13 @@ var (
verboseoutput bool
showversion bool
dataDir string
licenseKey string // GeoLite2 license key for updating
version = "dev"
)

// we set this in `main()` based on OS
// var dataDir (*string)

// URLs
const (
dbUpdateURL = "https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz"
releaseURL = "https://api.github.com/repos/axllent/goiplookup/releases/latest"
releaseURL = "https://api.github.com/repos/axllent/goiplookup/releases/latest"
)

// Main function
Expand All @@ -48,16 +46,12 @@ func main() {

if showversion {
fmt.Println(fmt.Sprintf("Version %s", version))
latest, err := LatestRelease()
if err == nil && version != latest {
fmt.Println(fmt.Sprintf("Version %s available", latest))
if _, err := GetUpdateURL(); err == nil {
fmt.Println(fmt.Sprintf("Run `%s self-update` to update", os.Args[0]))
}
} else {
fmt.Println("You have the latest version")

latest, _, _, err := ghru.Latest("axllent/goiplookup", "goiplookup")
if err == nil && ghru.GreaterThan(latest, version) {
fmt.Printf("Update available: %s\nRun `%s self-update` to update\n", latest, os.Args[0])
}
return
os.Exit(0)
}

if len(flag.Args()) != 1 || showhelp {
Expand All @@ -72,7 +66,12 @@ func main() {
UpdateGeoLite2Country()
} else if lookup == "self-update" {
// update app if needed
SelfUpdate()
rel, err := ghru.Update("axllent/goiplookup", "goiplookup", version)
if err != nil {
panic(err)
}
fmt.Printf("Updated %s to version %s\n", os.Args[0], rel)
os.Exit(0)
} else {
// lookup ip/hostname
Lookup(lookup)
Expand Down
80 changes: 13 additions & 67 deletions updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"archive/tar"
"compress/bzip2"
"compress/gzip"
"fmt"
"io"
Expand All @@ -17,6 +16,19 @@ import (

// UpdateGeoLite2Country updates GeoLite2-Country.mmdb
func UpdateGeoLite2Country() {

key := os.Getenv("LICENSEKEY")
if key == "" && licenseKey != "" {
key = licenseKey
}

if key == "" {
fmt.Println("Error: GeoIP License Key not set.\nPlease see https://github.com/axllent/goiplookup#database-updates")
os.Exit(1)
}

dbUpdateURL := fmt.Sprintf("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=%s&suffix=tar.gz", key)

Verbose("Updating GeoLite2-Country.mmdb")

tmpDir := os.TempDir()
Expand Down Expand Up @@ -126,69 +138,3 @@ func ExtractDatabaseFile(dst string, targz string) error {
}
}
}

// SelfUpdate is a built-in updater
func SelfUpdate() {
tmpDir := os.TempDir()
bz2file := filepath.Join(tmpDir, "goiplookup.bz2")
newexec := filepath.Join(tmpDir, "goiplookup.tmp")

downloadURL, err := GetUpdateURL()
fmt.Println(fmt.Sprintf("Updating %s", os.Args[0]))
if err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}

if err := DownloadToFile(bz2file, downloadURL); err != nil {
fmt.Println(err)
os.Exit(1)
}

Verbose(fmt.Sprintf("Opening %s", bz2file))
f, err := os.OpenFile(bz2file, 0, 0)
if err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}
defer f.Close()

// create a bzip2 reader
br := bzip2.NewReader(f)

// write the file
out, err := os.OpenFile(newexec, os.O_CREATE|os.O_RDWR, 0755)
if err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}

Verbose(fmt.Sprintf("Extracting %s", newexec))

_, err = io.Copy(out, br)
if err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}

// replace os.Args[0] with new file
// cannot overwrite open file so rename then delete
// get executable's absolute path
oldexec, _ := os.Readlink("/proc/self/exe")

err = ReplaceFile(oldexec, newexec)
if err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
fmt.Println("You may require root permissions.")
os.Exit(1)
}

// remove the src file
Verbose(fmt.Sprintf("Deleting %s", bz2file))
if err := os.Remove(bz2file); err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}

fmt.Println("Done")
}
Loading

0 comments on commit aa573f7

Please sign in to comment.