diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f6143e..6a3a7d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,19 +1,30 @@ # Changelog -## [0.0.2] - 2019-03-02 - -- Add README.md ([`87d7873`](https://github.com/axllent/goiplookup/commit/87d787309a5fbbea172f5b9c3bfd77a78042efba)) -- Use `db-update` for update flag ([`f5d0b1c`](https://github.com/axllent/goiplookup/commit/f5d0b1ceda54cb7760da9905831f1145b0e02915)) -- Fix typo ([`f0feace`](https://github.com/axllent/goiplookup/commit/f0feace55ac33d8eba26d8979aab973b1205cd6e)) -- Ignore compressed releases ([`87a9100`](https://github.com/axllent/goiplookup/commit/87a910028dd9046474e9754f7ec3f803ca6dc2b4)) - -## [0.0.1] - 2019-03-02 - -- Rename to goiplookup ([`3f213b0`](https://github.com/axllent/goiplookup/commit/3f213b09532d5bfa3f69cb091bd129200d4cef9d)) -- Add database update functionality ([`eea4e87`](https://github.com/axllent/goiplookup/commit/eea4e87033d13c5f6b140f82db2a9eb28e430fde)) -- Tidy code and basic versioning ([`1ac5154`](https://github.com/axllent/goiplookup/commit/1ac51546103ea61299688e7392fbe57e71ad5340)) -- Return IP Address not found ([`adb7a10`](https://github.com/axllent/goiplookup/commit/adb7a1044a1e4226881831358e0024bb49742f2b)) -- Update build script to use tags ([`675ff4c`](https://github.com/axllent/goiplookup/commit/675ff4c05719fedb8ee35f3caa6b98b858467d60)) -- Strip binary ([`ecf4260`](https://github.com/axllent/goiplookup/commit/ecf426092fbd860cb3565a1abce37821bc8e6fb9)) -- Usage to include hostname ([`abce859`](https://github.com/axllent/goiplookup/commit/abce85954c0cba6d9916f5f29728e261dc9ca884)) -- Fix debug info ([`a9d0668`](https://github.com/axllent/goiplookup/commit/a9d0668b7801408fe98a265d8873aa53a8f6645b)) +## [0.0.4] + +- Fix bug whereby executable path wasn't detected on `self-update` + + +## [0.0.3] + +- Split project into multiple files +- Add self-updater, `-v` will return current version plus latest if there is an update +- Support for darwin (Mac) +- Release multiple OS binaries + + +## [0.0.2] + +- Add README.md +- Use `db-update` for update flag + + +## [0.0.1] + +- Rename to goiplookup +- Add database update functionality +- Tidy code and basic versioning +- Return IP Address not found +- Update build script to use tags +- Usage to include hostname +- Fix debug info diff --git a/README.md b/README.md index 3d08836..cf3b570 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# GoIpLookup - geoiplookup for GeoLite2-Country written in Go +# GoipLookup - geoiplookup for GeoLite2 written in Go -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 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. @@ -8,27 +8,29 @@ It currently only supports the free GeoLite2-Country database, and there is no p ## Features -- Drop-in replacement for the now defunt geoiplookup utility +- Drop-in replacement for the now defunt `geoiplookup` utility, simply rename it - 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 self updater (if new version if available) +- Built-in self updater (if new release is available) ## Installing -Linux amd64 binaries are supplied with releases. +Multiple OS/Architecture binaries are supplied with releases. Extract the binary, make it executable, and move it to a location such as `/usr/local/bin`. + +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: ``` -bunzip2 goiplookup_linux_amd64.bz2 -chmod 755 goiplookup_linux_amd64 -sudo mv goiplookup_linux_amd64 /usr/local/bin/goiplookup -sudo chown root:root /usr/local/bin/goiplookup +goiplookup self-update ``` -If you with to replace an existing defunct implementation of geoiplookup, then simply name the file `geoiplookup`. - ## Compiling from source @@ -40,9 +42,11 @@ cd goiplookup make ``` + ## Basic usage + ``` -Usage: goiplookup [-i] [-c] [-d ] +Usage: goiplookup [-i] [-c] [-d ] Options: -V show version number diff --git a/goiplookup.go b/goiplookup.go index d6b8f91..93e16da 100644 --- a/goiplookup.go +++ b/goiplookup.go @@ -23,7 +23,6 @@ var data_dir (*string) // URLs const ( db_update_url = "https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz" - repo_url = "https://github.com/axllent/goiplookup/releases" release_url = "https://api.github.com/repos/axllent/goiplookup/releases/latest" ) @@ -34,9 +33,9 @@ func main() { data_dir = flag.String("d", "/usr/local/share/GeoIP", "database directory or file") } else { data_dir = flag.String("d", "/usr/share/GeoIP", "database directory or file") - } + } - // parse flags + // parse flags flag.Parse() if *showversion { diff --git a/updater.go b/updater.go index 01e39e9..29b6875 100644 --- a/updater.go +++ b/updater.go @@ -13,12 +13,10 @@ import ( // Update GeoLite2-Country.mmdb func UpdateGeoLite2Country() { - Verbose("Updating GeoLite2-Country.mmdb") - - tmp_dir := os.TempDir() - gzfile := filepath.Join(tmp_dir, "GeoLite2-Country.tar.gz") - + Verbose("Updating GeoLite2-Country.mmdb") + tmp_dir := os.TempDir() + gzfile := filepath.Join(tmp_dir, "GeoLite2-Country.tar.gz") // check the output directory is writeable if _, err := os.Stat(*data_dir); os.IsNotExist(err) { @@ -48,7 +46,7 @@ func UpdateGeoLite2Country() { // Extract just the GeoLite2-Country.mmdb from the tar.gz func ExtractDatabaseFile(dst string, targz string) error { - Verbose(fmt.Sprintf("Opening %s", targz)) + Verbose(fmt.Sprintf("Opening %s", targz)) re, _ := regexp.Compile(`GeoLite2\-Country\.mmdb$`) @@ -110,9 +108,9 @@ func ExtractDatabaseFile(dst string, targz string) error { // Built-in updater func SelfUpdate() { - tmp_dir := os.TempDir() + tmp_dir := os.TempDir() bz2file := filepath.Join(tmp_dir, "goiplookup.bz2") - binfile := filepath.Join(tmp_dir, "goiplookup.tmp") + newexec := filepath.Join(tmp_dir, "goiplookup.tmp") download_url, err := GetUpdateURL() if err != nil { @@ -137,13 +135,13 @@ func SelfUpdate() { br := bzip2.NewReader(f) // write the file - out, err := os.OpenFile(binfile, os.O_CREATE|os.O_RDWR, 0755) + 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", binfile)) + Verbose(fmt.Sprintf("Extracting %s", newexec)) _, err = io.Copy(out, br) if err != nil { @@ -153,7 +151,10 @@ func SelfUpdate() { // replace os.Args[0] with new file // cannot overwrite open file so rename then delete - err = ReplaceFile(os.Args[0], binfile) + // 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)) os.Exit(1) diff --git a/utils.go b/utils.go index f92b5fb..8fb99fa 100644 --- a/utils.go +++ b/utils.go @@ -231,14 +231,14 @@ func ReplaceFile(dst string, src string) error { } // rename the current executable to .old - Verbose(fmt.Sprintf("Renaming %s to %s", dst, dst_old)) - if err := os.Rename(dst, dst_old); err != nil { + Verbose(fmt.Sprintf("Renaming %s to %s", dst, old_tmp_abs)) + if err := os.Rename(dst, old_tmp_abs); err != nil { return err } // rename the .new to current executable - Verbose(fmt.Sprintf("Renaming %s to %s", new_tmp_abs, binary_filename)) - if err := os.Rename(new_tmp_abs, binary_filename); err != nil { + Verbose(fmt.Sprintf("Renaming %s to %s", new_tmp_abs, dst)) + if err := os.Rename(new_tmp_abs, dst); err != nil { return err }