Skip to content

Commit

Permalink
Version 1.0
Browse files Browse the repository at this point in the history
* Support for unix hosts with pyiface
  • Loading branch information
bendikro authored and bendikro committed Sep 27, 2015
0 parents commit 8246aee
Show file tree
Hide file tree
Showing 19 changed files with 2,108 additions and 0 deletions.
623 changes: 623 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Iface Watch : A Deluge plugin to monitor a network interface for IP changes ##

Author: bendikro <[email protected]>


License: GPLv3

## Building the plugin ##

```
#!bash
$ python setup.py bdist_egg
```


## Changelog ##

v1.0 - 2015-09-27

* Support for unix hosts with pyiface

(Tested with Deluge 1.3.11)
50 changes: 50 additions & 0 deletions ifacewatch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012-2015 bendikro [email protected]
#
# Based on work by:
# Copyright (C) 2009 Camillo Dell'mour <[email protected]>
#
# Basic plugin template created by:
# Copyright (C) 2008 Martijn Voncken <[email protected]>
# Copyright (C) 2007-2009 Andrew Resch <[email protected]>
# Copyright (C) 2009 Damien Churchill <[email protected]>
#
# This file is part of YaRSS2 and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#

import sys

import pkg_resources
from deluge.plugins.init import PluginInitBase

import ifacewatch.util.logger

log = ifacewatch.util.logger.Logger()


def load_libs():
egg = pkg_resources.require("IfaceWatch")[0]
for name in egg.get_entry_map("ifacewatch.libpaths"):
ep = egg.get_entry_info("ifacewatch.libpaths", name)
location = "%s/%s" % (egg.location, ep.module_name.replace(".", "/"))
sys.path.append(location)
log.info("Appending to sys.path: '%s'" % location)


class CorePlugin(PluginInitBase):
def __init__(self, plugin_name):
load_libs()
from core import Core as CorePluginClass
self._plugin_cls = CorePluginClass
super(CorePlugin, self).__init__(plugin_name)


class GtkUIPlugin(PluginInitBase):
def __init__(self, plugin_name):
load_libs()
from gtkui.gtkui import GtkUI as GtkUIPluginClass
self._plugin_cls = GtkUIPluginClass
super(GtkUIPlugin, self).__init__(plugin_name)
130 changes: 130 additions & 0 deletions ifacewatch/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 bendikro [email protected]
#
# This file is part of Iface Watch and is licensed under GNU General Public
# License 3.0, or later, with the additional special exception to link portions
# of this program with the OpenSSL library. See LICENSE for more details.
#


import deluge.common
import deluge.component as component
from deluge.core.rpcserver import export
from deluge.plugins.pluginbase import CorePluginBase
from twisted.internet.task import LoopingCall

import ifacewatch.util.common
import ifacewatch.util.logger
from ifacewatch.ifacewatch_config import IfacewatchConfig
from ifacewatch.lib.pyiface.iface import Interface
from ifacewatch.util.common import IfaceWatchIPChangedEvent


class Core(CorePluginBase):

def __init__(self, name):
"""Used for tests only"""
if name is not "test":
super(Core, self).__init__(name)
else:
# To avoid warnings when running tests
self._component_name = name

self.timer = None
self.config = None
self.ip = None
self.log = ifacewatch.util.logger.Logger()
self.log.info("Core init", gtkui=True)
self.core = component.get("Core")
self.core.config.register_set_function("listen_interface", self.interface_changed)

def interface_changed(self, iface, ip):
component.get("EventManager").emit(IfaceWatchIPChangedEvent(ip))

def enable(self, config=None):
if config is None:
self.config = IfacewatchConfig(self.log)
else:
self.config = config
self.log.info("Enabled Iface Watch %s" % ifacewatch.util.common.get_version())

self.scheduler_timer()
self.check_interface()

def scheduler_timer(self):
if self.timer:
if self.timer.running:
self.timer.stop()
else:
self.timer = LoopingCall(self.check_interface)

interval = int(self.config.get_config()["update_interval"])
if self.config.get_config()["active"]:
self.timer.start(interval * 6, now=True) # Multiply to get seconds
self.log.info("Scheduling watch with interval %s." %
self.config.get_config()["update_interval"], gtkui=True)
else:
self.log.info("Watch mode disabled", gtkui=True)

def disable(self):
self.config.save()

def update(self):
pass

def check_interface(self, *args, **kwargs):
if self.config is None:
return
prev_ip = self.ip
self.ip = None
iface = self.config.get_config()["interface"]

if iface.strip():
try:
iff = Interface(name=str(iface))
self.ip = iff.ip_str()
if not deluge.common.is_ip(self.ip):
self.log.info("Invalid IP returned for interface '%s': %s" % (iface, self.ip), gtkui=True)
self.ip = None
except TypeError as e:
self.log.error("TypeError: %s" % e, gtkui=True)

if self.ip is None:
self.ip = ""
iface = "<all>"

has_changed = prev_ip != self.ip

if prev_ip is not None and has_changed:
self.log.info("IP from interface %s is new: %s -> %s" %
(iface, prev_ip, self.ip if self.ip else "0.0.0.0"), gtkui=True)
if has_changed:
self.log.info("Updating with IP '%s'" % (self.ip if self.ip else "0.0.0.0"), gtkui=True)
self.core.set_config({"listen_interface": self.ip})

return True

@export
def get_ip(self):
"""Returns the config dictionary"""
return self.core.get_config_value("listen_interface")

@export
def get_config(self):
"""Returns the config dictionary"""
return self.config.get_config()

@export
def save_config(self, config):
newiface = "interface" in config and config["interface"] != self.config.get_config()["interface"]
newinterval = ("update_interval" in config and
config["update_interval"] != self.config.get_config()["update_interval"])
newstate = config["active"] != self.config.get_config()["active"]
self.config.set_config(config)
if newstate and config["active"] is True:
self.log.info("Watch mode enabled")
if newiface:
self.check_interface()
if newinterval or newstate:
self.scheduler_timer()
Loading

0 comments on commit 8246aee

Please sign in to comment.