Skip to content

Commit

Permalink
New update
Browse files Browse the repository at this point in the history
1. Add QR CODE
2. Add File.conf
  • Loading branch information
megoRU committed Jan 14, 2023
1 parent 6ca1bcc commit 6b0a7fb
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ https://jitpack.io/#megoRU/wg-easy-wrapper
<dependency>
<groupId>com.github.megoRU</groupId>
<artifactId>wg-easy-wrapper</artifactId>
<version>v1.0</version>
<version>v1.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.megoru</groupId>
<artifactId>wg-easy-wrapper</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/org/megoru/impl/WgEasyAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
74 changes: 73 additions & 1 deletion src/main/java/org/megoru/impl/WgEasyAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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> E execute(HttpRequestBase request, ResponseTransformer<E> responseTransformer) throws UnsuccessfulHttpException {
try {
Expand Down

0 comments on commit 6b0a7fb

Please sign in to comment.