def get_port(self):
        value = self.get_json_argument('port')
        if not value:
            return DEFAULT_PORT

        port = to_int(value)
        if port is None or not is_valid_port(port):
            raise InvalidValueError('Invalid port: {}'.format(value))
        return port
Exemplo n.º 2
0
    def get_port(self):
        value = self.get_value('port')
        try:
            port = int(value)
        except ValueError:
            pass
        else:
            if is_valid_port(port):
                return port

        raise InvalidException('Invalid port: {}'.format(value))
    def get_real_client_addr(self):
        ip = self.request.remote_ip

        if ip == self.request.headers.get('X-Real-Ip'):
            port = self.request.headers.get('X-Real-Port')
        elif ip in self.request.headers.get('X-Forwarded-For', ''):
            port = self.request.headers.get('X-Forwarded-Port')
        else:
            # not running behind an nginx server
            return

        port = to_int(port)
        if port is None or not is_valid_port(port):
            # fake port
            port = 65535

        return (ip, port)
Exemplo n.º 4
0
    def get_real_client_addr(self):
        ip = self.request.headers.get('X-Real-Ip')
        port = self.request.headers.get('X-Real-Port')

        if ip is None and port is None:  # suppose the server doesn't use nginx
            return

        if is_valid_ipv4_address(ip) or is_valid_ipv6_address(ip):
            try:
                port = int(port)
            except (TypeError, ValueError):
                pass
            else:
                if is_valid_port(port):
                    return (ip, port)

        logging.warning('Bad nginx configuration.')
        return False
Exemplo n.º 5
0
    def get_real_client_addr(self, xffirst=False):
        ip = self.request.remote_ip

        if ip == self.request.headers.get('X-Real-Ip'):
            port = self.request.headers.get('X-Real-Port')
        else:
            ips = self.request.headers.get('X-Forwarded-For', '')
            if not ips:
                return
            if not xffirst:
                if ip not in ips:
                    return
            else:
                ip = ips.split(',')[0].strip()
                if not netutil.is_valid_ip(ip):
                    return
            port = self.request.headers.get('X-Forwarded-Port')

        port = to_int(port)
        if port is None or not is_valid_port(port):
            # fake port
            port = 65535

        return (ip, port)
Exemplo n.º 6
0
 def test_is_valid_port(self):
     self.assertTrue(is_valid_port(80))
     self.assertFalse(is_valid_port(0))
     self.assertFalse(is_valid_port(65536))