Ejemplo n.º 1
0
    def recv(self, wsgi=False):
        try:
            stream = HttpStream(self.reader, kind=HTTP_REQUEST, parser_class=HttpParser, decompress=True)

            if bool(wsgi):
                environ = stream.wsgi_environ()
                environ['wsgi.url_scheme'] = guess_scheme(environ)
                environ['wsgi.input'] = stream.body_file()
                environ['wsgi.socket'] = self.socket
                return environ

            # BUG:
            # http-parser has an issue here, if we call 'method' before 'headers'
            # and invalid method name is returned...
            fields  = stream.headers()
            method  = stream.method()
            url     = stream.url()
            version = stream.version()
            content = stream.body_file()

            url      = urlparse(url)
            path     = url.path
            query    = parse_qs(url.query, keep_blank_values=True)
            fragment = url.fragment

            for k, v in six.iteritems(dict(query)):
                query[k] = parse_query_value(v)

            self.version = 'HTTP/%s.%s' % version
            return method, path, query, fragment, fields, content
        except NoMoreData:
            pass
Ejemplo n.º 2
0
def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect(('gunicorn.org', 80))
        s.send("GET / HTTP/1.1\r\nHost: gunicorn.org\r\n\r\n")
        p = HttpStream(SocketReader(s))
        print p.headers()
        print p.body_file().read()
    finally:
        s.close()
Ejemplo n.º 3
0
def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect(('gunicorn.org', 80))
        s.send("GET / HTTP/1.1\r\nHost: gunicorn.org\r\n\r\n")
        p = HttpStream(SocketReader(s))
        print p.headers()
        print p.body_file().read()
    finally:
        s.close()
Ejemplo n.º 4
0
    def get_response(self, request, connection):
        """ return final respons, it is only accessible via peform
        method """
        if log.isEnabledFor(logging.DEBUG):
            log.debug("Start to parse response")

        p = HttpStream(SocketReader(connection.socket()), kind=1,
                decompress=self.decompress)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Got response: %s %s" % (p.version(), p.status()))
            log.debug("headers: [%s]" % p.headers())

        location = p.headers().get('location')

        if self.follow_redirect:
            should_close = not p.should_keep_alive()
            if p.status_code() in (301, 302, 307,):

                # read full body and release the connection
                p.body_file().read()
                connection.release(should_close)

                if request.method in ('GET', 'HEAD',) or \
                        self.force_follow_redirect:
                    if hasattr(self.body, 'read'):
                        try:
                            self.body.seek(0)
                        except AttributeError:
                            raise RequestError("Can't redirect %s to %s "
                                    "because body has already been read"
                                    % (self.url, location))
                    return self.redirect(location, request)

            elif p.status_code() == 303 and self.method == "POST":
                # read full body and release the connection
                p.body_file().read()
                connection.release(should_close)

                request.method = "GET"
                request.body = None
                return self.redirect(location, request)

        # create response object
        resp = self.response_class(connection, request, p)

        # apply response filters
        for f in self.response_filters:
            f.on_response(resp, request)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("return response class")

        # return final response
        return resp
Ejemplo n.º 5
0
    def get_response(self, request, connection):
        """ return final respons, it is only accessible via peform
        method """
        if log.isEnabledFor(logging.DEBUG):
            log.debug("Start to parse response")

        p = HttpStream(SocketReader(connection.socket()), kind=1,
                decompress=self.decompress)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Got response: %s %s" % (p.version(), p.status()))
            log.debug("headers: [%s]" % p.headers())

        location = p.headers().get('location')

        if self.follow_redirect:
            should_close = not p.should_keep_alive()
            if p.status_code() in (301, 302, 307,):

                # read full body and release the connection
                p.body_file().read()
                connection.release(should_close)

                if request.method in ('GET', 'HEAD',) or \
                        self.force_follow_redirect:
                    if hasattr(self.body, 'read'):
                        try:
                            self.body.seek(0)
                        except AttributeError:
                            raise RequestError("Can't redirect %s to %s "
                                    "because body has already been read"
                                    % (self.url, location))
                    return self.redirect(location, request)

            elif p.status_code() == 303 and self.method == "POST":
                # read full body and release the connection
                p.body_file().read()
                connection.release(should_close)

                request.method = "GET"
                request.body = None
                return self.redirect(location, request)

        # create response object
        resp = self.response_class(connection, request, p)

        # apply response filters
        for f in self.response_filters:
            f.on_response(resp, request)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("return response class")

        # return final response
        return resp
Ejemplo n.º 6
0
Archivo: wsgi.py Proyecto: wong2/larus
def create(server, client, config):
    client.setblocking(1)
    request = HttpStream(SocketReader(client))
    body_stream = request.body_file()
    environ = request.wsgi_environ()

    ''' 
    the environ produced by Python and C parser is not consistent
    we have to set some missing values here
    '''
    host, port = server.getsockname()
    environ.update({
        'SERVER_NAME': host,
        'SERVER_PORT': str(port),
        'wsgi.version': (1, 0),
        'wsgi.url_scheme': utils.guess_scheme(environ),
        'wsgi.input': body_stream,
        'wsgi.multithread': False,
        'wsgi.multiprocess': config['workers'] > 1,
        'wsgi.run_once': False,
        'wsgi.file_wrapper': utils.FileWrapper,
    })

    response = Response(request, client)
    
    return environ, response
