Bläddra i källkod

mockup and network ports preparations to 20.05.02

Roberto Berto 5 år sedan
förälder
incheckning
cf09a97fcb

+ 2 - 3
vycontrol/config/views.py

@@ -20,10 +20,9 @@ from django.template.defaultfilters import register
 from perms import is_authenticated
 import perms
 
+from filters.vycontrol_filters import get_item
+
 
-@register.filter
-def get_item(dictionary, key):
-    return dictionary.get(key)
 
 @is_authenticated
 def index(request):

+ 19 - 0
vycontrol/filters/vycontrol_filters.py

@@ -10,3 +10,22 @@ def routepack(value):
 def routeunpack(value): 
     """UnpPack a route into a string"""
     return str(value).replace("!","/")
+
+@register.filter
+def get_item(dictionary, key):
+    return dictionary.get(key)
+
+@register.filter
+def get_item_port(dictionary, key):
+    d = dictionary.get(key)    
+    # n = port
+    # p = protocol
+    # d = description
+    return d['n']
+
+
+@register.filter
+def get_item_network(dictionary, key):
+    d = dictionary.get(key)    
+    return d['network']
+

+ 533 - 28
vycontrol/firewall/templates/firewall/addrule.html

@@ -5,14 +5,40 @@
 {% block username %}{{ username }}{% endblock %}
 
 {% block debug %}
-{{ firewall }}
+
 {{ firewall_name }}
+
+{{ services }}
+
+{{ services_common }}
+
+{{ firewall_networkgroup }}
+
+{{ firewall_addressgroup }}
+
+
 {% endblock %}
 
 {% block content %}
 
+{% comment %}
+<script type="text/javascript">
+    var firewall_networkgroup_data = '{{firewall_networkgroup_js|safe}}';
+    console.log(firewall_networkgroup_data);
+
+    var firewall_addressgroup_data = '{{firewall_addressgroup_js|safe}}';
+    console.log(firewall_addressgroup_data);    
+
+    var netservices_js = '{{netservices_js|safe}}';
+    console.log(netservices_js);    
+
+</script>
+{% endcomment %}
+
 
 <p class="submenu1">
+    <a href="{% url 'firewall:firewall-list' %}">Firewall List</a> | 
+
     <a href="{% url 'firewall:firewall-create' %}">Create new firewall</a> | 
     <a href="{% url 'firewall:firewall-addressgroup-list' %}">Address Group</a> | 
     <a href="{% url 'firewall:firewall-networkgroup-list' %}">Network Group</a>
@@ -26,40 +52,519 @@
 <form action="{% url 'firewall:addrule' firewall_name %}" method="post">
     {% csrf_token %}
     
