Пример #1
0
def tryDHTConnect():
    # Start the d.   Node that we would really like to avoid actually having to use this,
    # So although we publish to it, we try local discovery and the local cache first.

    if lastTriedDHTConnect[0] > (time.time() - 10 * 60):
        return

    if dhtContainer[0]:
        return
    try:
        import opendht as dht
    except:
        dht = None
        logger.info(
            "Unable to import openDHT.  If you would like to use this feature, install dhtnode if you are on debian."
        )

    if dht:
        try:
            node = dht.DhtRunner()
            node.run()

            # Join the network through any running node,
            # here using a known bootstrap node.
            node.bootstrap("bootstrap.jami.net", "4222")
            dhtContainer[0] = node
        except:
            logger.info(traceback.format_exc())
Пример #2
0
class DhtServer(resource.Resource):
    isLeaf = True
    node = dht.DhtRunner()

    def __init__(self, port, bootstrap):
        self.node.run(port=port)
        b_url = urlparse('//'+bootstrap)
        self.node.bootstrap(b_url.hostname, str(b_url.port) if b_url.port else '4222')

    def render_GET(self, req):
        uri = req.uri[1:]
        h = dht.InfoHash(uri) if len(uri) == 40 else dht.InfoHash.get(uri.decode())
        print('GET', '"'+uri.decode()+'"', h)
        res = self.node.get(h)
        req.setHeader(b"content-type", b"application/json")
        return json.dumps({'{:x}'.format(v.id):{'base64':base64.b64encode(v.data).decode()} for v in res}).encode()

    def render_POST(self, req):
        uri = req.uri[1:]
        data = req.args[b'data'][0] if b'data' in req.args else None
        if not data and b'base64' in req.args:
            data = base64.b64decode(req.args[b'base64'][0])
        h = dht.InfoHash(uri) if len(uri) == 40 else dht.InfoHash.get(uri.decode())
        print('POST', h, data)
        req.setHeader(b"content-type", b"application/json")
        if data:
            self.node.put(h, dht.Value(data))
            return json.dumps({'success':True}).encode()
        else:
            req.setResponseCode(400)
            return json.dumps({'success':False, 'error':'no data parameter'}).encode()
Пример #3
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("url", help="The URL to share.", nargs='?')
    parser.add_argument("-g",
                        "--get",
                        help="Get shared URL.",
                        action='store_true')
    pa = parser.parse_args()

    h = opendht.InfoHash.get(USER + PWD)
    print_debug("InfoHash: %s" % h.toString())

    dht = opendht.DhtRunner()
    dht.bootstrap(DEFAULT_DHT_BOOTSTRAP_HOST, DEFAULT_DHT_BOOTSTRAP_PORT)
    dht.run()
    print_debug("is running? %s" % dht.isRunning())

    if pa.get:
        lv = get_last_value(dht.get(h), PWD)
        if not lv:
            qute_print("No url found")
            return 1
        url = lv[b"data"].decode()
        qute_cmd(":open -t %s" % url)
        qute_print("URL found: %s" % url)
    else:
        if not pa.url:
            return
        lv = get_last_value(dht.get(h), PWD)
        last_id = lv[b"id"] if lv else -1
        pv = msgpack.packb({"id": last_id + 1, "data": pa.url})
        v = opendht.Value(encrypt(pv, PWD))
        print_debug(v)
        dht.put(h, v)
        qute_print("URL shared")
Пример #4
0
 def startDht(self):
     '''
     Start Dht node (for discovery)
     '''
     config = dht.DhtConfig()
     config.setBootstrapMode(False)  # Server
     self.dht = dht.DhtRunner()
     self.dhtPort = get_random_port()
     self.dht.run(port=self.dhtPort, ipv4=self.hostAddress, config=config)
Пример #5
0
class DhtServer(resource.Resource):
    isLeaf = True
    node = dht.DhtRunner()

    def __init__(self, port, bootstrap):
        self.node.run(port=port)
        b_url = urlparse('//' + bootstrap)
        self.node.bootstrap(b_url.hostname,
                            str(b_url.port) if b_url.port else '4222')

    def render_GET(self, req):
        uri = req.uri[1:].decode().rsplit('?', 1)[0]
        h = dht.InfoHash(
            uri.encode()) if len(uri) == 40 else dht.InfoHash.get(uri)
        w = dht.Where('WHERE ' + ''.join(
            k.decode() + '=' + req.args[k][0].decode() + ','
            for k in req.args.keys() if k in
            [b'id', b'user_type', b'value_type', b'owner', b'seq'])[:-1])
        print('GET', '"' + uri + '"', h, w)
        res = self.node.get(h, where=w)
        req.setHeader(b"content-type", b"application/json")
        return json.dumps({
            '{:x}'.format(v.id): {
                'base64': base64.b64encode(v.data).decode()
            }
            for v in res
        }).encode()

    def render_POST(self, req):
        uri = req.uri[1:]
        data = req.args[b'data'][0] if b'data' in req.args else None
        user_type = req.args[b'user_type'][0].decode(
        ) if b'user_type' in req.args else ""
        try:
            vid = int(req.args[b'id'][0].decode()) if b'id' in req.args else 0
        except ValueError:
            vid = 0
        if not data and b'base64' in req.args:
            data = base64.b64decode(req.args[b'base64'][0])
        h = dht.InfoHash(uri) if len(uri) == 40 else dht.InfoHash.get(
            uri.decode())
        print('POST', h, data)
        req.setHeader(b"content-type", b"application/json")
        if data:
            v = dht.Value(data)
            if vid != 0:
                v.id = vid
            v.user_type = user_type
            self.node.put(h, v)
            return json.dumps({'success': True}).encode()
        else:
            req.setResponseCode(400)
            return json.dumps({
                'success': False,
                'error': 'no data parameter'
            }).encode()