Ejemplo n.º 7
0
def http_serve(config, stdin, stdout):

    # Re-open the stdin and stdout file descriptors using raw unbuffered i/o.
    stdin = io.FileIO(stdin.fileno())
    stdout = io.FileIO(stdout.fileno(), 'w')

    # Parse an HTTP request on stdin.
    parser = HttpStream(stdin, kind=HTTP_REQUEST)
    wsgi_env = parser.wsgi_environ()
    request_method = wsgi_env['REQUEST_METHOD']
    request_path = wsgi_env['PATH_INFO']
    request_length = wsgi_env.get('HTTP_CONTENT_LENGTH', 0)
    request_body = parser.body_file()

    try:

        success = False

        if request_method != 'GET':
            raise HttpError(405)

        if request_path.endswith('/'):
            http_respond(stdout,
                         301,
                         location=request_path.rstrip('/'),
                         body='')
        else:
            path = resolve_path(request_path)
            mimetype = guess_type(path)[0]

            try:
                output = render(config, request_path)
                http_respond(stdout, 200, mimetype=mimetype, body=output)
            except TemplateNotFound as e:
                # Retry as static file.
                full_static_path = config.static_root + path
                with file(full_static_path) as static_file:
                    http_respond(stdout,
                                 200,
                                 mimetype=mimetype,
                                 body=static_file)

        success = True

    except InvalidRequestPath as e:
        http_respond(stdout, 400)
    except HttpError as e:
        http_respond(stdout, e.status)
    except IOError as e:
        if e.errno == errno.ENOENT:
            http_respond(stdout, 404)
        elif e.errno == errno.EACCES:
            http_respond(stdout, 403)
        else:
            http_respond(stdout, 500)
    except:
        http_respond(stdout, 500)

    return success
Ejemplo n.º 8
0
def recGETbody(HOST, request_headerGET):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        try:
            s.sendall(request_headerGET.encode('utf-8'))
        except:
            print("An error has occured while trying to send the GET request")
        dataReceived = SocketReader(s)
        parsed = HttpStream(dataReceived)
        body = parsed.body_file().read().decode('utf-8')
        return body
Ejemplo n.º 9
0
 def recv(self):
     try:
         stream  = HttpStream(self.reader, kind=HTTP_RESPONSE, parser_class=HttpParser, decompress=True)
         status  = stream.status_code()
         version = stream.version()
         fields  = stream.headers()
         content = stream.body_file()
         self.version = 'HTTP/%s.%s' % version
         return status, fields, content
     except NoMoreData:
         pass
Ejemplo n.º 10
0
def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect(('baike.baidu.com', 80))
        s.send(b("GET /view/262.htm HTTP/1.1\r\nHost: baike.baidu.com\r\n\r\n"))
        p = HttpStream(SocketReader(s), decompress=True)
        print(p.headers())

        print(p.body_file().read())
    finally:
        s.close()
Ejemplo n.º 11
0
def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect(('baike.baidu.com', 80))
        s.send(
            b("GET /view/262.htm HTTP/1.1\r\nHost: baike.baidu.com\r\n\r\n"))
        p = HttpStream(SocketReader(s), decompress=True)
        print(p.headers())

        print(p.body_file().read())
    finally:
        s.close()
Ejemplo n.º 12
0
def GETverbose(HOST, request_headerGET):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, 80))
        try:
            s.sendall(request_headerGET.encode('utf-8'))
        except:
            print("An error has occured while trying to send the GET request")
        dataReceived = SocketReader(s)
        parsed = HttpStream(dataReceived)
        body = parsed.body_file().read().decode('utf-8')
        header = parsed.headers()
        return pprint.pformat(header) + body
Ejemplo n.º 13
0
def rewrite_request(req):
    try: 
        while True:
            parser = HttpStream(req)
                
            new_headers = rewrite_headers(parser, {'Host': 'gunicorn.org'})
            if new_headers is None:
                break
            req.send(new_headers)
            body = parser.body_file()
            while True:
                data = body.read(8192)
                if not data:
                    break
                req.writeall(data)
    except (socket.error, NoMoreData):
        pass
Ejemplo n.º 14
0
def rewrite_request(req):
    try: 
        while True:
            parser = HttpStream(req)

            new_headers = rewrite_headers(parser, dict({'Host': 'api.mailgun.net'}, **basic_auth_headers('api', MAILGUN_APIKEY)))
            if new_headers is None:
                break
            req.send(new_headers)
            body = parser.body_file()
            while True:
                data = body.read(8192)
                if not data:
                    break
                req.writeall(data)
    except (socket.error, NoMoreData):
        pass