-    <p>
-        <label for="alias">rule number</label><br>
-        <input type="text" name="rulenumber" id="rulenumber" value="{{ rulenumber }}" size="5">
-    </p>
-    
-    <p>
-        <label for="hostname">action</label><br>
-        <input type="radio" name="action" id="action" value="accept"> accept
-        <input type="radio" name="action" id="action" value="drop"> drop
-        <input type="radio" name="action" id="action" value="reject"> reject        
-    </p>
-
-    <p>
-        <label for="hostname">protocol</label><br>
-        <input type="radio" name="protocol" id="protocol" value="tcp"> tcp
-        <input type="radio" name="protocol" id="protocol" value="udp"> udp
-    </p>   
-    
-    <p>
-        <label for="alias">destination port</label><br>
-        <input type="text" name="destinationport" id="destinationport" value="{{ destinationport }}" size="5">
-    </p>
+
+    <h3>Rule Config</h3>
+
+    <div class="container">
+        <div class="row">
+            <div class="col">
+                <p>
+                    <label for="alias">rule number</label><br>
+                    <input type="text" name="rulenumber" id="rulenumber" value="{{ rulenumber }}" size="5">
+                </p>
+            </div>
+
+            <div class="col">
+                <p>
+                    <label for="status">status</label><br>
+                    <input type="radio" name="status" id="status" value="enabled"> enabled
+                    <input type="radio" name="status" id="status" value="disabled"> disabled
+                </p>
+            </div>         
+
+            <div class="col">
+                <p>
+                    <label for="action">action</label><br>
+                    <input type="radio" name="action" id="action" value="accept"> accept
+                    <input type="radio" name="action" id="action" value="drop"> drop
+                    <input type="radio" name="action" id="action" value="reject"> reject        
+                </p>
+            </div>
+        </div>
+
+        <div class="row">
+            <div class="col">
+                <p>
+                    <label for="alias">description</label><br>
+                    <input type="text" name="description" id="description" value="{{ description }}" size="60">
+                </p>
+            </div>
+
+
+        </div>       
+    </div>
+
+
+    <h3><input type="checkbox" id="criteria_protocol" value="1"> Matching criteria - protocol</h3>
+    <div class="container" id="criteria_protocol_block" style="display: none">
+
+        <div class="row">
+
+            <div class="col">
+                <p>
+                    <input type="radio" class="protocol_criteria" name="protocol_criteria" id="protocol_criteria_all" value="1"> all protocols<br>
+                    <input type="radio" class="protocol_criteria" name="protocol_criteria" id="protocol_criteria_tcp" value="1"> tcp<br>
+                    <input type="radio" class="protocol_criteria" name="protocol_criteria" id="protocol_criteria_udp" value="1"> udp<br>
+                    <input type="radio" class="protocol_criteria" name="protocol_criteria" id="protocol_criteria_tcp_udp" value="1"> tcp and udp<br>
+                    <input type="radio" class="protocol_criteria" name="protocol_criteria" id="protocol_criteria_icmp" value="1"> icmp<br>
+                    <input type="radio" class="protocol_criteria" name="protocol_criteria" id="protocol_criteria_other" value="1"> other  
+                </p>
+
+
+            </div>
+
+            <div class="col">
+                
+
+                <p id="protocol_custom_block" style="display: none">
+                    <label for="protocol_custom">add other protocol</label><br>
+                    <input type="text" name="protocol_custom" id="protocol_custom" value="{{ protocol_custom }}" size="5"> 
+                    <input type="button" name="destinationport_custom_add" value="add" />
+                </p>                     
+                    
+
+                <p>
+                    <input type="checkbox" name="protocols_negate" id="protocols_negate" value="1">
+                    <label for="protocols_negate">negate </label>
+                </p>
+            </div>
+        </div>
+    </div>
+
+    <h3><input type="checkbox" id="criteria_port" value="1"> Matching criteria - port</h3>
+    <div class="container" id="criteria_port_block" style="display: none">
+        <div class="row">  
+            <div class="col">
+
+                
+                <p>
+                    <label for="destinationport">destination ports filtered (click to remove)</label><br>
+                    <select name="destinationport" size="10" style="width: 200px;">
+                    </select>
+                </p>
+
+                <p>
+                    <label for="destinationport_common">common destinations ports to add</label><br>
+                    <select name="destinationport_common" size="1">
+                        {% for p in services_common %}
+                        <option>{{ p }} - {{ services|get_item_port:p }} </option>
+                        {% endfor %}
+                    </select>
+
+                    <input type="checkbox" name="destinationport_common_negate" id="destinationport_common_negate" value="1">
+                    <label for="destinationport_common_negate">negate </label>
+                    <input type="button" name="destinationport_common_add" value="add" />
+                </p>
+
+
+                <p>
+                    <label for="destinationport_custom">add custom destination ports (use single 100 or range 100-200)</label><br>
+                    <input type="text" name="destinationport_custom" id="destinationport_custom" value="{{ destinationport_custom }}" size="14"> 
+                    <input type="checkbox" name="destinationport_custom_negate" id="destinationport_custom_negate" value="1"> 
+                    <label for="destinationport_custom_negate">negate </label>
+                    <input type="button" name="destinationport_custom_add" value="add" />
+                </p>                     
+                    
+
+            </div>
+          
+            <div class="col">
+                <p>
+                    <label for="sourceport">common source filtered (click to remove)</label><br>
+                    <select name="sourceport" size="10" style="width: 200px;">
+                    </select>
+                </p>
+
+                <p>
+                    <label for="sourceport_common">common source ports to add</label><br>
+                    <select name="sourceport_common" size="1">
+                        {% for p in services_common %}
+                        <option>{{ p }} - {{ services|get_item_port:p }} </option>
+                        {% endfor %}
+                    </select>
+
+                    <input type="checkbox" name="sourceport_common_negate" id="sourceport_common_negate" value="1">
+                    <label for="sourceport_common_negate">negate </label>
+                    <input type="button" name="sourceport_common_add" value="add" />
+                </p>   
+
+                <p>
+                    <label for="sourceport_custom">add custom destination ports (use single 100 or range 100-200)</label><br>
+                    <input type="text" name="sourceport_custom" id="sourceport_custom" value="{{ destinationport_custom }}" size="14"> 
+                    <input type="checkbox" name="sourceport_custom_negate" id="sourceport_custom_negate" value="1"> 
+                    <label for="sourceport_custom_negate">negate </label>
+                    <input type="button" name="sourceport_custom_add" value="add" />
+                </p>       
+               
+                
+            </div>
+        </div>
+
+    </div>
+
+
+    <!--
+    <h3>Matching criteria</h3>
+
+    <div class="container">
+        <div class="row">
+            <div class="col">
+                <p>
+                    <input type="radio" name="matching" id="sdaddress" value="sdaddress"> source address / destination address<br>
+                    <input type="radio" name="matching" id="smac" value="smac"> source mac-address<br>
+                    <input type="radio" name="matching" id="sdaddressgroup" value="sdaddressgroup"> source group address-group  / destination group address-group<br>
+                    <input type="radio" name="matching" id="adnetworkgroup" value="adnetworkgroup"> source group network-group / destination group network-group<br>
+                    <input type="radio" name="matching" id="sdportgroup" value="sdportgroup"> source group port-group / destination group port-group        
+                </p>
+
+            </div>
+        </div>
+    </div>
+    -->
+
+    <h3><input type="checkbox" id="criteria_address" value="1"> Matching criteria - address</h3>
+    <div class="container" id="criteria_address_block" style="display: none">
+
+
+        <div class="row">
+            <div class="col">    
+                <h4>Valid address examples</h4>
+            </div>
+        </div>
+ 
+        <div class="row">
+            <div class="col">
+                <dl>
+                    <dt>address</dt>
+                    <dd>1.1.1.1</dd>
+                </dl>
+            </div>
+
+            <div class="col">
+                <dl>
+                    <dt>address range</dt>
+                    <dd>1.1.1.1-1.1.1.10</dd>
+                </dl>
+            </div>
+            
+            <div class="col">
+                <dl>
+                    <dt>CIDR</dt>
+                    <dd>1.1.1.0/24</dd>   
+                </dl>
+            </div>
+            
+        </div>
     
