-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJunkTextFile.txt
More file actions
389 lines (327 loc) · 9.64 KB
/
JunkTextFile.txt
File metadata and controls
389 lines (327 loc) · 9.64 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
Ok, i'd say we are done with translation for now.
Let's work on a system so that we can sign into an account attached to a server.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using PaintPower.ProjectSystem;
using PaintPower.Logging;
namespace PaintPower.Networking;
// Networking class for the PaintPower Engine.
// Mainly to be used for the 'Coco xPaint Project', but it will lie
// here in the engine because it's open source. So anyone can create their own server.
public class Server
{
//*--- Domain security. ---*//
private static List<Domain> AllowedDomainsList = new List<Domain>();
private bool isConnected = false;
public void AllowDomain(Domain domain) => AllowedDomainsList.Add(domain);
public bool IsDomainAllowed(Domain domain) => AllowedDomainsList.Contains(domain);
public void ClearAllowedDomains() => AllowedDomainsList.Clear();
public void RemoveDomain(Domain domain) { AllowedDomainsList.Remove(domain); }
public Domain CurrentDomain = new Domain("www.cocoink.ink/f/PaintPower");
public void closeAllConnections() {
AllowedDomainsList.Clear();
}
// Make a valid url.
public string makeUrl(string addon = "")
{
return $"{URLifyer.URLify(CurrentDomain)}/{addon}";
}
// Create, register, and add default domains.
public void loadDefaultDomains() {
// Clear old list
AllowedDomainsList.Clear();
// Create Coco links, Paint links, random links, and more!
// Creating a custom server? Make a issue on GitHub and I'll add it here!
Domain d1 = new Domain("xpaint.cocoink.ink");
Domain d2 = new Domain("paint.cocoink.ink");
Domain d3 = new Domain("127.0.0.1:5500/f/xPaint");
Domain d4 = new Domain("127.0.0.1:5000/f/xPaint");
Domain d5 = new Domain("127.0.0.1:3000/f/xPaint");
Domain d12 = new Domain("0.0.0.0:5500/f/xPaint");
Domain d6 = new Domain("127.0.0.1:8000");
Domain d7 = new Domain("localhost:5500");
Domain d8 = new Domain("localhost:5000");
Domain d9 = new Domain("localhost:8000");
Domain d10 = new Domain("localhost:3000");
Domain d11 = new Domain("github.com");
Domain d13 = new Domain("paint-website.onrender.com");
Domain d14 = new Domain("paintpower.cocoink.ink");
Domain d15 = new Domain("www.cocoink.ink");
Domain d16 = new Domain("www.cocoink.ink/f/xPaint");
Domain d17 = new Domain("www.cocoink.ink/f/Paint");
Domain d18 = new Domain("www.cocoink.ink/f/PaintPower");
Domain d19 = new Domain("negro.org");
Domain d20 = new Domain("example.com");
// Add to list
AllowDomain(d1); AllowDomain(d2); AllowDomain(d3); AllowDomain(d4); AllowDomain(d5);
AllowDomain(d6); AllowDomain(d7); AllowDomain(d8); AllowDomain(d9); AllowDomain(d10);
AllowDomain(d11); AllowDomain(d12); AllowDomain(d13); AllowDomain(d14); AllowDomain(d15);
AllowDomain(d16); AllowDomain(d17); AllowDomain(d18); AllowDomain(d19); AllowDomain(d20);
#if DEBUG
setActiveDomain(d3);
#else
setActiveDomain(d16);
#endif
}
public void setActiveDomain(Domain domain)
{
CurrentDomain = domain;
}
//*--- Networking ---*//
public async Task InitServer()
{
loadDefaultDomains();
isConnected = await checkConnection();
}
public async Task<bool> checkConnection() {
var domain = CurrentDomain;
if (domain == null) throw new ArgumentNullException(nameof(domain));
if (!IsDomainAllowed(domain)) throw new UnauthorizedAccessException("Domain not allowed");
try {
return await Net.PerformGetRequest(makeUrl(Routes.checkActiveServer())) == "Ok.";
}
catch
{
return false;
}
}
public async Task<object?> GetFromServer(string url)
{
var domain = CurrentDomain;
if (domain == null) throw new ArgumentNullException(nameof(domain));
if (!IsDomainAllowed(domain)) throw new UnauthorizedAccessException("Domain not allowed");
return await Net.PerformGetRequest(url);
}
/* Download a project made by the user */
public async Task DownloadProject(string savePath)
{
string url = URLifyer.URLify(CurrentDomain);
await Net.DownloadFileAsync(url, savePath);
}
/* Save the project and load it into the editor. */
public async Task DownloadProjectAndLoad(string savePath)
{
try {
await DownloadProject(savePath);
} catch(Exception e)
{
Log.QuickLog(e.Message);
}
PaintPower_Engine.App.OpenProjectFile(savePath);
}
public async Task UploadProject(PaintProject project)
{
#pragma warning disable
PaintPower_Engine.App.RunSavingAnimation();
try
{
await Net.UploadFileAsync(
$"{URLifyer.URLify(CurrentDomain)}api/upload/projects/1/",
project.ProjectPath,
project.Metadata.name // send project title
);
}
finally
{
MainWindow.App._isSavingAnimationRunning = false;
}
}
// If the user is signed in, then get a list of their projects from the server.
public async Task ListUserProjects()
{
string url =
#if DEBUG
makeUrl(Routes.testServerListProjects());
#else
makeUrl(Routes.userProjectsRoute());
#endif
GetFromServer(url); // Do nothing with the data for now...
}
public Server() {
InitServer();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PaintPower.Networking;
// A class for a domain.
public class Domain
{
public bool IsConnected { get; set; }
public bool IsDisconnected { get; set; }
public bool IsAllowed { get; set; }
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string Type { get; set; } = string.Empty;
public string Protocol = string.Empty;
public string Host = string.Empty; public static string Port = string.Empty;
public string? Username = string.Empty;
public string? Password = string.Empty;
public string domain = string.Empty;
public string CSRF_Token = string.Empty;
public Domain(string nDomain, string nProtocol = "http", string nName = "", string nHost = "", string nPort = "") {
// Initalize domain
IsAllowed = false;
IsConnected = false;
IsDisconnected = false;
Name = nName;
Host = nHost;
Port = nPort;
domain = nDomain;
Protocol = nProtocol;
}
public override string ToString()
{
return $"Domain: {Name} ({Protocol}://{Host}:{Port})";
}
}
/*
File containing routes for the interweb, if you want to create your own server,
then use these routes or modify them for your server.
*/
namespace PaintPower.Networking;
public class Routes
{
// Server check routes
public static string serverCheck()
{
return "api/servercheck/";
}
public static string checkActiveServer()
{
return serverCheck();
}
// Upload project routes
public static string uploadNew() {
return "api/projects/new/upload/paintfile/";
}
public static string uploadUpdate(string id)
{
return $"api/projects/{id}/upload/paintfile/";
}
// Download project routes
public static string downloadProject(string id)
{
return $"api/projects/{id}/download";
}
// Mystuff routes
public static string userProjectsRoute(string username = "")
{
if (username != string.Empty)
{
return $"api/list/projects/{username}";
}
return "api/mystuff/projects";
}
// For testing a custom server.
public static string testServerListProjects()
{
return "api/listProjects/";
}
}
/*
Actual networking
*/
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using PaintPower;
class Net
{
// Shared HttpClient instance (recommended for performance)
private static readonly HttpClient client = new HttpClient();
private static async Task<string?> getCSRF_Token()
{
return PaintPower_Engine.App.server.CurrentDomain?.CSRF_Token;
}
// GET request method
public static async Task<string?> PerformGetRequest(string url)
{
try
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode(); // Throws if not 2xx
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("GET Response:");
Console.WriteLine(responseBody);
return responseBody;
}
catch (HttpRequestException e)
{
Console.WriteLine($"GET request error: {e.Message}");
return null;
}
}
// POST request method
public static async Task PerformPostRequest<T>(string url, T data)
{
try
{
string json = JsonSerializer.Serialize(data);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("POST Response:");
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine($"POST request error: {e.Message}");
}
}
public static async Task DownloadFileAsync(string url, string destinationPath)
{
try
{
using var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
await using var stream = await response.Content.ReadAsStreamAsync();
await using var fileStream = File.Create(destinationPath);
byte[] buffer = new byte[81920]; // 80 KB chunks
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await fileStream.WriteAsync(buffer, 0, bytesRead);
}
}
catch (Exception ex)
{
Console.WriteLine($"Download error: {ex.Message}");
}
}
public static async Task UploadFileAsync(string url, string filePath, string projectTitle)
{
Debug.WriteLine(url);
try
{
using var form = new MultipartFormDataContent();
await using var fileStream = File.OpenRead(filePath);
var fileContent = new StreamContent(fileStream);
fileContent.Headers.ContentType =
new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
// File field (multer expects "file")
form.Add(fileContent, "file", Path.GetFileName(filePath));
// Add project title
form.Add(new StringContent(projectTitle, Encoding.UTF8), "title");
using var response = await client.PostAsync(url, form);
response.EnsureSuccessStatusCode();
Debug.WriteLine("Upload complete.");
}
catch (Exception ex)
{
Debug.WriteLine($"Upload error: {ex.Message}");
}
}
}