Skip to content

Instantly share code, notes, and snippets.

@hasegawayosuke
Last active June 3, 2020 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasegawayosuke/56edd1ec5d69c821e3ecef43fa249b96 to your computer and use it in GitHub Desktop.
Save hasegawayosuke/56edd1ec5d69c821e3ecef43fa249b96 to your computer and use it in GitHub Desktop.
インターネット側から自宅のネットワーク(IPv6)へのアクセスがきちんとファイアウォールで遮断されているかを確認するための補助スクリプト
/* インターネット側から自宅のネットワーク(IPv6)へのアクセスがきちんとファイアウォールで遮断されているかを確認するためのスクリプト
*
* 1. 自PCのIPv6アドレスを列挙する
* 2. そのIPアドレスでlistenしたHTTPサーバーが起動される
* 3. そのサーバーのURLはQRコードとして表示される
* 4. スマホなどでWi-Fiではなくキャリア回線を通じて3.のQRコードにアクセスすることで疎通が確認できる
*
* node.js をインストールした環境で `node index.js` するだけ。あとは画面の指示に従ってスマホ等でアクセスする。
*/
const os = require('os')
const http = require('http')
const enumAddress = () => {
const interfaces = os.networkInterfaces()
const result = []
Object.keys(interfaces).forEach(interface => {
interfaces[interface].forEach(ifa => {
if (ifa.family === 'IPv6' && !ifa.address.toLowerCase().startsWith('fe80:') && !ifa.internal) {
result.push(ifa.address)
}
})
})
return result
}
const html1 = `<!doctype html><html><body>
このページが見えるということは、同じネットワーク内(自宅内など)でアクセスしているか、ファイアウォールが正しく働いていないということです。
</body>
`
const html2 = `<!doctype html><html><head><script>
document.addEventListener('DOMContentLoaded', (evt) => {
const urls = JSON.parse(decodeURIComponent(document.getElementById('urls').value))
const tbody = document.getElementById('tbody')
const template = '<tr><td><img src="%src%"></td><td>%url%</td></tr>'
urls.forEach(url => {
const img = \`https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=\${encodeURIComponent(url)}\`
const html = template.replace(/%([\\w]+)%/g, (x, s) => {
console.log(x, s)
if (s === 'url') return url
if (s === 'src') return img
})
tbody.insertAdjacentHTML('beforeend', html)
})
}, false)
</script></head><body>
アンチウイルスソフト / Windows のファイアウォールを一時的に無効にし、 以下のページに対して 1. 2. を試してください
<ol><li>Wi-Fiからアクセスしてみてください。ページが見えるのが正常です。</li><li>スマホ等でWi-Fiをオフにしてインターネット経由でアクセスしてください。見えるとやばいです。</li></ol>
<table id="table"><tbody id="tbody"></tbody></table>
<input type="hidden" value="%urls%" id="urls">
</body>
`
const start = {
'darwin': 'open',
'win32': 'start',
'win64': 'start',
'default': 'xdg-open'
}
const main = () => {
const addresses = enumAddress()
if (addresses.length === 0) {
console.log('You don\'t have IPv6 global address.')
return
}
const URLs = addresses.map(v6 => `http://[${v6}]/`)
console.log('Please access the following URLs from Internet with IPv6 address\n\n', addresses.map(v6 => `http://[${v6}]/`).join('\n '))
const handler = (req, res) => {
console.log(req.socket.remoteAddress)
if (req.url === '/') {
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'})
res.end(html1)
} else if (req.url === '/start') {
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'})
res.end(html2.replace(/%urls%/g, encodeURIComponent(JSON.stringify(URLs))))
} else {
res.writeHead(404, {'Content-Type':'text/plain'})
res.end('404 Not Found')
}
}
addresses.forEach(addr => {
http.createServer(handler).listen(80, addr)
})
require('child_process').exec(`${start[process.platform] || start.default} ${URLs[0]}start`)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment