Skip to content

Commit

Permalink
Adds ipify bee
Browse files Browse the repository at this point in the history
Queries ipify.org for the public IP and sends an event if the IP changed.
  • Loading branch information
rubiojr authored and muesli committed Oct 18, 2019
1 parent 0e8c95c commit d00f6f0
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 0 deletions.
Binary file added assets/bees/ipify.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions bees/ipify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Ipify Bee

Queries ipify.org for the public IP and sends an event if the IP changed.

## Configuration

* interval: Interval (in seconds) between requests sent to ipify.org

## Credits

ipify logo: https://github.com/rdegges/ipify-www/blob/master/static/images/globe.png
92 changes: 92 additions & 0 deletions bees/ipify/ipifybee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2019 Sergio Rubio
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Sergio Rubio <sergio@rubio.im>
*/

package ipify

import (
"time"

"github.com/muesli/beehive/bees"
"github.com/rdegges/go-ipify"
)

type IpifyBee struct {
bees.Bee
interval int
}

func (mod *IpifyBee) getIP(oldIP string, eventChan chan bees.Event) string {
ip, err := ipify.GetIp()
if err != nil {
ip, err = ipify.GetIp()
if err != nil {
panic(err)
}
}

if oldIP != ip {
ev := bees.Event{
Bee: mod.Name(),
Name: "ip",
Options: []bees.Placeholder{
{
Name: "ip",
Type: "string",
Value: ip,
},
},
}
eventChan <- ev
return ip
}

return oldIP
}

// Run executes the Bee's event loop.
func (mod *IpifyBee) Run(eventChan chan bees.Event) {
// protects us against a user setting the wrong value here
if mod.interval < 1 {
mod.interval = defaultUpdateInterval
}

oldIP := mod.getIP("", eventChan)

for {
select {
case <-mod.SigChan:
return
case <-time.After(time.Duration(mod.interval) * time.Minute):
mod.LogDebugf("Retrieving public IP from ipify.com")
oldIP = mod.getIP(oldIP, eventChan)
}
}
}

// Action triggers the action passed to it.
func (mod *IpifyBee) Action(action bees.Action) []bees.Placeholder {
return []bees.Placeholder{}
}

// ReloadOptions parses the config options and initializes the Bee.
func (mod *IpifyBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)
options.Bind("interval", &mod.interval)
}
100 changes: 100 additions & 0 deletions bees/ipify/ipifybeefactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2019 Sergio Rubio
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Sergio Rubio <sergio@rubio.im>
*/

package ipify

import (
"github.com/muesli/beehive/bees"
)

const defaultUpdateInterval int = 60

// IpifyBeeFactory takes care of initializing IpifyBee
type IpifyBeeFactory struct {
bees.BeeFactory
}

// New returns a new Bee instance configured with the supplied options.
func (factory *IpifyBeeFactory) New(name, description string, options bees.BeeOptions) bees.BeeInterface {
bee := IpifyBee{
Bee: bees.NewBee(name, factory.ID(), description, options),
}
bee.ReloadOptions(options)

return &bee
}

// ID returns the ID of this Bee.
func (factory *IpifyBeeFactory) ID() string {
return "ipify"
}

// Name returns the name of this Bee.
func (factory *IpifyBeeFactory) Name() string {
return "ipify"
}

// Description returns the description of this Bee.
func (factory *IpifyBeeFactory) Description() string {
return "Monitor your public IP address via ipify.org and notify when the IP changes"
}

// Image returns the asset name of this Bee (in the assets/bees folder)
func (factory *IpifyBeeFactory) Image() string {
return factory.Name() + ".png"
}

// Events describes the available events provided by this Bee.
func (factory *IpifyBeeFactory) Events() []bees.EventDescriptor {
events := []bees.EventDescriptor{
{
Namespace: factory.Name(),
Name: "ip",
Description: "The public IP retrieved from ipify.org",
Options: []bees.PlaceholderDescriptor{
{
Name: "ip",
Description: "IP address string",
Type: "string",
},
},
},
}
return events
}

// Options returns the options available to configure this Bee.
func (factory *IpifyBeeFactory) Options() []bees.BeeOptionDescriptor {
opts := []bees.BeeOptionDescriptor{
{
Name: "interval",
Description: "Interval in minutes to query ipify.org (60 minutes by default)",
Type: "int",
Default: defaultUpdateInterval,
Mandatory: false,
},
}
return opts
}

func init() {
f := IpifyBeeFactory{}
bees.RegisterFactory(&f)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4
github.com/jayeshsolanki93/devgorant v0.0.0-20160810172004-69fb03e5c3b1
github.com/jaytaylor/html2text v0.0.0-20190408195923-01ec452cbe43 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.6 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pretty v0.1.0
Expand Down Expand Up @@ -69,6 +70,7 @@ require (
github.com/olekukonko/tablewriter v0.0.1 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/prometheus/client_golang v0.9.2
github.com/rdegges/go-ipify v0.0.0-20150526035502-2d94a6a86c40
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4 // indirect
github.com/shuheiktgw/go-travis v0.1.10-0.20190502100712-2d0b3e9898f0
github.com/simplepush/simplepush-go v0.0.0-20170307205831-8980e96b7b02
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ github.com/jayeshsolanki93/devgorant v0.0.0-20160810172004-69fb03e5c3b1 h1:+rPUG
github.com/jayeshsolanki93/devgorant v0.0.0-20160810172004-69fb03e5c3b1/go.mod h1:C2TzmYXoz+ck9q8+lWXSkyba/l+eyKC39pw98R9MrhA=
github.com/jaytaylor/html2text v0.0.0-20190408195923-01ec452cbe43 h1:jTkyeF7NZ5oIr0ESmcrpiDgAfoidCBF4F5kJhjtaRwE=
github.com/jaytaylor/html2text v0.0.0-20190408195923-01ec452cbe43/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
Expand Down Expand Up @@ -167,6 +169,8 @@ github.com/prometheus/common v0.0.0-20181126121408-4724e9255275 h1:PnBWHBf+6L0jO
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a h1:9a8MnZMP0X2nLJdBg+pBmGgkJlSaKC2KaQmTCk1XDtE=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/rdegges/go-ipify v0.0.0-20150526035502-2d94a6a86c40 h1:31Y7UZ1yTYBU4E79CE52I/1IRi3TqiuwquXGNtZDXWs=
github.com/rdegges/go-ipify v0.0.0-20150526035502-2d94a6a86c40/go.mod h1:j4c6zEU0eMG1oiZPUy+zD4ykX0NIpjZAEOEAviTWC18=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4 h1:BN/Nyn2nWMoqGRA7G7paDNDqTXE30mXGqzzybrfo05w=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/shuheiktgw/go-travis v0.1.9 h1:qfu71EtAGkloDGS4jIA0f19g6goejqw782qParyaLPw=
Expand Down
1 change: 1 addition & 0 deletions hives.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ import (
_ "github.com/muesli/beehive/bees/twiliobee"
_ "github.com/muesli/beehive/bees/twitterbee"
_ "github.com/muesli/beehive/bees/webbee"
_ "github.com/muesli/beehive/bees/ipify"
)

0 comments on commit d00f6f0

Please sign in to comment.