通过运行Squid的Google Compute Engine从Google App Engine进行代理出站API调用

问题描述 投票:0回答:1

我正在尝试通过运行Squid代理服务器的Google Compute Engine服务器VM实例代理从Google App Engine应用程序发出的出站API调用。

目标是全部通过一个静态IP地址进行REST api调用,以便第三方API能够通过其防火墙识别并允许该调用。

我已阅读并遵循了此帖子上的说明:

connect Google App Engine and Google Compute Engine

到目前为止,我已经做到了以下几点:

  1. 创建了Google云计算VM,并成功为其分配了静态外部IP地址。
  2. 成功创建了无服务器VPC访问连接器(所有资源都位于同一GAE区域中。
  3. 将vpc_access_connector名称添加到Google App Engine项目(在Node.js上运行)的app.yaml中。
  4. [使用gcloud beta部署了应用程序,并且使用正确的Squid默认端口(3128)将api调用定向到代理服务器的内部IP地址。
  5. 在从GAE应用发出请求时,我可以从服务器日志中看到正在尝试正确的IP地址和端口,但收到以下错误:“错误:无法建立隧道套接字,原因=连接ECONNREFUSED [我-内部IP地址]:3128“

我也尝试过从云外壳界面运行curl命令,但是每次请求都超时。

如果有人可以帮助解决这个问题,我将非常感激。

google-app-engine google-compute-engine vpc
1个回答
0
投票

以下是一个可能的示例,该示例基于对可用的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 addressassign 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
© www.soinside.com 2019 - 2024. All rights reserved.