GoogleAppsScript(GAS)のIPアドレス範囲を調べる

GASからUrlFetchApp.fetch()した場合にどのIPアドレスからリクエストが来るか事前に知りたい場合がありますよね。社内や身内だけで使っているクローズドなサーバなどでです。今回はこの調査方法についてまとめます。

注意点

Googleが利用状況などを見て、特に告知など無しに変更しますので、定期的にチェック&更新を行う必要があります。また将来的に今回紹介する方法が利用できなくなる可能性もありますので最新のドキュメントを確認ください。

今回は以下のドキュメントを参考にしています。

Class UrlFetchApp
https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
Requests made using this service originate from a set pool of IP ranges. You can look up the full list of IP addresses if you need to whitelist or approve these requests.

Static IP Addresses and App Engine apps
https://cloud.google.com/appengine/kb/#static-ip
App Engine's current range of outgoing IP addresses are encoded in the sender policy framework (SPF) record of _cloud-netblocks.googleusercontent.com. You may need to recursively perform DNS SPF lookups to resolve the entire list of IP ranges.

調査方法

DNSのSPFレコードを確認する

まず最初に nslookup を以下のように叩きます。

$ nslookup -q=TXT _cloud-netblocks.googleusercontent.com 8.8.8.8
Server:     8.8.8.8
Address:    8.8.8.8#53

Non-authoritative answer:
_cloud-netblocks.googleusercontent.com  text = "v=spf1 include:_cloud-netblocks1.googleusercontent.com include:_cloud-netblocks2.googleusercontent.com include:_cloud-netblocks3.googleusercontent.com include:_cloud-netblocks4.googleusercontent.com include:_cloud-netblocks5.googleusercontent.com ?all"

返却されたSPFレコードの内容から、以下の5つのドメインが使用されているようです。

  • _cloud-netblocks1.googleusercontent.com
  • _cloud-netblocks2.googleusercontent.com
  • _cloud-netblocks3.googleusercontent.com
  • _cloud-netblocks4.googleusercontent.com
  • _cloud-netblocks5.googleusercontent.com

SPFレコードのドメインからIPアドレスを確認

次に上記ドメインに割り当てられているIPアドレスをすべて確認します。

$ nslookup -q=TXT _cloud-netblocks1.googleusercontent.com 8.8.8.8
Server:     8.8.8.8
Address:    8.8.8.8#53

Non-authoritative answer:
_cloud-netblocks1.googleusercontent.com text = "v=spf1 include:_cloud-netblocks6.googleusercontent.com ip4:8.34.208.0/20 ip4:8.35.192.0/21 ip4:8.35.200.0/23 ip4:108.59.80.0/20 ip4:108.170.192.0/20 ip4:108.170.208.0/21 ip4:108.170.216.0/22 ip4:108.170.220.0/23 ip4:108.170.222.0/24 ip4:35.224.0.0/13 ?all"

この返却結果から次のIPアドレスが使用されていることがわかります。

  • 8.34.208.0/20
  • 8.35.192.0/21
  • 8.35.200.0/23
  • 108.59.80.0/20
  • 108.170.192.0/20
  • 108.170.208.0/21
  • 108.170.216.0/22
  • 108.170.220.0/23
  • 108.170.222.0/24
  • 35.224.0.0/13

あとは同じように残り4回繰り返せばリストが作成できるというわけです。

執筆時点でのIPアドレス一覧

2019年6月5日時点でのIPアドレスは以下になるようです(適宜スクロールしてください)

プログラムで自動生成する

大した作業量でもないので毎回手作業でやっても良いのですが、前向きにやりたい作業でもないので雑なコードで恐縮ですがPHPで自動的にリストを作成するプログラムを置いておきます。ご自由に利用くださいませ。

参考ページ

teratail.com developers.google.com cloud.google.com php.net

詳解! GoogleAppsScript完全入門 ~GoogleApps & G Suiteの最新プログラミングガイド~
高橋宣成
秀和システム
売り上げランキング: 4,819