2014-05-04 17:26:43 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
|
2014-09-02 22:32:44 +00:00
|
|
|
|
|
|
|
from queue import Queue
|
2014-05-04 17:26:43 +00:00
|
|
|
from threading import Thread
|
|
|
|
import json
|
2014-10-31 14:47:54 +00:00
|
|
|
from io import BytesIO
|
2014-05-18 23:24:04 +00:00
|
|
|
import gzip
|
2014-09-02 22:32:44 +00:00
|
|
|
import urllib.request, urllib.error, urllib.parse
|
2014-05-04 17:26:43 +00:00
|
|
|
from datetime import datetime
|
|
|
|
import os
|
2014-05-19 15:00:33 +00:00
|
|
|
import time
|
2014-05-04 17:26:43 +00:00
|
|
|
|
|
|
|
import ox
|
2014-05-18 23:24:04 +00:00
|
|
|
from tornado.ioloop import PeriodicCallback
|
2014-05-04 17:26:43 +00:00
|
|
|
|
|
|
|
import settings
|
|
|
|
import user.models
|
|
|
|
from changelog import Changelog
|
|
|
|
|
|
|
|
from websocket import trigger_event
|
2014-05-12 12:57:47 +00:00
|
|
|
from localnodes import LocalNodes
|
2015-11-26 00:26:10 +00:00
|
|
|
from tor_request import get_opener
|
2016-02-08 09:03:21 +00:00
|
|
|
from utils import user_sort_key
|
2014-05-21 22:41:29 +00:00
|
|
|
import state
|
2014-08-09 16:14:14 +00:00
|
|
|
import db
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2014-05-17 14:26:59 +00:00
|
|
|
import logging
|
2015-11-29 14:56:38 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-05-17 14:26:59 +00:00
|
|
|
|
2014-05-04 17:26:43 +00:00
|
|
|
ENCODING='base64'
|
|
|
|
|
2014-05-19 15:00:33 +00:00
|
|
|
class Node(Thread):
|
2014-09-02 23:09:42 +00:00
|
|
|
host = None
|
2015-12-01 08:59:52 +00:00
|
|
|
local = None
|
2015-12-02 21:05:23 +00:00
|
|
|
_online = None
|
2014-05-04 17:26:43 +00:00
|
|
|
download_speed = 0
|
2014-05-18 23:50:05 +00:00
|
|
|
TIMEOUT = 5
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2014-05-12 12:57:47 +00:00
|
|
|
def __init__(self, nodes, user):
|
|
|
|
self._nodes = nodes
|
2016-01-12 07:32:39 +00:00
|
|
|
self.user = user
|
2014-05-04 17:26:43 +00:00
|
|
|
self.user_id = user.id
|
2015-11-26 00:26:10 +00:00
|
|
|
self._opener = get_opener(self.user_id)
|
2014-05-19 15:00:33 +00:00
|
|
|
self._q = Queue()
|
|
|
|
Thread.__init__(self)
|
|
|
|
self.daemon = True
|
|
|
|
self.ping()
|
2016-01-31 16:45:14 +00:00
|
|
|
self.start()
|
2014-05-19 15:00:33 +00:00
|
|
|
|
|
|
|
def run(self):
|
2016-01-31 16:45:14 +00:00
|
|
|
while not state.shutdown:
|
2014-08-09 16:14:14 +00:00
|
|
|
action = self._q.get()
|
2016-01-31 16:45:14 +00:00
|
|
|
if state.shutdown:
|
2014-08-09 16:14:14 +00:00
|
|
|
break
|
2016-01-17 13:12:56 +00:00
|
|
|
if action == 'send_response':
|
|
|
|
self._send_response()
|
2015-12-01 08:59:52 +00:00
|
|
|
elif action == 'ping':
|
|
|
|
self.online = self.can_connect()
|
|
|
|
else:
|
|
|
|
logger.debug('unknown action %s', action)
|
2014-05-19 15:00:33 +00:00
|
|
|
|
|
|
|
def join(self):
|
2015-12-01 08:59:52 +00:00
|
|
|
self._q.put('')
|
2014-08-09 18:32:41 +00:00
|
|
|
#return Thread.join(self)
|
2014-05-19 15:00:33 +00:00
|
|
|
|
|
|
|
def ping(self):
|
2015-12-02 21:05:23 +00:00
|
|
|
if state.online:
|
|
|
|
self._q.put('ping')
|
2014-05-19 15:00:33 +00:00
|
|
|
|
2014-05-04 17:26:43 +00:00
|
|
|
@property
|
|
|
|
def url(self):
|
2015-11-26 11:06:01 +00:00
|
|
|
url = None
|
2015-11-26 00:26:10 +00:00
|
|
|
if self.local:
|
|
|
|
if ':' in self.local:
|
|
|
|
url = 'https://[%s]:%s' % (self.local, self.port)
|
2014-05-12 12:57:47 +00:00
|
|
|
else:
|
2015-11-26 00:26:10 +00:00
|
|
|
url = 'https://%s:%s' % (self.local, self.port)
|
2015-11-26 11:06:01 +00:00
|
|
|
elif len(self.user_id) == 16:
|
2015-11-26 00:26:10 +00:00
|
|
|
url = 'https://%s.onion:9851' % self.user_id
|
2014-05-04 17:26:43 +00:00
|
|
|
return url
|
|
|
|
|
2015-12-02 21:05:23 +00:00
|
|
|
@property
|
|
|
|
def online(self):
|
|
|
|
return self._online
|
|
|
|
|
|
|
|
@online.setter
|
|
|
|
def online(self, online):
|
|
|
|
if self._online != online:
|
|
|
|
self._online = online
|
|
|
|
self.trigger_status()
|
|
|
|
else:
|
|
|
|
self._online = online
|
|
|
|
|
2014-05-13 10:36:02 +00:00
|
|
|
def resolve(self):
|
2015-12-01 10:51:58 +00:00
|
|
|
#logger.debug('resolve node %s', self.user_id)
|
2014-05-22 14:20:40 +00:00
|
|
|
r = self.get_local()
|
2014-05-04 17:26:43 +00:00
|
|
|
if r:
|
2015-11-26 00:26:10 +00:00
|
|
|
self.local = r['host']
|
2014-05-04 17:26:43 +00:00
|
|
|
if 'port' in r:
|
|
|
|
self.port = r['port']
|
|
|
|
else:
|
2015-11-26 00:26:10 +00:00
|
|
|
self.local = None
|
2014-05-04 17:26:43 +00:00
|
|
|
self.port = 9851
|
2015-11-26 11:06:01 +00:00
|
|
|
if len(self.user_id) == 43:
|
|
|
|
self.migrate_id()
|
|
|
|
|
|
|
|
|
|
|
|
def migrate_id(self):
|
2016-01-18 14:06:21 +00:00
|
|
|
import ed25519
|
2016-01-21 16:31:20 +00:00
|
|
|
from . import directory
|
2015-11-26 11:06:01 +00:00
|
|
|
key = self.user_id.encode()
|
|
|
|
vk = ed25519.VerifyingKey(key, encoding=ENCODING)
|
|
|
|
try:
|
|
|
|
r = directory.get(vk)
|
|
|
|
except:
|
2016-01-24 09:13:03 +00:00
|
|
|
logger.debug('directory failed', exc_info=True)
|
2015-11-26 11:06:01 +00:00
|
|
|
r = None
|
|
|
|
if r and 'id' in r and len(r['id']) == 16:
|
|
|
|
u = self.user
|
|
|
|
self.user_id = r['id']
|
2015-11-26 11:08:22 +00:00
|
|
|
u.migrate_id(self.user_id)
|
2015-11-26 12:40:39 +00:00
|
|
|
self._opener = get_opener(self.user_id)
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2014-05-12 12:57:47 +00:00
|
|
|
def get_local(self):
|
|
|
|
if self._nodes and self._nodes._local:
|
2015-11-26 00:26:10 +00:00
|
|
|
return self._nodes._local.get(self.user_id)
|
2014-05-12 12:57:47 +00:00
|
|
|
return None
|
|
|
|
|
2014-05-04 17:26:43 +00:00
|
|
|
def request(self, action, *args):
|
2015-12-02 15:30:37 +00:00
|
|
|
logger.debug('request[%s] %s%s', self.user_id, action, args)
|
2015-11-26 00:26:10 +00:00
|
|
|
self.resolve()
|
2014-05-13 10:36:02 +00:00
|
|
|
url = self.url
|
2015-12-01 08:59:52 +00:00
|
|
|
if not url:
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.debug('unable to find host %s', self.user_id)
|
2014-05-13 10:36:02 +00:00
|
|
|
self.online = False
|
2014-05-04 17:26:43 +00:00
|
|
|
return None
|
2015-12-02 15:30:37 +00:00
|
|
|
#logger.debug('url=%s', url)
|
2014-09-09 10:08:04 +00:00
|
|
|
content = json.dumps([action, args]).encode()
|
2014-05-04 17:26:43 +00:00
|
|
|
headers = {
|
|
|
|
'User-Agent': settings.USER_AGENT,
|
2014-05-21 22:41:29 +00:00
|
|
|
'X-Node-Protocol': settings.NODE_PROTOCOL,
|
2014-05-04 17:26:43 +00:00
|
|
|
'Accept': 'text/plain',
|
|
|
|
'Accept-Encoding': 'gzip',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
}
|
2015-11-26 00:26:10 +00:00
|
|
|
self._opener.addheaders = list(zip(headers.keys(), headers.values()))
|
2015-12-02 15:30:37 +00:00
|
|
|
#logger.debug('headers: %s', self._opener.addheaders)
|
2014-05-14 09:57:11 +00:00
|
|
|
try:
|
2014-09-02 23:09:42 +00:00
|
|
|
self._opener.timeout = self.TIMEOUT
|
|
|
|
r = self._opener.open(url, data=content)
|
2014-09-02 22:32:44 +00:00
|
|
|
except urllib.error.HTTPError as e:
|
2014-05-14 09:57:11 +00:00
|
|
|
if e.code == 403:
|
2016-01-14 12:26:01 +00:00
|
|
|
logger.debug('403: %s (%s)', url, self.user_id)
|
|
|
|
if state.tasks:
|
|
|
|
state.tasks.queue('peering', (self.user_id, False))
|
|
|
|
del self._nodes[self.user_id]
|
|
|
|
self.online = False
|
|
|
|
return None
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.debug('urllib2.HTTPError %s %s', e, e.code)
|
2014-05-14 09:57:11 +00:00
|
|
|
self.online = False
|
|
|
|
return None
|
2014-09-02 22:32:44 +00:00
|
|
|
except urllib.error.URLError as e:
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.debug('urllib2.URLError %s', e)
|
2014-05-14 09:57:11 +00:00
|
|
|
self.online = False
|
|
|
|
return None
|
|
|
|
except:
|
2016-01-24 09:13:03 +00:00
|
|
|
logger.debug('unknown url error', exc_info=True)
|
2014-05-14 09:57:11 +00:00
|
|
|
self.online = False
|
|
|
|
return None
|
|
|
|
data = r.read()
|
2014-05-18 23:24:04 +00:00
|
|
|
if r.headers.get('content-encoding', None) == 'gzip':
|
2014-10-31 14:47:54 +00:00
|
|
|
data = gzip.GzipFile(fileobj=BytesIO(data)).read()
|
2014-05-21 22:41:29 +00:00
|
|
|
|
|
|
|
version = r.headers.get('X-Node-Protocol', None)
|
|
|
|
if version != settings.NODE_PROTOCOL:
|
2016-01-17 15:00:19 +00:00
|
|
|
logger.debug('version does not match local: %s remote %s (%s)', settings.NODE_PROTOCOL, version, self.user_id)
|
2014-05-21 22:41:29 +00:00
|
|
|
self.online = False
|
|
|
|
if version > settings.NODE_PROTOCOL:
|
|
|
|
state.update_required = True
|
|
|
|
return None
|
|
|
|
|
2015-11-26 00:26:10 +00:00
|
|
|
response = json.loads(data.decode('utf-8'))
|
2014-05-04 17:26:43 +00:00
|
|
|
return response
|
|
|
|
|
2014-05-18 03:01:24 +00:00
|
|
|
def can_connect(self):
|
2015-12-01 08:59:52 +00:00
|
|
|
self.resolve()
|
|
|
|
url = self.url
|
2014-05-18 03:01:24 +00:00
|
|
|
try:
|
2014-05-22 14:20:40 +00:00
|
|
|
if url:
|
|
|
|
headers = {
|
|
|
|
'User-Agent': settings.USER_AGENT,
|
|
|
|
'X-Node-Protocol': settings.NODE_PROTOCOL,
|
|
|
|
'Accept-Encoding': 'gzip',
|
|
|
|
}
|
2015-11-26 00:26:10 +00:00
|
|
|
self._opener.addheaders = list(zip(headers.keys(), headers.values()))
|
2015-12-01 10:51:58 +00:00
|
|
|
self._opener.timeout = 2
|
2014-09-02 23:09:42 +00:00
|
|
|
r = self._opener.open(url)
|
2014-05-22 14:20:40 +00:00
|
|
|
version = r.headers.get('X-Node-Protocol', None)
|
|
|
|
if version != settings.NODE_PROTOCOL:
|
|
|
|
logger.debug('version does not match local: %s remote %s', settings.NODE_PROTOCOL, version)
|
|
|
|
return False
|
|
|
|
c = r.read()
|
2016-01-12 07:32:39 +00:00
|
|
|
logger.debug('can connect to: %s', url)
|
2014-05-22 14:20:40 +00:00
|
|
|
return True
|
2014-05-18 03:01:24 +00:00
|
|
|
except:
|
2016-01-12 07:32:39 +00:00
|
|
|
logger.debug('can not connect to: %s', url)
|
2014-05-18 03:01:24 +00:00
|
|
|
pass
|
|
|
|
return False
|
|
|
|
|
2015-12-01 08:59:52 +00:00
|
|
|
def is_online(self):
|
|
|
|
return self.online or self.get_local() != None
|
|
|
|
|
2016-01-17 13:12:56 +00:00
|
|
|
def send_response(self):
|
|
|
|
self._q.put('send_response')
|
|
|
|
|
|
|
|
def _send_response(self):
|
2016-01-12 07:32:39 +00:00
|
|
|
with db.session():
|
|
|
|
u = user.models.User.get_or_create(self.user_id)
|
|
|
|
if u.peered or u.queued:
|
2016-01-17 13:12:56 +00:00
|
|
|
logger.debug('go online peered=%s queued=%s %s (%s)', u.peered, u.queued, u.id, u.nickname)
|
2016-01-12 07:32:39 +00:00
|
|
|
try:
|
|
|
|
self.online = self.can_connect()
|
|
|
|
if self.online:
|
|
|
|
logger.debug('connected to %s', self.url)
|
|
|
|
if u.queued:
|
|
|
|
logger.debug('queued peering event pending=%s peered=%s', u.pending, u.peered)
|
|
|
|
if u.pending == 'sent':
|
|
|
|
self.peering('requestPeering')
|
|
|
|
elif u.pending == '' and u.peered:
|
|
|
|
self.peering('acceptPeering')
|
|
|
|
else:
|
|
|
|
#fixme, what about cancel/reject peering here?
|
|
|
|
self.peering('removePeering')
|
|
|
|
except:
|
|
|
|
logger.debug('failed to connect to %s', self.user_id)
|
|
|
|
self.online = False
|
2014-05-19 15:00:33 +00:00
|
|
|
|
|
|
|
def trigger_status(self):
|
2015-12-02 21:05:23 +00:00
|
|
|
if self.online is not None:
|
|
|
|
trigger_event('status', {
|
|
|
|
'id': self.user_id,
|
|
|
|
'online': self.online
|
|
|
|
})
|
2014-05-04 17:26:43 +00:00
|
|
|
|
|
|
|
def pullChanges(self):
|
2015-12-25 14:58:41 +00:00
|
|
|
with db.session():
|
2016-01-12 07:32:39 +00:00
|
|
|
u = user.models.User.get_or_create(self.user_id)
|
|
|
|
if not self.online or not u.peered:
|
|
|
|
return True
|
|
|
|
last = Changelog.query.filter_by(user_id=self.user_id).order_by('-revision').first()
|
|
|
|
from_revision = last.revision + 1 if last else 0
|
|
|
|
try:
|
|
|
|
changes = self.request('pullChanges', from_revision)
|
|
|
|
except:
|
|
|
|
self.online = False
|
|
|
|
logger.debug('%s went offline', u.name)
|
|
|
|
return False
|
|
|
|
if not changes:
|
|
|
|
return False
|
2016-01-24 09:11:40 +00:00
|
|
|
try:
|
|
|
|
r = Changelog.apply_changes(u, changes, first=from_revision == 0)
|
|
|
|
except:
|
|
|
|
logger.debug('apply_changes failed', exc_info=True)
|
|
|
|
r = False
|
2016-01-12 07:32:39 +00:00
|
|
|
return r
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2014-05-18 03:01:24 +00:00
|
|
|
def peering(self, action):
|
2016-01-12 07:32:39 +00:00
|
|
|
with db.session():
|
|
|
|
u = user.models.User.get_or_create(self.user_id)
|
|
|
|
if action in ('requestPeering', 'acceptPeering'):
|
|
|
|
r = self.request(action, settings.preferences['username'], u.info.get('message'))
|
|
|
|
else:
|
|
|
|
r = self.request(action, u.info.get('message'))
|
|
|
|
if r != None:
|
|
|
|
u.queued = False
|
|
|
|
if 'message' in u.info:
|
|
|
|
del u.info['message']
|
|
|
|
u.save()
|
|
|
|
else:
|
|
|
|
logger.debug('peering failed? %s %s', action, r)
|
|
|
|
if action in ('cancelPeering', 'rejectPeering', 'removePeering'):
|
|
|
|
self.online = False
|
|
|
|
trigger_event('peering.%s'%action.replace('Peering', ''), u.json())
|
|
|
|
return True
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2016-01-15 07:28:01 +00:00
|
|
|
headers = {
|
|
|
|
'X-Node-Protocol': settings.NODE_PROTOCOL,
|
|
|
|
'User-Agent': settings.USER_AGENT,
|
|
|
|
'Accept-Encoding': 'gzip',
|
|
|
|
}
|
|
|
|
|
2014-05-04 17:26:43 +00:00
|
|
|
def download(self, item):
|
2014-05-21 22:41:29 +00:00
|
|
|
from item.models import Transfer
|
2015-12-01 08:59:52 +00:00
|
|
|
self.resolve()
|
2014-05-04 17:26:43 +00:00
|
|
|
url = '%s/get/%s' % (self.url, item.id)
|
2014-05-20 00:43:54 +00:00
|
|
|
t1 = datetime.utcnow()
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.debug('download %s', url)
|
2016-01-15 07:28:01 +00:00
|
|
|
self._opener.addheaders = list(zip(self.headers.keys(), self.headers.values()))
|
2014-09-05 17:10:47 +00:00
|
|
|
try:
|
2016-01-18 15:45:59 +00:00
|
|
|
r = self._opener.open(url, timeout=self.TIMEOUT*5)
|
2014-09-05 17:10:47 +00:00
|
|
|
except:
|
2016-01-24 09:13:03 +00:00
|
|
|
logger.debug('openurl failed %s', url, exc_info=True)
|
2014-09-05 17:10:47 +00:00
|
|
|
return False
|
2014-05-14 09:57:11 +00:00
|
|
|
if r.getcode() == 200:
|
2014-09-05 17:10:47 +00:00
|
|
|
try:
|
2015-11-30 23:26:35 +00:00
|
|
|
fileobj = r
|
2014-09-05 17:10:47 +00:00
|
|
|
if r.headers.get('content-encoding', None) == 'gzip':
|
2015-11-30 23:26:35 +00:00
|
|
|
fileobj = gzip.GzipFile(fileobj=r)
|
|
|
|
content = b''
|
|
|
|
ct = datetime.utcnow()
|
|
|
|
size = 0
|
2016-01-18 15:45:59 +00:00
|
|
|
chunk_size = 16*1024
|
|
|
|
for chunk in iter(lambda: fileobj.read(chunk_size), b''):
|
2015-11-30 23:26:35 +00:00
|
|
|
content += chunk
|
|
|
|
size += len(chunk)
|
|
|
|
since_ct = (datetime.utcnow() - ct).total_seconds()
|
|
|
|
if since_ct > 1:
|
|
|
|
ct = datetime.utcnow()
|
|
|
|
t = Transfer.get(item.id)
|
2016-01-18 07:00:40 +00:00
|
|
|
if not t.added:
|
|
|
|
# transfer was canceled
|
2016-01-20 07:35:44 +00:00
|
|
|
trigger_event('transfer', {
|
|
|
|
'id': item.id, 'progress': -1
|
|
|
|
})
|
2016-01-18 07:00:40 +00:00
|
|
|
return False
|
|
|
|
else:
|
2016-01-18 15:45:59 +00:00
|
|
|
t.progress = size / item.info['size']
|
2016-01-18 07:00:40 +00:00
|
|
|
t.save()
|
|
|
|
trigger_event('transfer', {
|
|
|
|
'id': item.id, 'progress': t.progress
|
|
|
|
})
|
2016-01-18 15:45:59 +00:00
|
|
|
if state.bandwidth:
|
2016-01-31 16:45:14 +00:00
|
|
|
while not state.bandwidth.download(chunk_size) and not state.shutdown:
|
2016-01-18 15:45:59 +00:00
|
|
|
time.sleep(0.1)
|
2014-09-05 17:10:47 +00:00
|
|
|
t2 = datetime.utcnow()
|
|
|
|
duration = (t2-t1).total_seconds()
|
|
|
|
if duration:
|
2016-01-18 15:45:59 +00:00
|
|
|
self.download_speed = size / duration
|
2014-09-05 17:10:47 +00:00
|
|
|
return item.save_file(content)
|
|
|
|
except:
|
2016-01-24 09:13:03 +00:00
|
|
|
logger.debug('download failed %s', url, exc_info=True)
|
2014-09-05 17:10:47 +00:00
|
|
|
return False
|
2014-05-04 17:26:43 +00:00
|
|
|
else:
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.debug('FAILED %s', url)
|
2014-05-04 17:26:43 +00:00
|
|
|
return False
|
|
|
|
|
2016-01-21 07:05:49 +00:00
|
|
|
def download_preview(self, item_id):
|
2016-01-15 07:28:01 +00:00
|
|
|
from item.icons import icons
|
|
|
|
self.resolve()
|
2016-01-24 10:05:43 +00:00
|
|
|
logger.debug('download preview for %s from %s', item_id, self.url)
|
2016-01-21 07:05:49 +00:00
|
|
|
url = '%s/preview/%s' % (self.url, item_id)
|
2016-01-15 07:28:01 +00:00
|
|
|
self._opener.addheaders = list(zip(self.headers.keys(), self.headers.values()))
|
|
|
|
try:
|
|
|
|
r = self._opener.open(url, timeout=self.TIMEOUT*2)
|
|
|
|
except:
|
2016-01-24 10:05:43 +00:00
|
|
|
logger.debug('download failed %s', url, exc_info=True)
|
2016-02-04 13:07:19 +00:00
|
|
|
self.online = False
|
2016-01-15 07:28:01 +00:00
|
|
|
return False
|
|
|
|
code = r.getcode()
|
|
|
|
if code == 200:
|
|
|
|
try:
|
|
|
|
fileobj = r
|
|
|
|
if r.headers.get('content-encoding', None) == 'gzip':
|
|
|
|
fileobj = gzip.GzipFile(fileobj=r)
|
|
|
|
content = fileobj.read()
|
2016-01-21 07:05:49 +00:00
|
|
|
key = 'preview:' + item_id
|
2016-01-15 07:28:01 +00:00
|
|
|
icons[key] = content
|
2016-01-21 07:05:49 +00:00
|
|
|
icons.clear(key+':')
|
2016-01-15 07:28:01 +00:00
|
|
|
return True
|
|
|
|
except:
|
2016-01-24 09:13:03 +00:00
|
|
|
logger.debug('preview download failed %s', url, exc_info=True)
|
2016-01-15 07:28:01 +00:00
|
|
|
elif code == 404:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
logger.debug('FAILED %s', url)
|
2016-01-24 07:44:43 +00:00
|
|
|
return False
|
2016-01-15 07:28:01 +00:00
|
|
|
|
2014-05-14 09:57:11 +00:00
|
|
|
def download_upgrade(self, release):
|
|
|
|
for module in release['modules']:
|
|
|
|
path = os.path.join(settings.update_path, release['modules'][module]['name'])
|
2014-05-04 17:26:43 +00:00
|
|
|
if not os.path.exists(path):
|
2014-05-14 09:57:11 +00:00
|
|
|
url = '%s/oml/%s' % (self.url, release['modules'][module]['name'])
|
|
|
|
sha1 = release['modules'][module]['sha1']
|
2014-05-04 17:26:43 +00:00
|
|
|
headers = {
|
|
|
|
'User-Agent': settings.USER_AGENT,
|
|
|
|
}
|
2015-11-26 00:26:10 +00:00
|
|
|
self._opener.addheaders = list(zip(headers.keys(), headers.values()))
|
2014-05-14 09:57:11 +00:00
|
|
|
r = self._opener.open(url)
|
|
|
|
if r.getcode() == 200:
|
2014-05-04 17:26:43 +00:00
|
|
|
with open(path, 'w') as fd:
|
2014-05-14 09:57:11 +00:00
|
|
|
fd.write(r.read())
|
2014-05-04 17:26:43 +00:00
|
|
|
if (ox.sha1sum(path) != sha1):
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.error('invalid update!')
|
2014-05-04 17:26:43 +00:00
|
|
|
os.unlink(path)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
class Nodes(Thread):
|
|
|
|
_nodes = {}
|
2014-05-18 03:01:24 +00:00
|
|
|
_local = None
|
2016-01-23 16:49:34 +00:00
|
|
|
_pulling = False
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2014-08-09 16:33:59 +00:00
|
|
|
def __init__(self):
|
2014-05-04 17:26:43 +00:00
|
|
|
self._q = Queue()
|
2015-12-02 21:05:23 +00:00
|
|
|
with db.session():
|
|
|
|
for u in user.models.User.query.filter_by(peered=True):
|
|
|
|
if 'local' in u.info:
|
|
|
|
del u.info['local']
|
|
|
|
u.save()
|
|
|
|
self.queue('add', u.id)
|
|
|
|
for u in user.models.User.query.filter_by(queued=True):
|
|
|
|
logger.debug('adding queued node... %s', u.id)
|
2016-01-17 13:12:56 +00:00
|
|
|
self.queue('add', u.id, True)
|
2014-08-09 16:33:59 +00:00
|
|
|
self._local = LocalNodes()
|
2014-09-01 10:30:09 +00:00
|
|
|
self._cleanup = PeriodicCallback(lambda: self.queue('cleanup'), 120000)
|
|
|
|
self._cleanup.start()
|
2016-01-23 16:49:34 +00:00
|
|
|
self._pullcb = PeriodicCallback(self.pull, 60000)
|
|
|
|
self._pullcb.start()
|
2014-05-04 17:26:43 +00:00
|
|
|
Thread.__init__(self)
|
|
|
|
self.daemon = True
|
|
|
|
self.start()
|
|
|
|
|
2014-09-01 10:30:09 +00:00
|
|
|
def cleanup(self):
|
2016-01-31 16:45:14 +00:00
|
|
|
if not state.shutdown and self._local:
|
2014-09-01 10:30:09 +00:00
|
|
|
self._local.cleanup()
|
|
|
|
|
2016-01-23 16:49:34 +00:00
|
|
|
def pull(self):
|
|
|
|
if state.online and not self._pulling:
|
|
|
|
self.queue('pull')
|
|
|
|
|
2014-05-04 17:26:43 +00:00
|
|
|
def queue(self, *args):
|
|
|
|
self._q.put(list(args))
|
|
|
|
|
2014-05-21 22:41:29 +00:00
|
|
|
def is_online(self, id):
|
2015-12-01 08:59:52 +00:00
|
|
|
return id in self._nodes and self._nodes[id].is_online()
|
2014-05-04 17:26:43 +00:00
|
|
|
|
|
|
|
def download(self, id, item):
|
|
|
|
return id in self._nodes and self._nodes[id].download(item)
|
|
|
|
|
2016-01-21 07:05:49 +00:00
|
|
|
def download_preview(self, id, item):
|
|
|
|
return id in self._nodes and \
|
|
|
|
self._nodes[id].is_online() and \
|
|
|
|
self._nodes[id].download_preview(item)
|
|
|
|
|
2014-05-04 17:26:43 +00:00
|
|
|
def _call(self, target, action, *args):
|
|
|
|
if target == 'all':
|
2014-09-02 22:32:44 +00:00
|
|
|
nodes = list(self._nodes.values())
|
2014-05-14 09:57:11 +00:00
|
|
|
elif target == 'peered':
|
2014-09-02 22:32:44 +00:00
|
|
|
nodes = [n for n in list(self._nodes.values()) if n.user.peered]
|
2014-05-04 17:26:43 +00:00
|
|
|
elif target == 'online':
|
2014-09-02 22:32:44 +00:00
|
|
|
nodes = [n for n in list(self._nodes.values()) if n.online]
|
2014-05-04 17:26:43 +00:00
|
|
|
else:
|
2016-01-17 13:12:56 +00:00
|
|
|
if not target in self._nodes:
|
|
|
|
self._add(target)
|
2014-05-04 17:26:43 +00:00
|
|
|
nodes = [self._nodes[target]]
|
|
|
|
for node in nodes:
|
2015-11-26 00:26:10 +00:00
|
|
|
r = getattr(node, action)(*args)
|
|
|
|
logger.debug('call node api %s->%s%s = %s', node.user_id, action, args, r)
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2016-01-17 13:12:56 +00:00
|
|
|
def _add(self, user_id, send_response=False):
|
2014-05-04 17:26:43 +00:00
|
|
|
if user_id not in self._nodes:
|
|
|
|
from user.models import User
|
2014-08-09 16:14:14 +00:00
|
|
|
with db.session():
|
|
|
|
self._nodes[user_id] = Node(self, User.get_or_create(user_id))
|
2014-05-04 17:26:43 +00:00
|
|
|
else:
|
2014-05-18 03:01:24 +00:00
|
|
|
if not self._nodes[user_id].online:
|
2014-05-19 15:00:33 +00:00
|
|
|
self._nodes[user_id].ping()
|
2016-01-17 13:12:56 +00:00
|
|
|
if send_response:
|
|
|
|
self._nodes[user_id].send_response()
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2016-01-23 16:49:34 +00:00
|
|
|
def _pull(self):
|
|
|
|
if state.activity and state.activity.get('activity') == 'import':
|
|
|
|
return
|
|
|
|
self._pulling = True
|
2016-02-08 09:03:21 +00:00
|
|
|
users = []
|
|
|
|
with db.session():
|
|
|
|
from user.models import User
|
|
|
|
for u in User.query.filter(User.id!=settings.USER_ID).filter_by(peered=True).all():
|
|
|
|
users.append(u.json())
|
|
|
|
users.sort(key=user_sort_key)
|
|
|
|
for u in users:
|
|
|
|
if state.shutdown:
|
|
|
|
break
|
|
|
|
node = self._nodes.get(u['id'])
|
|
|
|
if node:
|
|
|
|
if not state.shutdown:
|
|
|
|
node.online = node.can_connect()
|
|
|
|
if not state.shutdown and node.online:
|
|
|
|
node.pullChanges()
|
2016-01-23 16:49:34 +00:00
|
|
|
self._pulling = False
|
|
|
|
|
2014-05-04 17:26:43 +00:00
|
|
|
def run(self):
|
2016-01-31 16:45:14 +00:00
|
|
|
while not state.shutdown:
|
2014-08-09 16:14:14 +00:00
|
|
|
args = self._q.get()
|
|
|
|
if args:
|
2014-09-01 10:30:09 +00:00
|
|
|
if args[0] == 'cleanup':
|
|
|
|
self.cleanup()
|
|
|
|
elif args[0] == 'add':
|
2016-01-17 13:12:56 +00:00
|
|
|
self._add(*args[1:])
|
2016-01-23 16:49:34 +00:00
|
|
|
elif args[0] == 'pull':
|
|
|
|
self._pull()
|
2014-08-09 16:14:14 +00:00
|
|
|
else:
|
|
|
|
self._call(*args)
|
2014-05-04 17:26:43 +00:00
|
|
|
|
|
|
|
def join(self):
|
|
|
|
self._q.put(None)
|
2014-09-02 22:32:44 +00:00
|
|
|
for node in list(self._nodes.values()):
|
2014-05-19 15:00:33 +00:00
|
|
|
node.join()
|
2015-11-26 00:26:10 +00:00
|
|
|
if self._local:
|
|
|
|
self._local.join()
|
2014-05-04 17:26:43 +00:00
|
|
|
return Thread.join(self)
|
2015-11-26 00:26:10 +00:00
|
|
|
|
|
|
|
def publish_node():
|
|
|
|
update_online()
|
|
|
|
state.check_nodes = PeriodicCallback(check_nodes, 120000)
|
|
|
|
state.check_nodes.start()
|
|
|
|
state._online = PeriodicCallback(update_online, 60000)
|
|
|
|
state._online.start()
|
2016-01-24 10:34:29 +00:00
|
|
|
state.nodes.pull()
|
2015-11-26 00:26:10 +00:00
|
|
|
|
|
|
|
def update_online():
|
|
|
|
online = state.tor and state.tor.is_online()
|
|
|
|
if online != state.online:
|
|
|
|
state.online = online
|
|
|
|
trigger_event('status', {
|
|
|
|
'id': settings.USER_ID,
|
|
|
|
'online': state.online
|
|
|
|
})
|
2016-01-16 12:31:04 +00:00
|
|
|
|
|
|
|
if settings.OLD_USER_ID and not settings.server.get('migrated_id', False):
|
2016-01-24 06:35:14 +00:00
|
|
|
from . import directory
|
2015-11-26 11:06:01 +00:00
|
|
|
r = directory.put(settings.sk, {
|
|
|
|
'id': settings.USER_ID,
|
|
|
|
})
|
|
|
|
logger.debug('push id to directory %s', r)
|
2015-11-26 11:16:09 +00:00
|
|
|
if r:
|
|
|
|
settings.server['migrated_id'] = True
|
2015-11-26 00:26:10 +00:00
|
|
|
|
|
|
|
def check_nodes():
|
|
|
|
if state.online:
|
|
|
|
with db.session():
|
|
|
|
for u in user.models.User.query.filter_by(queued=True):
|
|
|
|
if not state.nodes.is_online(u.id):
|
|
|
|
logger.debug('queued peering message for %s trying to connect...', u.id)
|
2016-01-17 13:12:56 +00:00
|
|
|
state.nodes.queue('add', u.id, True)
|