Skip to content

Commit

Permalink
#minor: remove sensitivity field, now support radius (kilometers)
Browse files Browse the repository at this point in the history
  • Loading branch information
circa10a committed Feb 20, 2022
1 parent 14458f2 commit 6e503a2
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 48 deletions.
11 changes: 3 additions & 8 deletions Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ route /* {
# freegeoip.app API token, this example reads from an environment variable
freegeoip_api_token {$FREEGEOIP_API_TOKEN}

# sensitivity (proximity)
# 0 - 111 km
# 1 - 11.1 km
# 2 - 1.11 km
# 3 111 meters
# 4 11.1 meters
# 5 1.11 meters
sensitivity 3
# radius is the the distance of the geofence, only clients within the distance will be allowed.
# If not supplied, will default to 0.0 kilometers
radius 1.0

# allow_private_ip_addresses is a boolean for whether or not to allow private ip ranges
# such as 192.X, 172.X, 10.X, [::1] (localhost)
Expand Down
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,10 @@ route /* {
# freegeoip.app API token, this example reads from an environment variable
freegeoip_api_token {$FREEGEOIP_API_TOKEN}
# sensitivity (proximity)
# 0 - 111 km
# 1 - 11.1 km
# 2 - 1.11 km
# 3 111 meters
# 4 11.1 meters
# 5 1.11 meters
sensitivity 3
// radius is the distance of the geofence in kilometers
// If not supplied, will default to 0.0 kilometers
// 1.0 => 1.0 kilometers
radius 1.0
# allow_private_ip_addresses is a boolean for whether or not to allow private ip ranges
# such as 192.X, 172.X, 10.X, [::1] (localhost)
Expand Down
27 changes: 8 additions & 19 deletions caddy_geofence.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
const (
// Infinite
defaultCacheTTL = -1
// 100m
defaultSensitivity = 3
// 403
defaultStatusCode = http.StatusForbidden
// Logger namespace string
Expand All @@ -43,14 +41,10 @@ type CaddyGeofence struct {
// Not specifying a TTL sets no expiration on cached items and will live until restart
// Valid time units are "ms", "s", "m", "h"
CacheTTL time.Duration `json:"cache_ttl,omitempty"`
// sensitivity is a 0-5 scale of the geofence proximity
// 0 - 111 km
// 1 - 11.1 km
// 2 - 1.11 km
// 3 111 meters
// 4 11.1 meters
// 5 1.11 meters
Sensitivity int `json:"sensitivity,omitempty"`
// radius is the distance of the geofence in kilometers
// If not supplied, will default to 0.0 kilometers
// 1.0 => 1.0 kilometers
Radius float64 `json:"radius"`
// allow_private_ip_addresses is a boolean for whether or not to allow private ip ranges
// such as 192.X, 172.X, 10.X, [::1] (localhost)
// false by default
Expand Down Expand Up @@ -86,22 +80,17 @@ func (cg *CaddyGeofence) Provision(ctx caddy.Context) error {
cg.CacheTTL = defaultCacheTTL
}

// Set sensitivity to mid range if not set
if cg.Sensitivity == 0 {
cg.Sensitivity = defaultSensitivity
}

// Set default status code if not set (403)
if cg.StatusCode == 0 {
cg.StatusCode = defaultStatusCode
}

// Setup client
geofenceClient, err := geofence.New(&geofence.Config{
IPAddress: cg.RemoteIP,
Token: cg.FreeGeoIPAPIToken,
Sensitivity: cg.Sensitivity,
CacheTTL: cg.CacheTTL,
IPAddress: cg.RemoteIP,
Token: cg.FreeGeoIPAPIToken,
Radius: cg.Radius,
CacheTTL: cg.CacheTTL,
})
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ func (cg *CaddyGeofence) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return err
}
cg.StatusCode = statusCode
case "sensitivity":
case "radius":
if !d.NextArg() {
return d.ArgErr()
}
sensitivity, err := strconv.Atoi(d.Val())
radius, err := strconv.ParseFloat(d.Val(), 64)
if err != nil {
return err
}
cg.Sensitivity = sensitivity
cg.Radius = radius
case "allow_private_ip_addresses":
if !d.NextArg() {
return d.ArgErr()
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ go 1.17

require (
github.com/caddyserver/caddy/v2 v2.4.6
github.com/circa10a/go-geofence v0.3.1
github.com/circa10a/go-geofence v0.4.0
go.uber.org/zap v1.19.0
)

require (
github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect
github.com/EpicStep/go-simple-geo/v2 v2.0.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
Expand Down Expand Up @@ -87,9 +88,9 @@ require (
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20210915214749-c084706c2272 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 // indirect
golang.org/x/sys v0.0.0-20210915083310-ed5796bab164 // indirect
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
Expand Down
16 changes: 10 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ github.com/Azure/azure-storage-blob-go v0.8.0/go.mod h1:lPI3aLPpuLTeUwh1sViKXFxw
github.com/Azure/go-autorest v12.0.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/EpicStep/go-simple-geo/v2 v2.0.1 h1:+suZRwgZVZCuH8NXNE/D+7EH0iY90dqx2eA3faQ2v7c=
github.com/EpicStep/go-simple-geo/v2 v2.0.1/go.mod h1:ELLmk0tgdNH4BLiL+jrSg+X6nz3aMgZrTRnHPWsaXvQ=
github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20191009163259-e802c2cb94ae/go.mod h1:mjwGPas4yKduTyubHvD1Atl9r1rUq8DfVy+gkVvZ+oo=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/Masterminds/glide v0.13.2/go.mod h1:STyF5vcenH/rUqTEv+/hBXlSTo7KYwg2oc2f4tzPWic=
Expand Down Expand Up @@ -190,8 +192,8 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5O
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/circa10a/go-geofence v0.3.1 h1:UvKkNBFe5uqaUdDt4jK1uPLtVXaJMdN5l2BA+jwS+aY=
github.com/circa10a/go-geofence v0.3.1/go.mod h1:0DFACqglBg6J36ksSzUpCdyBQlxEavqEbw3Yz/Otdic=
github.com/circa10a/go-geofence v0.4.0 h1:jeCZOqn/TN9X2svf2tjzg1jt+CPW0NFT4penV5T7NMY=
github.com/circa10a/go-geofence v0.4.0/go.mod h1:mxd1yyPw/Dr/dfn7OA8OR6hdUW9UusX4OgmpwbyNweA=
github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
Expand Down Expand Up @@ -1135,8 +1137,8 @@ golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210913180222-943fd674d43e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 h1:0qxwC5n+ttVOINCBeRHO0nq9X7uy8SDsPoi5OaCdIEI=
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -1254,11 +1256,13 @@ golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210915083310-ed5796bab164 h1:7ZDGnxgHAMw7thfC5bEos0RDAccZKxioiWBhfIe+tvw=
golang.org/x/sys v0.0.0-20210915083310-ed5796bab164/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down

0 comments on commit 6e503a2

Please sign in to comment.