Пример #1
0
import sys
import time

from struct import unpack
from socket import inet_ntoa

import monocle
from monocle import _o, Return

monocle.init('tornado')

from monocle.stack import eventloop
from monocle.stack.network import add_service, Service, Client

from dust.core.util import getPublicIP

from shared import pump

from dust.extensions.lite.lite_socket2 import lite_socket, makeSession, makeEphemeralSession, createEphemeralKeypair
from dust.core.dust_packet import IV_SIZE, KEY_SIZE


@_o
def handle_socksDust(conn):
    print('connection')
    client = Client()
    yield client.connect('blanu.net', 7051)

    coder = yield handshake(client)

    monocle.launch(pump, conn, client, coder.encrypt)
Пример #2
0
import sys

import monocle
from monocle import _o, Return
monocle.init(sys.argv[1])

from monocle.stack import eventloop
from monocle.stack.network import add_service
from monocle.stack.network.http import HttpHeaders, HttpServer


@_o
def hello_http(req):
    content = "Hello, World!"
    headers = HttpHeaders()
    headers.add('Content-Length', len(content))
    headers.add('Content-Type', 'text/plain')
    headers.add('Connection', 'close')
    headers.add('Set-Cookie', 'test0=blar; Path=/')
    headers.add('Set-Cookie', 'test1=blar; Path=/')
    yield Return(200, headers, content)

add_service(HttpServer(hello_http, 8088))
eventloop.run()
Пример #3
0
import sys
import time

import monocle
from monocle import _o
monocle.init('tornado')

from monocle.stack import eventloop
from monocle.stack.network import add_service, Service, Client, ConnectionLost

@_o
def pump(input, output):
  while True:
    try:
      message = yield input.read(1)
      yield output.write(message)
    except ConnectionLost:
      output.close()
      break

@_o
def handle_socks(conn):
  client = Client()
  yield client.connect('localhost', 8050)
  monocle.launch(pump, conn, client)
  yield pump(client, conn)

add_service(Service(handle_socks, port=7050))
eventloop.run_multicore(1)
Пример #4
0
import functools
import os

INIT = os.environ.get('BACKEND', 'twisted')
HOST = '127.0.0.1'
PORT = 5555

import monocle
monocle.init(INIT)

from monocle.twisted_stack.utils import cb_to_df
from twisted.trial.unittest import TestCase


def o(f):
    oroutine = monocle._o(f)
    return functools.update_wrapper(
        lambda *a, **k: cb_to_df(oroutine(*a, **k)), oroutine)
Пример #5
0
import sys

import monocle
monocle.init(sys.argv[1])
from monocle import _o, launch
from monocle.util import sleep
from monocle.stack import eventloop
from monocle.stack.network.http import HttpClient


@_o
def req():
    client = HttpClient()
    yield client.connect("localhost", 12344, timeout=1)


def die():
    raise Exception("boom")


@_o
def fifth():
    die()


def fourth():
    return fifth()


@_o
def third():
Пример #6
0
def main(args):
    parser = argparse.ArgumentParser(description="runs monocle tests")
    parser.add_argument('--verbose',
                        '-v',
                        action='store_true',
                        default=False,
                        help="verbose mode")
    parser.add_argument('stack', help="monocle stack to use")
    parser.add_argument('paths',
                        nargs=argparse.REMAINDER,
                        help="args for nosetests")
    args = parser.parse_args()

    monocle.init(args.stack)
    from monocle.stack import eventloop

    import rewrite
    hook = rewrite.AssertionRewritingHook()
    sys.meta_path.insert(0, hook)

    class State(object):
        debug = False

        def trace(self, msg):
            if self.debug:
                print msg

    class Config(object):
        _assertstate = State()

    class Session(object):
        config = Config()

        def isinitpath(self, fn):
            return False

    hook.session = Session()

    paths = set()
    all_tests = []
    test_files = []

    for path in args.paths:
        if os.path.isfile(path):
            paths.add(os.path.dirname(path))
            file = os.path.basename(path)
            all_tests.append(file[:-3])
            test_files.append(path)
        elif os.path.isdir(path):
            for top, dirs, files in os.walk(path):
                for file in files:
                    if file.endswith(".py"):
                        paths.add(top)
                        all_tests.append(file[:-3])
                        test_files.append(os.path.join(top, file))
        else:
            print "Unknown file or directory", path
            return

        sys.path.extend(paths)
        imported_tests = []
        hook.fnpats = test_files

        for test in all_tests:
            try:
                m = __import__(test, globals(), locals())
                if hasattr(m, "test"):
                    imported_tests.extend(m.test.func_globals['_tests'])
            except Exception, e:
                print test, str(e)
        _tests.extend(set(imported_tests))
