在ddev容器内访问IP(仅限VPN)

huangapple go评论60阅读模式
英文:

Access IP (available only in VPN) within ddev container

问题

我需要连接到一个VPN(使用Forticlient,但我认为这并不重要)以访问特定IP上的一个Web服务,比如说 http://1.2.3.4:8090/rest/endpoint
我可以从浏览器访问该URL,一切正常。

现在我需要从我的Drupal网站访问该URL。在我的本地机器上,我使用ddev进行本地开发,但它无法访问。

我不是网络专家,但是否有一种方法可以从ddev容器内的Drupal站点访问该IP?

英文:

I need to connect to a VPN (with Forticlient, but I don't think it matters) to access a web service on a specific ip, lets say http://1.2.3.4:8090/rest/endpoint.
I can access the URL from the browser and everything works.

Now I need to access that URL from my Drupal website. On my local machine I use ddev for local development, but it cannot reach it.

I'm not a network expert, but is there a way to access that IP from the drupal site within the ddev container?

答案1

得分: 1

请记住,DDEV Web 容器本身就是一台小型计算机,所以可能需要在其中安装 VPN 软件。这在很大程度上取决于您使用的 VPN 软件以及它与 Docker 网络的配合方式。

首先,在 Web 容器内部使用 curl 来测试您的 URL,例如 curl -I http://1.2.3.4:8090/rest/endpoint,并查看是否可以连接。然后,您需要确保这是一个 VPN 问题,而不仅仅是常规连接性问题,因为您在使用特定的 IP。如果无法连接,您需要向您的 VPN 供应商咨询如何在 Web 容器内安装 VPN 客户端,或者在主机 VPN 中启用与 Docker 网络的连接。

许多 VPN 不会对此造成问题... 确保您首先进行了常规的故障排除,关闭防火墙,以确保那不是问题的原因。

英文:

Remember that the DDEV web container is a little computer on its own, so it will probably need the VPN software installed in it. This depends a lot on the VPN software you're using and how it works with Docker networking.

Start by using curl inside the web container to test your URL, for example, curl -I http://1.2.3.4:8090/rest/endpoint and see if it can connect. Then you'll have to make sure it's a VPN issue vs just regular connectivity, since you're using a specific IP. If you can't connect, you'll want to check with your VPN vendor for how to install a VPN client inside the web container, or enable connectivity within the host VPN to Docker networking.

Many VPNs don't give trouble about this... Make sure you've done the normal troubleshooting things first, turning off your firewall, so you know that's not the trouble.

答案2

得分: 1

以下是翻译好的部分:

"最后,建立自己的代理要比牵涉“VPN专家”更快,我之所以选择了nodejs,只是因为我已经在主机上安装了它,不想再安装apache/php/其他东西,但无论如何,只要不与ddev端口冲突,都可以完成。

我是Drupal开发者,不是Nodejs开发者(实际上我对它几乎不了解...),但我找到了这个示例,并基于它创建了自己的代理 https://www.geeksforgeeks.org/how-to-build-a-node-js-proxy-server/
它非常简单(代理中没有复杂的逻辑,只有一个简单的URL重写,不仅因为示例中有,而且在我的情况下非常有用,因为我实际上必须代理两个不同的服务的两个不同IP,所以这种方式我可以使用“一个代理来控制它们所有”)。
以下是我编写的 app.js

const express = require("express");
const morgan = require("morgan");
const { createProxyMiddleware } = require("http-proxy-middleware");
require("dotenv").config();

// 创建Express服务器
const app = express();

// 配置
const { PORT } = process.env;
const { HOST } = process.env;
const { API_SEARCH_BASE_URL } = process.env;
const { API_INDEX_BASE_URL } = process.env;

// 记录请求
app.use(morgan("dev"));

// 代理逻辑:代理端点

// 搜索端点
app.use(
  "/search",
  createProxyMiddleware({
    target: API_SEARCH_BASE_URL,
    changeOrigin: true,
    pathRewrite: {
      "^/search/(.*)": "$1",
    },
  })
);

// 索引端点
app.use(
    "/index",
    createProxyMiddleware({
      target: API_INDEX_BASE_URL,
      changeOrigin: true,
      pathRewrite: {
        "^/index/(.*)": "$1",
      },
    })
);

// 启动代理服务器
app.listen(PORT, HOST, () => {
  console.log(`代理已启动在 ${HOST}:${PORT}`);
});

在我的 .env 文件中,我有以下内容:

HOST = 'myproxy.local'
PORT = 3000
API_SEARCH_BASE_URL = "http://1.2.3.4:8090"
API_INDEX_BASE_URL = "http://5.6.7.8:8080"

正如rfay所说,我将nodejs端口从3000更改为其他端口(我使用了8666,最后感觉有点邪恶),因为在3000端口上有很多竞争(可能导致一些初始问题)。
另外,HOST是什么并不重要(示例 .env 中是myproxy.local),因为最终必须使用rfay提到的host.docker.internal,而HOST仅用于在浏览器上测试,但在ddev中无法使用。
它运行得很好,可以转发我需要传递给Web服务的任何查询参数,我需要进行更多测试,但似乎运行得很好。再次感谢rfay的建议!"

英文:

In the end it was way more fast building my own proxy instead of involving the "VPN guys", I did it in nodejs just because I had it installed on my host and didn't want to install apache/php/other on it, but for sure it can be done in any way, as long you don't conflict with ddev ports.

I'm a Drupal guy, not a Nodejs guy (actually I almost don't know it.. ), but I found this example and based my own on this https://www.geeksforgeeks.org/how-to-build-a-node-js-proxy-server/
It's pretty straightforward (no logic on the proxy apart a simple url rewrite, not only because it was in the example but in my case it's also very useful as I actually have to proxy 2 different IPs for 2 different services, so in this way I use "one proxy to rule them all").
Here's how i wrote my own app.js:

