Simple pub/sub forwarder

    :param int frontend: fontend zeromq port
    :param int backend: backend zeromq port
326614
def forwarder(frontend, backend):
    
    try:
        context = zmq.Context()

        front_sub = context.socket(zmq.SUB)
        front_sub.bind("tcp://*:%d" % frontend)

        front_sub.setsockopt_string(zmq.SUBSCRIBE, "")

        back_pub = context.socket(zmq.PUB)
        back_pub.bind("tcp://*:%d" % backend)

        print("forwarder started, backend on port : %d\tfrontend on port: %d" % (backend, frontend))
        zmq.proxy(front_sub, back_pub)
    except Exception as e:
        print(e)
    finally:
        front_sub.close()
        back_pub.close()
        context.term()
175783
def build_backend(tasks, default_host=('127.0.0.1', DEFAULT_PORT), *args, 
                  **kw):
    
    host, port = default_host
    if len(sys.argv) > 1:
        host = sys.argv[1]
    if len(sys.argv) > 2:
        port = sys.argv[2]
    
    return BackEnd(tasks, host, port, *args, **kw)
978738
def list_port_fwd(zone, permanent=True):
    '''
    
    '''
    ret = []

    cmd = '--zone={0} --list-forward-ports'.format(zone)

    if permanent:
        cmd += ' --permanent'

    for i in __firewall_cmd(cmd).splitlines():
        (src, proto, dest, addr) = i.split(':')

        ret.append(
            {'Source port': src.split('=')[1],
             'Protocol': proto.split('=')[1],
             'Destination port': dest.split('=')[1],
             'Destination address': addr.split('=')[1]}
        )

    return ret
667784
def _setup_frontend(self):
        ''' 
        '''
        self.frontends = []
        self.fe_poller = zmq.Poller()
        for actual_frontend_address in self.frontend_address:
            # Subscriber or server socket
            actual_frontend = (actual_frontend_address,
                               self.context.socket(self.frontend_socket_type))
            # Wait 0.5 s before termating socket
            actual_frontend[1].setsockopt(zmq.LINGER, 500)
            # Buffer only 10 meassages, then throw data away
            actual_frontend[1].set_hwm(10)
            # A suscriber has to set to not filter any data
            if self.frontend_socket_type == zmq.SUB:
                actual_frontend[1].setsockopt_string(zmq.SUBSCRIBE, u'')
            actual_frontend[1].connect(actual_frontend_address)
            self.frontends.append(actual_frontend)
            self.fe_poller.register(actual_frontend[1], zmq.POLLIN)
        self.raw_data = queue.Queue()
        self.fe_stop = threading.Event()
101159
def setup_backend(
    serve=True,
    host=DEFAULT_HOST,
    port=DEFAULT_PORT,
    debug=False,
    open_browser=True,
    browsername=None,
    testing=False,
    private_key=None,
    certificate=None,
    LLDB=False,
):
    """"""
    app.config["LLDB"] = LLDB

    kwargs = {}
    ssl_context = get_ssl_context(private_key, certificate)
    if ssl_context:
        # got valid ssl context
        # force everything through https
        SSLify(app)
        # pass ssl_context to flask
        kwargs["ssl_context"] = ssl_context

    url = "%s:%s" % (host, port)
    if kwargs.get("ssl_context"):
        protocol = "https://"
        url_with_prefix = "https://" + url
    else:
        protocol = "http://"
        url_with_prefix = "http://" + url

    if debug:
        async_mode = "eventlet"
    else:
        async_mode = "gevent"

    socketio.server_options["async_mode"] = async_mode
    try:
        socketio.init_app(app)
    except Exception:
        print(
            'failed to initialize socketio app with async mode "%s". Continuing with async mode "threading".'
            % async_mode
        )
        socketio.server_options["async_mode"] = "threading"
        socketio.init_app(app)

    if testing is False:
        if host == DEFAULT_HOST:
            url = (DEFAULT_HOST, port)
        else:
            try:
                url = (socket.gethostbyname(socket.gethostname()), port)
            except Exception:
                url = (host, port)

        if open_browser is True and debug is False:
            browsertext = repr(browsername) if browsername else "default browser"
            args = (browsertext,) + url
            text = ("Opening gdbgui with %s at " + protocol + "%s:%d") % args
            print(colorize(text))
            b = webbrowser.get(browsername) if browsername else webbrowser
            b.open(url_with_prefix)
        else:
            print(colorize("View gdbgui at %s%s:%d" % (protocol, url[0], url[1])))

        print("exit gdbgui by pressing CTRL+C")

        try:
            socketio.run(
                app,
                debug=debug,
                port=int(port),
                host=host,
                extra_files=get_extra_files(),
                **kwargs
            )
        except KeyboardInterrupt:
            # Process was interrupted by ctrl+c on keyboard, show message
            pass
