-
Notifications
You must be signed in to change notification settings - Fork 123
/
Proxy.cs
71 lines (59 loc) · 2.13 KB
/
Proxy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using ProxyKit;
using AdtExplorer.Functions.Utilities;
using Azure.Identity;
using Azure.Core;
namespace AdtExplorer.Functions
{
public class Proxy
{
private readonly IRequestProcessor _requestProcessor;
private readonly ILogger<Proxy> _log;
private static readonly DefaultAzureCredential _credential = new DefaultAzureCredential();
private static AccessToken token;
public Proxy(IRequestProcessor requestProcessor, ILogger<Proxy> log)
{
_requestProcessor = requestProcessor;
_log = log;
}
[FunctionName("proxy")]
public async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "GET", "POST", "PUT", "PATCH", "DELETE", Route = "proxy/{*wildcard}")] HttpRequest req)
{
var rpr = _requestProcessor.Process(req);
if (!rpr.IsSuccess)
{
return HttpUtilities.BadRequest(rpr.Message);
}
if (token.ExpiresOn < DateTime.UtcNow)
{
token = await GetAccessToken();
}
req.Headers["authorization"] = $"Bearer {token.Token}";
var ctx = req.HttpContext.ForwardTo(new UpstreamHost("https", new HostString(rpr.Context.Host)));
var uri = new UriBuilder(ctx.UpstreamRequest.RequestUri);
uri.Path = uri.Path.Replace("api/proxy/", string.Empty);
ctx.UpstreamRequest.RequestUri = uri.Uri;
foreach (var header in new[] { Constants.AdtHostHeaderName, "origin", "referer" })
{
ctx.UpstreamRequest.Headers.Remove(header);
}
_log.LogInformation($"Sending proxy request to {ctx.UpstreamRequest.RequestUri}");
var res = await ctx.Send();
res.Headers.Remove("transfer-encoding");
return res;
}
private ValueTask<AccessToken> GetAccessToken()
{
return _credential.GetTokenAsync(new TokenRequestContext(new string[] {"https://digitaltwins.azure.net/.default"}));
}
}
}