Ejemplo n.º 15
0
def processData(newsocketconn, fromaddr, context, ipaddr, q):
    if useSSL:
        connstreamout = context.wrap_socket(newsocketconn, server_side=True)
    else:
        connstreamout = newsocketconn
    try:
        try:
            ### Read the json response using Socket Reader and split header and body
            r = SocketReader(connstreamout)
            p = HttpStream(r)
            headers = p.headers()
            q.put(makeLogMessage("INFO", "idrac", "ProcessData"))

            if p.method() == 'POST':
                bodydata = p.body_file().read()
                bodydata = bodydata.decode("utf-8", errors='ignore')

                ### Read the json response and print the output
                # outdata = dict()
                try:
                    with open(f"{ipaddr[3]}/output/{ipaddr[2]}", "a+") as fd:
                        fd.write(bodydata)
                        fd.write("\n")
                except Exception as ex:
                    print("Exception Occured =", ex)

                StatusCode = """HTTP/1.1 200 OK\r\n\r\n"""
                connstreamout.send(bytes(StatusCode, 'UTF-8'))

            # if p.method() == 'GET':

            #     res = "HTTP/1.1 200 OK\n" \
            #           "Content-Type: application/json\n" \
            #           "\n" + json.dumps(data_buffer)
            #     connstreamout.send(res.encode())
            #     data_buffer.clear()

        except Exception as err:
            outdata = connstreamout.read()
            traceback.print_exc()
            print("Data needs to read in normal Text format.")

    finally:
        connstreamout.shutdown(socket.SHUT_RDWR)
        connstreamout.close()
Ejemplo n.º 16
0
    def send(self, protocol ,host, header):
        h = Http()
        h.build_header()
        data = header
        str_response_body=""
        str_response_header=""

        if protocol == "http":
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((host,80))
            s.send(data.encode('utf-8'))
            """
            response = b""
            while True:
                d = client.recv(1024)
                response = response + d
                if not d:
                    break
            #print(response)
            """
            p = HttpStream(SocketReader(s))

        elif protocol == "https":
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((host, 443))
            s = ssl.wrap_socket(s, keyfile=None, certfile=None, server_side=False, cert_reqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_SSLv23)
            s.sendall(data.encode('utf-8'))
            """
            response = b""
            while True:
                d = s.recv(4096)
                response = response + d
                if not d:
                    break
            """
            p = HttpStream(SocketReader(s))
        #return html.escape(str(response, errors='replace'))
        # Transfer-Encoding: chunked の対処をする。
        
        str_response_header += p.status()+"\n"
        for h in p.headers():
            str_response_header += h+": "+p.headers()[h]+"\n"
        str_response_body += str(p.body_file().read(),  errors='replace')
        return {"body":str_response_body, "header":str_response_header}
Ejemplo n.º 17
0
    def rewrite_response(self, resp, extra):
        try:
            if extra.get('rewrite_response', False):
                parser = HttpStream(resp, decompress=True)

                rw = RewriteResponse(parser, resp, extra)
                rw.execute()

            else:
                parser = HttpStream(resp)
                headers = parser.headers()
                headers['connection'] = 'close'

                new_headers = headers_lines(parser, headers)
                resp.writeall("".join(new_headers) + "\r\n")

                body = parser.body_file()
                send_body(resp, body, parser.is_chunked())
        except (socket.error, NoMoreData, ParserError):
            pass
Ejemplo n.º 18
0
    def rewrite_response(self, resp, extra):
        try:
            if extra.get('rewrite_response', False):
                parser = HttpStream(resp, decompress=True)
                
                rw = RewriteResponse(parser, resp, extra)
                rw.execute()

            else:
                parser = HttpStream(resp)
                headers = parser.headers()
                headers['connection'] = 'close'
                
                new_headers = headers_lines(parser, headers)
                resp.writeall("".join(new_headers) + "\r\n")

                body = parser.body_file()
                send_body(resp, body, parser.is_chunked())
        except (socket.error, NoMoreData, ParserError):
            pass
Ejemplo n.º 19
0
def rewrite_request(req):
    log = logging.getLogger("rewrite_request")
    peername = req._src.getpeername()
    try:
        while True:
            request_id = str(uuid.uuid4())
            parser = HttpStream(req)
            new_headers = rewrite_headers(
                parser, peername, {'x-nimbus-io-user-request-id': request_id})
            if new_headers is None:
                break
            log.debug("rewriting request %s" % ( request_id, ))
            req.send(new_headers)
            body = parser.body_file()
            while True:
                data = body.read(8192)
                if not data:
                    break
                req.writeall(data)
    except (socket.error, NoMoreData):
        pass
Ejemplo n.º 20
0
def rewrite_request(req):
    log = logging.getLogger("rewrite_request")
    peername = req._src.getpeername()
    try:
        while True:
            request_id = str(uuid.uuid4())
            parser = HttpStream(req)
            new_headers = rewrite_headers(
                parser, peername, {'x-nimbus-io-user-request-id': request_id})
            if new_headers is None:
                break
            log.debug("rewriting request %s" % (request_id, ))
            req.send(new_headers)
            body = parser.body_file()
            while True:
                data = body.read(8192)
                if not data:
                    break
                req.writeall(data)
    except (socket.error, NoMoreData):
        pass
Ejemplo n.º 21
0
    def rewrite_request(self, req, extra):
    
        try:
            while True:
                if extra.get('rewrite_location', True):
                    parser = HttpStream(req)
                    
                    prefix = extra.get('prefix', '')
                    location = parser.url()
                    if prefix:
                        try:
                            location = location.split(prefix, 1)[1] or '/'
                        except IndexError:
                            pass
                    
                    headers = rewrite_headers(parser, location,
                            [('host', extra.get('host'))])

                    if headers is None:
                        break

                    extra['path'] = parser.path()

                    req.writeall(headers)
                    body = parser.body_file()
                    while True:
                        data = body.read(8192)
                        if not data:
                            break
                        req.writeall(data)
                else:
                    while True:
                        data = req.read(io.DEFAULT_BUFFER_SIZE)
                        if not data:
                            break
                        req.writeall(data) 
        except (socket.error, NoMoreData):
            pass
Ejemplo n.º 22
0
    def rewrite_request(self, req, extra):

        try:
            while True:
                if extra.get('rewrite_location', True):
                    parser = HttpStream(req)

                    prefix = extra.get('prefix', '')
                    location = parser.url()
                    if prefix:
                        try:
                            location = location.split(prefix, 1)[1] or '/'
                        except IndexError:
                            pass

                    headers = rewrite_headers(parser, location,
                                              [('host', extra.get('host'))])

                    if headers is None:
                        break

                    extra['path'] = parser.path()

                    req.writeall(headers)
                    body = parser.body_file()
                    while True:
                        data = body.read(8192)
                        if not data:
                            break
                        req.writeall(data)
                else:
                    while True:
                        data = req.read(io.DEFAULT_BUFFER_SIZE)
                        if not data:
                            break
                        req.writeall(data)
        except (socket.error, NoMoreData):
            pass
Ejemplo n.º 23
0
def rewrite_request(req):
    try:
        while True:
            parser = HttpStream(req)
            headers = parser.headers()

            parsed_url = urlparse.urlparse(parser.url())

            is_ssl = parsed_url.scheme == "https"

            host = get_host(parse_address(parsed_url.netloc, 80),
                            is_ssl=is_ssl)
            headers['Host'] = host
            headers['Connection'] = 'close'

            if 'Proxy-Connection' in headers:
                del headers['Proxy-Connection']

            location = urlparse.urlunparse(
                ('', '', parsed_url.path, parsed_url.params, parsed_url.query,
                 parsed_url.fragment))

            httpver = "HTTP/%s" % ".".join(map(str, parser.version()))

            new_headers = [
                "%s %s %s\r\n" % (parser.method(), location, httpver)
            ]

            new_headers.extend(["%s: %s\r\n" % (hname, hvalue) \
                    for hname, hvalue in headers.items()])

            req.writeall(bytes("".join(new_headers) + "\r\n"))
            body = parser.body_file()
            send_body(req, body, parser.is_chunked())

    except (socket.error, NoMoreData, ParserError):
        pass
Ejemplo n.º 24
0
def rewrite_request(req):
    try:
        while True:
            parser = HttpStream(req)
            headers = parser.headers()

            parsed_url = urlparse.urlparse(parser.url())

            is_ssl = parsed_url.scheme == "https"

            host = get_host(parse_address(parsed_url.netloc, 80),
                is_ssl=is_ssl)
            headers['Host'] = host
            headers['Connection'] = 'close'

            if 'Proxy-Connection' in headers:
                del headers['Proxy-Connection']


            location = urlparse.urlunparse(('', '', parsed_url.path,
                parsed_url.params, parsed_url.query, parsed_url.fragment))

            httpver = "HTTP/%s" % ".".join(map(str, 
                        parser.version()))

            new_headers = ["%s %s %s\r\n" % (parser.method(), location, 
                httpver)]

            new_headers.extend(["%s: %s\r\n" % (hname, hvalue) \
                    for hname, hvalue in headers.items()])

            req.writeall(bytes("".join(new_headers) + "\r\n"))
            body = parser.body_file()
            send_body(req, body, parser.is_chunked())

    except (socket.error, NoMoreData, ParserError):
            pass
