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"}
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"}
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
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".
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?
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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]))*$/).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?