Skip to content

Commit

Permalink
Add webhook testing tool references
Browse files Browse the repository at this point in the history
  • Loading branch information
cristipufu committed Aug 25, 2024
1 parent fc5c2ee commit 6b3f50e
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 85 deletions.
153 changes: 84 additions & 69 deletions src/Tunnelite.Server/HttpTunnel/HttpAppExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public static void UseHttpTunneling(this WebApplication app)
{
// reserved
if (payload.Subdomain.Equals("localhost", StringComparison.OrdinalIgnoreCase) ||
payload.Subdomain.Equals("tunnelite", StringComparison.OrdinalIgnoreCase))
payload.Subdomain.Equals("tunnelite", StringComparison.OrdinalIgnoreCase) ||
payload.Subdomain.Equals("webhooks", StringComparison.OrdinalIgnoreCase))
{
payload.Subdomain = RandomSubdomain();
}
Expand Down Expand Up @@ -253,79 +254,93 @@ await context.Response.WriteAsJsonAsync(new

static async Task TunnelRequestAsync(WebApplication app, HttpContext context, IHubContext<HttpTunnelHub> hubContext, HttpRequestsQueue requestsQueue, HttpTunnelStore tunnelStore, string path, ILogger<Program> logger)
{
try
{
HttpTunnelRequest? tunnel = null;

var subdomain = context.Request.Host.Host.Split('.')[0];

if (subdomain.Equals("localhost", StringComparison.OrdinalIgnoreCase))
{
tunnel = tunnelStore.Tunnels.FirstOrDefault().Value;
}
else if (subdomain.Equals("tunnelite"))
{
var filePath = Path.Combine(app.Environment.WebRootPath, "index.html");

if (File.Exists(filePath))
{
context.Response.ContentType = "text/html";
await context.Response.SendFileAsync(filePath);
}
else
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
await context.Response.WriteAsync("Index page not found");
}
return;
}
else
{
tunnelStore.Tunnels.TryGetValue(subdomain, out tunnel);
}
var filePath = Path.Combine(app.Environment.WebRootPath, "index.html");

if (tunnel == null)
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
await context.Response.WriteAsync(NotFound);
return;
}

if (!tunnelStore.Connections.TryGetValue(tunnel!.ClientId, out var connectionId))
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
await context.Response.WriteAsync(NotFound);
return;
}

var requestId = Guid.NewGuid();

var completionTask = requestsQueue.WaitForCompletionAsync(requestId, context, timeout: TimeSpan.FromSeconds(30), context.RequestAborted);

await hubContext.Clients.Client(connectionId).SendAsync("NewHttpConnection", new HttpConnection
{
RequestId = requestId,
ContentType = context.Request.ContentType,
Method = context.Request.Method,
Path = $"{tunnel.LocalUrl}/{path}{context.Request.QueryString}",
});

await completionTask;
}
catch (TaskCanceledException)
if (File.Exists(filePath))
{
// ignore
context.Response.ContentType = "text/html";
await context.Response.SendFileAsync(filePath);
}
catch (Exception ex)
else
{
if (!context.Response.HasStarted)
{
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
await context.Response.WriteAsync("An error occurred while processing the tunnel.");
}

logger.LogError(ex, "Error processing request tunnel: {Message}", ex.Message);
context.Response.StatusCode = StatusCodes.Status404NotFound;
await context.Response.WriteAsync("Index page not found");
}
return;

//try
//{
// HttpTunnelRequest? tunnel = null;

// var subdomain = context.Request.Host.Host.Split('.')[0];

// if (subdomain.Equals("localhost", StringComparison.OrdinalIgnoreCase))
// {
// tunnel = tunnelStore.Tunnels.FirstOrDefault().Value;
// }
// else if (subdomain.Equals("tunnelite"))
// {
// var filePath = Path.Combine(app.Environment.WebRootPath, "index.html");

// if (File.Exists(filePath))
// {
// context.Response.ContentType = "text/html";
// await context.Response.SendFileAsync(filePath);
// }
// else
// {
// context.Response.StatusCode = StatusCodes.Status404NotFound;
// await context.Response.WriteAsync("Index page not found");
// }
// return;
// }
// else
// {
// tunnelStore.Tunnels.TryGetValue(subdomain, out tunnel);
// }

