Exemplo n.º 1
0
 def post_message(self, msg, callback=None, nak=None, onions=None):
     if msg in self.headers:
         return
     raw = msg.serialize()
     nhdr = RawMessageHeader.deserialize(raw)
     fields = []
     files = [('message', 'message', raw.decode())]
     content_type, body = encode_multipart_formdata(fields, files)
     headers = {
         "Content-Type": content_type,
         'content-length': str(len(body))
     }
     r = self.post(_upload_message,
                   body,
                   headers=headers,
                   callback=callback,
                   nak=nak,
                   onions=onions)
     if r is None:
         return None
     self._insert_lock.acquire()
     if nhdr not in self.headers:
         self.headers.insert(0, nhdr)
     self._insert_lock.release()
     self.cache_dirty = True
     return r
Exemplo n.º 2
0
 def _sync_headers(self):
     now = time.time()
     if not self.cache_dirty:
         delay = now - self.last_sync
         if (now - self.last_sync) < _cache_expire_time:
             return True
     #print('request headers from ' + self.baseurl)
     r = None
     try:
         r = requests.get(self.baseurl + _server_time,
                          timeout=_default_timeout)
     except (Timeout, ConnectionError, HTTPError):
         return False
     if r.status_code != 200:
         return False
     servertime = json.loads(r.text)['time']
     for h in self.headers:
         if servertime > h.expire:
             self._insert_lock.acquire()
             #print('expiring ' + h.Iraw().decode())
             self.headers.remove(h)
             self._insert_lock.release()
     self.last_sync = time.time()
     try:
         r = self.session.get(self.baseurl + _headers_since +
                              str(self.servertime))
     except (Timeout, ConnectionError, HTTPError):
         return False
     if r.status_code != 200:
         return False
     self.servertime = servertime
     self.cache_dirty = False
     #remote = sorted(json.loads(r.text)['header_list'],
     #                key=lambda k: int(k[6:14],16), reverse=True)
     remote = json.loads(r.text)['header_list']
     for rstr in reversed(remote):
         rhdr = RawMessageHeader()
         if rhdr._deserialize_header(rstr.encode()):
             self._insert_lock.acquire()
             if rhdr not in self.headers:
                 self.headers.insert(0, rhdr)
             self._insert_lock.release()
     self._insert_lock.acquire()
     self.headers.sort(reverse=True)
     self._insert_lock.release()
     return True
Exemplo n.º 3
0
 def _sync_headers(self):
     now = time.time()
     if not self.cache_dirty:
         delay = now - self.last_sync
         if (now - self.last_sync) < _cache_expire_time:
             return True
     #print('request headers from ' + self.baseurl)
     r = None
     try:
         r = requests.get(self.baseurl + _server_time, timeout = _default_timeout)
     except (Timeout, ConnectionError, HTTPError):
         return False
     if r.status_code != 200:
         return False
     servertime = json.loads(r.text)['time']
     for h in self.headers:
         if servertime > h.expire:
             self._insert_lock.acquire()
             #print('expiring ' + h.Iraw().decode())
             self.headers.remove(h)
             self._insert_lock.release()
     self.last_sync = time.time()
     try:
         r = self.session.get(self.baseurl + _headers_since + str(self.servertime))
     except (Timeout, ConnectionError, HTTPError):
         return False
     if r.status_code != 200:
         return False
     self.servertime = servertime
     self.cache_dirty = False
     #remote = sorted(json.loads(r.text)['header_list'],
     #                key=lambda k: int(k[6:14],16), reverse=True)
     remote = json.loads(r.text)['header_list']
     for rstr in reversed(remote):
         rhdr = RawMessageHeader()
         if rhdr._deserialize_header(rstr.encode()):
             self._insert_lock.acquire()
             if rhdr not in self.headers:
                 self.headers.insert(0, rhdr)
             self._insert_lock.release()
     self._insert_lock.acquire()
     self.headers.sort(reverse=True)
     self._insert_lock.release()
     return True
Exemplo n.º 4
0
 def _sync_headers(self, onions=None):
     if self.Pkey is None:
         self.refresh()
     now = time.time()
     if not self.cache_dirty:
         delay = now - self.last_sync
         if (now - self.last_sync) < _cache_expire_time:
             return True
     r = self.get(_server_time)
     if r is None:
         return False
     servertime = json.loads(r.decode())['time']
     for h in self.headers:
         if servertime > h.expire:
             self._insert_lock.acquire()
             # print('expiring ' + h.I.compress().decode())
             self.headers.remove(h)
             self._insert_lock.release()
     self.last_sync = time.time()
     r = self.get(_headers_since + str(self.servertime))
     if r is None:
         return False
     self.servertime = servertime
     self.cache_dirty = False
     #remote = sorted(json.loads(r.decode())['header_list'],
     #                key=lambda k: int(k[6:14],16), reverse=True)
     remote = json.loads(r.decode())['header_list']
     for rstr in reversed(remote):
         rhdr = RawMessageHeader()
         if rhdr._deserialize_header(rstr.encode()):
             self._insert_lock.acquire()
             if rhdr not in self.headers:
                 self.headers.insert(0, rhdr)
             self._insert_lock.release()
     self._insert_lock.acquire()
     self.headers.sort(reverse=True)
     self._insert_lock.release()
     return True
Exemplo n.º 5
0
 def _sync_headers(self, onions=None):
     if self.Pkey is None:
         self.refresh()
     now = time.time()
     if not self.cache_dirty:
         delay = now - self.last_sync
         if (now - self.last_sync) < _cache_expire_time:
             return True
     r = self.get(_server_time)
     if r is None:
         return False
     servertime = json.loads(r.decode())['time']
     for h in self.headers:
         if servertime > h.expire:
             self._insert_lock.acquire()
             # print('expiring ' + h.I.compress().decode())
             self.headers.remove(h)
             self._insert_lock.release()
     self.last_sync = time.time()
     r = self.get(_headers_since + str(self.servertime))
     if r is None:
         return False
     self.servertime = servertime
     self.cache_dirty = False
     #remote = sorted(json.loads(r.decode())['header_list'],
     #                key=lambda k: int(k[6:14],16), reverse=True)
     remote = json.loads(r.decode())['header_list']
     for rstr in reversed(remote):
         rhdr = RawMessageHeader()
         if rhdr._deserialize_header(rstr.encode()):
             self._insert_lock.acquire()
             if rhdr not in self.headers:
                 self.headers.insert(0, rhdr)
             self._insert_lock.release()
     self._insert_lock.acquire()
     self.headers.sort(reverse=True)
     self._insert_lock.release()
     return True
Exemplo n.º 6
0
 def post_message(self, msg):
     if msg in self.headers:
         return
     raw = msg.serialize()
     nhdr = RawMessageHeader.deserialize(raw)
     f = io.StringIO(raw.decode())
     files = {'message': ('message', f)}
     try:
         r = self.session.post(self.baseurl + _upload_message, files=files)
     except (Timeout, ConnectionError, HTTPError):
         return
     if r.status_code != 200:
         return
     self._insert_lock.acquire()
     if nhdr not in self.headers:
         self.headers.insert(0,nhdr)
     self._insert_lock.release()
     self.cache_dirty = True
Exemplo n.º 7
0
 def post_message(self, msg):
     if msg in self.headers:
         return
     raw = msg.serialize()
     nhdr = RawMessageHeader.deserialize(raw)
     f = io.StringIO(raw.decode())
     files = {'message': ('message', f)}
     try:
         r = self.session.post(self.baseurl + _upload_message, files=files)
     except (Timeout, ConnectionError, HTTPError):
         return
     if r.status_code != 200:
         return
     self._insert_lock.acquire()
     if nhdr not in self.headers:
         self.headers.insert(0, nhdr)
     self._insert_lock.release()
     self.cache_dirty = True
Exemplo n.º 8
0
 def post_message(self, msg, callback=None, nak=None, onions=None):
     if msg in self.headers:
         return
     raw = msg.serialize()
     nhdr = RawMessageHeader.deserialize(raw)
     fields = []
     files = [('message', 'message', raw.decode())]
     content_type, body = encode_multipart_formdata(fields, files)
     headers = {"Content-Type": content_type, 'content-length': str(len(body))}
     r = self.post(_upload_message, body, headers=headers, callback=callback, nak=nak, onions=onions)
     if r is None:
         return None
     self._insert_lock.acquire()
     if nhdr not in self.headers:
         self.headers.insert(0,nhdr)
     self._insert_lock.release()
     self.cache_dirty = True
     return r
