-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgReverseProxy.cs
49 lines (38 loc) · 1.47 KB
/
tgReverseProxy.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
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Primitives;
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace tgMessageReverseProxy
{
public static class tgReverseProxy
{
private const string _tgHost = "https://api.telegram.org";
[FunctionName("tgReverseProxy")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "delete", Route = "tg/{botToken}/{*path}")]
Microsoft.AspNetCore.Http.HttpRequest req, string botToken, string path)
{
var method =
req.Method.ToLowerInvariant() switch
{
"get" => HttpMethod.Get,
"post" => HttpMethod.Post,
"put" => HttpMethod.Put,
"delete" => HttpMethod.Delete
};
var uri = new Uri($"{_tgHost}/{botToken}/{path}{req.QueryString.ToUriComponent()}");
var message = new HttpRequestMessage(method, uri) { Content = new ProxyContent(req) };
var client = new HttpClient();
var tgResponse = await client.SendAsync(message).ConfigureAwait(false);
var resp = req.HttpContext.Response;
resp.StatusCode = (int)tgResponse.StatusCode;
foreach (var (k, v) in tgResponse.Headers)
resp.Headers.Add(k, new StringValues(v.ToArray()));
await tgResponse.Content.CopyToAsync(resp.Body).ConfigureAwait(false);
await resp.Body.FlushAsync().ConfigureAwait(false);
}
}
}