From de5bed964925870ad89d9970d17576a7a9cbd9b2 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 27 Mar 2015 21:50:03 -0700 Subject: [PATCH] GUAC-1138: Add support for IPv4 and IPv6 network tokens. --- .../main/webapp/app/list/types/FilterToken.js | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/guacamole/src/main/webapp/app/list/types/FilterToken.js b/guacamole/src/main/webapp/app/list/types/FilterToken.js index d89839f5b..915f072ea 100644 --- a/guacamole/src/main/webapp/app/list/types/FilterToken.js +++ b/guacamole/src/main/webapp/app/list/types/FilterToken.js @@ -23,8 +23,12 @@ /** * A service for defining the FilterToken class. */ -angular.module('list').factory('FilterToken', [ - function defineFilterToken() { +angular.module('list').factory('FilterToken', ['$injector', + function defineFilterToken($injector) { + + // Required types + var IPv4Network = $injector.get('IPv4Network'); + var IPv6Network = $injector.get('IPv6Network'); /** * An arbitrary token having an associated type and value. @@ -76,6 +80,50 @@ angular.module('list').factory('FilterToken', [ */ FilterToken.Types = { + /** + * An IPv4 address or subnet. The value of an IPV4_NETWORK token is an + * IPv4Network. + */ + IPV4_NETWORK: function parseIPv4(str) { + + var pattern = /^\S+/; + + // Read first word via regex + var matches = pattern.exec(str); + if (!matches) + return null; + + // Validate and parse as IPv4 address + var network = IPv4Network.parse(matches[0]); + if (!network) + return null; + + return new FilterToken(matches[0], 'IPV4_NETWORK', network); + + }, + + /** + * An IPv6 address or subnet. The value of an IPV6_NETWORK token is an + * IPv6Network. + */ + IPV6_NETWORK: function parseIPv6(str) { + + var pattern = /^\S+/; + + // Read first word via regex + var matches = pattern.exec(str); + if (!matches) + return null; + + // Validate and parse as IPv6 address + var network = IPv6Network.parse(matches[0]); + if (!network) + return null; + + return new FilterToken(matches[0], 'IPV6_NETWORK', network); + + }, + /** * A string literal, which may be quoted. The value of a LITERAL token * is a String.