-
-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Queries ipify.org for the public IP and sends an event if the IP changed.
- Loading branch information
Showing
7 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters