Removed PHP dependency and useless API-like endpoints, visual redo coming later, probably.

This commit is contained in:
flash 2023-06-24 01:43:26 +00:00
parent 39981857bf
commit 3d6d5b87ab
6 changed files with 113 additions and 181 deletions

12
nginx.conf Normal file
View file

@ -0,0 +1,12 @@
server {
server_name ipv4.flash.moe ipv6.flash.moe;
location / {
default_type 'text/plain; charset=utf-8';
add_header Access-Control-Allow-Origin '*';
add_header Cache-Control 'no-cache';
return 200 $remote_addr;
}
}

View file

@ -1,11 +1,10 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<meta charset="utf-8">
<title>Your IP Addresses</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link href="/style.css" type="text/css" rel="stylesheet"/>
<link href="//flash.moe/css/electrolize/style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<noscript>
@ -20,18 +19,18 @@
</div>
</noscript>
<div class="boxgrid">
<div class="ipbox ipv4">
<div class="ipbox ipv4" id="ipv4-click">
<div class="ipbox-background"></div>
<div class="ipbox-content">
<div class="ipbox-title">Your IPv4 is</div>
<div class="ipbox-address" data-ipv="4">loading...</div>
<div class="ipbox-title">Your IPv4 address is</div>
<div class="ipbox-address" id="ipv4">loading...</div>
</div>
</div>
<div class="ipbox ipv6">
<div class="ipbox ipv6" id="ipv6-click">
<div class="ipbox-background"></div>
<div class="ipbox-content">
<div class="ipbox-title">Your IPv6 is</div>
<div class="ipbox-address" data-ipv="6">loading...</div>
<div class="ipbox-title">Your IPv6 address is</div>
<div class="ipbox-address" id="ipv6">loading...</div>
</div>
</div>
</div>

View file

@ -1,82 +0,0 @@
<?php
if($_SERVER['REQUEST_METHOD'] !== 'GET') {
http_response_code(405);
header('Content-Type: text/plain');
echo 'Unsupported request method.';
return;
}
$reqPath = '/' . trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
if($_SERVER['HTTP_HOST'] !== 'ip.flash.moe') {
$address = $_SERVER['REMOTE_ADDR'];
$version = (int)substr($_SERVER['HTTP_HOST'], 3, 1);
$packed = bin2hex(inet_pton($address));
if($reqPath === '/json') {
header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache');
header('Content-Type: application/json; charset=utf-8');
echo json_encode([
'a' => $address,
'h' => $packed,
'v' => $version,
]);
return;
}
if($reqPath === '/xml') {
header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache');
header('Content-Type: application/xml; charset=utf-8');
$document = new DOMDocument('1.0', 'utf-8');
$root = $document->appendChild(new DOMElement('IPAddress'));
$root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$root->setAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
$root->setAttribute('version', $version);
$root->setAttribute('packed', $packed);
$root->appendChild(new DOMText($address));
echo $document->saveXML();
return;
}
if($reqPath === '/packed') {
header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache');
header('Content-Type: text/plain');
echo $packed;
return;
}
if($reqPath === '/') {
header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache');
header('Content-Type: text/plain');
echo $address;
return;
}
} else {
if($reqPath === '/style.css') {
header('Content-Type: text/css; charset=utf-8');
echo file_get_contents(__DIR__ . '/../style.css');
return;
}
if($reqPath === '/script.js') {
header('Content-Type: application/javascript; charset=utf-8');
echo file_get_contents(__DIR__ . '/../script.js');
return;
}
if($reqPath === '/') {
header('Content-Type: text/html; charset=utf-8');
echo file_get_contents(__DIR__ . '/../index.html');
return;
}
}
http_response_code(404);
header('Content-Type: text/plain');
echo 'Path not found.';

90
public/script.js Normal file
View file

