Ver Fonte

added files

Benedikt Heß há 10 anos atrás
pai
commit
fee2cd47ab
3 ficheiros alterados com 163 adições e 0 exclusões
  1. 12 0
      README.md
  2. 96 0
      main.py
  3. 55 0
      templates/main.html

+ 12 - 0
README.md

@@ -0,0 +1,12 @@
+# Requirements
+
+- tested with ubuntu 16.04 LTS
+- netem is required
+    - install with `sudo apt-get install iproute`
+- more information: https://calomel.org/network_loss_emulation.html
+
+
+# Usage
+
+- Execute the main.py file and go to http://localhost:5000
+- The tool will read your interfaces and the current setup every time the site is reloaded

+ 96 - 0
main.py

@@ -0,0 +1,96 @@
+import subprocess, os
+from flask import Flask, render_template, redirect, request, url_for
+
+
+app = Flask(__name__)
+
+
+@app.route("/")
+def main():
+    rules = get_active_rules()
+    return render_template('main.html', rules=rules)
+
+
+@app.route('/new_rule/<interface>', methods=['POST'])
+def new_rule(interface):
+    delay = request.form['Delay']
+    loss = request.form['Loss']
+    duplicate = request.form['Duplicate']
+    reorder = request.form['Reorder']
+    corrupt = request.form['Corrupt']
+
+    # remove old setup
+    command = 'tc qdisc del dev %s root netem' % interface
+    command = command.split(' ')
+    proc = subprocess.Popen(command)
+    proc.wait()
+
+    # apply new setup
+    command = 'tc qdisc add dev %s root netem' % interface
+    if delay != '':
+        command += ' delay %sms' % delay
+    if loss != '':
+        command += ' loss %s%%' % loss
+    if duplicate != '':
+        command += ' duplicate %s%%' % duplicate
+    if reorder != '':
+        command += ' reorder %s%%' % reorder
+    if corrupt != '':
+        command += ' corrupt %s%%' % corrupt
+    print(command)
+    command = command.split(' ')
+    proc = subprocess.Popen(command)
+    proc.wait()
+    return redirect(url_for('main'))
+
+
+@app.route('/remove_rule/<interface>', methods=['POST'])
+def remove_rule(interface):
+    # remove old setup
+    command = 'tc qdisc del dev %s root netem' % interface
+    command = command.split(' ')
+    proc = subprocess.Popen(command)
+    proc.wait()
+    return redirect(url_for('main'))
+
+def get_active_rules():
+    proc = subprocess.Popen(['tc', 'qdisc'], stdout=subprocess.PIPE)
+    output = proc.communicate()[0].decode()
+    lines = output.split('\n')[:-1]
+    rules = []
+    for line in lines:
+        arguments = line.split(' ')
+        rules.append(parse_rule(arguments))
+    return rules
+
+
+def parse_rule(splitted_rule):
+    rule = {'name':      None,
+            'delay':     None,
+            'loss':      None,
+            'duplicate': None,
+            'reorder':   None,
+            'corrupt':   None}
+    i = 0
+    for argument in splitted_rule:
+        if argument == 'dev':
+            rule['name'] = splitted_rule[i+1]
+        elif argument == 'delay':
+            rule['delay'] = splitted_rule[i + 1]
+        elif argument == 'loss':
+            rule['loss'] = splitted_rule[i + 1]
+        elif argument == 'duplicate':
+            rule['duplicate'] = splitted_rule[i + 1]
+        elif argument == 'reorder':
+            rule['reorder'] = splitted_rule[i + 1]
+        elif argument == 'corrupt':
+            rule['corrupt'] = splitted_rule[i + 1]
+        i += 1
+    return rule
+
+
+if __name__ == "__main__":
+    #if os.geteuid() != 0:
+    #    exit("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
+    app.debug = True
+    app.run()

+ 55 - 0
templates/main.html

@@ -0,0 +1,55 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>DemoSetup</title>
+</head>
+<body>
+<h4>Available Interfaces</h4>
+<ul>
+    {% for rule in rules %}
+        <li>
+            {{ rule['name'] }}
+            <form method="POST" action="{{ url_for('new_rule', interface=rule['name']) }}">
+                <table border="1">
+                    <tr>
+                        <th>Name</th>
+                        <th>Current Value</th>
+                        <th>New Value</th>
+                    </tr>
+                    <tr>
+                        <td>Delay:</td>
+                        <td>{{ rule['delay'] }}</td>
+                        <td><input type="text" name="Delay" size="5"> in ms</td>
+                    </tr>
+                    <tr>
+                        <td>Loss:</td>
+                        <td>{{ rule['loss'] }}</td>
+                        <td><input type="text" name="Loss" size="5"> in %</td>
+                    </tr>
+                    <tr>
+                        <td>Duplicate:</td>
+                        <td>{{ rule['duplicate'] }}</td>
+                        <td><input type="text" name="Duplicate" size="5"> in %</td>
+                    </tr>
+                    <tr>
+                        <td>Reorder:</td>
+                        <td>{{ rule['reorder'] }}</td>
+                        <td><input type="text" name="Reorder" size="5"> in %</td>
+                    </tr>
+                    <tr>
+                        <td>Corrupt</td>
+                        <td>{{ rule['corrupt'] }}</td>
+                        <td><input type="text" name="Corrupt" size="5"> in %</td>
+                    </tr>
+                </table>
+                <input type="submit" value="Apply Rules">
+            </form>
+            <form method="POST" action="{{ url_for('remove_rule', interface= rule['name']) }}">
+                <input type="submit" value="Remove Rules">
+            </form>
+        </li>
+    {% endfor %}
+</ul>
+</body>
+</html>