0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

IPv6で接続されたHTTPクライアントのIPv4アドレスを確認する方法

Last updated at Posted at 2023-08-25

確認くん(IPv6/HTTP2 対応版)

REMOTE_ADDR には IPv6 しか入って来ないのでIPv4のアドレスを取得する為にもう一本の新しいセッションを張る必要がある。

別セッションでもまたIPv6が使われてしまわないように、DNSにIPv4専用にAレコードのみを記載したサブドメインを用意。

# host -a ipv4.berdysh.net

Trying "ipv4.berdysh.net"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47657
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 0

;; QUESTION SECTION:
;ipv4.berdysh.net.              IN      ANY

;; ANSWER SECTION:
ipv4.berdysh.net.       3600    IN      A       160.16.79.241

同様にIPv6専用のAAAAのみのレコードをDNSを登録

# host -a ipv6.berdysh.net

Trying "ipv6.berdysh.net"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 26972
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 0

;; QUESTION SECTION:
;ipv6.berdysh.net.              IN      ANY

;; ANSWER SECTION:
ipv6.berdysh.net.       3600    IN      AAAA    2001:e42:102:1515:160:16:79:241

javascript から IPv4専用のURLを呼び出す

    fetch('https://ipv4.berdysh.net/IpChecker/', {
        method: 'get',
        headers: { 'Origin': 'https://ipchecker.berdysh.net' }
    })
    .then(function (data) {
        return data.json() ;
    }).then(function (json){
        console.log(json) ;
    }) ;

JSONでIPv4のアドレスを応答する

{
    "REMOTE_ADDR": "*.*.*.*"
}

javascript の fetch を使う場合接続先のドメインが異なるとセキュリティエラーになってしまうので、CORS を設定する必要がある。

Uncaught (in promise) TypeError: Failed to fetch
    at EvCheck ((index):72:5)
    at EvLoad ((index):86:5)
    at window.onload ((index):90:5)
berdysh.net/:1 Access to fetch at 'https://ipv4.berdysh.net/IpChecker/' from origin 'https://berdysh.net' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
ipv4.berdysh.net/IpChecker/:1          Failed to load resource: net::ERR_FAILED

サーバー側はPHPで記述

header('Access-Control-Allow-Origin: *') ;
header('Content-Type: application/json; charset=utf-8') ;

この方法だと、DNS の設定権限でワイルドカード証明書が必要になってしまう。

フリーのAPIを使う方法もありそうだが、それは次回に試してみます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?