Ejemplo n.º 25
0
def read_output_data(newsocketconn, fromaddr):
    if useSSL:
        connstreamout = context.wrap_socket(newsocketconn, server_side=True)
    else:
        connstreamout = newsocketconn
    ### Output File Name
    outputfile = "Events_" + str(fromaddr[0]) + ".txt"
    global event_count
    outdata = headers = HostDetails = ""
    try:
        try:
            ### Read the json response using Socket Reader and split header and body
            r = SocketReader(connstreamout)
            p = HttpStream(r)
            headers = p.headers()
            print("headers: ", headers)
            bodydata = p.body_file().read()
            bodydata = bodydata.decode("utf-8")
            print("bodydata: ", bodydata)
            for eachHeader in headers.items():
                if eachHeader[0] == 'Host' or eachHeader[0] == 'host':
                    HostDetails = eachHeader[1]

            ### Read the json response and print the output
            print("\n")
            print("Server IP Address is ", fromaddr[0])
            print("Server PORT number is ", fromaddr[1])
            print("Listener IP is ", HostDetails)
            outdata = json.loads(bodydata)
            event_array = outdata['Events']
            for event in event_array:
                print("EventType is ", event['EventType'])
                print("MessageId is ", event['MessageId'])
                if 'EventId' in event:
                    print("EventId is ", event['EventId'])
                if 'EventTimestamp' in event:
                    print("EventTimestamp is ", event['EventTimestamp'])
                if 'Severity' in event:
                    print("Severity is ", event['Severity'])
                if 'Message' in event:
                    print("Message is ", event['Message'])
                if 'MessageArgs' in event:
                    print("MessageArgs is ", event['MessageArgs'])
                if 'Context' in outdata:
                    print("Context is ", outdata['Context'])
                print("\n")

        except Exception as err:
            outdata = connstreamout.read()
            print("Data needs to read in normal Text format.")
            print(outdata)

        ### Check the context and send the status OK if context matches
        if outdata.get('Context', None) != ContextDetail:
            print("Context ({}) does not match with the server ({}).".format(
                outdata.get('Context', None), ContextDetail))
        StatusCode = """HTTP/1.1 200 OK\r\n\r\n"""
        connstreamout.send(bytes(StatusCode, 'UTF-8'))

        try:
            if event_count.get(str(fromaddr[0])):
                event_count[str(
                    fromaddr[0])] = event_count[str(fromaddr[0])] + 1
            else:
                event_count[str(fromaddr[0])] = 1

            print("Event Counter for Host %s is %s" %
                  (str(fromaddr[0]), event_count[fromaddr[0]]))
            print("\n")
            fd = open(outputfile, "a")
            fd.write("Time:%s Count:%s\nHost IP:%s\nEvent Details:%s\n" %
                     (time.ctime(), event_count[str(
                         fromaddr[0])], str(fromaddr), outdata))
            fd.close()
        except Exception as err:
            print(traceback.print_exc())

    finally:
        connstreamout.shutdown(socket.SHUT_RDWR)
        connstreamout.close()
Ejemplo n.º 26
0
def process_data(newsocketconn, fromaddr, threads):
    if useSSL:
        connstreamout = context.wrap_socket(newsocketconn, server_side=True)
    else:
        connstreamout = newsocketconn
    ### Output File Name
    outputfile = "Events_" + str(fromaddr[0]) + ".txt"
    logfile = "TimeStamp.log"
    global event_count, data_buffer
    outdata = headers = HostDetails = ""
    try:
        try:
            ### Read the json response using Socket Reader and split header and body
            r = SocketReader(connstreamout)
            p = HttpStream(r)
            headers = p.headers()
            #print("headers: ", headers)

            if p.method() == 'POST':
                bodydata = p.body_file().read()
                bodydata = bodydata.decode("utf-8", errors='ignore')
                for eachHeader in headers.items():
                    if eachHeader[0] == 'Host' or eachHeader[0] == 'host':
                        HostDetails = eachHeader[1]

                ### Read the json response and print the output
                #logging.info(f"Server IP Address is {}".format(fromaddr[0])
                outdata = dict()
                current_date = DT.now().strftime("%Y%m%d")
                folder_suffix = "-{}".format(
                    listenerport) if listenerport != '443' else ''
                directory = '{}{}/{}/{}'.format(report_location, folder_suffix,
                                                fromaddr[0], current_date)
                try:
                    outdata = json.loads(bodydata)
                except json.decoder.JSONDecodeError:
                    raw_invalid_file_thread = threading.Thread(
                        target=writeInvalidJason,
                        args=(directory, outdata),
                        name="{}_F".format(fromaddr[0]))
                    threads.append(raw_invalid_file_thread)
                    raw_invalid_file_thread.start()
                if outdata:
                    #writeRawJson(directory, outdata)
                    raw_file_thread = threading.Thread(
                        target=writeRawJson,
                        args=(directory, outdata, fromaddr[0]),
                        name="{}_F".format(fromaddr[0]))
                    threads.append(raw_file_thread)
                    raw_file_thread.start()

                StatusCode = """HTTP/1.1 200 OK\r\n\r\n"""
                connstreamout.send(bytes(StatusCode, 'UTF-8'))
                try:
                    if event_count.get(str(fromaddr[0])):
                        event_count[str(
                            fromaddr[0])] = event_count[str(fromaddr[0])] + 1
                    else:
                        event_count[str(fromaddr[0])] = 1
                    logging.info("Event Counter for Host %s = %s" %
                                 (str(fromaddr[0]), event_count[fromaddr[0]]))
                except Exception as err:
                    logging.error(err)
                    #print(traceback.print_exc())
                for th in threads:
                    th.join()

            if p.method() == 'GET':
                res = "HTTP/1.1 200 OK\n" \
                      "Content-Type: application/json\n" \
                      "\n" + json.dumps(data_buffer)
                connstreamout.send(res.encode())
                data_buffer.clear()
        except Exception as err:
            outdata = connstreamout.read()
            traceback.print_exc()
            #logging.exception(f"Data needs to read in normal Text format.{err}. Message is : {str(outdata)}")
    finally:
        connstreamout.shutdown(socket.SHUT_RDWR)
        connstreamout.close()
        logging.debug("Connection closed")