Exemplo n.º 9
0
    def post(self):
        filereq = self.request
        filedata = self.request.files['message'][0]
        #print('filereq =', filereq)
        #print('filedata =', str(filedata))
        recvpath = config['receive_dir'] + str(int(time.time() * 1000))
        logging.debug('receiving file as ' + recvpath)
        fh = open(recvpath, 'wb')
        fh.write(filedata['body'])
        fh.close()

        with open(recvpath, 'rb') as f:
            mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
            ver = mm[:3]
            if ver == 'M01':
                header = mm[:_header_size_w_sig_v1].decode('UTF-8')
            else:
                header = mm[:_header_size_w_sig_b64_v2].decode('UTF-8')
            mm.close()
        mh = RawMessageHeader.deserialize(header)
        I = mh.Iraw().decode()

        logging.debug('received message ' + str(I))
        seek = mcache.get(I)
        if seek is not None:
            logging.info('dup detected')
            os.remove(recvpath)
            self.set_status(400)
            return

        m = MessageFile()
        if m.ingest(recvpath, I) != True:
            logging.info('ingest failed for message ' + I)
            os.remove(recvpath)
            self.set_status(400)
            return

        msgpath = config['message_dir'] + I
        m.move_to(msgpath)
        mcache.add(m)
        #messagelist.insert(0,m)
        self.write(m.metadata())
Exemplo n.º 10
0
    def post(self):
        filereq = self.request
        filedata = self.request.files['message'][0]
        #print('filereq =', filereq)
        #print('filedata =', str(filedata))
        recvpath = config['receive_dir'] + str(int(time.time() * 1000))
        logging.debug('receiving file as ' + recvpath )
        fh = open(recvpath, 'wb')
        fh.write(filedata['body'])
        fh.close()
    
        with open(recvpath,'rb') as f :
            mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
            ver = mm[:3]
            if ver == 'M01':
                header = mm[:_header_size_w_sig_v1].decode('UTF-8')
            else:
                header = mm[:_header_size_w_sig_b64_v2].decode('UTF-8')
            mm.close()
        mh = RawMessageHeader.deserialize(header)
        I = mh.Iraw().decode()

        logging.debug('received message ' + str(I) )
        seek = mcache.get(I)
        if seek is not None:
            logging.info ('dup detected')
            os.remove(recvpath)
            self.set_status(400)
            return
        
        m = MessageFile()
        if m.ingest(recvpath,I) != True :
            logging.info('ingest failed for message ' + I)
            os.remove(recvpath)
            self.set_status(400)
            return

        msgpath = config['message_dir'] + I
        m.move_to(msgpath)
        mcache.add(m)
        #messagelist.insert(0,m)
        self.write(m.metadata())
Exemplo n.º 11
0
        md = Message.deserialize(ms)
        mid = Message.deserialize(mis)
        mad = Message.deserialize(mas)

        assert md.decode(pkey[t])
        assert md.decode_sent(pkey[f], m.altK)
        assert md.is_from(Pkey[f])
        assert mid.decode(pkey[t])
        assert mid.decode_sent(pkey[f], mi.altK)
        assert mid.is_from(Pkey[f])
        assert mad.decode(pkey[t])
        assert not mad.decode_sent(pkey[f], ma.altK)
        assert not mad.is_from(Pkey[f])

        nmh = MessageHeader.deserialize(ms)
        nrmh = RawMessageHeader.deserialize(ms)

        assert nmh == nrmh

        assert (m != mi)
        if m > mi:
            assert mi < m
            assert m >= mi
            assert not m <= mi
            assert mi < nmh
            assert nmh >= mi
            assert not nmh <= mi
            assert mi < nrmh
            assert nrmh >= mi
            assert not nrmh <= mi
        else:
        md = Message.deserialize(ms)
        mid = Message.deserialize(mis)
        mad = Message.deserialize(mas)

        assert md.decode(pkey[t])
        assert md.decode_sent(pkey[f], m.altK)
        assert md.is_from(Pkey[f])
        assert mid.decode(pkey[t])
        assert mid.decode_sent(pkey[f], mi.altK)
        assert mid.is_from(Pkey[f])
        assert mad.decode(pkey[t])
        assert not mad.decode_sent(pkey[f], ma.altK)
        assert not mad.is_from(Pkey[f])

        nmh = MessageHeader.deserialize(ms)
        nrmh = RawMessageHeader.deserialize(ms)

        assert nmh == nrmh

        assert (m != mi)
        if m > mi:
            assert mi < m
            assert m >= mi
            assert not m <= mi
            assert mi < nmh
            assert nmh >= mi
            assert not nmh <= mi
            assert mi < nrmh
            assert nrmh >= mi
            assert not nrmh <= mi
        else: