예제 #1
0
    def __init__(self, *args, **kwargs):
        super(MainHandler, self).__init__(*args,**kwargs)
        self.remote_ip = self.request.headers.get('X-Forwarded-For', self.request.remote_ip)
        logger.info('%s: connected.' % self.remote_ip)
        self.thread = None
        self.mac = ''
        self.allowance = RATE #unit: messages
        self.last_check = time.time() #floating-point, e.g. usec accuracy. Unit: seconds
        self.upstream = RateLimitingState(RATE, name='upstream', clientip=self.remote_ip)
        self.downstream = RateLimitingState(RATE, name='downstream', clientip=self.remote_ip)

        ping_future = delay_future(time.time()+PING_INTERVAL, self.do_ping)
        loop.add_future(ping_future, lambda: None)
예제 #2
0
    def __init__(self, *args, **kwargs):
        super(MainHandler, self).__init__(*args, **kwargs)
        self.remote_ip = self.request.headers.get('X-Forwarded-For',
                                                  self.request.remote_ip)
        logger.info('%s: connected.' % self.remote_ip)
        self.thread = None
        self.mac = ''
        self.allowance = RATE  #unit: messages
        self.last_check = time.time(
        )  #floating-point, e.g. usec accuracy. Unit: seconds
        self.upstream = RateLimitingState(RATE,
                                          name='upstream',
                                          clientip=self.remote_ip)
        self.downstream = RateLimitingState(RATE,
                                            name='downstream',
                                            clientip=self.remote_ip)

        ping_future = delay_future(time.time() + PING_INTERVAL, self.do_ping)
        loop.add_future(ping_future, lambda: None)
예제 #3
0
class MainHandler(websocket.WebSocketHandler):
    def __init__(self, *args, **kwargs):
        super(MainHandler, self).__init__(*args, **kwargs)
        self.remote_ip = self.request.headers.get('X-Forwarded-For',
                                                  self.request.remote_ip)
        logger.info('%s: connected.' % self.remote_ip)
        self.thread = None
        self.mac = ''
        self.allowance = RATE  #unit: messages
        self.last_check = time.time(
        )  #floating-point, e.g. usec accuracy. Unit: seconds
        self.upstream = RateLimitingState(RATE,
                                          name='upstream',
                                          clientip=self.remote_ip)
        self.downstream = RateLimitingState(RATE,
                                            name='downstream',
                                            clientip=self.remote_ip)

        ping_future = delay_future(time.time() + PING_INTERVAL, self.do_ping)
        loop.add_future(ping_future, lambda: None)

    def do_ping(self, timestamp):
        self.ping(str(timestamp))

        ping_future = delay_future(time.time() + PING_INTERVAL, self.do_ping)
        loop.add_future(ping_future, lambda: None)

    def on_pong(self, data):
        pass

    def rate_limited_downstream(self, message):
        if self.downstream.do_throttle(message):
            self.write_message(message, binary=True)

    def open(self):
        self.set_nodelay(True)

    def on_message(self, message):
        #TODO: log IP headers in the future

        #Logs which user is tied to which MAC so that we detect which user is acting maliciously
        if self.mac != message[6:12]:
            if macmap.get(self.mac, False):
                del macmap[self.mac]

            self.mac = message[6:12]
            formatted_mac = ':'.join('{0:02x}'.format(ord(a))
                                     for a in message[6:12])
            logger.info('%s: using mac %s' % (self.remote_ip, formatted_mac))

            macmap[self.mac] = self

        dest = message[0:6]
        try:
            if dest == BROADCAST or (ord(dest[0]) & 0x1) == 1:
                if self.upstream.do_throttle(message):
                    for socket in macmap.values():
                        try:
                            socket.write_message(str(message), binary=True)
                        except:
                            pass

                    tunthread.write(message)
            elif macmap.get(dest, False):
                if self.upstream.do_throttle(message):
                    try:
                        macmap[dest].write_message(str(message), binary=True)
                    except:
                        pass
            else:
                if self.upstream.do_throttle(message):
                    tunthread.write(message)

        except:
            tb = traceback.format_exc()
            logger.error('%s: error on receive. Closing\n%s' %
                         (self.remote_ip, tb))
            try:
                self.close()
            except:
                pass

    def on_close(self):
        logger.info('%s: disconnected.' % self.remote_ip)

        if self.thread is not None:
            self.thread.running = False

        try:
            del macmap[self.mac]
        except:
            pass
예제 #4
0
class MainHandler(websocket.WebSocketHandler):
    def __init__(self, *args, **kwargs):
        super(MainHandler, self).__init__(*args, **kwargs)
        self.remote_ip = self.request.headers.get('X-Forwarded-For',
                                                  self.request.remote_ip)
        logger.info('new user port[%s]: connected to switch device.' %
                    self.remote_ip)
        self.thread = None
        self.mac = ''
        self.allowance = RATE  #unit: messages
        self.last_check = time.time(
        )  #floating-point, e.g. usec accuracy. Unit: seconds
        self.upstream = RateLimitingState(RATE,
                                          name='upstream',
                                          clientip=self.remote_ip)
        self.downstream = RateLimitingState(RATE,
                                            name='downstream',
                                            clientip=self.remote_ip)

        ping_future = delay_future(time.time() + PING_INTERVAL, self.do_ping)
        loop.add_future(ping_future, lambda: None)

    def do_ping(self, timestamp):
        self.ping(str(timestamp))

        ping_future = delay_future(time.time() + PING_INTERVAL, self.do_ping)
        loop.add_future(ping_future, lambda: None)

    def on_pong(self, data):
        pass

    def rate_limited_downstream(self, message):
        if self.downstream.do_throttle(message):
            self.write_message(message, binary=True)

    def open(self):
        self.set_nodelay(True)

    def on_message(self, message):
        #TODO: log IP headers in the future
        dst_mac = ':'.join('{0:02x}'.format(ord(a)) for a in message[0:6])
        src_mac = ':'.join('{0:02x}'.format(ord(a)) for a in message[6:12])
        logger.info('recv packat from client:src mac:%s, dest mac:%s' %
                    (src_mac, dst_mac))
        #print("data:", message)

        #Logs which user is tied to which MAC so that we detect which user is acting maliciously
        if self.mac != message[6:12]:
            if macmap.get(self.mac, False):
                del macmap[self.mac]

            self.mac = message[6:12]
            formatted_mac = ':'.join('{0:02x}'.format(ord(a))
                                     for a in message[6:12])
            logger.info('user[%s]: update mac %s' %
                        (self.remote_ip, formatted_mac))
            # update mac forward table
            macmap[self.mac] = self

        dest = message[0:6]
        try:
            if dest == BROADCAST or (ord(dest[0]) & 0x1) == 1:
                print('broadcast or multicast frame recvd')
                if self.upstream.do_throttle(message):
                    print("broadcast to all port:", macmap.keys())
                    for socket in macmap.values():
                        try:
                            socket.write_message(str(message), binary=True)
                        except Exception:
                            print('write broad packat except:', e)
                            pass

                    print("broadcast to tap0")
                    tunthread.write(message)
            elif macmap.get(dest, False):
                print('found dst client')
                if self.upstream.do_throttle(message):
                    try:
                        macmap[dest].write_message(str(message), binary=True)
                    except:
                        print('write err')
                        pass
            else:
                print('no found dst mac, default forward to tap0')
                if self.upstream.do_throttle(message):
                    tunthread.write(message)

        except:
            tb = traceback.format_exc()
            logger.error('%s: error on receive. Closing\n%s' %
                         (self.remote_ip, tb))
            try:
                self.close()
            except:
                pass

    def on_close(self):
        logger.info('%s: disconnected.' % self.remote_ip)

        if self.thread is not None:
            self.thread.running = False

        try:
            del macmap[self.mac]
        except:
            pass
예제 #5
0
class MainHandler(websocket.WebSocketHandler):
    def __init__(self, *args, **kwargs):
        super(MainHandler, self).__init__(*args,**kwargs)
        self.remote_ip = self.request.headers.get('X-Forwarded-For', self.request.remote_ip)
        logger.info('%s: connected.' % self.remote_ip)
        self.thread = None
        self.mac = ''
        self.allowance = RATE #unit: messages
        self.last_check = time.time() #floating-point, e.g. usec accuracy. Unit: seconds
        self.upstream = RateLimitingState(RATE, name='upstream', clientip=self.remote_ip)
        self.downstream = RateLimitingState(RATE, name='downstream', clientip=self.remote_ip)

        ping_future = delay_future(time.time()+PING_INTERVAL, self.do_ping)
        loop.add_future(ping_future, lambda: None)

    def do_ping(self, timestamp):
        self.ping(str(timestamp))

        ping_future = delay_future(time.time()+PING_INTERVAL, self.do_ping)
        loop.add_future(ping_future, lambda: None)

    def on_pong(self, data):
        pass

    def rate_limited_downstream(self, message):
        if self.downstream.do_throttle(message):
            self.write_message(message, binary=True)

    def open(self):
        self.set_nodelay(True)

    def on_message(self, message):
        #TODO: log IP headers in the future

        #Logs which user is tied to which MAC so that we detect which user is acting maliciously
        if self.mac != message[6:12]:
            if macmap.get(self.mac, False):
                del macmap[self.mac]

            self.mac = message[6:12]
            formatted_mac = ':'.join('{0:02x}'.format(ord(a)) for a in message[6:12]) 
            logger.info('%s: using mac %s' % (self.remote_ip, formatted_mac))

            macmap[self.mac] = self

        dest = message[0:6]
        try:
            if dest == BROADCAST or (ord(dest[0]) & 0x1) == 1:
                if self.upstream.do_throttle(message):
                    for socket in macmap.values():
                        try:
                                socket.write_message(str(message),binary=True)
                        except:
                            pass

                    tunthread.write(message)
            elif macmap.get(dest, False):
                if self.upstream.do_throttle(message):
                    try:
                        macmap[dest].write_message(str(message),binary=True)
                    except:
                        pass
            else:
                if self.upstream.do_throttle(message):
                    tunthread.write(message)

        except:
            tb = traceback.format_exc()
            logger.error('%s: error on receive. Closing\n%s' % (self.remote_ip, tb))
            try:
                self.close()
            except:
                pass

    def on_close(self):
        logger.info('%s: disconnected.' % self.remote_ip)

        if self.thread is not None:
            self.thread.running = False

        try:
            del macmap[self.mac]
        except:
            pass