26056
function PubConnection (node) {
  let connection = new EventEmitter()

  const _debug = D('dnsmq-messagebus:PUB')
  const debug = (...args) => _debug(node.name, ...args)

  let _socket = null
  let _connectingMaster = null

  /**
   * Provides a pub socket connecting to master.endpoint
   * @private
   * @param  {object} master - Map of properties about the actual master node
   * @return {object} A `zeromq` socket
   */
  const _getSocket = (master) => {
    let socket = zmq.socket('pub')
    socket._master = master
    socket.monitor(5, 0)
    socket.connect(master.endpoint)
    return socket
  }
  const _publishStream = new EventEmitter()
  _publishStream.on('message', (...args) => {
    if (_socket) _socket.send(args)
  })

  /**
   * creates a new pub socket and tries to connect it to the provided master;
   * after connection the socket is used to publish messages in the bus
   * and an eventual previous socket is closed
   * @param  {object} master
   * @return {object} the publishConnection instance
   */
  function connect (master) {
    if (_socket && _socket._master.name === master.name) {
      debug(`already connected to master ${master.name}`)
      return
    }
    if (_connectingMaster && _connectingMaster.name === master.name) {
      debug(`already connecting to master ${master.name}`)
      return
    }

    _connectingMaster = master
    let newSocket = _getSocket(master)

    let _messagesToResend = []
    let _onMessagePublished = (...args) => {
      if (_socket) _messagesToResend.push(args)
    }
    _publishStream.on('message', _onMessagePublished)

    debug(`connecting to ${master.name} at ${master.endpoint}`)

    let connectionStart = Date.now()
    const onSocketConnected = timingoutCallback((err) => {
      _publishStream.removeListener('message', _onMessagePublished)
      newSocket.unmonitor()

      if (
        err ||
        !_connectingMaster ||
        _connectingMaster.name === !master.name
      ) {
        newSocket.close()
        if (err) {
          debug(`failed to connect to ${master.name} at ${master.endpoint}`)
          disconnect()
        }
        return
      }

      _connectingMaster = null
      let previousSocket = _socket

      debug(`${previousSocket ? 'switched' : 'connected'} to ${master.name} at ${master.endpoint} in ${Date.now() - connectionStart} ms`)

      _socket = newSocket

      let totalMessagesToResend = _messagesToResend.length
      if (totalMessagesToResend) {
        debug(`resending ${totalMessagesToResend} messages published while transitioning`)
        _messagesToResend.forEach(message => _socket.send(message))
      }

      if (previousSocket) {
        previousSocket.close()
        debug(`closed previous connection to ${previousSocket._master.name} at ${previousSocket._master.endpoint}`)
      } else {
        connection.emit('connect')
      }
    }, 500)

    newSocket.once('connect', () => onSocketConnected())
    return connection
  }
  /**
   * if present, closes the pub socket
   * @return {object} the publishConnection instance
   */
  function disconnect () {
    _connectingMaster = null
    if (_socket) {
      _socket.close()
      _socket = null
      debug('disconnected')
      connection.emit('disconnect')
    }
    return connection
  }
  /**
   * takes a list of strings|buffers to publish as message's frames
   * in the channel passed as first argument;
   * decorates each message with a uid
   * @param  {string} channel
   * @param  {[string|buffer]} args
   */
  function publish (channel, ...args) {
    _publishStream.emit('message', channel, uniqueId(`${node.name}_`), ...args)
  }

  return Object.defineProperties(connection, {
    connected: {get: () => !!_socket},
    master: {get: () => _socket && {..._socket._master}},
    connect: {value: connect},
    disconnect: {value: disconnect},
    publish: {value: publish}
  })
}
585819
function fc() {
        for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) {
            params[_key] = arguments[_key];
        }

        return createFrontendConnector(parametrize(c, params), debug);
    }
178952
def _setup_ipc(self):
        '''
        
        '''
        self.ctx = zmq.Context()
        log.debug('Setting up the %s publisher subscriber #%d', self._transport_type, self.pub_id)
        self.sub = self.ctx.socket(zmq.SUB)
        self.sub.connect(PUB_IPC_URL)
        self.sub.setsockopt(zmq.SUBSCRIBE, b'')
        try:
            self.sub.setsockopt(zmq.HWM, self.opts['hwm'])
            # zmq 2
        except AttributeError:
            # zmq 3
            self.sub.setsockopt(zmq.RCVHWM, self.opts['hwm'])
614387
def _init_zmq(self, port_publish, port_subscribe):
        

        log.debug('kernel {} publishing on port {}'
                  ''.format(self.analysis.id_, port_publish))
        self.zmq_publish = zmq.Context().socket(zmq.PUB)
        self.zmq_publish.connect('tcp://127.0.0.1:{}'.format(port_publish))

        log.debug('kernel {} subscribed on port {}'
                  ''.format(self.analysis.id_, port_subscribe))
        self.zmq_sub_ctx = zmq.Context()
        self.zmq_sub = self.zmq_sub_ctx.socket(zmq.SUB)
        self.zmq_sub.setsockopt(zmq.SUBSCRIBE,
                                self.analysis.id_.encode('utf-8'))
        self.zmq_sub.connect('tcp://127.0.0.1:{}'.format(port_subscribe))

        self.zmq_stream_sub = zmq.eventloop.zmqstream.ZMQStream(self.zmq_sub)
        self.zmq_stream_sub.on_recv(self.zmq_listener)

        # send zmq handshakes until a zmq ack is received
        self.zmq_ack = False
        self.send_handshake()
