Skip to content
This repository was archived by the owner on Nov 1, 2025. It is now read-only.

Commit 6a59971

Browse files
authored
Fix methods DownloadConfig() & DownloadQrCode()
And new cookie session system for modern .NET
1 parent ac160e8 commit 6a59971

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/WgEasyManager.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,24 @@ public class WgEasyClient {
3131
///<param name="hasSsl">If you have SSL - we recommend set <b>true</b>, false - if you hasn't SSL</param>
3232
public WgEasyClient(string serverUrl, string password, bool hasSsl) {
3333
_password = password;
34-
_serverUrl = serverUrl;
34+
_serverUrl = serverUrl.ToLower();
3535
HasSsl = hasSsl;
36-
_session_file = "wg-sessions/" + serverUrl + "_server" + ".wgmanager";
36+
_session_file = "wg-sessions/" + ConvertToUrlOrIp(_serverUrl) + "_server" + ".wgmanager";
3737
loadingSession();
3838
}
3939

40+
private string ConvertToUrlOrIp(string url) {
41+
if(url.StartsWith("https://") || url.StartsWith("http://")) {
42+
if(url.StartsWith("https://")) {
43+
return url.Split("https://")[1];
44+
}
45+
else {
46+
return url.Split("http://")[1];
47+
}
48+
}
49+
return url;
50+
}
51+
4052
private Task makeRequest(string method, string urlMethod, string key, string value, out string data) {
4153
try {
4254
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(_serverUrl + "/" + urlMethod);
@@ -101,27 +113,24 @@ private Task makeRequest(string method, string urlMethod, out byte[] data) {
101113
}
102114

103115
private Task createSession() {
116+
if(!Directory.Exists("wg-sessions"))
117+
Directory.CreateDirectory("wg-sessions");
118+
104119
if(!File.Exists(_session_file)) {
105-
using(FileStream stream = File.Create(_session_file)) {
106-
var session = JsonConvert.SerializeObject(_cookies);
107-
File.WriteAllText(_session_file, session);
108-
}
120+
File.Create(_session_file).Dispose();
121+
var session = JsonConvert.SerializeObject(_cookies.GetCookies(new Uri(_serverUrl)));
122+
File.WriteAllText(_session_file, session);
109123
}
110124
else {
111-
File.Delete(_session_file);
112-
using(FileStream stream = File.Create(_session_file)) {
113-
var session = JsonConvert.SerializeObject(_cookies);
114-
File.WriteAllText(_session_file, session);
115-
}
125+
var session = JsonConvert.SerializeObject(_cookies.GetCookies(new Uri(_serverUrl)));
126+
File.WriteAllText(_session_file, session);
116127
}
117128
return Task.CompletedTask;
118129
}
119130
private Task loadingSession() {
120131
if(File.Exists(_session_file)) {
121-
using(Stream stream = File.OpenRead(_session_file)) {
122-
var session = File.ReadAllText(_session_file);
123-
_cookies = JsonConvert.DeserializeObject<CookieContainer>(session);
124-
}
132+
var session = File.ReadAllText(_session_file);
133+
updateCookieContainer(JsonConvert.DeserializeObject<CookieCollection>(session));
125134
}
126135
return Task.CompletedTask;
127136
}
@@ -275,7 +284,9 @@ public async Task DownloadConfig(string clientId, string path) {
275284
await updateCookieContainer(cash);
276285
await createSession();
277286
}
278-
await makeRequest("POST", $"api/wireguard/client/{clientId}/configuration", out var data);
287+
if(!Directory.Exists(path))
288+
Directory.CreateDirectory(path);
289+
await makeRequest("GET", $"api/wireguard/client/{clientId}/configuration", out var data);
279290
await File.WriteAllBytesAsync($"{path}/{clientId}.conf", data);
280291
}
281292

@@ -291,7 +302,9 @@ public async Task DownloadQrCode(string clientId, string path) {
291302
await updateCookieContainer(cash);
292303
await createSession();
293304
}
294-
await makeRequest("POST", $"api/wireguard/client/{clientId}/qrcode.svg", out var data);
305+
await makeRequest("GET", $"api/wireguard/client/{clientId}/qrcode.svg", out var data);
306+
if(!Directory.Exists(path))
307+
Directory.CreateDirectory(path);
295308
await File.WriteAllBytesAsync($"{path}/{clientId}.svg", data);
296309
}
297310
}

0 commit comments

Comments
 (0)