@ -0,0 +1,90 @@
var createXHR = function() {
if('all' in document && !('atob' in window) && 'XDomainRequest' in window)
return new XDomainRequest;
if('XMLHttpRequest' in window)
return new XMLHttpRequest;
if('ActiveXObject' in window)
try {
return new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
try {
return new ActiveXObject('Microsoft.XMLHTTP');
} catch(e) {}
}
return null;
}
var getRemoteString = function(url, callback) {
var xhr = createXHR();
xhr.onload = function(ev) {
callback({ success: true, info: ev, text: xhr.responseText });
};
xhr.onerror = function(ev) {
callback({ success: false, info: ev });
};
xhr.open('GET', url);
xhr.send();
};
var selectTextInElement = function(target) {
// MSIE
if(document.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(target);
range.select();
return;
}
// Mozilla
if(window.getSelection) {
var select = window.getSelection(),
range = document.createRange();
range.selectNodeContents(target);
select.removeAllRanges();
select.addRange(range);
return;
}
};
var copySelectedText = function() {
if(document.execCommand) {
document.execCommand('copy');
return;
}
};
(function() {
var fields = [
{ target: 'ipv4', ext: 'v4', clickTarget: 'ipv4-click' },
{ target: 'ipv6', ext: 'v6', clickTarget: 'ipv6-click' },
];
for(var i in fields)
(function(field) {
var host = location.host.split('.');
host[0] += field.ext;
var url = location.protocol + '//' + host.join('.') + '/';
getRemoteString(url, function(info) {
var target = document.getElementById(field.target),
prop = 'textContent' in target ? 'textContent' : 'innerText';
if(info.success) {
target[prop] = info.text;
if(field.clickTarget) {
var clickTarget = document.getElementById(field.clickTarget);
if(clickTarget)
clickTarget.onclick = function() {
selectTextInElement(target);
copySelectedText();
};
}
} else
target[prop] = 'not available';
});
})(fields[i]);
})();

View file

@ -37,8 +37,8 @@ code {
align-items: center;
}
.ipv4 { --ipv-colour: #437675; /*margin-left: 22px !important;*/ }
.ipv6 { --ipv-colour: #66678d; /*margin-left: -22px !important;*/ }
.ipv4 { --ipv-colour: #437675; margin-left: 22px !important; }
.ipv6 { --ipv-colour: #66678d; margin-left: -22px !important; }
.ipbox {
max-width: 600px;

View file

@ -1,87 +0,0 @@
window.fwip = (function() {
this.ipv4s = document.querySelectorAll('[data-ipv="4"]');
this.ipv6s = document.querySelectorAll('[data-ipv="6"]');
this.doAddressLookup = function(version, callback) {
version = parseInt(version || 4).toString();
var url = '//ipv' + version + '.flash.moe/json';
fetch(url)
.then(resp => resp.json())
.then(data => callback(data))
.catch(err => callback({error: 'not available'}));
};
var lookupCallback = function(set) {
return function(result) {
for(var i = 0; i < set.length; ++i) {
if(!result.a)
set[i].classList.add('ip-lookup-failed');
else {
var copyTarget = set[i],
clickTarget = set[i].parentNode.parentNode;
clickTarget.onclick = function() { this.doAddressCopy(copyTarget) }.bind(this);
}
set[i].textContent = result.a || result.error || 'gone';
}
}.bind(this);
}.bind(this);
this.doAddressLookup(4, lookupCallback(this.ipv4s));
this.doAddressLookup(6, lookupCallback(this.ipv6s));
this.selectTextInElement = function(elem) {
// MSIE
if(document.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(elem);
range.select();
return;
}
// Mozilla
if(window.getSelection) {
var select = window.getSelection(),
range = document.createRange();
range.selectNodeContents(elem);
select.removeAllRanges();
select.addRange(range);
return;
}
console.warn('Unable to select text.');
};
this.copySelectedText = function() {
if(document.execCommand) {
document.execCommand('copy');
return;
}
console.warn('Unable to copy text.');
};
this.selectNothing = function() {
// MSIE
if(document.body.createTextRange) {
document.body.createTextRange().select();
return;
}
// Mozilla
if(window.getSelection) {
window.getSelection().removeAllRanges();
return;
}
console.warn('Unable to select text.');
};
this.doAddressCopy = function(elem) {
this.selectTextInElement(elem);
this.copySelectedText();
//this.selectNothing();
};
return this;
}).call(window.fwip || {});