Skip to content

Commit f26a380

Browse files
committed
added main.py
1 parent 5bacef9 commit f26a380

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

gcp_cloud_function_proxy/main.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
# vim:ts=4:sts=4:sw=4:et
4+
#
5+
# Author: Hari Sekhon
6+
# Date: 2021-05-24 16:03:30 +0100 (Mon, 24 May 2021)
7+
#
8+
# https://github.com/HariSekhon/pytools
9+
#
10+
# License: see accompanying Hari Sekhon LICENSE file
11+
#
12+
# If you're using my code you're welcome to connect with me on LinkedIn
13+
# and optionally send me feedback to help steer this or other code I publish
14+
#
15+
# https://www.linkedin.com/in/HariSekhon
16+
#
17+
18+
"""
19+
20+
GCP Cloud Function to query ifconfig.co to show our IP information for debugging VPC Connector access
21+
routing via specified VPC Network to using default NAT Gateway
22+
23+
Example usage:
24+
25+
Check GCF source IP to compare if it's permitted through Cloudflare / Firewall rules
26+
27+
Test request examples:
28+
29+
{ "url": "http://ifconfig.co/json" }
30+
31+
defaults to http:// if not specified:
32+
33+
{ "url": "ifconfig.co/json" }
34+
35+
36+
Tested on GCP Cloud Functions with Python 3.9
37+
38+
"""
39+
40+
# https://cloud.google.com/functions/docs/writing/specifying-dependencies-python
41+
42+
import json
43+
import requests
44+
45+
def main(request):
46+
"""Responds to any HTTP request.
47+
Args:
48+
request (flask.Request): HTTP request object.
49+
Returns:
50+
The response text or any set of values that can be turned into a
51+
Response object using
52+
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
53+
"""
54+
data = json.loads(request.data)
55+
url = data['url']
56+
if '://' not in url:
57+
url = 'http://' + url
58+
req = requests.get(url)
59+
status_code = req.status_code
60+
status_message = req.reason
61+
content = req.text
62+
return "{} {}\n\n{}".format(status_code, status_message, content)

0 commit comments

Comments
 (0)