見出し画像

【Node.js】whois-parsed でドメインの有効期限を確認する方法

 ドメインには有効期限があります。意図せずに有効期限が切れてしまうとそのドメインを使用していた Web サイトが閲覧できなくなったり、メールが送受信できなくなったりといった障害が発生します。

 過去にこんなことにも遭遇しました(復旧済み)。

 そういった期限切れを回避するために、ドメインの有効期限を調べる方法があるか調べてみたら whois-parsed が簡単に使えそうだったので、軽く触ってみた!

インストール

npm install whois-parsed

軽く触ってみる

 試しに note の旧ドメインである note.mu のドメイン情報を取得してみたいと思います。

index.ts

const whois = require('whois-parsed');

(async () => {
   const results = await whois.lookup('note.mu');
   console.log(JSON.stringify(results, null, 2));
})()

 シンプル!

 実行してみる。

node ./index.js

 結果はこちら。

looking up whois for note.mu
{
 "domainName": "note.mu",
 "registrar": "EPAG Domainservices",
 "updatedDate": "2019-12-17T05:00:14.000Z",
 "creationDate": "2013-12-17T05:53:22.000Z",
 "expirationDate": "2020-12-17T05:53:23.000Z",
 "status": [
   "ok https://icann.org/epp#ok"
 ],
 "isAvailable": false
}

 ドメイン取得日や更新日、そして有効期限が分かりますね! 2020/12/17になっても expirationDate が変わらなければ取得チャンスがあります😁

 isAvailable が取得可能かどうかを表すフラグで、true ならば取得できます。

注意点

 TLD によっては非対応です。試しに以前 freenomで取得した無料ドメインの dafujii.ga を指定してみたところ例外が発生しました。

const whois = require('whois-parsed');

(async () => {
   const results = await whois.lookup('dafujii.ga');
   console.log(JSON.stringify(results, null, 2));
})()
looking up whois for dafujii.ga
Error: TLD not supported
   at parseRawData (S:\note\node_modules\whois-parsed\parse-raw-data.js:422:23)
   at Object.lookup (S:\note\node_modules\whois-parsed\index.js:28:30)
   at async S:\note\index.js:4:21
(node:13908) UnhandledPromiseRejectionWarning: Error: TLD not supported
   at parseRawData (S:\note\node_modules\whois-parsed\parse-raw-data.js:422:23)
   at Object.lookup (S:\note\node_modules\whois-parsed\index.js:28:30)
   at async S:\note\index.js:4:21
(node:13908) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13908) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

最後に

 whois-parsed でドメインの有効期限を取得することができました。

 有効期限が近付いたらアラート投げたり、自動更新もしたり、夢が広がりますね。

 ちなみに、もしドメインの有効期限が意図せずに切れてしまった場合でも猶予期間内ならば手数料が発生しますが買い戻すことが可能だそうです。



😉