Added ssh tunnel for connceting to a remote deluge RPC client. Also added check for values in config file.

This commit is contained in:
2018-05-06 00:29:38 +02:00
parent 2b3291aefd
commit 84e78c55f0
2 changed files with 25 additions and 2 deletions

View File

@@ -29,6 +29,7 @@ import argparse
import os import os
import re import re
import signal import signal
import socket
import logging import logging
import logging.config import logging.config
import configparser import configparser
@@ -37,6 +38,7 @@ from distutils.util import strtobool
from pprint import pprint from pprint import pprint
from deluge_client import DelugeRPCClient from deluge_client import DelugeRPCClient
from sshtunnel import SSHTunnelForwarder
from docopt import docopt from docopt import docopt
from utils import ColorizeFilter, convert from utils import ColorizeFilter, convert
@@ -68,6 +70,14 @@ class Deluge(object):
self.port = int(config['Deluge']['PORT']) self.port = int(config['Deluge']['PORT'])
self.user = config['Deluge']['USER'] self.user = config['Deluge']['USER']
self.password = config['Deluge']['PASSWORD'] self.password = config['Deluge']['PASSWORD']
self.ssh_host = config['ssh']['HOST']
self.ssh_user = config['ssh']['USER']
self.ssh_pkey = config['ssh']['PKEY']
# self.ssh_host = config['ssh']['HOST']
# self.ssh_user = config['ssh']['USER']
# self.ssh_pkey = config['ssh']['PKEY']
self._connect() self._connect()
def parseResponse(self, response): def parseResponse(self, response):
@@ -78,7 +88,12 @@ class Deluge(object):
return torrents return torrents
def _connect(self): def _connect(self):
print(self.host, self.port, self.user, self.password) logger.info('Checking if script on same server as deluge RPC')
if (socket.gethostbyname(socket.gethostname()) != self.host):
self.tunnel = SSHTunnelForwarder(self.ssh_host, ssh_username=self.ssh_user, ssh_pkey=self.ssh_pkey,
local_bind_address=('localhost', self.port), remote_bind_address=('localhost', self.port))
self.tunnel.start()
self.client = DelugeRPCClient(self.host, self.port, self.user, self.password) self.client = DelugeRPCClient(self.host, self.port, self.user, self.password)
self.client.connect() self.client.connect()
@@ -87,7 +102,7 @@ class Deluge(object):
return self.client.call('core.add_torrent_magnet', url, {}) return self.client.call('core.add_torrent_magnet', url, {})
def ls(self, _filter=None): def ls(self, _filter=None):
if (type(_filter) is list): if (type(_filter) is list and len(_filter)):
if ('seeding' in _filter): if ('seeding' in _filter):
response = self.client.call('core.get_torrents_status', {'state': 'Seeding'}, []) response = self.client.call('core.get_torrents_status', {'state': 'Seeding'}, [])
elif ('downloading' in _filter): elif ('downloading' in _filter):
@@ -130,6 +145,11 @@ class Deluge(object):
response = self.client.call('core.get_torrents_status', {}, ['progress']) response = self.client.call('core.get_torrents_status', {}, ['progress'])
torrents = self.parseResponse(response) torrents = self.parseResponse(response)
def __del__(self):
if hasattr(self, 'tunnel'):
logger.info('Closing ssh tunnel')
self.tunnel.stop()
class Torrent(object): class Torrent(object):
def __init__(self, key, name, progress, eta, save_path, state, paused, finished, files): def __init__(self, key, name, progress, eta, save_path, state, paused, finished, files):
super(Torrent, self).__init__() super(Torrent, self).__init__()
@@ -174,6 +194,8 @@ def getConfig():
config.read(config_dir) config.read(config_dir)
config_values = list(dict(config.items('Deluge')).values()) config_values = list(dict(config.items('Deluge')).values())
config_values.extend(list(dict(config.items('ssh')).values()))
if any(value.startswith('YOUR') for value in config_values): if any(value.startswith('YOUR') for value in config_values):
raise ValueError('Please set variables in config.ini file.') raise ValueError('Please set variables in config.ini file.')

View File

@@ -1,3 +1,4 @@
deluge-client>=1.4.0 deluge-client>=1.4.0
docopt==0.6.2 docopt==0.6.2
colored==1.3.5 colored==1.3.5
sshtunnel>=0.1.3