Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/circa10a/go-geofence
A small library to detect if an IP address is close to yours or another of your choosing using https://ipbase.com
https://github.com/circa10a/go-geofence
geofence geofence-api geofencing go golang golang-library golang-package
Last synced: 3 days ago
JSON representation
A small library to detect if an IP address is close to yours or another of your choosing using https://ipbase.com
- Host: GitHub
- URL: https://github.com/circa10a/go-geofence
- Owner: circa10a
- License: mit
- Created: 2021-11-08T14:57:16.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-21T01:26:37.000Z (3 months ago)
- Last Synced: 2024-07-21T02:27:36.766Z (3 months ago)
- Topics: geofence, geofence-api, geofencing, go, golang, golang-library, golang-package
- Language: Go
- Homepage:
- Size: 39.1 KB
- Stars: 18
- Watchers: 3
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-geofence
![GitHub tag (latest semver)](https://img.shields.io/github/v/tag/circa10a/go-geofence?style=plastic)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/circa10a/go-geofence)](https://pkg.go.dev/github.com/circa10a/go-geofence?tab=overview)
[![Go Report Card](https://goreportcard.com/badge/github.com/circa10a/go-geofence)](https://goreportcard.com/report/github.com/circa10a/go-geofence)A small library to detect if an IP address is close to yours or another of your choosing using https://ipbase.com/
## Usage
First you will need a free API Token from [ipbase.com](https://ipbase.com/)
```bash
go get github.com/circa10a/go-geofence
``````go
package mainimport (
"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: "",
// ipbase.com API token
Token: "YOUR_IPBASE_API_TOKEN",
// Maximum radius of the geofence in kilometers, only clients less than or equal to this distance will return true with IsIPAddressNear()
// 1 kilometer
Radius: 1.0,
// Allow 192.X, 172.X, 10.X and loopback addresses
AllowPrivateIPAddresses: true,
// How long to cache if any ip address is nearby
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)
}
```## Caching
To cache keys indefinitely, set `CacheTTL: -1`
### Local (in-memory)
By default, the library will use an in-memory cache that will be used to reduce the number of calls to ipbase.com and increase performance. If no `CacheTTL` value is set (`0`), the in-memory cache is disabled.
### Persistent
If you need a persistent cache to live outside of your application, [Redis](https://redis.io/) is supported by this library. To have the library cache address proximity using a Redis instance, simply provide a `RedisOptions` struct using the `cache` package to `geofence.Config.RedisOptions`. If `RedisOptions` is configured, the in-memory cache will not be used.
> Note: Only Redis 7 is currently supported at the time of this writing.
#### Example Redis Usage
```go
package mainimport (
"fmt"
"log"
"time""github.com/circa10a/go-geofence"
geofencecache "github.com/circa10a/go-geofence/cache"
)func main() {
geofence, err := geofence.New(&geofence.Config{
IPAddress: "",
Token: "YOUR_IPBASE_API_TOKEN",
Radius: 1.0,
AllowPrivateIPAddresses: true,
CacheTTL: 7 * (24 * time.Hour), // 1 week
// Use Redis for caching
RedisOptions: &geofencecache.RedisOptions{
Addr: "localhost:6379",
Username: "", // no username set
Password: "", // no password set
DB: 0, // use default DB
},
})
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)
}
```