-    <p>
-        <label for="alias">source port</label><br>
-        <input type="text" name="sourceport" id="sourceport" value="{{ sourceport }}" size="5">
-    </p>    
+        <div class="row">
+            <div class="col">
+                <p>
+                    <label for="sdaddress_source">source address</label><br>
+                    <input type="text" name="sdaddress_source" id="sdaddress_source" value="" size="30">
+                </p>
+
+                <p>
+                    <input type="checkbox" name="sdaddress_source_negate" id="sdaddress_source_negate" value="1"> <label for="sdaddress_source_negate">negate source address</label>
+                </p>         
+
+            </div>
+
+            <div class="col">
+                <p>
+                    <label for="sdaddress_destination">destination address</label><br>
+                    <input type="text" name="sdaddress_destination" id="sdaddress_destination" value="" size="30">
+                </p>
+
+                <p>
+                    <input type="checkbox" name="sdaddress_destination_negate" id="sdaddress_destination_negate" value="1"> <label for="sdaddress_destination_negate">negate destination address</label>
+                </p>         
+
+          </div>
+        </div>
+
+    </div> 
+  
     
+    <h3><input type="checkbox" id="criteria_addressgroup" value="1"> Matching criteria - address-group</h3>
+    <div class="container" id="criteria_addressgroup_block" style="display: none">    
+
+        <div class="row">
+            <div class="col">
+                <p>
+                    <label for="sdaddressgroup_source">source address-group</label><br>
+                    <select name="sdaddressgroup_source" size="10">
+                        {% for f in firewall_addressgroup %}
+                        <option>{{ f }}</option>
+                        {% endfor %}      
+                    </select>
+                </p>
+
+            </div>
+
+            <div class="col">
+                <p>
+                    <label for="sdaddressgroup_destination">destination address-group</label><br>
+                    <select name="sdaddressgroup_destination" size="10">
+                        {% for f in firewall_addressgroup %}
+                        <option>{{ f }}</option>
+                        {% endfor %}        
+                    </select>
+                </p>
+
+
+          </div>
+        </div>
+
+    </div>
+
+
+    <h3><input type="checkbox" id="criteria_networkgroup" value="1"> Matching criteria - network-group</h3>
+    <div class="container" id="criteria_networkgroup_block" style="display: none">
+        <div class="row">
+            <div class="col">
+                <p>
+                    <label for="sdnetworkgroup_source">source network-group</label><br>
+                    <select name="sdnetworkgroup_source" size="10">
+                        {% for f in firewall_networkgroup %}
+                        <option>{{ f }}</option>
+                        {% endfor %}
+                    </select>
+                </p>         
+                
+            </div>
+
+            <div class="col">
+                <p>
+                    <label for="sdnetworkgroup_destination">destination network-group</label><br>
+                    <select name="sdnetworkgroup_destination" size="10">
+                        {% for f in firewall_networkgroup %}
+                        <option>{{ f }}</option>
+                        {% endfor %}
+                    </select>
+                </p>
+
+
+          </div>
+        </div>
+
+    </div>    
+
+    <h3><input type="checkbox" id="criteria_portgroup" value="1"> Matching criteria - port group</h3>
+    <div class="container" id="criteria_portgroup_block" style="display: none">
+        <div class="row">
+            <div class="col">
+                <p>
+                    <label for="sdportgroup_source">source port group</label><br>
+                    <select name="sdportgroup_source" size="10">
+                        <option>http - 80</option>
+                        <option>https - 443</option>
+                        <option>smtp - 25</option>
+                        <option>ssh - 22</option>
+                    </select>
+                </p>
+
+            </div>
+
+            <div class="col">
+                <p>
+                    <label for="sdportgroup_destination">destination port group</label><br>
+                    <select name="sdportgroup_destination" size="10">
+                        <option>http - 80</option>
+                        <option>https - 443</option>
+                        <option>smtp - 25</option>
+                        <option>ssh - 22</option>
+                    </select>
+                </p>
+
+
+          </div>
+        </div>
+
+    </div>    
+
+
+    <h3><input type="checkbox" id="criteria_sourcemac" value="1"> Matching criteria - source mac address</h3>  
+    <div class="container" id="criteria_sourcemac_block" style="display: none">
+        <div class="row">
+            <div class="col">
+                <p>
+                    <label for="smac_source">mac address</label><br>
+                    <input type="text" name="smac_source" id="smac_source" value="" size="30">
+                </p>
+
+                <p>
+                    <input type="checkbox" name="smac_source_negate" id="smac_source_negate" value="1"> <label for="smac_source_negate">negate source mac address</label>
+                </p>                         
+            </div>
+
+        </div>
+
+    </div>
+
+    <h3><input type="checkbox" id="criteria_tcpflags" value="1"> Matching criteria - TCP Flags</h3>  
+    <div class="container" id="criteria_tcpflags_block" style="display: none">
+        <div class="row">
+            <div class="col">
+                <table width="100%">
+                    <tr>
+                        <th>Allow flag</th>
+                        <th>Negate flag</th>
+                    </tr>
+                    <tr>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="SYN"> SYN</td>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="!SYN"> !SYN</td>
+                    </tr>
+                    <tr>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="ACK"> ACK</td>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="!ACK"> !ACK</td>
+                    </tr>
+                    <tr>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="FIN"> FIN</td>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="!FIN"> !FIN</td>
+                    </tr>
+                    <tr>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="RST"> RST</td>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="!RST"> !RST</td>
+                    </tr>
+                    <tr>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="URG"> URG</td>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="!URG"> !URG</td>
+                    </tr>
+                    <tr>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="PSH"> PSH</td>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="!PSH"> !PSH</td>
+                    </tr>
+                    <tr>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="ALL"> ALL</td>
+                        <td><input type="checkbox" name="tcpflags" id="tcpflags" value="!ALL"> !ALL</td>
+                    </tr>                        
+                </table>
+            </div>
+
+        </div>
+    </div>
+
+
+    <h3><input type="checkbox" id="criteria_packetstate" value="1"> Matching criteria - Packet State</h3>  
+    <div class="container" id="criteria_packetstate_block" style="display: none">
+        <div class="row">
+            <div class="col">
+                <p>
+                    <input type="checkbox" name="state" id="state_established" value="established"> established
+                    <input type="checkbox" name="state" id="state_invalid" value="invalid"> invalid
+                    <input type="checkbox" name="state" id="state_new" value="new"> new
+                    <input type="checkbox" name="state" id="state_releated" value="releated"> releated        
+                </p>
+            </div>
+
+        </div>            
+    </div>
     
     <input type="submit" value="Add Rule">
     </form>
 
     