Ejemplo n.º 27
0
def processData(newsocketconn, fromaddr, context):
    if useSSL:
        connstreamout = context.wrap_socket(newsocketconn, server_side=True)
    else:
        connstreamout = newsocketconn
    global event_count, data_buffer
    outdata = headers = HostDetails = ""
    try:
        try:
            ### Read the json response using Socket Reader and split header and body
            r = SocketReader(connstreamout)
            p = HttpStream(r)
            headers = p.headers()

            if p.method() == 'POST':
                bodydata = p.body_file().read()
                bodydata = bodydata.decode("utf-8", errors='ignore')
                for eachHeader in headers.items():
                    if eachHeader[0] == 'Host' or eachHeader[0] == 'host':
                        HostDetails = eachHeader[1]

                ### Read the json response and print the output
                print("Server IP Address is ", fromaddr[0])
                outdata = dict()
                # current_date = DT.now().strftime("%Y%m%d")
                # folder_suffix = "-{}".format(listenerport)  if listenerport != '443' else ''
                # directory = '{}{}/{}/{}'.format(report_location,folder_suffix,fromaddr[0],current_date)
                try:
                    print(bodydata)
                    with open("output.json", "a+") as f:
                        f.write(bodydata)
                        f.write("\n")
                except json.decoder.JSONDecodeError:
                    print("Exception occurred while processing report")

                # influx_thread = threading.Thread(target=writeReportToInflux, args=(fromaddr[0],directory, outdata))
                # threads.append(influx_thread)
                # influx_thread.start()

                StatusCode = """HTTP/1.1 200 OK\r\n\r\n"""
                connstreamout.send(bytes(StatusCode, 'UTF-8'))
                # try:
                #    if event_count.get(str(fromaddr[0])):
                #        event_count[str(fromaddr[0])] = event_count[str(fromaddr[0])] + 1
                #    else:
                #        event_count[str(fromaddr[0])] = 1
                #    # logger.info("Event Counter for Host %s = %s" % (str(fromaddr[0]), event_count[fromaddr[0]]))
                # except Exception as err:
                #    print(traceback.print_exc())
                # for th in threads:
                #     th.join()   

            if p.method() == 'GET':
                
                res = "HTTP/1.1 200 OK\n" \
                      "Content-Type: application/json\n" \
                      "\n" + json.dumps(data_buffer)
                connstreamout.send(res.encode())
                data_buffer.clear()

        except Exception as err:
            outdata = connstreamout.read()
            traceback.print_exc()
            print("Data needs to read in normal Text format.")
            print(outdata)

    finally:
        connstreamout.shutdown(socket.SHUT_RDWR)
        connstreamout.close()