// if (tunnel == null)
// {
// context.Response.StatusCode = StatusCodes.Status404NotFound;
// await context.Response.WriteAsync(NotFound);
// return;
// }

// if (!tunnelStore.Connections.TryGetValue(tunnel!.ClientId, out var connectionId))
// {
// context.Response.StatusCode = StatusCodes.Status404NotFound;
// await context.Response.WriteAsync(NotFound);
// return;
// }

// var requestId = Guid.NewGuid();

// var completionTask = requestsQueue.WaitForCompletionAsync(requestId, context, timeout: TimeSpan.FromSeconds(30), context.RequestAborted);

// await hubContext.Clients.Client(connectionId).SendAsync("NewHttpConnection", new HttpConnection
// {
// RequestId = requestId,
// ContentType = context.Request.ContentType,
// Method = context.Request.Method,
// Path = $"{tunnel.LocalUrl}/{path}{context.Request.QueryString}",
// });

// await completionTask;
//}
//catch (TaskCanceledException)
//{
// // ignore
//}
//catch (Exception ex)
//{
// if (!context.Response.HasStarted)
// {
// context.Response.StatusCode = StatusCodes.Status500InternalServerError;
// await context.Response.WriteAsync("An error occurred while processing the tunnel.");
// }

// logger.LogError(ex, "Error processing request tunnel: {Message}", ex.Message);
//}
}

static string RandomSubdomain(int length = 8)
Expand Down
65 changes: 49 additions & 16 deletions src/Tunnelite.Server/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,57 @@
margin: 0;
padding: 0;
}
header {
background-color: #f8f9fa;
padding: 1rem 0;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 20px;
}
header {
background-color: #fff;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
padding: 20px 0;
padding: 0 1rem;
}

nav {
display: flex;
justify-content: space-between;
align-items: center;
}

.logo {
font-size: 24px;
font-size: 1.5rem;
font-weight: bold;
color: var(--primary-color);
}
.nav-links a {
margin-left: 20px;
text-decoration: none;
color: var(--text-color);

.nav-links {
display: flex;
align-items: center;
gap: 1rem;
}

.nav-links a {
text-decoration: none;
color: #333;
font-size: 1rem;
transition: color 0.3s ease;
}

.nav-links a:hover {
color: #007bff;
}

.github-logo {
display: inline-block;
vertical-align: middle;
width: 24px;
height: 24px;
fill: currentColor;
}

.nav-links a[aria-label="GitHub repository"] {
display: flex;
align-items: center;
height: 100%;
padding: 0;
}
.hero {
text-align: center;
Expand Down Expand Up @@ -151,13 +178,18 @@
<body>
<header>
<nav class="container">
<div class="logo">Tunnelite</div>
<div class="logo"></div>
<div class="nav-links">
<a href="#features">Features</a>
<a href="#installation">Installation</a>
<a href="#usage">Usage</a>
<a href="#self-hosting">Self-Hosting</a>
<a href="https://github.com/cristipufu/tunnelite" target="_blank" rel="noopener noreferrer">GitHub</a>
<a href="https://webhooks.tunnelite.com" target="_blank">Webhook Tester</a>
<a href="https://github.com/cristipufu/tunnelite" target="_blank" aria-label="GitHub repository">
<svg class="github-logo" viewBox="0 0 16 16" width="24" height="24" aria-hidden="true">
<path fill-rule="evenodd" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"></path>
</svg>
</a>
</div>
</nav>
</header>
Expand All @@ -177,8 +209,9 @@ <h3>Expose Local Apps</h3>
<p>Share your locally-hosted web applications for testing or demos.</p>
</div>
<div class="feature">
<h3>Test Webhooks</h3>
<p>Easily test and debug webhook integrations by exposing your local endpoints to the internet.</p>
<h3>Test and Debug Webhooks</h3>
<p>Easily test webhook integrations by exposing local endpoints. </p>
<span>Try our <a href="https://webhooks.tunnelite.com" target="_blank">webhook testing tool</a> to inspect and debug incoming requests.</span>
</div>
<div class="feature">
<h3>Hackathon Ready</h3>
Expand Down

0 comments on commit 6b3f50e

Please sign in to comment.