Skip to content

Commit

Permalink
Merge branch 'release/0.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
axllent committed Mar 6, 2019
2 parents ecad0ba + 677ce60 commit 2b7c351
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 47 deletions.
45 changes: 28 additions & 17 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
# 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.


## 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

Expand All @@ -40,9 +42,11 @@ cd goiplookup
make
```


## Basic usage

```
Usage: goiplookup [-i] [-c] [-d <database directory>] <ipaddress|hostname|db-update>
Usage: goiplookup [-i] [-c] [-d <database directory>] <ipaddress|hostname|db-update|self-update>
Options:
-V show version number
Expand Down
5 changes: 2 additions & 3 deletions goiplookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down
23 changes: 12 additions & 11 deletions updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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$`)

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ func ReplaceFile(dst string, src string) error {
}

// rename the current executable to <binary>.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 <binary>.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
}

Expand Down

0 comments on commit 2b7c351

Please sign in to comment.