#!/usr/bin/env python

import json, pickle, socket, websockets
from functools import partial
from twisted.internet import protocol, reactor, tcp
from twisted.web import resource, server, static

table = {}

# Connected WebSocket clients
transport = set()

# Connected log collation clients
clients = 0

# Log collation server
class factory(protocol.Factory):
  class protocol:
    def connectionLost(ctx, reason):
      global clients

      clients -= 1
      for itm in transport:
        itm.write(json.dumps(clients))

    def dataReceived(ctx, data):

      # Ignore collation_secret

      @partial(setattr, ctx, 'dataReceived')
      def dataReceived(data):

        # Read IP address of the client's host machine and proxy response
        # transfer length for each entry in segment, update table and build
        # delta of changed rows

        delta = []

        entryCount = pickle.decode_long(data[20:24])

        offset = 188
        for _ in range(entryCount):
          entryLen = pickle.decode_long(data[offset + 12:offset + 16])

          chi = socket.inet_ntop(socket.AF_INET, data[offset + 36:offset + 40])
          psql = pickle.decode_long(data[offset + 56:offset + 60])

          try:
            row = table[chi]

          except KeyError:

            # TypeError: 'tuple' object does not support item assignment
            row = table[chi] = [chi, 1, psql]

          else:
            row[1] += 1
            row[2] += psql

          delta.append(row)

          offset += entryLen

        for itm in transport:
          itm.write(json.dumps(delta))

    def makeConnection(ctx, nstTransport):
      global clients

      clients += 1
      for itm in transport:
        itm.write(json.dumps(clients))

tcp.Port(8085, factory()).startListening()

class open(resource.Resource):
  render = lambda ctx, request: json.dumps((table.values(), clients))

# Add to and remove from connected WebSocket clients
class factory(protocol.Factory):
  class protocol:
    def connectionLost(ctx, reason):
      transport.remove(ctx.transport)

    def makeConnection(ctx, nstTransport):
      ctx.transport = nstTransport

      transport.add(ctx.transport)

resource = static.File(__file__ + '/..')

resource.putChild('open', open())
resource.putChild('skt', websockets.WebSocketsResource(factory()))

tcp.Port(6011, server.Site(resource)).startListening()

reactor.run()

b5a8565b7cab3fd83131ab03f06264de7860b338
<mask token>


class factory(protocol.Factory):


    class protocol:

        def connectionLost(ctx, reason):
            global clients
            clients -= 1
            for itm in transport:
                itm.write(json.dumps(clients))

        def dataReceived(ctx, data):

            @partial(setattr, ctx, 'dataReceived')
            def dataReceived(data):
                delta = []
                entryCount = pickle.decode_long(data[20:24])
                offset = 188
                for _ in range(entryCount):
                    entryLen = pickle.decode_long(data[offset + 12:offset + 16]
                        )
                    chi = socket.inet_ntop(socket.AF_INET, data[offset + 36
                        :offset + 40])
                    psql = pickle.decode_long(data[offset + 56:offset + 60])
                    try:
                        row = table[chi]
                    except KeyError:
                        row = table[chi] = [chi, 1, psql]
                    else:
                        row[1] += 1
                        row[2] += psql
                    delta.append(row)
                    offset += entryLen
                for itm in transport:
                    itm.write(json.dumps(delta))

        def makeConnection(ctx, nstTransport):
            global clients
            clients += 1
            for itm in transport:
                itm.write(json.dumps(clients))


<mask token>


class open(resource.Resource):
    render = lambda ctx, request: json.dumps((table.values(), clients))


class factory(protocol.Factory):


    class protocol:

        def connectionLost(ctx, reason):
            transport.remove(ctx.transport)

        def makeConnection(ctx, nstTransport):
            ctx.transport = nstTransport
            transport.add(ctx.transport)


<mask token>

<mask token>


class factory(protocol.Factory):


    class protocol:

        def connectionLost(ctx, reason):
            global clients
            clients -= 1
            for itm in transport:
                itm.write(json.dumps(clients))

        def dataReceived(ctx, data):

            @partial(setattr, ctx, 'dataReceived')
            def dataReceived(data):
                delta = []
                entryCount = pickle.decode_long(data[20:24])
                offset = 188
                for _ in range(entryCount):
                    entryLen = pickle.decode_long(data[offset + 12:offset + 16]
                        )
                    chi = socket.inet_ntop(socket.AF_INET, data[offset + 36
                        :offset + 40])
                    psql = pickle.decode_long(data[offset + 56:offset + 60])
                    try:
                        row = table[chi]
                    except KeyError:
                        row = table[chi] = [chi, 1, psql]
                    else:
                        row[1] += 1
                        row[2] += psql
                    delta.append(row)
                    offset += entryLen
                for itm in transport:
                    itm.write(json.dumps(delta))

        def makeConnection(ctx, nstTransport):
            global clients
            clients += 1
            for itm in transport:
                itm.write(json.dumps(clients))


tcp.Port(8085, factory()).startListening()


class open(resource.Resource):
    render = lambda ctx, request: json.dumps((table.values(), clients))


class factory(protocol.Factory):


    class protocol:

        def connectionLost(ctx, reason):
            transport.remove(ctx.transport)

        def makeConnection(ctx, nstTransport):
            ctx.transport = nstTransport
            transport.add(ctx.transport)


<mask token>
resource.putChild('open', open())
resource.putChild('skt', websockets.WebSocketsResource(factory()))
tcp.Port(6011, server.Site(resource)).startListening()
reactor.run()

<mask token>
table = {}
transport = set()
clients = 0


