我正在尝试通过运行Squid代理服务器的Google Compute Engine服务器VM实例代理从Google App Engine应用程序发出的出站API调用。
目标是全部通过一个静态IP地址进行REST api调用,以便第三方API能够通过其防火墙识别并允许该调用。
我已阅读并遵循了此帖子上的说明:
connect Google App Engine and Google Compute Engine
到目前为止,我已经做到了以下几点:
我也尝试过从云外壳界面运行curl命令,但是每次请求都超时。
如果有人可以帮助解决这个问题,我将非常感激。
以下是一个可能的示例,该示例基于对可用的Google Cloud Platform文档1 2的略微修改,以及如何通过运行Squid的Compute Engine VM在NodeJS运行时上代理来自App Engine Standard应用程序的出站HTTP请求,以及快速入门3。
1。创建无服务器VPC访问连接器:基本按照2创建连接器。更新gcloud
组件并在运行以下命令的项目上启用无服务器VPC访问API后,就足够了:
gcloud compute networks vpc-access connectors create [CONNECTOR_NAME] \
--network [VPC_NETWORK] \
--region [REGION] \
--range [IP_RANGE]
2。创建一个Compute Engine VM用作代理]:基本按照1设置一个Squid代理服务器:
a。 Reserve a static external IP address和assign it to a Compute Engine VM。
b。添加一个Firewall rule以允许在Squid的默认端口3128上进行通信。如果您正在使用default VPC network,则此命令应该起作用:gcloud compute firewall-rules create [FIREWALL_RULE_NAME] --network default --allow tcp:3128
c。使用以下命令sudo apt-get install squid3
在VM上安装Squid。
d。为VPC访问连接器启用Squid配置文件中的acl localnet src
条目:
sudo sed -i 's:#\(http_access allow localnet\):\1:' /etc/squid/squid.conf
sudo sed -i 's:#\(acl localnet src [IP_RANGE]/28.*\):\1:' /etc/squid/squid.conf
例如:如果您使用10.8.0.0作为[IP_RANGE]字段的值来创建连接器,则其外观应类似于sudo sed -i 's:#\(acl localnet src 10.8.0.0/28.*\):\1:' /etc/squid/squid.conf
e。通过sudo service squid start
]启动服务器
3。 [对App Engine应用程序的修改
::基于Quickstart for Node.js修改以下文件,以创建一个crawls a webpage using the request-promise library并显示网页HTML的应用程序。将该请求发送到网页using the VPC Access connector和VM,作为对app.yaml和app.js文件进行修改的代理。a。 package.json
... "test": "mocha --exit test/*.test.js" }, "dependencies": { "express": "^4.16.3", "request": "^2.88.0", "request-promise": "^4.2.5" }, "devDependencies": { "mocha": "^7.0.0", ...
b。 app.js
'use strict'; // [START gae_node_request_example] const express = require('express'); const app = express(); app.get('/', (req, res) => { res .status(200) .send('Hello, world!') .end(); }); //Add a handler to test the web crawler app.get('/test', (req, res) => { var request = require('request-promise'); request('http://www.input-your-awesome-website.com') .then(function (htmlString) { res.send(htmlString) .end(); }) .catch(function (err) { res.send("Crawling Failed...") .end(); }); }); // Start the server const PORT = process.env.PORT || 8080; app.listen(PORT, () => { console.log(`App listening on port ${PORT}`); console.log('Press Ctrl+C to quit.'); }); // [END gae_node_request_example]
c。 app.yaml
runtime: nodejs10 vpc_access_connector: name: "projects/[PROJECT]/locations/[REGION]/connectors/[CONNECTOR_NAME]" env_variables: HTTP_PROXY: "http://10.128.15.195:3128" HTTPS_PROXY: "http://10.128.15.195:3128"
每次您进入
/test
处理程序监视器,通过使用来自VM的sudo tail -f /var/log/squid/access.log
命令并检查日志中的更改,请求均通过代理。
Notes
:连接器,应用程序和VM必须位于同一区域才能工作,并且this are the supported regions for the connector。