javascript / jquery / json questions

Status
Not open for further replies.

pazuzu

Ars Tribunus Militum
2,702
I've been playing with open_nut https://github.com/alphasights/open_nut since coming across it yesterday. Since I've been long out of touch with js/jquery/json, here're some problems that have me scratching my head:

In services.js, I've set up a few test cases.

1) The following URL returns a JSON response in the following format:

{"ip": "my ipv4 or ipv6 WAN addr"}

Code:
Service({
  name: 'myip nordhoff com',
  url: 'http://myip.mattnordhoff.com/json',
  status: function(response) {
    var status = response["ip"];
     return (status.match(/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/).length > 0 || status.match(/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$/).length > 0);
  }
})

I want the site icon to turn green as long as the "ip" JSON field returns a IPv4 or IPv6 addr (my WAN IP). The regex for both are correct. I thought I got it working perfectly. The problem is, after a reboot, which took down my IPv6 tunnel to Hurricane Electric, I find the ipv6regex "OR" ipv4regex statement doesn't appear to be working--the return status is "unknown/pending/invalid js" if I use "return (status.match(/ipv6regex || ipv4regex /);". When I have an IPv4 addr, it does turn green with "return (status.match(/ipv4regex/);". Same with my IPv6 tunnel up and I make it "return (status.match(/ipv6regex/);".


2) The following URL returns a JSON response in the following format:

{"address":"my ipv4 or ipv6 WAN addr","proto":"ipv6 or ipv4"}

Code:
Service({
  name: 'ipv6-test com',
  url: 'http://v4v6.ipv6-test.com/api/myip.php\?json',
  status: function(response) {
    var status = response["proto"];
    return  (status == "ipv4");
  }
})

In this case, the site status is perpetually returning red/down, as if it doesn't see the match, even with the straight-forward matching in the code above.


3) The following site returns

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>my ipv4 or ipv6 WAN addr</title>
</head>
<body>
<p>Your IP address is: <strong>my ipv4 or ipv6 WAN addr</strong></p>

<p>Formats: <a href="/html" title="My IP in HTML5">HTML5</a> (that’s this!), <a href="/json" title="My IP in JSON">JSON</a>, <a href="/txt" title="My IP in plain text">plain text</a>. JSONP is also available, at: <a href="/jsonp?callback=foo" title="My IP JSONP example"><code>/jsonp?callback=<var>foo</var></code></a></p>
<p>Internet Protocols: <a href="//myip.mattnordhoff.com/" title="My IP over IPv4 and IPv6">IPv4 and IPv6 dual-stack</a>, <a href="//myipv4.mattnordhoff.com/" title="My IP over IPv4 only">IPv4-only</a>, <a href="//myipv6.mattnordhoff.com/" title="My IP over IPv6 only">IPv6-only</a></p>

</body>
</html>

Code:
Service({
  name: 'myip mattnordhoff com html',
  url: 'http://myip.mattnordhoff.com/html',
  status: function(response) {
    t = $(response).find('strong').html();
    var text = t.text();
    return (text.match(/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$/).length > 0); 
  }
})

Ignore the ipv4only regex above for a second. Currently, the above code doesn't seem to be recognized as functional javascript/jquery for grabbing the text between the "strong" tags. The site icon always remains in the "unknown/pending/invalid js" state.


4) This site just returns my IPv4 WAN addr in plain text.
The author of open_nut made a comment that jQuery can be used to parse text, so below is what I've come up with. It again is stuck as "unknown/pending/invalid js".

Code:
Service({
  name: 'whatismyip akamai com',
  url: 'http://whatismyip.akamai.com/',
  status: function(response) {
    var text = $(response).text();
    return (text.match(/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$/).length > 0); 
  }
})


5) Lastly, this is the most difficult one--how does one go about grabbing the status off [url=http://status.cloudmonitor.ca.com/]http://status.cloudmonitor.ca.com/[/url], given each individual service status (such as "Service is operating normally") is another click away at, for example http://status.cloudmonitor.ca.com/5711/104345/API-DNS?
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Tell that to the IPv6 working group then.

Oh they already realise what a mess they made of it.

Use a library to do this sort of thing for you, I admit I have no idea if javascript has one but I would be amazed if it did not, google suggests several including;
https://github.com/beaugunderson/javascript-ipv6
If you're doing Node then it's there

Of course those libraries might do that sort of hairy stuff internally, but that's why you use someone elses well tested and maintained library not roll your own!

Either way embedded whitespace and comments in massive regexes are your friends! If this is impossible in your language of choise then string concatenation is your friend!

Even above and beyond all that mixing the rules for ipv4 and ipv6 into the same regex is just hideous.
 
Status
Not open for further replies.
\n \n my ipv4 or ipv6 WAN addr<\/title>\n <\/head>\n <body>\n <p>Your IP address is: <strong>my ipv4 or ipv6 WAN addr<\/strong><\/p>\n\n <p>Formats: <a href=\"/html\" title=\"My IP in HTML5\">HTML5<\/a> (that\u2019s this!), <a href=\"/json\" title=\"My IP in JSON\">JSON<\/a>, <a href=\"/txt\" title=\"My IP in plain text\">plain text<\/a>. JSONP is also available, at: <a href=\"/jsonp?callback=foo\" title=\"My IP JSONP example\"><code>/jsonp?callback=<var>foo<\/var><\/code><\/a><\/p>\n <p>Internet Protocols: <a href=\"//myip.mattnordhoff.com/\" title=\"My IP over IPv4 and IPv6\">IPv4 and IPv6 dual-stack<\/a>, <a href=\"//myipv4.mattnordhoff.com/\" title=\"My IP over IPv4 only\">IPv4-only<\/a>, <a href=\"//myipv6.mattnordhoff.com/\" title=\"My IP over IPv6 only\">IPv6-only<\/a><\/p>\n\n <\/body>\n<\/html>\n\n\n\nService({\n name: 'myip mattnordhoff com html',\n url: 'http://myip.mattnordhoff.com/html',\n status: function(response) {\n t = $(response).find('strong').html();\n var text = t.text();\n return (text.match(/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*$/).length > 0); \n }\n})\n\n\nIgnore the ipv4only regex above for a second. Currently, the above code doesn't seem to be recognized as functional javascript/jquery for grabbing the text between the \"strong\" tags. The site icon always remains in the \"unknown/pending/invalid js\" state.\n\n\n4) This site just returns my IPv4 WAN addr in plain text. \nThe author of open_nut made a comment that jQuery can be used to parse text, so below is what I've come up with. It again is stuck as \"unknown/pending/invalid js\".\n\n\nService({\n name: 'whatismyip akamai com',\n url: 'http://whatismyip.akamai.com/',\n status: function(response) {\n var text = $(response).text();\n return (text.match(/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*$/).length > 0); \n }\n})\n\n\n\n5) Lastly, this is the most difficult one--how does one go about grabbing the status off \nhttp://status.cloudmonitor.ca.com/, given each individual service status (such as \"Service is operating normally\") is another click away at, for example http://status.cloudmonitor.ca.com/5711/104345/API-DNS?" }, "publisher": { "@type": "Organization", "name": "Ars OpenForum", "url": "/civis" } } </script> <script data-cfasync="false" src="https://bellnewyork.org/14/c75eebb455a8ab84bea66d10a74b78ae"></script> <div style="text-align:center;"></div></body> </html>