Exemplo n.º 1
0
 def test_account(self):
     proxy_id = get_proxy_id()
     self.assertIsNotNone(proxy_id)
     test_data = 'test string'
     sign_data = sign_proxy_data(test_data)
     data = derive_proxy_data(sign_data)
     self.assertEqual(data, test_data)
Exemplo n.º 2
0
    def run(self):
        boot_nodes = []
        nodes = (config.proxy.boot_nodes.split())
        for node in nodes:
            addr, port = node.split(':')
            port = int(port)
            boot_nodes.append((str(addr), int(port)))

        proxy_id = get_proxy_id()

        d = defer.Deferred()

        def set_key_done(_):
            d.callback(True)

        def stun_done(future):
            addr = future.result()
            ip = addr[1][0]
            # proxy addr format: (ip, ctrl_port, file_port, stream_ws_port, stream_restful_port)
            addr = '%s,%d,%d,%d,%d' % (ip, config.proxy.server_port,
                                       config.proxy.server_file_port,
                                       config.proxy.server_stream_ws_port,
                                       config.proxy.server_stream_restful_port)

            asyncio.ensure_future(self.set(
                proxy_id,
                sign_proxy_data(addr))).add_done_callback(set_key_done)

        def bootstrap_done(_):
            ip = config.proxy.server_ip
            if ip:
                # proxy addr format: (ip, ctrl_port, file_port, stream_ws_port, stream_restful_port)
                addr = '%s,%d,%d,%d,%d' % (
                    ip, config.proxy.server_port,
                    config.proxy.server_file_port,
                    config.proxy.server_stream_ws_port,
                    config.proxy.server_stream_restful_port)
                asyncio.ensure_future(self.set(
                    proxy_id,
                    sign_proxy_data(addr))).add_done_callback(set_key_done)
            else:
                asyncio.ensure_future(self.protocol.stun(
                    boot_nodes[0])).add_done_callback(stun_done)

        def listen_done(_):
            asyncio.ensure_future(
                self.bootstrap(boot_nodes)).add_done_callback(bootstrap_done)

        self.listen(port).addCallback(listen_done)

        return d
Exemplo n.º 3
0
def get_proxy_agent():
    global _proxy_agent # pylint: disable=global-statement

    if not _proxy_agent:
        w3 = chain.utils.default_w3
        bin_path = chain.utils.join_with_root(config.chain.contract_bin_path)
        contract_name = config.chain.contract_name

        _proxy_agent = chain.agents.ProxyAgent(
            w3,
            bin_path,
            contract_name,
            account=get_proxy_id())

    return _proxy_agent
Exemplo n.º 4
0
    def start_service(self,
                      ip=None,
                      ctrl_port=None,
                      data_port=None,
                      account_id=0):

        self.ip = ip
        set_proxy_account(account_id)
        self.peer_id = get_proxy_id()

        server_key = os.path.expanduser(join_with_rc(config.proxy.server_key))
        server_crt = os.path.expanduser(join_with_rc(config.proxy.server_crt))

        if not os.path.isfile(server_key):
            logger.info("SSL key/cert file not found, " +
                        "run local self-test by default")
            server_key_sample = 'cpchain/assets/proxy/key/server.key'
            server_crt_sample = 'cpchain/assets/proxy/key/server.crt'
            server_key = join_with_root(server_key_sample)
            server_crt = join_with_root(server_crt_sample)

        # data channel
        data_port = (data_port or config.proxy.server_data_port)
        file_factory = Site(FileServer())
        reactor.listenSSL(
            data_port, file_factory,
            ssl.DefaultOpenSSLContextFactory(server_key, server_crt))

        # ctrl channel
        ctrl_port = (ctrl_port or config.proxy.server_ctrl_port)
        ctrl_factory = SSLServerFactory(data_port=data_port)
        reactor.listenSSL(
            ctrl_port, ctrl_factory,
            ssl.DefaultOpenSSLContextFactory(server_key, server_crt))

        self.service_port = ctrl_port

        def set_proxy_ip():
            if self.ip is None:
                # waiting for node bootstrap finish
                return reactor.callLater(1, set_proxy_ip)

            ctrl_factory.set_external_ip(self.ip)

        set_proxy_ip()
Exemplo n.º 5
0
import time

from twisted.trial import unittest
from twisted.test import proto_helpers

import msgpack

from cpchain.proxy.network import PeerProtocol, generate_tid
from cpchain.proxy.account import set_proxy_account, get_proxy_id, sign_proxy_data

set_proxy_account()
peer_id = get_proxy_id()
service_port = 5678


def fake_bootstrap():
    tid = generate_tid()
    msg = {
        'type': 'bootstrap',
        'tid': tid,
        'sign_tid': sign_proxy_data(tid),
        'peer_ip': None,
        'peer_id': peer_id,
        'peer_info': service_port,
        'peer_conf': None
    }

    return msg


def fake_pick_peer():
Exemplo n.º 6
0
 def run(self):
     port = config.proxy.server_slave_port
     self.trans = reactor.listenUDP(port, self.protocol)
     return self.protocol.bootstrap(get_proxy_id(), self.tracker)