Search code examples
c#azureasp.net-coreproxysquid

Connect to REST API thru Proxy from Azure Functions(c#)


I create a Azure Function(C#, ASP.net core 3.1)(A) to to access a API(Azure Function(B)) thru a Squid proxy server to change a source port to the Function(B).(config information is shown below.)

I can access Azure Function(B) thru Proxy, but source port does not change.

When I accessed Function(B) twice, the squid proxy server logged the first access only. But Function(B) logged it was accessed thru the proxy every time.(logged proxy ip address.)

Please tell me what happens on the connection between Function(A) and Proxy, and how do I change Function(A) or squid configuration.

┌────────────────────┐
│ Azure Function (A) │
└────────────────────┘
         ↓
         ↓
 ┌──────────────┐
 │ Proxy Server │
 └──────────────┘
         ↓
         ↓
┌────────────────────┐
│ Azure Function (B) │
└────────────────────┘

Function(A)'s code is here.

// 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;
    }
}

And squid.conf is like this.

# 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 core 3.1.2
  • squid v3.5.27
  • squid server os: ubuntu 18.04
    • squid server is Azure VM

Solution

  • I have solved this problem by myself. When I accessed an external API, not Function(B), source ports on different requests were different. I think the cause of the same source ports is that Azure Functions return the Connection Header with the value keep-alive.