Пример #7
0
#! /usr/bin/env python

import sys
import time

import monocle
from monocle import _o
monocle.init("twisted")

from monocle.stack import eventloop
from monocle.stack.network import add_service, Service, Client, ConnectionLost

def make_pumper(direction):
    @_o
    def pump(input, output):
        while True:
            try:
                message = yield input.read_some()
                print "==== %s says: =======================================" % direction
                print message
                yield output.write(message)
            except ConnectionLost:
                output.close()
                break
    return pump

def make_proxy(backend_host, backend_port):
    @_o
    def proxy(conn):
        client = Client()
        yield client.connect(backend_host, backend_port)
Пример #8
0
import monocle
from monocle import _o, Return
monocle.init("tornado")

from monocle.stack import eventloop
from monocle.stack.network import add_service
from monocle.stack.network.http import HttpHeaders, HttpServer

@_o
def hello_http(req):
    content = "Hello, World!"
    headers = HttpHeaders()
    headers['Content-Length'] = len(content)
    headers['Content-Type'] = 'text/plain'
    yield Return(200, headers, content)

add_service(HttpServer(hello_http, 8088))
eventloop.run()
Пример #9
0
import sys
import new
import simplejson

import monocle
from monocle import _o, Return
monocle.init('tornado') # Only works with Tornado right now

from monocle.stack import eventloop
from monocle.stack.network import add_service, Service
from monocle.stack.network.http import HttpHeaders, WebSocketServer

SOCKET_HTML = """
<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>WebSocket Echo</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  </head>
  <body>
    <h1>WebSocket Reverse Echo</h1>
    <section>Your message backwards: <span id="content"></span></section>
    <input id="message" type="text" style="margin: 20px 0px;"/>
    <section>(You typed: <span id="realContent"></span>)</section>

    <script>
        var ws = new WebSocket('ws://localhost:8088/ws/');
        ws.onopen = function(event) { console.log("Opened socket conn"); };
        ws.onclose = function(event) { console.log("Closed socket conn"); };
        ws.onmessage = function(event) {
Пример #10
0
import sys
import monocle
from monocle import _o
monocle.init("tornado")

from monocle.stack import eventloop
from monocle.experimental import Channel

@_o
def main():
    s = 2
    ch = Channel(s)
    for i in xrange(s):
        print i
        yield ch.send(i)

    print ch.bufsize, len(ch._msgs)
    for i in xrange(s):
        print (yield ch.recv())
    print "done"
    sys.exit(0)

monocle.launch(main)
eventloop.run()
Пример #11
0
def main(args):
    parser = argparse.ArgumentParser(
        description="runs monocle tests")
    parser.add_argument('--verbose', '-v', action='store_true', default=False,
                        help="verbose mode")
    parser.add_argument('stack', help="monocle stack to use")
    parser.add_argument('paths', nargs=argparse.REMAINDER, help="args for nosetests")
    args = parser.parse_args()

    monocle.init(args.stack)
    from monocle.stack import eventloop

    import rewrite
    hook = rewrite.AssertionRewritingHook()
    sys.meta_path.insert(0, hook)

    class State(object):
        debug = False
        def trace(self, msg):
            if self.debug:
                print msg

    class Config(object):
        _assertstate = State()

    class Session(object):
        config = Config()
        def isinitpath(self, fn):
            return False

    hook.session = Session()

    paths = set()
    all_tests = []
    test_files = []

    for path in args.paths:
        if os.path.isfile(path):
            paths.add(os.path.dirname(path))
            file = os.path.basename(path)
            all_tests.append(file[:-3])
            test_files.append(path)
        elif os.path.isdir(path):
            for top, dirs, files in os.walk(path):
                for file in files:
                    if file.endswith(".py"):
                        paths.add(top)
                        all_tests.append(file[:-3])
                        test_files.append(os.path.join(top, file))
        else:
            print "Unknown file or directory", path
            return

        sys.path.extend(paths)
        imported_tests = []
        hook.fnpats = test_files

        for test in all_tests:
            try:
                m = __import__(test, globals(), locals())
                if hasattr(m, "test"):
                    imported_tests.extend(m.test.func_globals['_tests'])
            except Exception, e:
                print test, str(e)
        _tests.extend(set(imported_tests))
Пример #12
0
import functools
import os

INIT = os.environ.get('BACKEND', 'twisted')
HOST = '127.0.0.1'
PORT = 5555

import monocle
monocle.init(INIT)

from monocle.twisted_stack.utils import cb_to_df
from twisted.trial.unittest import TestCase

def o(f):
    oroutine = monocle._o(f)
    return functools.update_wrapper(
        lambda *a, **k: cb_to_df(oroutine(*a, **k)), oroutine)