From df5a86a9734879dee4eebb6d5b372a407eb85682 Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Fri, 23 Jan 2015 00:22:17 -0800 Subject: [PATCH] network_ports validation, wip --- .../core/xoslib/static/js/xoslib/xos-util.js | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/planetstack/core/xoslib/static/js/xoslib/xos-util.js b/planetstack/core/xoslib/static/js/xoslib/xos-util.js index 1c42e26..2fa38a6 100644 --- a/planetstack/core/xoslib/static/js/xoslib/xos-util.js +++ b/planetstack/core/xoslib/static/js/xoslib/xos-util.js @@ -136,3 +136,62 @@ function make_same_width(containerSelector, itemSelector) { console.log(maxWidth); $(containerSelector).find(itemSelector).each( function(index) { $(this).width(maxWidth); }); } + +function parse_portlist(ports) { + /* Support a list of ports in the format "protocol:port, protocol:port, ..." + examples: + tcp 123 + tcp 123:133 + tcp 123, tcp 124, tcp 125, udp 201, udp 202 + + User can put either a "/" or a " " between protocol and ports + Port ranges can be specified with "-" or ":" + + This is a straightforward port of the code in core/models/network.py + */ + + var nats = []; + if (ports) { + parts = ports.split(",") + $.each(parts, function(index, part) { + part = $.trim(part); + if (part.indexOf("/")>=0) { + parts2 = part.split("/",2); + protocol=parts2[0]; + ports=parts2[1]; + } else if (part.indexOf(" ")>=0) { + parts2 = part.split(" +",2); + protocol=parts2[0]; + ports=parts2[1]; + } else { + throw 'malformed port specifier ' + part + ', format example: "tcp 123, tcp 201:206, udp 333"'; + } + + protocol = $.trim(protocol); + ports = $.trim(ports); + + console.log(ports); + + if (protocol!="tcp" && protocol!="udp") { + throw 'unknown protocol ' + protocol; + } + + if (ports.indexOf("-")>=0) { + parts2 = ports.split("-"); + first = parseInt($.trim(parts2[0])); + last = parseInt($.trim(parts2[1])); + portStr = first + ":" + last; + } else if (ports.indexOf(":")>=0) { + parts2 = ports.split(":"); + first = parseInt($.trim(parts2[0])); + last = parseInt($.trim(parts2[1])); + portStr = first + ":" + last; + } else { + portStr = parseInt(ports).toString(); + } + + nats.push( {l4_protocol: protocol, l4_port: portStr} ); + }); /* end $.each(ports) */ + } + return nats +} -- 2.43.0