2023-01-01 22:50:39 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2023-07-03 23:24:35 -04:00
|
|
|
using System.Linq;
|
2023-01-01 22:50:39 -05:00
|
|
|
using System.Net.Http;
|
|
|
|
using System.Net.Http.Json;
|
2023-08-21 10:58:17 -04:00
|
|
|
using System.Net.Mime;
|
2023-01-01 22:50:39 -05:00
|
|
|
using System.Text.Json;
|
|
|
|
using System.Text.Json.Serialization.Metadata;
|
2023-07-03 23:24:35 -04:00
|
|
|
using System.Threading;
|
2023-01-01 22:50:39 -05:00
|
|
|
using System.Threading.Tasks;
|
2023-07-03 23:24:35 -04:00
|
|
|
using Luski.net.Enums;
|
|
|
|
using Luski.net.Interfaces;
|
|
|
|
using Luski.net.JsonTypes.BaseTypes;
|
2023-08-21 10:58:17 -04:00
|
|
|
using Luski.net.JsonTypes.WSS;
|
2024-03-20 23:18:34 -04:00
|
|
|
using Luski.Shared.PublicServers.V1.ClientToServer.HTTP;
|
|
|
|
using Luski.Shared.PublicServers.V1.Enums;
|
|
|
|
using Luski.Shared.PublicServers.V1.ServerToClient.HTTP;
|
2023-07-03 23:24:35 -04:00
|
|
|
using File = System.IO.File;
|
2023-01-01 22:50:39 -05:00
|
|
|
|
|
|
|
namespace Luski.net;
|
|
|
|
|
2023-07-08 09:06:13 -04:00
|
|
|
public partial class Server
|
2023-01-01 22:50:39 -05:00
|
|
|
{
|
2023-10-01 13:12:27 -04:00
|
|
|
|
|
|
|
internal Server(string Domain, string API_Version, bool Secure = true)
|
2023-07-10 07:35:05 -04:00
|
|
|
{
|
2023-07-10 14:19:03 -04:00
|
|
|
this.Domain = Domain;
|
|
|
|
this.ApiVersion = API_Version;
|
2023-08-21 10:58:17 -04:00
|
|
|
this.Secure = Secure;
|
2023-07-10 14:19:03 -04:00
|
|
|
Storage = new(Domain);
|
2023-08-25 12:07:36 -04:00
|
|
|
EncryptionHandler = new(Storage);
|
2023-07-10 07:35:05 -04:00
|
|
|
}
|
2023-10-01 13:12:27 -04:00
|
|
|
|
2023-08-21 10:58:17 -04:00
|
|
|
internal bool Secure = true;
|
2023-12-22 11:13:31 -05:00
|
|
|
internal string wssurl { get; set; } = "";
|
2023-08-21 10:58:17 -04:00
|
|
|
|
2023-07-10 14:19:03 -04:00
|
|
|
public ServerEncryption EncryptionHandler { get; }
|
|
|
|
public ServerStorage Storage { get; }
|
2023-07-03 23:24:35 -04:00
|
|
|
|
2023-08-21 10:58:17 -04:00
|
|
|
public async Task<Stream> GetAvatar(CancellationToken CancellationToken)
|
2023-01-01 22:50:39 -05:00
|
|
|
{
|
2023-07-10 07:35:05 -04:00
|
|
|
bool isc = File.Exists(Storage.GetStorageDirectory(StorageDirectory.ServerAssets) + "Icon");
|
2023-08-21 10:58:17 -04:00
|
|
|
if (!isc) await GetFromServer($"socketserver/Icon/", Storage.GetStorageDirectory(StorageDirectory.ServerAssets) + "Icon", CancellationToken);
|
|
|
|
return Storage.GetResourceStream(StorageDirectory.ServerAssets, "Icon");
|
2023-01-01 22:50:39 -05:00
|
|
|
}
|
|
|
|
|
2023-08-21 10:58:17 -04:00
|
|
|
public void SendServerOld<Tvalue>(Tvalue Payload, JsonTypeInfo<Tvalue> jsonTypeInfo) where Tvalue : IncomingWSS
|
2023-01-01 22:50:39 -05:00
|
|
|
{
|
|
|
|
ServerOut?.Send(JsonSerializer.Serialize(Payload, jsonTypeInfo));
|
|
|
|
}
|
2023-08-21 10:58:17 -04:00
|
|
|
|
2024-03-20 23:18:34 -04:00
|
|
|
public void SendServer(Luski.Shared.PublicServers.V1.Enums.DataType Type, IServerEvent Payload)
|
2023-08-21 10:58:17 -04:00
|
|
|
{
|
|
|
|
ServerOut?.Send(JsonSerializer.Serialize(new WSSOut()
|
|
|
|
{
|
|
|
|
Data = new()
|
|
|
|
{
|
|
|
|
Type = Type,
|
|
|
|
Data = Payload
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-01-01 22:50:39 -05:00
|
|
|
|
2023-07-03 23:24:35 -04:00
|
|
|
public HttpResponseMessage GetFromServer(string Path, CancellationToken CancellationToken, params KeyValuePair<string, string?>[] Headers)
|
2023-01-01 22:50:39 -05:00
|
|
|
{
|
|
|
|
using HttpClient web = new();
|
|
|
|
web.Timeout = TimeSpan.FromSeconds(10);
|
|
|
|
if (!login) web.DefaultRequestHeaders.Add("token", Token);
|
|
|
|
if (Headers is not null && Headers.Length > 0) foreach (KeyValuePair<string, string?> header in Headers) web.DefaultRequestHeaders.Add(header.Key, header.Value);
|
2023-08-21 10:58:17 -04:00
|
|
|
return web.GetAsync($"{(Secure ? "https" : "http" )}://{Domain}/{ApiVersion}/{Path}", cancellationToken: CancellationToken).Result;
|
2023-01-01 22:50:39 -05:00
|
|
|
}
|
|
|
|
|
2023-07-03 23:24:35 -04:00
|
|
|
public Task GetFromServer(string Path, string File, CancellationToken CancellationToken, params KeyValuePair<string, string?>[] Headers)
|
2023-01-01 22:50:39 -05:00
|
|
|
{
|
|
|
|
using HttpClient web = new();
|
|
|
|
web.Timeout = TimeSpan.FromMinutes(10);
|
|
|
|
if (!login) web.DefaultRequestHeaders.Add("token", Token);
|
|
|
|
if (Headers is not null && Headers.Length > 0) foreach (KeyValuePair<string, string?> header in Headers) web.DefaultRequestHeaders.Add(header.Key, header.Value);
|
2023-08-21 10:58:17 -04:00
|
|
|
HttpResponseMessage Response = web.GetAsync($"{(Secure ? "https" : "http" )}://{Domain}/{ApiVersion}/{Path}", CancellationToken).Result;
|
2023-07-03 23:24:35 -04:00
|
|
|
Stream stream = Response.Content.ReadAsStreamAsync(CancellationToken).Result;
|
2023-01-01 22:50:39 -05:00
|
|
|
using FileStream fs = System.IO.File.Create(File);
|
|
|
|
stream.CopyTo(fs);
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2024-03-20 23:18:34 -04:00
|
|
|
public async Task<Tresult> GetFromServer<Tresult>(string Path, JsonTypeInfo<Tresult> Type, CancellationToken CancellationToken, params KeyValuePair<string, string?>[] Headers) where Tresult : STC, new()
|
2023-01-01 22:50:39 -05:00
|
|
|
{
|
2023-07-03 23:24:35 -04:00
|
|
|
HttpResponseMessage ServerResponce = GetFromServer(Path, CancellationToken, Headers);
|
2023-08-21 10:58:17 -04:00
|
|
|
Tresult temp = new();
|
|
|
|
string raw = ServerResponce.Content.ReadAsStringAsync(CancellationToken).Result;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
temp = JsonSerializer.Deserialize(raw, Type)!;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine("JSON parse failed for the following data as type {0}\n{1}", temp.GetType(), raw);
|
|
|
|
}
|
2024-08-27 10:57:22 -04:00
|
|
|
if (temp is null) return new Tresult() { StatusCode = ServerResponce.StatusCode, Error = ErrorCode.ServerError, ErrorMessage = $"Server responded with empty data" };
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tresult GetFromServerRaw<Tresult>(string Path, JsonTypeInfo<Tresult> Type, params KeyValuePair<string, string?>[] Headers) where Tresult : STC, new()
|
|
|
|
{
|
|
|
|
HttpResponseMessage ServerResponce = GetFromServer(Path, CancellationToken.None, Headers);
|
|
|
|
Tresult temp = new();
|
|
|
|
string raw ="";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
temp = JsonSerializer.Deserialize(ServerResponce.Content.ReadAsStream(), Type)!;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine("JSON parse failed for the following data as type {0}\n{1}", temp.GetType(), raw);
|
|
|
|
}
|
2023-01-01 22:50:39 -05:00
|
|
|
if (temp is null) return new Tresult() { StatusCode = ServerResponce.StatusCode, Error = ErrorCode.ServerError, ErrorMessage = $"Server responded with empty data" };
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2024-03-20 23:18:34 -04:00
|
|
|
public async Task<Tresult> SendServer<Tvalue, Tresult>(string Path, Tvalue Payload, JsonTypeInfo<Tvalue> jsonTypeInfo, JsonTypeInfo<Tresult> ReturnjsonTypeInfo, CancellationToken CancellationToken, params KeyValuePair<string, string?>[] Headers) where Tvalue : CTS where Tresult : STC, new()
|
2023-01-01 22:50:39 -05:00
|
|
|
{
|
|
|
|
using HttpClient web = new();
|
|
|
|
if (!login) web.DefaultRequestHeaders.Add("token", Token);
|
|
|
|
if (Headers is not null && Headers.Length > 0) foreach (KeyValuePair<string, string?> header in Headers) web.DefaultRequestHeaders.Add(header.Key, header.Value);
|
2023-08-21 10:58:17 -04:00
|
|
|
HttpResponseMessage ServerResponce = web.PostAsJsonAsync($"{(Secure ? "https" : "http" )}://{Domain}/{ApiVersion}/{Path}", Payload, jsonTypeInfo, CancellationToken).Result;
|
|
|
|
//if (!ServerResponce.IsSuccessStatusCode) return new Tresult() { StatusCode = ServerResponce.StatusCode, Error = ErrorCode.ServerError, ErrorMessage = $"Server responded with status code {(int)ServerResponce.StatusCode}:{ServerResponce.StatusCode}" };
|
|
|
|
Tresult error = new() { StatusCode = ServerResponce.StatusCode, Error = null, ErrorMessage = $"Server responded with empty data" };
|
|
|
|
if (string.IsNullOrWhiteSpace(ServerResponce.Content.ReadAsStringAsync(CancellationToken).Result)) return error;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Tresult? temp = JsonSerializer.Deserialize(ServerResponce.Content.ReadAsStreamAsync(CancellationToken).Result, ReturnjsonTypeInfo);
|
|
|
|
if (temp is null) return error;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
catch { return error; }
|
|
|
|
}
|
|
|
|
|
2024-03-20 23:18:34 -04:00
|
|
|
public async Task<Tresult> SendServerPatch<Tvalue, Tresult>(string Path, Tvalue Payload, JsonTypeInfo<Tvalue> jsonTypeInfo, JsonTypeInfo<Tresult> ReturnjsonTypeInfo, CancellationToken CancellationToken, params KeyValuePair<string, string?>[] Headers) where Tvalue : CTS where Tresult : STC, new()
|
2023-08-21 10:58:17 -04:00
|
|
|
{
|
|
|
|
using HttpClient web = new();
|
|
|
|
if (!login) web.DefaultRequestHeaders.Add("token", Token);
|
|
|
|
if (Headers is not null && Headers.Length > 0) foreach (KeyValuePair<string, string?> header in Headers) web.DefaultRequestHeaders.Add(header.Key, header.Value);
|
|
|
|
HttpResponseMessage ServerResponce = await web.PatchAsJsonAsync($"{(Secure ? "https" : "http" )}://{Domain}/{ApiVersion}/{Path}", Payload, jsonTypeInfo, CancellationToken);
|
|
|
|
// HttpResponseMessage ServerResponce = await web.PatchAsync($"{(Secure ? "https" : "http" )}://{Domain}/{ApiVersion}/{Path}", new StringContent(JsonSerializer.Serialize(Payload, jsonTypeInfo)),
|
|
|
|
// CancellationToken);
|
|
|
|
//if (!ServerResponce.IsSuccessStatusCode) return new Tresult() { StatusCode = ServerResponce.StatusCode, Error = ErrorCode.ServerError, ErrorMessage = $"Server responded with status code {(int)ServerResponce.StatusCode}:{ServerResponce.StatusCode}" };
|
|
|
|
Tresult error = new() { StatusCode = ServerResponce.StatusCode, Error = null, ErrorMessage = $"Server responded with empty data" };
|
2023-07-03 23:24:35 -04:00
|
|
|
if (string.IsNullOrWhiteSpace(ServerResponce.Content.ReadAsStringAsync(CancellationToken).Result)) return error;
|
2023-01-01 22:50:39 -05:00
|
|
|
try
|
|
|
|
{
|
2023-07-03 23:24:35 -04:00
|
|
|
Tresult? temp = JsonSerializer.Deserialize(ServerResponce.Content.ReadAsStreamAsync(CancellationToken).Result, ReturnjsonTypeInfo);
|
2023-01-01 22:50:39 -05:00
|
|
|
if (temp is null) return error;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
catch { return error; }
|
|
|
|
}
|
|
|
|
|
2024-03-20 23:18:34 -04:00
|
|
|
public async Task<Tresult> SendServer<Tresult>(string Path, string File, JsonTypeInfo<Tresult> ReturnjsonTypeInfo, CancellationToken CancellationToken, params KeyValuePair<string, string?>[] Headers) where Tresult : STC, new()
|
2023-01-01 22:50:39 -05:00
|
|
|
{
|
|
|
|
var fs = System.IO.File.OpenRead(File);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
using HttpClient web = new();
|
|
|
|
if (!login) web.DefaultRequestHeaders.Add("token", Token);
|
|
|
|
web.Timeout = new TimeSpan(0, 10, 0);
|
|
|
|
if (Headers is not null && Headers.Length > 0) foreach (KeyValuePair<string, string?> header in Headers) web.DefaultRequestHeaders.Add(header.Key, header.Value);
|
2023-08-21 10:58:17 -04:00
|
|
|
//web.DefaultRequestHeaders.Add("Content-Type", MediaTypeNames.Application.Octet);
|
|
|
|
HttpResponseMessage ServerResponce = web.PostAsync($"{(Secure ? "https" : "http" )}://{Domain}/{ApiVersion}/{Path}", new StreamContent(fs), CancellationToken).Result;
|
2023-01-01 22:50:39 -05:00
|
|
|
try
|
|
|
|
{
|
2023-07-03 23:24:35 -04:00
|
|
|
Tresult? temp = JsonSerializer.Deserialize(ServerResponce.Content.ReadAsStreamAsync(CancellationToken).Result, ReturnjsonTypeInfo);
|
2023-08-21 10:58:17 -04:00
|
|
|
if (temp is null) return new Tresult() { StatusCode = ServerResponce.StatusCode, Error = null, ErrorMessage = $"Server responded with empty data" };
|
2023-01-01 22:50:39 -05:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
catch { return new Tresult() { StatusCode = ServerResponce.StatusCode, Error = ErrorCode.ServerError, ErrorMessage = $"Server responded with empty data" }; }
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
fs.Close();
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 23:24:35 -04:00
|
|
|
}
|