2017-02-15 17:12:19 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from hashlib import sha1
|
|
|
|
|
|
|
|
from util.config.validators import BaseValidator, ConfigValidationException
|
2017-06-06 19:11:42 +00:00
|
|
|
from util.registry.torrent import jwt_from_infohash
|
2017-02-15 17:12:19 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class BittorrentValidator(BaseValidator):
|
|
|
|
name = "bittorrent"
|
|
|
|
|
|
|
|
@classmethod
|
2018-05-25 19:42:27 +00:00
|
|
|
def validate(cls, validator_context):
|
2017-02-15 17:12:19 +00:00
|
|
|
""" Validates the configuration for using BitTorrent for downloads. """
|
2018-05-25 19:42:27 +00:00
|
|
|
config = validator_context.config
|
|
|
|
client = validator_context.http_client
|
|
|
|
|
2017-02-15 17:12:19 +00:00
|
|
|
announce_url = config.get('BITTORRENT_ANNOUNCE_URL')
|
|
|
|
if not announce_url:
|
|
|
|
raise ConfigValidationException('Missing announce URL')
|
|
|
|
|
|
|
|
# Ensure that the tracker is reachable and accepts requests signed with a registry key.
|
|
|
|
params = {
|
2017-06-06 19:11:42 +00:00
|
|
|
'info_hash': sha1('test').digest(),
|
2017-02-15 17:12:19 +00:00
|
|
|
'peer_id': '-QUAY00-6wfG2wk6wWLc',
|
|
|
|
'uploaded': 0,
|
|
|
|
'downloaded': 0,
|
|
|
|
'left': 0,
|
|
|
|
'numwant': 0,
|
|
|
|
'port': 80,
|
|
|
|
}
|
|
|
|
|
2017-06-06 19:11:42 +00:00
|
|
|
encoded_jwt = jwt_from_infohash(params['info_hash'])
|
2017-02-15 17:12:19 +00:00
|
|
|
params['jwt'] = encoded_jwt
|
|
|
|
|
|
|
|
resp = client.get(announce_url, timeout=5, params=params)
|
|
|
|
logger.debug('Got tracker response: %s: %s', resp.status_code, resp.text)
|
|
|
|
|
|
|
|
if resp.status_code == 404:
|
|
|
|
raise ConfigValidationException('Announce path not found; did you forget `/announce`?')
|
|
|
|
|
|
|
|
if resp.status_code == 500:
|
|
|
|
raise ConfigValidationException('Did not get expected response from Tracker; ' +
|
|
|
|
'please check your settings')
|
|
|
|
|
|
|
|
if resp.status_code == 200:
|
|
|
|
if 'invalid jwt' in resp.text:
|
|
|
|
raise ConfigValidationException('Could not authorize to Tracker; is your Tracker ' +
|
|
|
|
'properly configured?')
|
|
|
|
|
|
|
|
if 'failure reason' in resp.text:
|
|
|
|
raise ConfigValidationException('Could not validate signed announce request: ' + resp.text)
|
2018-04-02 21:59:48 +00:00
|
|
|
|
|
|
|
if 'go_goroutines' in resp.text:
|
|
|
|
raise ConfigValidationException('Could not validate signed announce request: ' +
|
|
|
|
'provided port is used for Prometheus')
|