diff --git a/README.md b/README.md
index 91a0783..5acda75 100644
--- a/README.md
+++ b/README.md
@@ -18,7 +18,7 @@ https://jitpack.io/#megoRU/wg-easy-wrapper
com.github.megoRU
wg-easy-wrapper
- v1.0
+ v1.1
```
diff --git a/pom.xml b/pom.xml
index d2d28c5..c6f0962 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
org.megoru
wg-easy-wrapper
- 1.0-SNAPSHOT
+ 1.1
11
diff --git a/src/main/java/org/megoru/impl/WgEasyAPI.java b/src/main/java/org/megoru/impl/WgEasyAPI.java
index 691edcd..e2a5d2f 100644
--- a/src/main/java/org/megoru/impl/WgEasyAPI.java
+++ b/src/main/java/org/megoru/impl/WgEasyAPI.java
@@ -3,12 +3,33 @@
import org.jetbrains.annotations.Nullable;
import org.megoru.entity.api.Clients;
import org.megoru.entity.api.Create;
-import org.megoru.entity.api.Status;
import org.megoru.entity.api.Session;
+import org.megoru.entity.api.Status;
import org.megoru.io.UnsuccessfulHttpException;
+import java.io.File;
+
public interface WgEasyAPI {
+
+ /**
+ * Get qr code config
+ *
+ * @param userId - it`s userId
+ * @param fileName - file name without file extension
+ * @return {@link File}
+ */
+ File getQRCode(String userId, String fileName) throws UnsuccessfulHttpException;
+
+ /**
+ * Get user config
+ *
+ * @param userId - it`s userId
+ * @param fileName - file name without file extension
+ * @return {@link File}
+ */
+ File getConfig(String userId, String fileName) throws UnsuccessfulHttpException;
+
/**
* Create user
*
diff --git a/src/main/java/org/megoru/impl/WgEasyAPIImpl.java b/src/main/java/org/megoru/impl/WgEasyAPIImpl.java
index 82a81a1..7efa983 100644
--- a/src/main/java/org/megoru/impl/WgEasyAPIImpl.java
+++ b/src/main/java/org/megoru/impl/WgEasyAPIImpl.java
@@ -25,8 +25,9 @@
import org.megoru.io.ResponseTransformer;
import org.megoru.io.UnsuccessfulHttpException;
-import java.io.IOException;
+import java.io.*;
import java.lang.reflect.Type;
+import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
@@ -90,6 +91,39 @@ public LocalDateTime deserialize(JsonElement json, Type type, JsonDeserializatio
setSession();
}
+ @Override
+ public File getQRCode(String userId, String fileName) throws UnsuccessfulHttpException {
+ HttpUrl url = baseUrl.newBuilder()
+ .addPathSegment("api")
+ .addPathSegment("wireguard")
+ .addPathSegment("client")
+ .addPathSegment(userId)
+ .addPathSegment("qrcode.svg")
+ .build();
+
+ HttpGet request = new HttpGet(url.uri());
+ request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
+
+ return execute(request, fileName + ".svg");
+ }
+
+ @Override
+ public File getConfig(String userId, String fileName) throws UnsuccessfulHttpException {
+ //https://vpn.megoru.ru/api/wireguard/client/83e7877e-9eea-4695-823e-b729cddb5d8c/configuration
+ HttpUrl url = baseUrl.newBuilder()
+ .addPathSegment("api")
+ .addPathSegment("wireguard")
+ .addPathSegment("client")
+ .addPathSegment(userId)
+ .addPathSegment("configuration")
+ .build();
+
+ HttpGet request = new HttpGet(url.uri());
+ request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
+
+ return execute(request, fileName + ".conf");
+ }
+
@Override
public Create createClient(String name) throws UnsuccessfulHttpException {
HttpUrl url = baseUrl.newBuilder()
@@ -307,6 +341,44 @@ private void setCookie(Cookie cookies) {
httpClient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
}
+ private File writeToFile(String text, String fileName) {
+ try {
+ int read;
+ File file = new File(fileName);
+ InputStream inputStream = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
+ final FileOutputStream fileOutputStream = new FileOutputStream(file);
+ while ((read = inputStream.read()) != -1) {
+ fileOutputStream.write(read);
+ }
+ fileOutputStream.flush();
+ fileOutputStream.close();
+
+ return file;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ private File execute(HttpRequestBase request, String fileName) throws UnsuccessfulHttpException {
+ try {
+ CloseableHttpResponse response = httpClient.execute(request);
+
+ HttpEntity entity = response.getEntity();
+ String body = EntityUtils.toString(entity);
+
+ if (response.getStatusLine().getStatusCode() == 200) {
+ return writeToFile(body, fileName);
+ }
+
+ throw new UnsuccessfulHttpException(response.getStatusLine().getStatusCode(), "Client Not Found");
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
@Nullable
private E execute(HttpRequestBase request, ResponseTransformer responseTransformer) throws UnsuccessfulHttpException {
try {