-    
+<script>
+    $(document).ready(function () {                            
+        $(".protocol_criteria").change(function () {
+            if ($("#protocol_criteria_other").is(":checked")) {
+                $('#protocol_custom_block').show();
+            }
+            else if ($("#addresstype_range").not(":checked")) {
+                $('#protocol_custom_block').hide();
+            }
+        });
+
+        $("#criteria_protocol").change(function () {
+            if ($("#criteria_protocol").is(":checked")) {
+                $('#criteria_protocol_block').show();
+            }
+            else if ($("#criteria_protocol").not(":checked")) {
+                $('#criteria_protocol_block').hide();
+            }
+        });
+
+        $("#criteria_port").change(function () {
+            if ($("#criteria_port").is(":checked")) {
+                $('#criteria_port_block').show();
+            }
+            else if ($("#criteria_port").not(":checked")) {
+                $('#criteria_port_block').hide();
+            }
+        });  
+
+        $("#criteria_address").change(function () {
+            if ($("#criteria_address").is(":checked")) {
+                $('#criteria_address_block').show();
+            }
+            else if ($("#criteria_address").not(":checked")) {
+                $('#criteria_address_block').hide();
+            }
+        });    
+
+        $("#criteria_addressgroup").change(function () {
+            if ($("#criteria_addressgroup").is(":checked")) {
+                $('#criteria_addressgroup_block').show();
+            }
+            else if ($("#criteria_addressgroup").not(":checked")) {
+                $('#criteria_addressgroup_block').hide();
+            }
+        });    
+
+        $("#criteria_networkgroup").change(function () {
+            if ($("#criteria_networkgroup").is(":checked")) {
+                $('#criteria_networkgroup_block').show();
+            }
+            else if ($("#criteria_networkgroup").not(":checked")) {
+                $('#criteria_networkgroup_block').hide();
+            }
+        });   
+
+        $("#criteria_portgroup").change(function () {
+            if ($("#criteria_portgroup").is(":checked")) {
+                $('#criteria_portgroup_block').show();
+            }
+            else if ($("#criteria_portgroup").not(":checked")) {
+                $('#criteria_portgroup_block').hide();
+            }
+        });                                        
+
+
+        $("#criteria_sourcemac").change(function () {
+            if ($("#criteria_sourcemac").is(":checked")) {
+                $('#criteria_sourcemac_block').show();
+            }
+            else if ($("#criteria_sourcemac").not(":checked")) {
+                $('#criteria_sourcemac_block').hide();
+            }
+        });     
+
+        $("#criteria_tcpflags").change(function () {
+            if ($("#criteria_tcpflags").is(":checked")) {
+                $('#criteria_tcpflags_block').show();
+            }
+            else if ($("#criteria_tcpflags").not(":checked")) {
+                $('#criteria_tcpflags_block').hide();
+            }
+        });
+
+
+        $("#criteria_packetstate").change(function () {
+            if ($("#criteria_packetstate").is(":checked")) {
+                $('#criteria_packetstate_block').show();
+            }
+            else if ($("#criteria_packetstate").not(":checked")) {
+                $('#criteria_packetstate_block').hide();
+            }
+        });                                                        
+
+           
+                     
+        
+
+    });
+</script>
+          
 
 
 {% endblock %}