301857
def get_backend_symbol(self, backend):
        
        out = SymbolHandle()
        check_call(_LIB.MXGenBackendSubgraph(self.handle, c_str(backend), ctypes.byref(out)))
        return Symbol(out)
1174297
def connect_pub(self, timeout=None):
        '''
        
        '''
        if self.cpub:
            return True

        if self._run_io_loop_sync:
            with salt.utils.asynchronous.current_ioloop(self.io_loop):
                if self.subscriber is None:
                    self.subscriber = salt.transport.ipc.IPCMessageSubscriber(
                    self.puburi,
                    io_loop=self.io_loop
                )
                try:
                    self.io_loop.run_sync(
                        lambda: self.subscriber.connect(timeout=timeout))
                    self.cpub = True
                except Exception:
                    pass
        else:
            if self.subscriber is None:
                self.subscriber = salt.transport.ipc.IPCMessageSubscriber(
                self.puburi,
                io_loop=self.io_loop
            )

            # For the asynchronous case, the connect will be defered to when
            # set_event_handler() is invoked.
            self.cpub = True
        return self.cpub
261069
def add_port_fwd(zone, src, dest, proto='tcp', dstaddr='', permanent=True):
    '''
    
    '''
    cmd = '--zone={0} --add-forward-port=port={1}:proto={2}:toport={3}:toaddr={4}'.format(
        zone,
        src,
        proto,
        dest,
        dstaddr
    )

    if permanent:
        cmd += ' --permanent'

    return __firewall_cmd(cmd)
1355401
def backend_add(cls, name, backend):
        
        oper = cls.call(
            'hosting.rproxy.server.create', cls.usable_id(name), backend)
        cls.echo('Adding backend %s:%s into webaccelerator' %
                 (backend['ip'], backend['port']))
        cls.display_progress(oper)
        cls.echo('Backend added')
        return oper
247030
def serveInBackground(port, serverName, prefix='/status/'):
  """"""
  import flask, threading
  from wsgiref.simple_server import make_server
  app = flask.Flask(__name__)
  registerStatsHandler(app, serverName, prefix)
  server = threading.Thread(target=make_server('', port, app).serve_forever)
  server.daemon = True
  server.start()
  return server
1285081
def send(self, targetPath, parent, diff, showProgress=True, allowDryRun=True):
        
        if parent is not None:
            cmd = ["btrfs", "send", "-p", parent, targetPath]
        else:
            cmd = ["btrfs", "send", targetPath]

        if Store.skipDryRun(logger, self.dryrun and allowDryRun)("Command: %s", cmd):
            return None

        process = subprocess.Popen(
            cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=DEVNULL)
        _makeNice(process)

        return _Reader(process, process.stdout, targetPath, diff, showProgress)
354098
def _setup_ipc(self):
        '''
        
        '''
        log.debug('Setting up the internal IPC proxy')
        self.ctx = zmq.Context()
        # Frontend
        self.sub = self.ctx.socket(zmq.SUB)
        self.sub.bind(PUB_PX_IPC_URL)
        self.sub.setsockopt(zmq.SUBSCRIBE, b'')
        log.debug('Setting HWM for the proxy frontend: %d', self.hwm)
        try:
            self.sub.setsockopt(zmq.HWM, self.hwm)
            # zmq 2
        except AttributeError:
            # zmq 3
            self.sub.setsockopt(zmq.SNDHWM, self.hwm)
        # Backend
        self.pub = self.ctx.socket(zmq.PUB)
        self.pub.bind(PUB_IPC_URL)
        log.debug('Setting HWM for the proxy backend: %d', self.hwm)
        try:
            self.pub.setsockopt(zmq.HWM, self.hwm)
            # zmq 2
        except AttributeError:
            # zmq 3
            self.pub.setsockopt(zmq.SNDHWM, self.hwm)
260477
function forward(){
  return function forward(req, res, next){
    var url  = utils.processUrl(req);
    var options = {
      url: url,
      method: req.method,
      headers: req.headers
    }
    var buffers = [];

    log.debug('forward: ' + url);
    
    if(req.method === 'POST'){
      req.on('data', function(chunk){
        buffers.push(chunk);
      });

      req.on('end', function(){
        options.data = Buffer.concat(buffers);
        utils.request(options, function(err, data, proxyRes){
          _forwardHandler(err, data, proxyRes, res);
        });
      });
    }else{
      utils.request(options, function(err, data, proxyRes){
        _forwardHandler(err, data, proxyRes, res)
      }); 
    }
  };
}