Skip to content

Commit

Permalink
restructered the servers and introduced a parent class
Browse files Browse the repository at this point in the history
started the reimplementation of the web interface as flask webapp
  • Loading branch information
susperius committed Oct 23, 2016
1 parent b6c5768 commit d03a585
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 33 deletions.
19 changes: 11 additions & 8 deletions communication/beaconserver.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
__author__ = 'susperius'

import logging
import gevent
import gevent.monkey
from gevent.server import DatagramServer

from server import Server
gevent.monkey.patch_all()


class BeaconServer:
class BeaconServer(Server):
def __init__(self, port, task_queue):
self._port = port
self._serving = False
Expand All @@ -18,17 +16,22 @@ def __init__(self, port, task_queue):
self._task_queue = task_queue

def __serve(self):
self._logger.info("[Beacon Server] initialized on port " + str(self._port) + " ...")
self._logger.info("[BeaconServer] initialized on port " + str(self._port) + " ...")
self._beacon_server = DatagramServer(('', self._port), self.__beacon_receiver)
self._beacon_server.serve_forever()

def __beacon_receiver(self, msg, address):
self._task_queue.put([address, msg])

def serve(self):
def start_server(self):
if not self._serving:
self._serving_greenlet = gevent.spawn(self.__serve)
self._serving = True
gevent.sleep(0)
else:
pass

def stop_server(self):
if self._serving:
gevent.kill(self._serving_greenlet)
self._serving = False
self._beacon_server.close()
self._logger.info("[BeaconServer] shut down")
20 changes: 11 additions & 9 deletions communication/reportserver.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
__author__ = 'susperius'


import logging

import gevent
import gevent.monkey
import gevent.socket as socket
from gevent.server import StreamServer

from server import Server
gevent.monkey.patch_all()


class ReportServer:
class ReportServer(Server):
def __init__(self, port, task_queue):
self._port = port
self._serving = False
Expand All @@ -34,15 +31,20 @@ def __report_receiver(self, sock, address):
self._task_queue.put((address[0], report))

def __serve(self):
self._logger.info("[Report Server] initialized on port " + str(self._port) + " ...")
self._logger.info("[ReportServer] initialized on port " + str(self._port) + " ...")
self._report_server = StreamServer(('', self._port), self.__report_receiver)
self._report_server.serve_forever()
gevent.sleep(0)

def serve(self):
def start_server(self):
if not self._serving:
self._serving_greenlet = gevent.spawn(self.__serve)
self._serving = True
gevent.sleep(0)
else:
pass

def stop_server(self):
if self._serving:
gevent.kill(self._serving_greenlet)
self._serving = False
self._report_server.close()
self._logger.info("[ReportServer] shut down")
7 changes: 7 additions & 0 deletions communication/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Server:

def start_server(self):
raise NotImplementedError("ABSTRACT METHOD")

def stop_server(self):
raise NotImplementedError("ABSTRACT METHOD")
19 changes: 11 additions & 8 deletions communication/webserver.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
__author__ = 'susperius'

import logging
import gevent
from gevent import pywsgi
from server import Server


class WebServer:
def __init__(self, port, task_queue, web_main_function):
class WebServer(Server):
def __init__(self, port, web_main_function):
self._port = port
self._serving = False
self._serving_greenlet = None
self._web_server = None
self._logger = logging.getLogger(__name__)
self._task_queue = task_queue
self._web_main_function = web_main_function

def __serve(self):
self._logger.info("[WebServer] initialized on port " + str(self._port) + " ...")
self._web_server = pywsgi.WSGIServer(('', self._port), self._web_main_function)
self._web_server.serve_forever()

def serve(self):
def start_server(self):
if not self._serving:
self._serving_greenlet = gevent.spawn(self.__serve)
self._serving = True
gevent.sleep(0)
else:
pass

def stop_server(self):
if self._serving:
gevent.kill(self._serving_greenlet)
self._serving = False
self._web_server.close()
self._logger.info("[WebServer] shut down")



25 changes: 17 additions & 8 deletions pyfuzz2_server.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
__author__ = 'susperius'

import logging
from urlparse import parse_qs

import gevent
import gevent.monkey
from gevent.queue import Queue
from urlparse import parse_qs

import node.model.config
from communication.beaconserver import BeaconServer
Expand All @@ -18,6 +15,7 @@
from worker.nodeclientworker import NodeClientWorker
from web.main import WebSite
from node.model.message_types import MESSAGE_TYPES
from web.app import WebInterface
gevent.monkey.patch_all()

CONFIG_FILENAME = "server_config.xml"
Expand Down Expand Up @@ -45,23 +43,34 @@ def __init__(self, logger, config_filename=CONFIG_FILENAME):
self._report_server = ReportServer(report_port, self._report_queue)
self._report_worker = ReportWorker(self._report_queue, self._db_queue, self._node_dict, self._crash_dict)
self._node_client_worker = NodeClientWorker(self._node_queue)
self._web_server = WebServer(web_port, self._web_queue, self.web_main)
self._web_intf = WebInterface(self._web_queue)
self._web_server = WebServer(web_port, self._web_queue, self._web_intf.app)

def main(self):
self._logger.info("PyFuzz2 Server started...")
self._beacon_server.serve()
self._beacon_server.start_server()
self._beacon_worker.start_worker()
self._report_server.serve()
self._report_server.start_server()
self._report_worker.start_worker()
self._web_server.serve()
self._web_server.start_server()
self._node_client_worker.start_worker()
self._db_worker.start_worker()
while True:
try:
gevent.wait()
except KeyboardInterrupt:
self.__shut_down()
exit(0)

def __shut_down(self):
self._beacon_server.stop_server()
self._beacon_worker.stop_worker()
self._report_server.stop_server()
self._report_worker.stop_worker()
self._web_server.stop_server()
self._node_client_worker.stop_worker()
self._db_worker.stop_worker()

def web_main(self, environ, start_response):
site = WebSite()
func = "home"
Expand Down
16 changes: 16 additions & 0 deletions web/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from flask import Flask, render_template, send_file
from gevent.queue import Queue


class WebInterface():
def __init__(self, web_queue):
self._web_queue = web_queue
self.app = Flask(__name__)
self.app.add_url_rule("/", "index", self.index_site)

def index_site(self):
return render_template("index.html")

if __name__ == "__main__":
intf = WebInterface(Queue())
intf.app.run("127.0.0.1", 5000, debug=True)
35 changes: 35 additions & 0 deletions web/static/scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Created by susperius on 11.07.15.
*/

FUZZERS;


function changeFuzzer(){
var x = -1;
var fuzz_table = document.getElementById("fuzz_table");
var old_inputs = document.getElementById("fuzz_config");
var new_inputs = document.createElement("div");
var fuzz_select = document.getElementById("fuzzers");
new_inputs.id = "fuzz_config";
for(i=0; i<fuzzers.length; i+=2){
if(fuzzers[i] == fuzz_select.value){
x = i + 1;
}
}

for(i=0; i<fuzzers[x].length; i++){
var input = document.createElement("input");
input.type = "text";
input.name = fuzzers[x][i];
new_inputs.appendChild(input);
}
old_inputs.parentNode.replaceChild(new_inputs, old_inputs);
}

function set_select_value(name){
var fuzz_select = document.getElementById("fuzzers");
fuzz_select.value = name;
}

//
97 changes: 97 additions & 0 deletions web/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#header {
background-color:darkorange;
color:black;
text-align: left;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
padding:5px;
}
#nav {
background-color: darkorange;
color: black;
line-height:30px;
height:800px;
width:15%;
float:left;
padding:5px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-style: oblique;
text-transform: uppercase;
}
#nav li{
margin: 0;
padding: 0;
list-style: none;
}
#nav a:hover{
background-color: white;
border-bottom: 1px solid black;
color: black;
padding-bottom: 8px;
}
#nav a{
background: darkorange;
border-bottom: 1px solid white;
color: black;
display: block;
margin: 0;
padding: 8px 12px;
text-decoration: none;
font-weight: normal;
}
#section {
width:80%;
height: 780px;
float:left;
padding:10px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
overflow: scroll;
}

#section h2 {
text-align: center;
}

#footer {
background-color: darkorange;
color:black;
clear:both;
text-align:center;
padding:5px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

table {
width:100%;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
table tr:nth-child(even) {
background-color: #eee;
}
table tr:nth-child(odd) {
background-color:#fff;
}
table th {
background-color: black;
color: white;
}
table a:link {
color:black;
text-decoration: underline;
}

table a:hover {
color: black;
text-decoration: none;
}

table a:visited{
color: black;
text-decoration: underline;
}
28 changes: 28 additions & 0 deletions web/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<link rel="stylesheet" href="{{ url_for("static", filename="style.css")}}">
<title>PyFuzz 2</title>
<script src="{{ url_for("static", filename="scripts.js") }}"></script>
</head>
<body>
<div id="header">
<h1>PyFuzz 2</h1>
</div>
<div id="nav">
<li class="active"><a href='index.py?func=home'><span>Overview</span></a></li>
<li class="active"><a href='index.py?func=stats'><span>Stats</span></a></li>
<li class="active"><a href='index.py?func=about'><span>About</span></a></li>
</div>
<div id="section">
<h2>SECTION_TITLE</h2>

REPLACE_ME<br>
</div>

<div id="footer">
[email protected] 2016
</div>
</body>
</html>

0 comments on commit d03a585

Please sign in to comment.