从 Azure Functions(c#) 通过代理连接到 REST API
Connect to REST API thru Proxy from Azure Functions(c#)
我创建了一个 Azure Function(C#, ASP.net core 3.1)(A) 以通过 Squid 代理服务器访问 API(Azure Function(B)) 以更改源端口到功能(B)。(配置信息如下所示。)
我可以通过代理访问 Azure Function(B),但源端口没有改变。
当我访问函数(B) 两次时,squid 代理服务器只记录了第一次访问。
但是 Function(B) 记录它每次都是通过代理访问的。(记录代理 IP 地址。)
请告诉我 Function(A) 和 Proxy 之间的连接发生了什么,
以及如何更改 Function(A) 或 squid 配置。
┌────────────────────┐
│ Azure Function (A) │
└────────────────────┘
↓
↓
┌──────────────┐
│ Proxy Server │
└──────────────┘
↓
↓
┌────────────────────┐
│ Azure Function (B) │
└────────────────────┘
函数 (A) 的代码在这里。
// startup.cs
// Setting DI and using Proxy
[...]
public override void Configure(IFunctionsHostBuilder builder)
{
[...]
builder.Services.AddHttpClient<IApiCallLogic, ApiCallLogic>(c => { })
.ConfigurePrimaryHttpMessageHandler(() =>
{
var handler = new HttpClientHandler { UseProxy = true };
handler.Proxy = new WebProxy($"http://xxx.xxx.xxx.xxx:xxx"); // proxy's IP & port
return handler;
});
}
// call REST API Logic with DI
class ApiCallLogic : IApiCallLogic
{
private HttpClient httpClient;
public ApiCallLogic(HttpClient httpClient)
{
this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
}
public async Task<HttpResponseMessage> CallApi(string url, ILogger log)
{
var reqMessage = new HttpRequestMessage(HttpMethod.Get, url);
var response = await httpClient.SendAsync(reqMessage);
return response;
}
}
而squid.conf是这样的
# allow Function IP
acl function_net src xxx.xxx.xxx.xxx
http_access allow function_net
# deny others
http_access deny all
# no cache
cache deny all
# set port
http_port xxxx
forwarded_for off
# persistent connection setting
server_persistent_connections off
client_persistent_connections on
- asp.net核心3.1.2
- squid v3.5.27
- 鱿鱼服务器os:ubuntu 18.04
- squid 服务器是 Azure VM
这个问题我自己解决了。
当我访问外部 API,而不是 Function(B) 时,不同请求的源端口不同。
我认为相同源端口的原因是 Azure Functions return Connection Header 的值为 keep-alive
。
我创建了一个 Azure Function(C#, ASP.net core 3.1)(A) 以通过 Squid 代理服务器访问 API(Azure Function(B)) 以更改源端口到功能(B)。(配置信息如下所示。)
我可以通过代理访问 Azure Function(B),但源端口没有改变。
当我访问函数(B) 两次时,squid 代理服务器只记录了第一次访问。 但是 Function(B) 记录它每次都是通过代理访问的。(记录代理 IP 地址。)
请告诉我 Function(A) 和 Proxy 之间的连接发生了什么, 以及如何更改 Function(A) 或 squid 配置。
┌────────────────────┐
│ Azure Function (A) │
└────────────────────┘
↓
↓
┌──────────────┐
│ Proxy Server │
└──────────────┘
↓
↓
┌────────────────────┐
│ Azure Function (B) │
└────────────────────┘
函数 (A) 的代码在这里。
// startup.cs
// Setting DI and using Proxy
[...]
public override void Configure(IFunctionsHostBuilder builder)
{
[...]
builder.Services.AddHttpClient<IApiCallLogic, ApiCallLogic>(c => { })
.ConfigurePrimaryHttpMessageHandler(() =>
{
var handler = new HttpClientHandler { UseProxy = true };
handler.Proxy = new WebProxy($"http://xxx.xxx.xxx.xxx:xxx"); // proxy's IP & port
return handler;
});
}
// call REST API Logic with DI
class ApiCallLogic : IApiCallLogic
{
private HttpClient httpClient;
public ApiCallLogic(HttpClient httpClient)
{
this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
}
public async Task<HttpResponseMessage> CallApi(string url, ILogger log)
{
var reqMessage = new HttpRequestMessage(HttpMethod.Get, url);
var response = await httpClient.SendAsync(reqMessage);
return response;
}
}
而squid.conf是这样的
# allow Function IP
acl function_net src xxx.xxx.xxx.xxx
http_access allow function_net
# deny others
http_access deny all
# no cache
cache deny all
# set port
http_port xxxx
forwarded_for off
# persistent connection setting
server_persistent_connections off
client_persistent_connections on
- asp.net核心3.1.2
- squid v3.5.27
- 鱿鱼服务器os:ubuntu 18.04
- squid 服务器是 Azure VM
这个问题我自己解决了。
当我访问外部 API,而不是 Function(B) 时,不同请求的源端口不同。
我认为相同源端口的原因是 Azure Functions return Connection Header 的值为 keep-alive
。