Ejemplo n.º 28
0
def processData(newsocketconn, fromaddr, context, ipaddr):
    if useSSL:
        connstreamout = context.wrap_socket(newsocketconn, server_side=True)
    else:
        connstreamout = newsocketconn
    global event_count, data_buffer
    outdata = headers = HostDetails = ""
    try:
        try:
            ### Read the json response using Socket Reader and split header and body
            r = SocketReader(connstreamout)
            p = HttpStream(r)
            headers = p.headers()

            if p.method() == 'POST':
                bodydata = p.body_file().read()
                bodydata = bodydata.decode("utf-8", errors='ignore')

                ### Read the json response and print the output
                # outdata = dict()
                try:
                    old_time = getTimestampfromFilename(ipaddr[2])
                    new_time = (datetime.strptime(old_time, "%Y%m%d%H%M%S") +
                                timedelta(seconds=int(ipaddr[4]))
                                ).strftime("%Y%m%d%H%M%S")
                    cur_time = datetime.now().strftime("%Y%m%d%H%M%S")
                    a = np.where(
                        int(cur_time) > int(new_time), new_time, False)
                    fd = ipaddr[2]
                    if eval(a.item(0)):
                        filename = fd.name.split("/")[-1]
                        # print(filename, "<--test")

                        os.system(f"gzip {ipaddr[3]}/output/{filename}")
                        shutil.move(f"{ipaddr[3]}/output/{filename}.gz",
                                    f"{ipaddr[3]}/complete/")

                        fd.close()

                        filename = ipaddr[0] + '_' + ipaddr[
                            1] + '_' + fromaddr[0] + '_' + new_time + ".jsonl"
                        try:
                            fd = open(f"{ipaddr[3]}/output/{filename}", "a+")
                        except FileNotFoundError as ex:
                            if not os.path.exists(f"{file_collection_path}"):
                                os.makedirs(file_collection_path)
                        ipaddr[2] = open(f"{ipaddr[3]}/output/{filename}",
                                         "a+")
                    # print(bodydata, type(bodydata), "<---bodydata")
                    fd.write(bodydata)
                    fd.write("\n")
                except Exception as ex:
                    print("Exception Occured =", ex)

                StatusCode = """HTTP/1.1 200 OK\r\n\r\n"""
                connstreamout.send(bytes(StatusCode, 'UTF-8'))
                # try:
                #    if event_count.get(str(fromaddr[0])):
                #        event_count[str(fromaddr[0])] = event_count[str(fromaddr[0])] + 1
                #    else:
                #        event_count[str(fromaddr[0])] = 1
                #    # logger.info("Event Counter for Host %s = %s" % (str(fromaddr[0]), event_count[fromaddr[0]]))
                # except Exception as err:
                #    print(traceback.print_exc())
                # for th in threads:
                #     th.join()

            if p.method() == 'GET':

                res = "HTTP/1.1 200 OK\n" \
                      "Content-Type: application/json\n" \
                      "\n" + json.dumps(data_buffer)
                connstreamout.send(res.encode())
                data_buffer.clear()

        except Exception as err:
            outdata = connstreamout.read()
            traceback.print_exc()
            print("Data needs to read in normal Text format.")
            print(outdata)

    finally:
        connstreamout.shutdown(socket.SHUT_RDWR)
        connstreamout.close()
Ejemplo n.º 29
0
async def processData(newsocketconn, fromaddr, context, ipaddr):
    if useSSL:
        connstreamout = context.wrap_socket(newsocketconn, server_side=True)
    else:
        connstreamout = newsocketconn
    global event_count, data_buffer
    outdata = headers = HostDetails = ""
    try:
        try:
            ### Read the json response using Socket Reader and split header and body
            r = SocketReader(connstreamout)
            p = HttpStream(r)
            headers = p.headers()

            if p.method() == 'POST':
                bodydata = p.body_file().read()
                bodydata = bodydata.decode("utf-8", errors='ignore')
                for eachHeader in headers.items():
                    if eachHeader[0] == 'Host' or eachHeader[0] == 'host':
                        HostDetails = eachHeader[1]

                ### Read the json response and print the output
                outdata = dict()
                try:
                    # get Current fd Timestamp
                    timestamp = getTimestampfromFilename(ipaddr)
                    # createJsonFile(ipaddr)
                    fd = ipaddr[2]
                    newFile = canGernerateNewJsonFile(timestamp, ipaddr[4])
                    if newFile:
                        print(f"newfile Creates for iDRAC {fromaddr[0]}")
                        fd = createJsonFile(ipaddr, fromaddr[0])
                        ipaddr[2] = fd
                    fd.write(bodydata)
                    fd.write("\n")

                except Exception as ex:
                    print("Exception occurred while processing report", ex)

                # influx_thread = threading.Thread(target=writeReportToInflux, args=(fromaddr[0],directory, outdata))
                # threads.append(influx_thread)
                # influx_thread.start()

                StatusCode = """HTTP/1.1 200 OK\r\n\r\n"""
                connstreamout.send(bytes(StatusCode, 'UTF-8'))
                # try:
                #    if event_count.get(str(fromaddr[0])):
                #        event_count[str(fromaddr[0])] = event_count[str(fromaddr[0])] + 1
                #    else:
                #        event_count[str(fromaddr[0])] = 1
                #    # logger.info("Event Counter for Host %s = %s" % (str(fromaddr[0]), event_count[fromaddr[0]]))
                # except Exception as err:
                #    print(traceback.print_exc())
                # for th in threads:
                #     th.join()

            if p.method() == 'GET':

                res = "HTTP/1.1 200 OK\n" \
                      "Content-Type: application/json\n" \
                      "\n" + json.dumps(data_buffer)
                connstreamout.send(res.encode())
                data_buffer.clear()

        except Exception as err:
            outdata = connstreamout.read()
            traceback.print_exc()
            print("Data needs to read in normal Text format.")
            print(outdata)

    finally:
        connstreamout.shutdown(socket.SHUT_RDWR)
        connstreamout.close()
def process_data(newsocketconn, fromaddr):
    if useSSL:
        connstreamout = context.wrap_socket(newsocketconn, server_side=True)
    else:
        connstreamout = newsocketconn
    ### Output File Name
    outputfile = "Events_" + str(fromaddr[0]) + ".txt"
    logfile = "TimeStamp.log"
    global event_count, data_buffer
    outdata = headers = HostDetails = ""
    try:
        try:
            ### Read the json response using Socket Reader and split header and body
            r = SocketReader(connstreamout)
            p = HttpStream(r)
            headers = p.headers()
            my_logger.info("headers: %s", headers)

            if p.method() == 'POST':
                bodydata = p.body_file().read()
                bodydata = bodydata.decode("utf-8")
                my_logger.info("\n")
                my_logger.info("bodydata: %s", bodydata)
                data_buffer.append(bodydata)
                for eachHeader in headers.items():
                    if eachHeader[0] == 'Host' or eachHeader[0] == 'host':
                        HostDetails = eachHeader[1]

                ### Read the json response and print the output
                my_logger.info("\n")
                my_logger.info("Server IP Address is %s", fromaddr[0])
                my_logger.info("Server PORT number is %s", fromaddr[1])
                my_logger.info("Listener IP is %s", HostDetails)
                my_logger.info("\n")
                outdata = json.loads(bodydata)
                if 'Events' in outdata and config['verbose']:
                    event_array = outdata['Events']
                    for event in event_array:
                        my_logger.info("EventType is %s", event['EventType'])
                        my_logger.info("MessageId is %s", event['MessageId'])
                        if 'EventId' in event:
                            my_logger.info("EventId is %s", event['EventId'])
                        if 'EventGroupId' in event:
                            my_logger.info("EventGroupId is %s", event['EventGroupId'])
                        if 'EventTimestamp' in event:
                            my_logger.info("EventTimestamp is %s", event['EventTimestamp'])
                        if 'Severity' in event:
                            my_logger.info("Severity is %s", event['Severity'])
                        if 'MessageSeverity' in event:
                            my_logger.info("MessageSeverity is %s", event['MessageSeverity'])
                        if 'Message' in event:
                            my_logger.info("Message is %s", event['Message'])
                        if 'MessageArgs' in event:
                            my_logger.info("MessageArgs is %s", event['MessageArgs'])
                        if 'Context' in outdata:
                            my_logger.info("Context is %s", outdata['Context'])
                        my_logger.info("\n")
                if 'MetricValues' in outdata and config['verbose']:
                    metric_array = outdata['MetricValues']
                    my_logger.info("Metric Report Name is: %s", outdata.get('Name'))
                    for metric in metric_array:
                        my_logger.info("Member ID is: %s", metric.get('MetricId'))
                        my_logger.info("Metric Value is: %s", metric.get('MetricValue'))
                        my_logger.info("TimeStamp is: %s", metric.get('Timestamp'))
                        if 'MetricProperty' in metric:
                            my_logger.info("Metric Property is: %s", metric['MetricProperty'])
                        my_logger.info("\n")

                ### Check the context and send the status OK if context matches
                if config['contextdetail'] is not None and outdata.get('Context', None) != config['contextdetail']:
                    my_logger.info("Context ({}) does not match with the server ({})."
                          .format(outdata.get('Context', None), config['contextdetail']))
                StatusCode = """HTTP/1.1 200 OK\r\n\r\n"""
                connstreamout.send(bytes(StatusCode, 'UTF-8'))
                with open(logfile, 'a') as f:
                    if 'EventTimestamp' in outdata:
                        receTime = datetime.now()
                        sentTime = datetime.strptime(outdata['EventTimestamp'], "%Y-%m-%d %H:%M:%S.%f")
                        f.write("%s    %s    %sms\n" % (
                            sentTime.strftime("%Y-%m-%d %H:%M:%S.%f"), receTime, (receTime - sentTime).microseconds / 1000))
                    else:
                        f.write('No available timestamp.')

                try:
                    if event_count.get(str(fromaddr[0])):
                        event_count[str(fromaddr[0])] = event_count[str(fromaddr[0])] + 1
                    else:
                        event_count[str(fromaddr[0])] = 1

                    my_logger.info("Event Counter for Host %s = %s" % (str(fromaddr[0]), event_count[fromaddr[0]]))
                    my_logger.info("\n")
                    fd = open(outputfile, "a")
                    fd.write("Time:%s Count:%s\nHost IP:%s\nEvent Details:%s\n" % (
                        datetime.now(), event_count[str(fromaddr[0])], str(fromaddr), json.dumps(outdata)))
                    fd.close()
                except Exception as err:
                    my_logger.info(traceback.print_exc())

            if p.method() == 'GET':
                # for x in data_buffer:
                #     my_logger.info(x)
                res = "HTTP/1.1 200 OK\n" \
                      "Content-Type: application/json\n" \
                      "\n" + json.dumps(data_buffer)
                connstreamout.send(res.encode())
                data_buffer.clear()

        except Exception as err:
            outdata = connstreamout.read()
            my_logger.info("Data needs to read in normal Text format.")
            my_logger.info(outdata)

    finally:
        connstreamout.shutdown(socket.SHUT_RDWR)
        connstreamout.close()