-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketController.cs
More file actions
55 lines (46 loc) · 1.7 KB
/
WebSocketController.cs
File metadata and controls
55 lines (46 loc) · 1.7 KB
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
using System.Runtime.Intrinsics.Arm;
using System.Security.Cryptography;
using System.Text;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Validations.Rules;
namespace wasm_http_reverse_proxy;
public class WebSocketController : ControllerBase
{
private readonly TunnelClientRepository _repository;
public WebSocketController(TunnelClientRepository repository)
{
_repository = repository;
}
[Route("tunnel")]
[HttpPost]
public async Task<IActionResult> Tunnel()
{
// Compute a unique name/identifier for the websocket client
string value = UrlEncoder.Default.Encode(Convert.ToBase64String(
SHA512.HashData(
Encoding.UTF8.GetBytes($"{HttpContext.Connection.RemoteIpAddress}_{HttpContext.Connection.RemotePort}")
)
));
// We created nothing, but response format is appropriate
return Created($"/ws/{value}", null);
}
[Route("/ws/{name}")]
[Swashbuckle.AspNetCore.Annotations.SwaggerIgnore]
public async Task Get(string name)
{
if (HttpContext.WebSockets.IsWebSocketRequest)
{
var client = await TunnelClientFactory.CreateAsync(HttpContext, name, CancellationToken.None);
_repository.Add(client);
// Keep the websocket alive, but i'm not sure if this is the best way to
await client.Listener.KeepAlive();
// You know what, out of this scope, the websocket is closed, so we remove it from the repository
_repository.Remove(client.Id);
}
else
{
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
}
}
}