Giter VIP home page Giter VIP logo

go-geofence's Introduction

go-geofence

GitHub tag (latest semver) PkgGoDev Go Report Card

A small library to detect if an IP address is close to yours or another of your choosing using https://freegeoip.app/

Usage

First you will need a free API Token from freegeoip.app

go get github.com/circa10a/go-geofence
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/circa10a/go-geofence"
)

func main() {
	geofence, err := geofence.New(&geofence.Config{
		// Empty string to geofence your current public IP address, or you can monitor a remote address by supplying it as the first parameter
		IPAddress: "",
		// freegeoip.app API token
		Token: "YOUR_FREEGEOIP_API_TOKEN",
		// Sensitivity
		// 0 - 111 km
		// 1 - 11.1 km
		// 2 - 1.11 km
		// 3 111 meters
		// 4 11.1 meters
		// 5 1.11 meters
		Sensitivity: 3,                    // 3 is recommended
		CacheTTL:    7 * (24 * time.Hour), // 1 week
	})
	if err != nil {
		log.Fatal(err)
	}
	isAddressNearby, err := geofence.IsIPAddressNear("8.8.8.8")
	if err != nil {
		log.Fatal(err)
	}
	// Address nearby: false
	fmt.Println("Address nearby: ", isAddressNearby)
}

Private IP Addresses

Private IP's will always result in false since their coordinates will come back as 0.0000. You can work around these like so:

package main

import (
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/circa10a/go-geofence"
)

func main() {
	// Provide an IP Address to test with
	ipAddress := "192.168.1.100"
	geofence, err := geofence.New(&geofence.Config{
		// Empty string to geofence your current public IP address, or you can monitor a remote address by supplying it as the first parameter
		IPAddress: ipAddress,
		// freegeoip.app API token
		Token: "YOUR_FREEGEOIP_API_TOKEN",
		// Sensitivity
		// 0 - 111 km
		// 1 - 11.1 km
		// 2 - 1.11 km
		// 3 111 meters
		// 4 11.1 meters
		// 5 1.11 meters
		Sensitivity: 3,                    // 3 is recommended
		CacheTTL:    7 * (24 * time.Hour), // 1 week
	})
	if err != nil {
		log.Fatal(err)
	}
	// Skip Private IP analysis as it will always be false
	if !strings.HasPrefix(ipAddress, "192.") && !strings.HasPrefix(ipAddress, "172.") && !strings.HasPrefix(ipAddress, "10.") {
		isAddressNearby, err := geofence.IsIPAddressNear(ipAddress)
		if err != nil {
			log.Fatal(err)
		}
		// Address nearby: false
		fmt.Println("Address nearby: ", isAddressNearby)
	}
}

go-geofence's People

Contributors

circa10a avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.