2023-01-14 21:09:45 +00:00
|
|
|
|
using LANCommander.SDK;
|
|
|
|
|
using LANCommander.SDK.Models;
|
2023-01-05 02:31:02 +00:00
|
|
|
|
using RestSharp;
|
2023-01-07 04:12:03 +00:00
|
|
|
|
using RestSharp.Extensions;
|
2023-01-05 02:31:02 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-01-07 04:12:03 +00:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.IO;
|
2023-01-05 02:31:02 +00:00
|
|
|
|
using System.Linq;
|
2023-01-05 07:07:17 +00:00
|
|
|
|
using System.Net;
|
2023-01-16 02:45:37 +00:00
|
|
|
|
using System.Net.NetworkInformation;
|
2023-01-05 02:31:02 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-03-14 07:31:42 +00:00
|
|
|
|
using System.Windows.Media.Converters;
|
2023-01-05 02:31:02 +00:00
|
|
|
|
|
2023-01-08 18:09:57 +00:00
|
|
|
|
namespace LANCommander.PlaynitePlugin
|
2023-01-05 02:31:02 +00:00
|
|
|
|
{
|
|
|
|
|
internal class LANCommanderClient
|
|
|
|
|
{
|
2023-01-07 18:34:12 +00:00
|
|
|
|
public readonly RestClient Client;
|
2023-01-06 08:39:00 +00:00
|
|
|
|
public AuthToken Token;
|
2023-01-05 02:31:02 +00:00
|
|
|
|
|
2023-01-07 18:34:12 +00:00
|
|
|
|
public LANCommanderClient(string baseUrl)
|
2023-01-05 02:31:02 +00:00
|
|
|
|
{
|
2023-01-26 03:08:05 +00:00
|
|
|
|
if (!String.IsNullOrWhiteSpace(baseUrl))
|
|
|
|
|
Client = new RestClient(baseUrl);
|
2023-01-05 02:31:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-05 07:07:17 +00:00
|
|
|
|
private T PostRequest<T>(string route, object body)
|
|
|
|
|
{
|
|
|
|
|
var request = new RestRequest(route)
|
|
|
|
|
.AddJsonBody(body)
|
|
|
|
|
.AddHeader("Authorization", $"Bearer {Token.AccessToken}");
|
|
|
|
|
|
|
|
|
|
var response = Client.Post<T>(request);
|
|
|
|
|
|
|
|
|
|
return response.Data;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 08:39:00 +00:00
|
|
|
|
private T GetRequest<T>(string route)
|
|
|
|
|
{
|
|
|
|
|
var request = new RestRequest(route)
|
|
|
|
|
.AddHeader("Authorization", $"Bearer {Token.AccessToken}");
|
|
|
|
|
|
|
|
|
|
var response = Client.Get<T>(request);
|
|
|
|
|
|
|
|
|
|
return response.Data;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 04:12:03 +00:00
|
|
|
|
private string DownloadRequest(string route, Action<DownloadProgressChangedEventArgs> progressHandler, Action<AsyncCompletedEventArgs> completeHandler)
|
|
|
|
|
{
|
2023-01-14 22:11:28 +00:00
|
|
|
|
route = route.TrimStart('/');
|
|
|
|
|
|
2023-01-07 04:12:03 +00:00
|
|
|
|
var client = new WebClient();
|
|
|
|
|
var tempFile = Path.GetTempFileName();
|
|
|
|
|
|
|
|
|
|
client.Headers.Add("Authorization", $"Bearer {Token.AccessToken}");
|
|
|
|
|
client.DownloadProgressChanged += (s, e) => progressHandler(e);
|
|
|
|
|
client.DownloadFileCompleted += (s, e) => completeHandler(e);
|
|
|
|
|
|
|
|
|
|
client.DownloadFileAsync(new Uri($"{Client.BaseUrl}{route}"), tempFile);
|
|
|
|
|
|
|
|
|
|
return tempFile;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-13 22:37:36 +00:00
|
|
|
|
public async Task<AuthResponse> AuthenticateAsync(string username, string password)
|
2023-01-05 07:07:17 +00:00
|
|
|
|
{
|
2023-03-13 22:37:36 +00:00
|
|
|
|
var response = await Client.ExecuteAsync<AuthResponse>(new RestRequest("/api/Auth", Method.POST).AddJsonBody(new AuthRequest()
|
2023-01-05 07:07:17 +00:00
|
|
|
|
{
|
|
|
|
|
UserName = username,
|
|
|
|
|
Password = password
|
|
|
|
|
}));
|
|
|
|
|
|
2023-01-06 00:35:58 +00:00
|
|
|
|
switch (response.StatusCode)
|
|
|
|
|
{
|
|
|
|
|
case HttpStatusCode.OK:
|
|
|
|
|
return response.Data;
|
|
|
|
|
|
|
|
|
|
case HttpStatusCode.Forbidden:
|
|
|
|
|
case HttpStatusCode.BadRequest:
|
2023-03-14 06:10:02 +00:00
|
|
|
|
case HttpStatusCode.Unauthorized:
|
2023-01-06 00:35:58 +00:00
|
|
|
|
throw new WebException("Invalid username or password");
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new WebException("Could not communicate with the server");
|
|
|
|
|
}
|
2023-01-05 07:07:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-14 07:31:42 +00:00
|
|
|
|
public async Task<AuthResponse> RegisterAsync(string username, string password)
|
|
|
|
|
{
|
|
|
|
|
var response = await Client.ExecuteAsync<AuthResponse>(new RestRequest("/api/auth/register", Method.POST).AddJsonBody(new AuthRequest()
|
|
|
|
|
{
|
|
|
|
|
UserName = username,
|
|
|
|
|
Password = password
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
switch (response.StatusCode)
|
|
|
|
|
{
|
|
|
|
|
case HttpStatusCode.OK:
|
|
|
|
|
return response.Data;
|
|
|
|
|
|
|
|
|
|
case HttpStatusCode.BadRequest:
|
|
|
|
|
case HttpStatusCode.Forbidden:
|
|
|
|
|
case HttpStatusCode.Unauthorized:
|
|
|
|
|
throw new WebException(response.Data.Message);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new WebException("Could not communicate with the server");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> PingAsync()
|
|
|
|
|
{
|
|
|
|
|
var response = await Client.ExecuteAsync(new RestRequest("/api/Ping", Method.GET));
|
|
|
|
|
|
|
|
|
|
return response.StatusCode == HttpStatusCode.OK;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-05 07:07:17 +00:00
|
|
|
|
public AuthResponse RefreshToken(AuthToken token)
|
|
|
|
|
{
|
|
|
|
|
var request = new RestRequest("/api/Auth/Refresh")
|
|
|
|
|
.AddJsonBody(token);
|
|
|
|
|
|
|
|
|
|
var response = Client.Post<AuthResponse>(request);
|
|
|
|
|
|
2023-01-07 18:34:12 +00:00
|
|
|
|
if (response.StatusCode != HttpStatusCode.OK)
|
|
|
|
|
throw new WebException(response.ErrorMessage);
|
|
|
|
|
|
2023-01-05 07:07:17 +00:00
|
|
|
|
return response.Data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ValidateToken(AuthToken token)
|
|
|
|
|
{
|
2023-03-17 07:07:25 +00:00
|
|
|
|
if (token == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-01-05 07:07:17 +00:00
|
|
|
|
var request = new RestRequest("/api/Auth/Validate")
|
|
|
|
|
.AddHeader("Authorization", $"Bearer {token.AccessToken}");
|
|
|
|
|
|
2023-03-17 07:07:25 +00:00
|
|
|
|
if (String.IsNullOrEmpty(token.AccessToken) || String.IsNullOrEmpty(token.RefreshToken))
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-01-05 07:07:17 +00:00
|
|
|
|
var response = Client.Post(request);
|
|
|
|
|
|
|
|
|
|
return response.StatusCode == HttpStatusCode.OK;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-05 02:31:02 +00:00
|
|
|
|
public IEnumerable<Game> GetGames()
|
|
|
|
|
{
|
2023-01-06 08:39:00 +00:00
|
|
|
|
return GetRequest<IEnumerable<Game>>("/api/Games");
|
2023-01-05 07:07:17 +00:00
|
|
|
|
}
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
|
|
|
|
public Game GetGame(Guid id)
|
|
|
|
|
{
|
|
|
|
|
return GetRequest<Game>($"/api/Games/{id}");
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-14 21:09:45 +00:00
|
|
|
|
public GameManifest GetGameManifest(Guid id)
|
|
|
|
|
{
|
|
|
|
|
return GetRequest<GameManifest>($"/api/Games/{id}/Manifest");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string DownloadGame(Guid id, Action<DownloadProgressChangedEventArgs> progressHandler, Action<AsyncCompletedEventArgs> completeHandler)
|
|
|
|
|
{
|
|
|
|
|
return DownloadRequest($"/api/Games/{id}/Download", progressHandler, completeHandler);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 04:12:03 +00:00
|
|
|
|
public string DownloadArchive(Guid id, Action<DownloadProgressChangedEventArgs> progressHandler, Action<AsyncCompletedEventArgs> completeHandler)
|
|
|
|
|
{
|
2023-01-14 22:11:28 +00:00
|
|
|
|
return DownloadRequest($"/api/Archives/Download/{id}", progressHandler, completeHandler);
|
2023-01-07 04:12:03 +00:00
|
|
|
|
}
|
2023-01-16 02:45:37 +00:00
|
|
|
|
|
|
|
|
|
public string GetKey(Guid id)
|
|
|
|
|
{
|
|
|
|
|
var macAddress = GetMacAddress();
|
|
|
|
|
|
|
|
|
|
var request = new KeyRequest()
|
|
|
|
|
{
|
|
|
|
|
GameId = id,
|
|
|
|
|
MacAddress = macAddress,
|
|
|
|
|
ComputerName = Environment.MachineName,
|
|
|
|
|
IpAddress = GetIpAddress(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = PostRequest<Key>($"/api/Keys/Get", request);
|
|
|
|
|
|
|
|
|
|
return response.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 23:18:41 +00:00
|
|
|
|
public string GetAllocatedKey(Guid id)
|
|
|
|
|
{
|
|
|
|
|
var macAddress = GetMacAddress();
|
|
|
|
|
|
|
|
|
|
var request = new KeyRequest()
|
|
|
|
|
{
|
|
|
|
|
GameId = id,
|
|
|
|
|
MacAddress = macAddress,
|
|
|
|
|
ComputerName = Environment.MachineName,
|
|
|
|
|
IpAddress = GetIpAddress(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = PostRequest<Key>($"/api/Keys/GetAllocated/{id}", request);
|
|
|
|
|
|
|
|
|
|
if (response == null)
|
|
|
|
|
return String.Empty;
|
|
|
|
|
|
|
|
|
|
return response.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 02:45:37 +00:00
|
|
|
|
public string GetNewKey(Guid id)
|
|
|
|
|
{
|
|
|
|
|
var macAddress = GetMacAddress();
|
|
|
|
|
|
|
|
|
|
var request = new KeyRequest()
|
|
|
|
|
{
|
|
|
|
|
GameId = id,
|
|
|
|
|
MacAddress = macAddress,
|
|
|
|
|
ComputerName = Environment.MachineName,
|
|
|
|
|
IpAddress = GetIpAddress(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = PostRequest<Key>($"/api/Keys/Allocate/{id}", request);
|
|
|
|
|
|
|
|
|
|
if (response == null)
|
|
|
|
|
return String.Empty;
|
|
|
|
|
|
|
|
|
|
return response.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetMacAddress()
|
|
|
|
|
{
|
|
|
|
|
return NetworkInterface.GetAllNetworkInterfaces()
|
|
|
|
|
.Where(nic => nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
|
|
|
|
|
.Select(nic => nic.GetPhysicalAddress().ToString())
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetIpAddress()
|
|
|
|
|
{
|
|
|
|
|
return Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString();
|
|
|
|
|
}
|
2023-01-05 02:31:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|