-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
216 lines (167 loc) · 5.83 KB
/
Copy pathProgram.cs
File metadata and controls
216 lines (167 loc) · 5.83 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Microsoft.AspNetCore.HttpOverrides;
using synk;
bool cliOK = GlobalConfig.CommandLineParse(args);
var builder = WebApplication.CreateBuilder(args);
if (!cliOK)
{
DBg.d(LogLevel.Critical, "Command line parsing failed. Exiting.");
Environment.Exit(1);
}
DBg.d(LogLevel.Information, $"synk:{GlobalConfig.bldVersion}");
builder.WebHost.UseUrls($"http://{GlobalConfig.Bind}:{GlobalConfig.Port}");
var app = builder.Build();
// this configures the middleware to respect the X-Forwarded-For and X-Forwarded-Proto headers
// that are set by any reverse proxy server (nginx, apache, etc.)
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseRouting();
app.MapGet("/about", (HttpContext httpContext) =>
{
string fn = "/about"; DBg.d(LogLevel.Trace, fn);
return Results.Text(GlobalStatic.staticAboutPage, "text/html");
});
//redirect pathless requests to to the about page
app.MapGet("/", (HttpContext httpContext) =>
{
string fn = "/"; DBg.d(LogLevel.Trace, fn);
return Results.Redirect("/about");
});
app.MapGet("/key", (HttpContext httpContext) =>
{
string fn = "/key"; DBg.d(LogLevel.Trace, fn);
Guid id = Guid.NewGuid();
string guid = id.ToString();
string threeword = GlobalStatic.ThreeWords();
// generate a valid 512 bit key suitable for use as a private key
string randomkey = GlobalStatic.RandomHexKey();
return Results.Text($"{guid}\n{threeword}\n{randomkey}", "text/plain");
}).AllowAnonymous();
// endpoint that adds a blobkey and stores/updates its data
app.MapPut("/blob/{key}", async (HttpContext httpContext, string key) =>
{
string fn = "/blob"; DBg.d(LogLevel.Trace, fn);
if (blobController.IsUrlEncoded(key))
{
// decode the key
key = blobController.UrlDecode(key);
}
// we allow keys a max length of 512 characters
if (key.Length > 512)
{
DBg.d(LogLevel.Error, $"Blob key {key} is too long");
return Results.StatusCode(StatusCodes.Status400BadRequest);
}
long payloadSize = httpContext.Request.ContentLength ?? 0;
// if there is no Put data, return a 400
if (payloadSize == 0)
{
DBg.d(LogLevel.Error, $"No Data provided to {key}..");
return Results.StatusCode(StatusCodes.Status400BadRequest);
}
// if the content length is > however much free space in our synkstore
// return http 413
long currentStoreSize = GlobalStatic.synkStoreSize();
if (currentStoreSize + payloadSize > GlobalConfig.maxSynkStoreSize)
{
DBg.d(LogLevel.Error, $"Blob store size {GlobalStatic.PrettySize(currentStoreSize)} (existing) + {GlobalStatic.PrettySize(payloadSize)} (new) is larger than MAX {GlobalStatic.PrettySize(GlobalConfig.maxSynkStoreSize)}");
return Results.StatusCode(StatusCodes.Status413PayloadTooLarge);
}
// get the blob data from the request body
using (var ms = new MemoryStream())
{
await httpContext.Request.Body.CopyToAsync(ms);
byte[] data = ms.ToArray();
// write the blob file
try
{
blobController.WriteBlobFile(key, data);
return Results.Ok();
}
catch (Exception ex)
{
DBg.d(LogLevel.Error, $"Exception occurred: {ex}");
return Results.StatusCode(StatusCodes.Status500InternalServerError);
}
}
}).AllowAnonymous();
// endpoint that returns the blob data for the provided key
app.MapGet("/blob/{key}", (HttpContext httpContext, string key) =>
{
string fn = "/blob"; DBg.d(LogLevel.Trace, fn);
if (blobController.IsUrlEncoded(key))
{
// decode the key
key = blobController.UrlDecode(key);
}
// we allow keys a max length of 512 characters
if (key.Length > 512)
{
DBg.d(LogLevel.Error, $"Blob key {key} is too long");
return Results.StatusCode(StatusCodes.Status400BadRequest);
}
// read the blob file and return the data
byte[]? data = blobController.ReadBlobFile(key);
if (data == null)
{
DBg.d(LogLevel.Error, $"Blob key {key} not found");
return Results.NoContent();
}
else
{
return Results.File(data, "application/octet-stream");
}
}).AllowAnonymous();
// a DELETE endpoint that deletes the blob file for the provided key
app.MapDelete("/blob/{key}", (HttpContext httpContext, string key) =>
{
string fn = "/blob"; DBg.d(LogLevel.Trace, fn);
if (blobController.IsUrlEncoded(key))
{
// decode the key
key = blobController.UrlDecode(key);
}
// we allow keys a max length of 512 characters
if (key.Length > 512)
{
DBg.d(LogLevel.Error, $"Blob key {key} is too long");
return Results.StatusCode(StatusCodes.Status400BadRequest);
}
// delete the blob file
try
{
blobController.DeleteBlobFile(key);
return Results.Ok();
}
catch (Exception ex)
{
// DeleteBlobFile throws KeyNotFoundException if the key is not found - return http no data
if (ex is KeyNotFoundException)
{
DBg.d(LogLevel.Error, $"Blob key {key} not found");
return Results.NoContent();
}
else
{
DBg.d(LogLevel.Error, $"Exception occurred: {ex}");
return Results.StatusCode(StatusCodes.Status500InternalServerError);
}
}
}).AllowAnonymous();
// Mutex to ensure only one of us is running
bool createdNew;
using (var mutex = new Mutex(true, GlobalStatic.applicationName, out createdNew))
{
if (createdNew)
{
// initial load any existing blobkeys
blobController.LoadBlobKeys();
blobController.VerifyBlobKeyFiles();
app.Run();
}
else
{
Console.WriteLine("Another instance of the application is already running.");
}
}