class factory(protocol.Factory):


    class protocol:

        def connectionLost(ctx, reason):
            global clients
            clients -= 1
            for itm in transport:
                itm.write(json.dumps(clients))

        def dataReceived(ctx, data):

            @partial(setattr, ctx, 'dataReceived')
            def dataReceived(data):
                delta = []
                entryCount = pickle.decode_long(data[20:24])
                offset = 188
                for _ in range(entryCount):
                    entryLen = pickle.decode_long(data[offset + 12:offset + 16]
                        )
                    chi = socket.inet_ntop(socket.AF_INET, data[offset + 36
                        :offset + 40])
                    psql = pickle.decode_long(data[offset + 56:offset + 60])
                    try:
                        row = table[chi]
                    except KeyError:
                        row = table[chi] = [chi, 1, psql]
                    else:
                        row[1] += 1
                        row[2] += psql
                    delta.append(row)
                    offset += entryLen
                for itm in transport:
                    itm.write(json.dumps(delta))

        def makeConnection(ctx, nstTransport):
            global clients
            clients += 1
            for itm in transport:
                itm.write(json.dumps(clients))


tcp.Port(8085, factory()).startListening()


class open(resource.Resource):
    render = lambda ctx, request: json.dumps((table.values(), clients))


class factory(protocol.Factory):


    class protocol:

        def connectionLost(ctx, reason):
            transport.remove(ctx.transport)

        def makeConnection(ctx, nstTransport):
            ctx.transport = nstTransport
            transport.add(ctx.transport)


resource = static.File(__file__ + '/..')
resource.putChild('open', open())
resource.putChild('skt', websockets.WebSocketsResource(factory()))
tcp.Port(6011, server.Site(resource)).startListening()
reactor.run()

import json, pickle, socket, websockets
from functools import partial
from twisted.internet import protocol, reactor, tcp
from twisted.web import resource, server, static
table = {}
transport = set()
clients = 0


class factory(protocol.Factory):


    class protocol:

        def connectionLost(ctx, reason):
            global clients
            clients -= 1
            for itm in transport:
                itm.write(json.dumps(clients))

        def dataReceived(ctx, data):

            @partial(setattr, ctx, 'dataReceived')
            def dataReceived(data):
                delta = []
                entryCount = pickle.decode_long(data[20:24])
                offset = 188
                for _ in range(entryCount):
                    entryLen = pickle.decode_long(data[offset + 12:offset + 16]
                        )
                    chi = socket.inet_ntop(socket.AF_INET, data[offset + 36
                        :offset + 40])
                    psql = pickle.decode_long(data[offset + 56:offset + 60])
                    try:
                        row = table[chi]
                    except KeyError:
                        row = table[chi] = [chi, 1, psql]
                    else:
                        row[1] += 1
                        row[2] += psql
                    delta.append(row)
                    offset += entryLen
                for itm in transport:
                    itm.write(json.dumps(delta))

        def makeConnection(ctx, nstTransport):
            global clients
            clients += 1
            for itm in transport:
                itm.write(json.dumps(clients))


tcp.Port(8085, factory()).startListening()


class open(resource.Resource):
    render = lambda ctx, request: json.dumps((table.values(), clients))


class factory(protocol.Factory):


    class protocol:

        def connectionLost(ctx, reason):
            transport.remove(ctx.transport)

        def makeConnection(ctx, nstTransport):
            ctx.transport = nstTransport
            transport.add(ctx.transport)


resource = static.File(__file__ + '/..')
resource.putChild('open', open())
resource.putChild('skt', websockets.WebSocketsResource(factory()))
tcp.Port(6011, server.Site(resource)).startListening()
reactor.run()

#!/usr/bin/env python

import json, pickle, socket, websockets
from functools import partial
from twisted.internet import protocol, reactor, tcp
from twisted.web import resource, server, static

table = {}

# Connected WebSocket clients
transport = set()

# Connected log collation clients
clients = 0

# Log collation server
class factory(protocol.Factory):
  class protocol:
    def connectionLost(ctx, reason):
      global clients

      clients -= 1
      for itm in transport:
        itm.write(json.dumps(clients))

    def dataReceived(ctx, data):

      # Ignore collation_secret

      @partial(setattr, ctx, 'dataReceived')
      def dataReceived(data):

        # Read IP address of the client's host machine and proxy response
        # transfer length for each entry in segment, update table and build
        # delta of changed rows

        delta = []

        entryCount = pickle.decode_long(data[20:24])

        offset = 188
        for _ in range(entryCount):
          entryLen = pickle.decode_long(data[offset + 12:offset + 16])

          chi = socket.inet_ntop(socket.AF_INET, data[offset + 36:offset + 40])
          psql = pickle.decode_long(data[offset + 56:offset + 60])

          try:
            row = table[chi]

          except KeyError:

            # TypeError: 'tuple' object does not support item assignment
            row = table[chi] = [chi, 1, psql]

          else:
            row[1] += 1
            row[2] += psql

          delta.append(row)

          offset += entryLen

        for itm in transport:
          itm.write(json.dumps(delta))

    def makeConnection(ctx, nstTransport):
      global clients

      clients += 1
      for itm in transport:
        itm.write(json.dumps(clients))

tcp.Port(8085, factory()).startListening()

class open(resource.Resource):
  render = lambda ctx, request: json.dumps((table.values(), clients))

# Add to and remove from connected WebSocket clients
class factory(protocol.Factory):
  class protocol:
    def connectionLost(ctx, reason):
      transport.remove(ctx.transport)

    def makeConnection(ctx, nstTransport):
      ctx.transport = nstTransport

      transport.add(ctx.transport)

resource = static.File(__file__ + '/..')

resource.putChild('open', open())
resource.putChild('skt', websockets.WebSocketsResource(factory()))

tcp.Port(6011, server.Site(resource)).startListening()

reactor.run()
