Skip to content

Commit

Permalink
Added check authorization in public methods
Browse files Browse the repository at this point in the history
And fix in private method "createSession()"
  • Loading branch information
TheLanc3 authored Apr 10, 2023
1 parent 6da491f commit 2be83ad
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/WgEasyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ private Task createSession() {
binary.Serialize(stream, _cookies);
}
}
else {
File.Delete(_session_file);
using(FileStream stream = File.Create(_session_file)) {
BinaryFormatter binary = new BinaryFormatter();
binary.Serialize(stream, _cookies);
}
}
return Task.CompletedTask;
}
private Task loadingSession() {
Expand Down Expand Up @@ -138,6 +145,12 @@ public async Task LoginToServerIfNeeded() {
///</summary>
///<returns><see cref="System.Collections.Generic.List"/> with <see cref="T:WgEasyManager.Types.WireGuardKey"/></returns>
public async Task<List<WireGuardKey>> GetKeys() {
var status = await checkAuthrization();
if(!status.Authenticated) {
await makeRequest("POST", "api/session", "password", _password, out _);
await updateCookieContainer(cash);
await createSession();
}
await makeRequest("GET", "api/wireguard/client", "none", "none", out var data);
return JArray.Parse(data).ToObject<List<WireGuardKey>>();
}
Expand All @@ -147,6 +160,12 @@ public async Task<List<WireGuardKey>> GetKeys() {
///</summary>
///<param name="name">Name of new key</param>
public async Task<WireGuardKey> CreateKey(string name) {
var status = await checkAuthrization();
if(!status.Authenticated) {
await makeRequest("POST", "api/session", "password", _password, out _);
await updateCookieContainer(cash);
await createSession();
}
await makeRequest("POST", "api/wireguard/client", "name", name, out var data);
return (JObject.Parse(data)).ToObject<WireGuardKey>();
}
Expand All @@ -156,6 +175,12 @@ public async Task<WireGuardKey> CreateKey(string name) {
///</summary>
///<param name="clientId">Id of client</param>
public async Task DeleteKey(string clientId) {
var status = await checkAuthrization();
if(!status.Authenticated) {
await makeRequest("POST", "api/session", "password", _password, out _);
await updateCookieContainer(cash);
await createSession();
}
await makeRequest("DELETE", $"api/wireguard/client/{clientId}", withoutParametr, withoutParametr, out _);
}

Expand All @@ -164,6 +189,12 @@ public async Task DeleteKey(string clientId) {
///</summary>
///<param name="clientId">Id of client</param>
public async Task UnbanKey(string clientId) {
var status = await checkAuthrization();
if(!status.Authenticated) {
await makeRequest("POST", "api/session", "password", _password, out _);
await updateCookieContainer(cash);
await createSession();
}
await makeRequest("POST", $"api/wireguard/client/{clientId}/enable", withoutParametr, withoutParametr, out _);
}

Expand All @@ -172,6 +203,12 @@ public async Task UnbanKey(string clientId) {
///</summary>
///<param name="clientId">Id of client</param>
public async Task BlockKey(string clientId) {
var status = await checkAuthrization();
if(!status.Authenticated) {
await makeRequest("POST", "api/session", "password", _password, out _);
await updateCookieContainer(cash);
await createSession();
}
await makeRequest("POST", $"api/wireguard/client/{clientId}/disable", withoutParametr, withoutParametr, out _);
}

Expand All @@ -181,6 +218,12 @@ public async Task BlockKey(string clientId) {
///<param name="clientId">Id of client</param>
///<param name="name">New name for key</param>
public async Task RenameKey(string clientId, string name) {
var status = await checkAuthrization();
if(!status.Authenticated) {
await makeRequest("POST", "api/session", "password", _password, out _);
await updateCookieContainer(cash);
await createSession();
}
await makeRequest("PUT", $"api/wireguard/client/{clientId}/name/", "name", name, out _);
}

Expand All @@ -190,6 +233,12 @@ public async Task RenameKey(string clientId, string name) {
///<param name="clientId">Id of client</param>
///<param name="address">IP Adress for connection</param>
public async Task SetNewIp(string clientId, string address) {
var status = await checkAuthrization();
if(!status.Authenticated) {
await makeRequest("POST", "api/session", "password", _password, out _);
await updateCookieContainer(cash);
await createSession();
}
await makeRequest("PUT", $"api/wireguard/client/{clientId}/address/", "address", address, out _);
}

Expand All @@ -199,6 +248,12 @@ public async Task SetNewIp(string clientId, string address) {
///<param name="clientId">Id of client</param>
///<param name="path">Path for saving config file</param>
public async Task DownloadConfig(string clientId, string path) {
var status = await checkAuthrization();
if(!status.Authenticated) {
await makeRequest("POST", "api/session", "password", _password, out _);
await updateCookieContainer(cash);
await createSession();
}
await makeRequest("POST", $"api/wireguard/client/{clientId}/configuration", out var data);
await File.WriteAllBytesAsync($"{path}/{clientId}.config", data);
}
Expand All @@ -209,6 +264,12 @@ public async Task DownloadConfig(string clientId, string path) {
///<param name="clientId">Id of client</param>
///<param name="path">Path for saving QR-Code in .svg</param>
public async Task DownloadQrCode(string clientId, string path) {
var status = await checkAuthrization();
if(!status.Authenticated) {
await makeRequest("POST", "api/session", "password", _password, out _);
await updateCookieContainer(cash);
await createSession();
}
await makeRequest("POST", $"api/wireguard/client/{clientId}/qrcode.svg", out var data);
await File.WriteAllBytesAsync($"{path}/{clientId}.svg", data);
}
Expand Down

0 comments on commit 2be83ad

Please sign in to comment.