家のサーバーのIPv6のIPアドレスをDNSに登録した。
DNSに問い合わせれば、家のサーバーのIPv6アドレスが分かる。
|
1 2 3 4 |
Name: silverfox.noizumi.org Address: 183.76.197.88 Name: silverfox.noizumi.org Address: 2405:6587:c200:7500:6c7b:5eff:feac:7a1 |
IPv6のアドレスは、半固定だからIPv4みたいに変わってしまう事を余り考慮しなくて良いという認識だったが、家のルーターのIPv6のフィルター見てみると、IPv6のアドレスが変わっていた。
端末のIPv6アドレスはルーターの再起動などで変わってしまうので、その度にルーターの設定を変更しないといけないが、そういう面倒を避けるには、メーカー曰く、プロバイダに固定IPなどの契約をしてくださいとの事。
でもまあ、IPv6で接続不能になっていても、IPv4で家のルーターに接続して設定変更すれば、一応、IPv6で通信できるようになるから、プロバイダに固定IPの課金はしない 🙂
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
<?php /* * ipcheck.php * */ $api_key = '[APIキー]'; $server_id = 'xsvx1023505'; $servername = $server_id.'.xsrv.jp'; $api_base_url = 'https://api.xserver.ne.jp'; $api_base_url .= '/v1/server/'.$servername; $target = 'silverfox'; $domain = 'noizumi.org'; $url_ip_prog = 'https://irc.noizumi.org/cgi-bin/ip.py'; /** * DNSレコード全部を取得する * @global string $api_key * @global string $api_base_url * @return array */ function getDNSRecord(){ global $api_key; global $api_base_url; $headers = array( 'Content-Type: application/json', 'Authorization: Bearer '.$api_key ); $ch = curl_init(); //curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); // IPv4 Only. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // HTTPレスポンス 30x を追跡する。 curl_setopt($ch, CURLOPT_HEADER, false); $command = '/dns'; curl_setopt($ch, CURLOPT_URL, $api_base_url.$command); $output = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); curl_close($ch); return json_decode($output, true); } /** * DNSレコードを更新する * @global string $api_base_url * @global string $api_key * @param string $dns_id * @param array $params */ function setDNS($dns_id, $params){ global $api_base_url; global $api_key; $fields = json_encode($params); $headers = array( 'Content-Type: application/json', 'Content-Length: ' . strlen($fields), 'Authorization: Bearer '.$api_key ); $ch = curl_init(); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // HTTPレスポンス 30x を追跡する。 curl_setopt($ch, CURLOPT_HEADER, false); $command = '/dns/'.$dns_id; curl_setopt($ch, CURLOPT_URL, $api_base_url.$command); $output = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); curl_close($ch); } // DNS一覧から変更するレコードのIDと設定値を取り出す。 // ※事前にDNSレコードは登録しておく。 $dns_record = getDNSRecord(); $ipv4_done=false; $ipv6_done=false; foreach($dns_record['records'] as $index=>$rec){ // ホスト名に合致し、IPv4のレコードなら処理する。 if(strpos($rec['host'], $target)!==false && $rec['type']=='A' && !$ipv4_done){ $dns_id4 = $rec['id']; $hosts4 = $rec['content']; $ipv4_done=true; } // ホスト名に合致し、IPv6のレコードなら処理する。 if(strpos($rec['host'], $target)!==false && $rec['type']=='AAAA' && !$ipv6_done){ $dns_id6 = $rec['id']; $hosts6 = $rec['content']; $ipv6_done=true; } if($ipv4_done && $ipv6_done){ break; } } // IPv4を明示的に指定して接続するコンテキストを作成 $context4 = stream_context_create([ 'socket' => [ // IPv4アドレスでバインドする(通常は0.0.0.0:0) 'bindto' => '0.0.0.0:0', ], ]); // IPv4でサイトにアクセスしてIPアドレスを得る $response4 = file_get_contents($url_ip_prog, false, $context4); if(preg_match('/Your IP address is ([0-9.]*)/', $response4, $matches)){ $myip = $matches[1]; if(!empty($hosts4) && $myip != $hosts4){ $params = array( 'domain' => $domain, 'host' => $target, 'type' => 'A', 'content' => $myip ); setDNS($dns_id4, $params); } } // IPv6を明示的に指定して接続するコンテキストを作成 $context6 = stream_context_create([ 'socket' => [ // IPv6アドレスでバインドする(通常は[::]:0) 'bindto' => '[::]:0', ], ]); // IPv6でサイトにアクセスしてIPアドレスを得る // ※IPv4環境だと、IPv4のIPアドレスが返ってくるので、IPv6のパターンにマッチした時だけ登録する。 $response6 = file_get_contents($url_ip_prog, false, $context6); if(preg_match('/Your IP address is ((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-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-f]{1,4}:){5}(((:[0-9a-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-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-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-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-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-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-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-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-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-f]{1,4}){1,7})|((:[0-9a-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*$/', $response6, $matches)){ $myip = $matches[1]; if(!empty($hosts6) && $myip != $hosts6){ $params = array( 'domain' => $domain, 'host' => $target, 'type' => 'AAAA', 'content' => $myip ); setDNS($dns_id6, $params); } } ?> |
XSERVER は IPv4環境なので、IPv4のIPアドレスしか返さないから、KAGOYA VPSの irc.noizumi.orgに接続する。
irc.noizumi.org は当初25GBしかディスク容量が無かったので、できるだけ最少構成にすべく、PHPはインストールせずに、apache2のCGIモジュールを有効にし、CGIでpythonスクリプトにより、IPアドレスを返すようにしている。
|
1 2 3 4 5 6 |
#!/usr/bin/python3 import os from pathlib import Path Path("/var/www/html/ip.txt").write_text(os.environ.get("REMOTE_ADDR"), encoding="utf-8") print("Content-Type: text/html\n") print("Your IP address is", os.environ.get("REMOTE_ADDR")) |