+ 11 - 0
vycontrol/firewall/templates/firewall/editrule.html

@@ -14,6 +14,17 @@
 {% block content %}
 
 
+<p class="submenu1">
+    <a href="{% url 'firewall:firewall-list' %}">Firewall List</a> | 
+
+    <a href="{% url 'firewall:firewall-create' %}">Create new firewall</a> | 
+    <a href="{% url 'firewall:firewall-addressgroup-list' %}">Address Group</a> | 
+    <a href="{% url 'firewall:firewall-networkgroup-list' %}">Network Group</a>
+</p>
+<p class="submenu2">
+    
+</p>
+
 
 
 {% if firewall %}

+ 7 - 3
vycontrol/firewall/templates/firewall/show.html

@@ -42,18 +42,22 @@
 
 {% if firewall.rule %}
     <table border="1" width="100%">
-    <tr><th>rule #</th><th>description</th><th>protocol</th><th>destination port</th><th>source port</th><th>firewall action</th></tr>
+    <tr><th>rule #</th><th>description</th><th>protocol</th><th>destination port</th><th>source port</th><th>firewall action</th><th>admin action</th></tr>
 
     {% for key, value in firewall.items %}
         
         {% for ifkey, ifvalue in value.items %}
             <tr>
-            <td><a href="{% url 'firewall:editrule' firewall_name ifkey %}">{{ ifkey }}</a> <a href="{% url 'firewall:firewall-removerule' firewall_name ifkey %}">remove rule</a></td>
+            <td><a href="{% url 'firewall:editrule' firewall_name ifkey %}">{{ ifkey }}</a></td>
             <td>{{ ifvalue.description }}</td>
             <td>{{ ifvalue.protocol }}</td>
             <td>{{ ifvalue.destination.port }}</td>
             <td>{{ ifvalue.source.port }}</td>
-            <td>{{ ifvalue.action }}</td>                        
+            <td>{{ ifvalue.action }}</td>       
+            <td>
+                <a href="{% url 'firewall:editrule' firewall_name ifkey %}">edit</a> | 
+                <a href="{% url 'firewall:firewall-removerule' firewall_name ifkey %}">remove</a>
+            </td>
             </tr>
         {% endfor %}
         

+ 25 - 4
vycontrol/firewall/views.py

@@ -5,13 +5,16 @@ from django.shortcuts import redirect
 from django.conf import settings
 from django.urls import reverse
 
-
 import vyos
 from performance import timer
 from perms import is_authenticated
 import perms
+import network
+import json
 
-
+from filters.vycontrol_filters import get_item
+from filters.vycontrol_filters import get_item_port
+from filters.vycontrol_filters import get_item_network
 
 @is_authenticated
 def index(request):
@@ -86,9 +89,16 @@ def addrule(request, firewall_name):
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
     is_superuser = perms.get_is_superuser(request.user)
-
     firewall = vyos.get_firewall(hostname_default, firewall_name)
-    
+    firewall_networkgroup = vyos.get_firewall_networkgroup(hostname_default)
+    firewall_addressgroup = vyos.get_firewall_addressgroup(hostname_default)
+    firewall_networkgroup_js = json.dumps(firewall_networkgroup['network-group'])
+    firewall_addressgroup_js = json.dumps(firewall_addressgroup['address-group'])
+
+    netservices = network.get_services()
+    netservices_js = json.dumps(netservices)
+
+
     changed = False
     if 'action' in request.POST:
         cmd = {"op": "set", "path": ["firewall", "name", firewall_name, "rule", request.POST['rulenumber'], "action", request.POST['action']]}
