migrate users
This commit is contained in:
parent
199fcfa418
commit
504fb1b238
4 changed files with 55 additions and 9 deletions
|
@ -5,15 +5,16 @@
|
|||
|
||||
import logging
|
||||
|
||||
import requests
|
||||
import ed25519
|
||||
import json
|
||||
import tor_request
|
||||
|
||||
import settings
|
||||
|
||||
logger = logging.getLogger('oml.directory')
|
||||
|
||||
base = settings.server['directory_service']
|
||||
base = 'http://hpjats6xixrleoqg.onion:25519'
|
||||
|
||||
def get(vk):
|
||||
id = vk.to_ascii(encoding='base64').decode()
|
||||
|
@ -22,12 +23,14 @@ def get(vk):
|
|||
'User-Agent': settings.USER_AGENT
|
||||
}
|
||||
try:
|
||||
r = requests.get(url, headers=headers)
|
||||
opener = tor_request.get_opener()
|
||||
opener.addheaders = list(zip(headers.keys(), headers.values()))
|
||||
r = opener.open(url)
|
||||
except:
|
||||
logger.info('get failed %s', url)
|
||||
logger.info('get failed %s', url, exc_info=1)
|
||||
return None
|
||||
sig = r.headers.get('X-Ed25519-Signature')
|
||||
data = r.content
|
||||
data = r.read()
|
||||
if sig and data:
|
||||
vk = ed25519.VerifyingKey(id, encoding='base64')
|
||||
try:
|
||||
|
@ -49,8 +52,11 @@ def put(sk, data):
|
|||
'X-Ed25519-Signature': sig
|
||||
}
|
||||
try:
|
||||
r = requests.put(url, data, headers=headers, timeout=2)
|
||||
#r = requests.put(url, data, headers=headers, timeout=2)
|
||||
opener = tor_request.get_opener()
|
||||
opener.addheaders = list(zip(headers.keys(), headers.values()))
|
||||
r = opener.open(url, data)
|
||||
except:
|
||||
logger.info('put failed: %s', data)
|
||||
logger.info('put failed: %s', data, exc_info=1)
|
||||
return False
|
||||
return r.status_code == 200
|
||||
return r.status == 200
|
||||
|
|
28
oml/nodes.py
28
oml/nodes.py
|
@ -76,12 +76,13 @@ class Node(Thread):
|
|||
|
||||
@property
|
||||
def url(self):
|
||||
url = None
|
||||
if self.local:
|
||||
if ':' in self.local:
|
||||
url = 'https://[%s]:%s' % (self.local, self.port)
|
||||
else:
|
||||
url = 'https://%s:%s' % (self.local, self.port)
|
||||
else:
|
||||
elif len(self.user_id) == 16:
|
||||
url = 'https://%s.onion:9851' % self.user_id
|
||||
return url
|
||||
|
||||
|
@ -95,6 +96,22 @@ class Node(Thread):
|
|||
else:
|
||||
self.local = None
|
||||
self.port = 9851
|
||||
if len(self.user_id) == 43:
|
||||
self.migrate_id()
|
||||
|
||||
|
||||
def migrate_id(self):
|
||||
key = self.user_id.encode()
|
||||
vk = ed25519.VerifyingKey(key, encoding=ENCODING)
|
||||
try:
|
||||
r = directory.get(vk)
|
||||
except:
|
||||
logger.debug('directory failed', exc_info=1)
|
||||
r = None
|
||||
if r and 'id' in r and len(r['id']) == 16:
|
||||
u = self.user
|
||||
self.user_id = r['id']
|
||||
u.migreate_id(self.user_id)
|
||||
|
||||
def get_local(self):
|
||||
if self._nodes and self._nodes._local:
|
||||
|
@ -421,6 +438,7 @@ class Nodes(Thread):
|
|||
return Thread.join(self)
|
||||
|
||||
def publish_node():
|
||||
logger.debug('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
|
||||
update_online()
|
||||
state.check_nodes = PeriodicCallback(check_nodes, 120000)
|
||||
state.check_nodes.start()
|
||||
|
@ -428,13 +446,21 @@ def publish_node():
|
|||
state._online.start()
|
||||
|
||||
def update_online():
|
||||
logger.debug('=======================================================')
|
||||
online = state.tor and state.tor.is_online()
|
||||
logger.debug('update online %s', online)
|
||||
if online != state.online:
|
||||
state.online = online
|
||||
trigger_event('status', {
|
||||
'id': settings.USER_ID,
|
||||
'online': state.online
|
||||
})
|
||||
if state.online:
|
||||
r = directory.put(settings.sk, {
|
||||
'id': settings.USER_ID,
|
||||
})
|
||||
logger.debug('push id to directory %s', r)
|
||||
logger.debug('=======================================================')
|
||||
|
||||
def check_nodes():
|
||||
if state.online:
|
||||
|
|
|
@ -46,7 +46,8 @@ def create_tor_connection(address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
|
|||
sock = socks.socksocket(af, socktype, proto)
|
||||
if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
|
||||
sock.settimeout(timeout)
|
||||
sock.set_proxy(socks.SOCKS5, "localhost", state.tor.socks_port, True)
|
||||
socks_port = state.tor.socks_port if state.tor else 9150
|
||||
sock.set_proxy(socks.SOCKS5, "localhost", socks_port, True)
|
||||
if source_address:
|
||||
sock.bind(source_address)
|
||||
sock.connect(sa)
|
||||
|
|
|
@ -128,6 +128,19 @@ class User(db.Model):
|
|||
n += 1
|
||||
self.nickname = nickname
|
||||
|
||||
def migrate_id(self, service_id):
|
||||
if len(service_id) == 16:
|
||||
statements = [
|
||||
"UPDATE user SET id = '{nid}' WHERE id = '{oid}'",
|
||||
"UPDATE list SET user_id = '{nid}' WHERE user_id = '{oid}'",
|
||||
"UPDATE useritem SET user_id = '{nid}' WHERE user_id = '{oid}'",
|
||||
"UPDATE changelog SET user_id = '{nid}' WHERE user_id = '{oid}'",
|
||||
]
|
||||
with db.session() as session:
|
||||
for sql in statements:
|
||||
session.connection().execut(sql.format(oid=self.id, nid=service_id))
|
||||
session.commit()
|
||||
|
||||
list_items = sa.Table('listitem', db.metadata,
|
||||
sa.Column('list_id', sa.Integer(), sa.ForeignKey('list.id')),
|
||||
sa.Column('item_id', sa.String(32), sa.ForeignKey('item.id'))
|
||||
|
|
Loading…
Reference in a new issue