const express = require("express");
const morgan = require("morgan");
const { createProxyMiddleware } = require("http-proxy-middleware");
require("dotenv").config();

// Create Express Server
const app = express();

// Configuration
const { PORT } = process.env;
const { HOST } = process.env;
const { API_SEARCH_BASE_URL } = process.env;
const { API_INDEX_BASE_URL } = process.env;

// Logging the requests
app.use(morgan("dev"));

// Proxy Logic : Proxy endpoints

// Searcher endpoint.
app.use(
  "/search",
  createProxyMiddleware({
    target: API_SEARCH_BASE_URL,
    changeOrigin: true,
    pathRewrite: {
      "^/search/(.*)": "$1",
    },
  })
);

// Index endpoint.
app.use(
    "/index",
    createProxyMiddleware({
      target: API_INDEX_BASE_URL,
      changeOrigin: true,
      pathRewrite: {
        "^/index/(.*)": "$1",
      },
    })
);

// Starting our Proxy server
app.listen(PORT, HOST, () => {
  console.log(`Starting Proxy at ${HOST}:${PORT}`);
});

in my .env I have this:

HOST = 'myproxy.local'
PORT = 3000
API_SEARCH_BASE_URL = "http://1.2.3.4:8090"
API_INDEX_BASE_URL = "http://5.6.7.8:8080"

As said by rfay i then changed the nodejs port from 3000 to something else (I used 8666, i felt a bit evil-ish in the end), as there's a lot of competition on that 3000 port (and probably was causing some of the initial problems).
Also it doesn't really matter what the HOST is (myproxy.local in the example .env) as in the end one has to use host.docker.internal as rfay stated, and the HOST is used only to test it on the browser, but not usable inside ddev.
It works fine, and it forwards any query parameter i need to pass to the webservice, I'll need to test it a bit more, but seems to work like a charm.
Thanks again to rfay for the inputs!

huangapple
  • 本文由 发表于 2023年2月23日 22:16:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定