@@ -118,6 +128,10 @@ def addrule(request, firewall_name):
         return redirect('firewall:show', firewall_name)
         
 
+
+
+
+
     template = loader.get_template('firewall/addrule.html')
     context = { 
         #'interfaces': interfaces,
@@ -127,6 +141,13 @@ def addrule(request, firewall_name):
         'firewall_name': firewall_name,
         'username': request.user,
         'is_superuser' : is_superuser,
+        'services' : netservices['services'],
+        'services_common' : netservices['common'],
+        'firewall_networkgroup': firewall_networkgroup['network-group'],
+        'firewall_addressgroup': firewall_addressgroup['address-group'],
+        'firewall_networkgroup_js': firewall_networkgroup_js,
+        'firewall_addressgroup_js': firewall_addressgroup_js,
+        'netservices_js' : netservices_js,
     }  
     return HttpResponse(template.render(context, request))
 

+ 127 - 0
vycontrol/network.py

@@ -0,0 +1,127 @@
+import sys
+import socket
+import pprint
+import re
+
+def get_protocols():
+    file = '/etc/protocols'
+    
+    protocols = {}
+    # Iterate through the file, one line at a time
+    for line in open(file):
+        line = line.replace("'","")
+        line = line.replace('"',"")
+        if line[0:1] != '#' and not line.isspace():
+            linesplited = re.split(r'([\t\s]+)', line, maxsplit=2)
+            #pprint.pprint(linesplited)
+            protocol_name = linesplited[0].strip()
+            protocol_id = linesplited[2].strip()
+            protocols[protocol_id] = protocol_name
+            #print(linesplited[0], linesplited[2])
+
+    #pprint.pprint(protocols)
+
+    inv_map = {v: k for k, v in protocols.items()}
+
+    common = ['tcp', 'udp', 'icmp', 'gre', 'ospf', 'igmp', 'egp', 'igp', 'ipv6', 'ip', 'isis']
+
+    return {'all_by_id': protocols, 'all_by_name': inv_map, 'common': common}
+
+def get_services():
+    # set the file name depending on the operating system
+    if sys.platform == 'win32':
+        file = r'C:\WINDOWS\system32\drivers\etc\services'
+    else:
+        file = '/etc/services'
+
+    def is_number(s):
+        try:
+            complex(s) # for int, long, float and complex
+        except ValueError:
+            return False
+
+        return True
+
+
+    protocols = []
+    service_name = {}
+    portprotocol = {}
+
+    # Iterate through the file, one line at a time
+    for line in open(file):
+        line = line.replace("'","")
+        line = line.replace('"',"")
+
+
+
+        if line[0:1] != '#' and not line.isspace():
+            #pprint.pprint(line.strip())
+
+
+            linesplited = re.split(r'([\t\s]+)', line, maxsplit=2)
+            linesplited_clean = {}
+            #pprint.pprint(linesplited)
+            x = 0
+            service_name_actual = None
+            for line_clean in linesplited:
+                line_clean = re.sub('#', '', line_clean.strip())
+                line_clean_strip = re.sub(r'\s+', '', line_clean)
+
+                if x == 0 and line_clean_strip == "":
+                    linesplited_clean['service_name'] = None
+                elif x == 0 and is_number(line_clean_strip) == False: 
+                    linesplited_clean['service_name'] = line_clean_strip
+                    service_name_actual = line_clean_strip
+                    service_name[line_clean_strip] = {}
+
+                    #print('isnumberfalse', is_number(line_clean_strip), line_clean_strip)
+                elif x == 2 and len(line_clean_strip) > 0:
+                    linesplited_clean['port_protocol'] = line_clean_strip
+                    
+                    portprotocol = line_clean_strip.split('/')
+                    linesplited_clean['port'] = portprotocol[0]
+                    linesplited_clean['protocol'] = portprotocol[1]
+
+                    if str(portprotocol[1]) not in protocols:
+                        protocols.append(str(portprotocol[1]))
+
+                    if service_name_actual != None:
+                        service_name[service_name_actual]['p'] = str(portprotocol[1])
+                        service_name[service_name_actual]['n'] = str(portprotocol[0])
+
+                elif x == 4 and len(line_clean_strip) > 0:
+                    linesplited_clean['description'] = line_clean_strip
+                    if service_name_actual != None:
+                        service_name[service_name_actual]['d'] = line_clean_strip
+
+                #re.sub('#', '', line_clean_strip)
+
+                x = x + 1
+            #pprint.pprint(linesplited_clean)
+            #print("#####################")
+
+    common = {
+        'http' : 'http',
+        'https' : 'https',
+        'ftp' : 'ftp',
+        'ftp-data' : 'ftp-data',
+        'ssh' : 'ssh',
+        'telnet' : 'telnet',
+        'smtp' : 'smtp', 
+        'nicname' : 'whois',
+        'domain' : 'dns',
+        'pop3' : 'pop3',
+        'sftp' : 'sftp',
+        'ntp' : 'ntp',
+        'snmp' : 'snmp',
+        'snmptrap' : 'snmptrap',
+        'bgp' : 'bgp',
+        'imaps' : 'imaps',
+        'pop3s' : 'pop3s',
+        'ftps-data' : 'ftps-data',
+        'ftps' : 'ftps',
+        'pop3s' : 'pop3s',
+    }
+
+    return {'protocols': protocols, 'services': service_name, 'common': common}
+

+ 10 - 0
vycontrol/s/main.css

@@ -89,6 +89,13 @@ form.instancedefault {
   font-size: 14px;
 }
 
+#central h4 {
+  margin-top: 10px;
+  font-size: 13px;
+  font-weight: bold;
+}
+
+
 #central {
   font-size: 12px;
 }
@@ -104,6 +111,8 @@ form.instancedefault {
 input[type=submit] {
   background-color: #EACD65;
   border: 0;
+  margin-bottom: 20px;
+  margin-top: 20px;
 }
 
 .separe-form {
@@ -145,3 +154,4 @@ input[type=submit] {
   margin-right: 10px;
   color:#af1d1d ;
 }
+

+ 1 - 1
vycontrol/vycenter/templates/base.html

@@ -8,7 +8,7 @@
 
     <!-- Bootstrap CSS -->
     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
-    <link rel="stylesheet" href="{% static "main.css" %}?17">
+    <link rel="stylesheet" href="{% static "main.css" %}?19">
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
 
     <title>{% block header_title %}{% endblock %} - VyControl</title>