Пример #6
0
 def run_node(ip4, ip6, p, bootstrap=None, is_bootstrap=False, logfile=None):
     logger.info("Running node on port %s with bootstrap %s", p, bootstrap)
     n = dht.DhtRunner()
     n.run(ipv4=ip4 if ip4 else "", ipv6=ip6 if ip6 else "", port=p, is_bootstrap=is_bootstrap)
     if logfile:
         n.enableFileLogging(logfile)
     if bootstrap:
         n.bootstrap(bootstrap[0], bootstrap[1])
     time.sleep(.01)
     return ((ip4, ip6, p), n, id)
Пример #7
0
 def run_node(ip4,
              ip6,
              p,
              bootstrap=None,
              is_bootstrap=False,
              logfile=None):
     print("run_node", ip4, ip6, p, bootstrap, logfile)
     n = dht.DhtRunner()
     n.run(ipv4=ip4 if ip4 else "",
           ipv6=ip6 if ip6 else "",
           port=p,
           is_bootstrap=is_bootstrap)
     if logfile:
         n.enableFileLogging(logfile)
     if bootstrap:
         n.bootstrap(bootstrap[0], bootstrap[1])
     time.sleep(.01)
     return ((ip4, ip6, p), n, id)
Пример #8
0
 def start(self):
     '''
     Start the database: connect to the root dht
     '''
     if self.root != None:
         pair = re.split(":",
                         self.root)  # Root host, probably run by control
         bootHost = str(pair[0])
         bootPort = int(pair[1])
     else:
         bootHost = const.discoDhtHost  # Default host,
         bootPort = const.discoDhtPort
     try:
         self.logger.info("launching dht")
         config = dht.DhtConfig()
         config.setBootstrapMode(True)
         self.dht = dht.DhtRunner()
         self.dhtPort = get_random_port()
         self.dht.run(port=self.dhtPort,
                      config=config)  # Run on a random, free port
     except Exception:
         raise DatabaseError("dht.start: %s" % sys.exc_info()[0])
     if bootHost and bootPort:
         try:
             self.dht.bootstrap(str(bootHost), str(bootPort))
         except Exception:
             raise DatabaseError("dht.bootstrap: %s" % sys.exc_info()[0])
     # Create and start peer monitor
     self.peerMon = DhtPeerMon(self.context, self.hostAddress,
                               self.riapsHome, self.dht, self.dhtPort)
     self.peerMon.setup()
     self.peerMon.start()
     time.sleep(0.1)
     self.cleanupRegDb(
     )  # If something in the backup db, discard from the dht
     self.republisherThread.start()  # Start republisher
Пример #9
0
                    help='number of concurrent operations on the DHT',
                    type=int,
                    default=8)
parser.add_argument('-p',
                    '--period',
                    help='duration between each test (seconds)',
                    type=int,
                    default=60)
parser.add_argument('-t',
                    '--timeout',
                    help='timeout for a test to complete (seconds)',
                    type=float,
                    default=15)
args = parser.parse_args()

node1 = dht.DhtRunner()
node1.run()

node2 = dht.DhtRunner()
node2.run()

node1.bootstrap(args.bootstrap)
node2.bootstrap(args.bootstrap)
loop = asyncio.get_event_loop()

pending_tests = {}
keys = [dht.InfoHash.getRandom() for _ in range(args.num_ops)]


def listen_cb(key, val, expired):
    global pending_tests
Пример #10
0
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <https://www.gnu.org/licenses/>.

import opendht as dht
import time
import asyncio

ping_node = dht.DhtRunner()
ping_node.run()
#ping_node.enableLogging()
#ping_node.bootstrap("bootstrap.ring.cx", "4222")

pong_node = dht.DhtRunner()
pong_node.run()
#pong_node.enableLogging()
pong_node.ping(ping_node.getBound())

loc_ping = dht.InfoHash.get("toto99")
loc_pong = dht.InfoHash.get(str(loc_ping))

i = 0
MAX = 2048
Пример #11
0
#!/usr/bin/env python

import opendht as dht
import argparse
import json
from optparse import OptionParser
from flask import Flask, request
from flask_restful import Resource, Api

node = dht.DhtRunner()
app = Flask(__name__)
api = Api(app)


class Device(Resource):
    def get(self, log_name):
        return [
            v.data.decode("utf-8")
            for v in node.get(dht.InfoHash.get(log_name))
        ]

    def put(self, log_name):
        log_name = log_name.replace("\x00", "").replace("\\u0000", "")
        print("dht_service - put: log_name = " + log_name)
        guid = request.form["guid"]
        data = bytes(
            json.dumps(request.form).replace("\x00",
                                             "").replace("\\u0000", ""),
            "utf-8")
        print("dht_service - put: creating new with data = " + str(data))
        node.put(dht.InfoHash.get(str(log_name)), dht.Value(data))
Пример #12
0
 def __init__(self, subdir, bootstrapHost="bootstrap.ring.cx", bootstrapPort="4222"):
     self.subdir = subdir 
     self.node = opendht.DhtRunner()
     print(type(self.node))
     self.node.run()
     self.node.bootstrap(bootstrapHost, bootstrapPort)
Пример #13
0
 def test_instance(self):
     for i in range(10):
         r = dht.DhtRunner()
         r.run()
         del r
Пример #14
0
 def test_bootstrap(self):
     a = dht.DhtRunner()
     a.run()
     b = dht.DhtRunner()
     b.run()
     self.assertTrue(b.ping(a.getBound()))