Skip to content

Commit

Permalink
New update v2.3
Browse files Browse the repository at this point in the history
1. Add HTTP/2
2. Now getSessions not included password GET request
  • Loading branch information
megoRU committed Apr 23, 2023
1 parent c893dfa commit 96ce329
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
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>2.2</version>
<version>2.3</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/megoru/impl/WgEasyAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class Builder {
private String domain;
private int port;
private String ip;
private boolean http2;

/**
* This enables LOGS
Expand All @@ -136,6 +137,11 @@ public Builder ip(String ip) {
return this;
}

public Builder enableHTTP2() {
this.http2 = true;
return this;
}

/**
* @param domain It`s domain address with out https://. Example: vpn.megoru.ru
*/
Expand Down Expand Up @@ -163,10 +169,10 @@ public WgEasyAPI build() {
throw new IllegalArgumentException("The provided ip and domain cannot be null!");

if (domain != null && ip == null)
return new WgEasyAPIImpl(password, domain, devMode);
return new WgEasyAPIImpl(password, domain, devMode, http2);

if (port > 0)
return new WgEasyAPIImpl(password, ip, port, devMode);
return new WgEasyAPIImpl(password, ip, port, devMode, http2);

throw new IllegalArgumentException("You a made error");
}
Expand Down
56 changes: 41 additions & 15 deletions src/main/java/org/megoru/impl/WgEasyAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.cookie.Cookie;
import org.apache.hc.client5.http.cookie.CookieStore;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.*;
import org.apache.hc.core5.http.io.entity.EntityUtils;
Expand All @@ -43,18 +41,18 @@
import java.time.temporal.TemporalAccessor;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class WgEasyAPIImpl implements WgEasyAPI {

private final HttpUrl baseUrl;

private final Gson gson;
private CloseableHttpClient httpClient = HttpClients.createDefault();
private final BasicHttpContext basicHttpContext = new BasicHttpContext();
private final String password;
private final boolean devMode;

protected WgEasyAPIImpl(String password, String domain, boolean devMode) {
protected WgEasyAPIImpl(String password, String domain, boolean devMode, boolean http2) {
this.devMode = devMode;
this.password = password;

Expand All @@ -75,10 +73,18 @@ public LocalDateTime deserialize(JsonElement json, Type type, JsonDeserializatio

}).setPrettyPrinting().create();

ProtocolVersion protocolVersion;
if (http2) {
protocolVersion = new ProtocolVersion("HTTP", 2, 0);
} else {
protocolVersion = new ProtocolVersion("HTTP", 1, 1);
}
basicHttpContext.setProtocolVersion(protocolVersion);

setSession();
}

protected WgEasyAPIImpl(String password, String ip, int port, boolean devMode) {
protected WgEasyAPIImpl(String password, String ip, int port, boolean devMode, boolean http2) {
this.devMode = devMode;
this.password = password;

Expand All @@ -100,6 +106,14 @@ public LocalDateTime deserialize(JsonElement json, Type type, JsonDeserializatio

}).setPrettyPrinting().create();

ProtocolVersion protocolVersion;
if (http2) {
protocolVersion = new ProtocolVersion("HTTP", 2, 0);
} else {
protocolVersion = new ProtocolVersion("HTTP", 1, 1);
}
basicHttpContext.setProtocolVersion(protocolVersion);

setSession();
}

Expand All @@ -120,7 +134,7 @@ public File getQRCode(String userId, String fileName) {
}

@Override
public File getConfig(String userId, String fileName) {
public File getConfig(String userId, String fileName) {
//https://vpn.megoru.ru/api/wireguard/client/83e7877e-9eea-4695-823e-b729cddb5d8c/configuration
HttpUrl url = baseUrl.newBuilder()
.addPathSegment("api")
Expand Down Expand Up @@ -156,7 +170,7 @@ public Create createClient(String name) {
}

@Override
public Status updateClientAddress(String userId, String address) {
public Status updateClientAddress(String userId, String address) {
HttpUrl url = baseUrl.newBuilder()
.addPathSegment("api")
.addPathSegment("wireguard")
Expand All @@ -167,7 +181,6 @@ public Status updateClientAddress(String userId, String address) {

JSONObject json = new JSONObject();


try {
json.put("address ", address);
} catch (JSONException e) {
Expand All @@ -193,7 +206,7 @@ public Status disableClient(String userId) {
}

@Override
public Status enableClient(String userId) {
public Status enableClient(String userId) {
HttpUrl url = baseUrl.newBuilder()
.addPathSegment("api")
.addPathSegment("wireguard")
Expand All @@ -208,7 +221,7 @@ public Status enableClient(String userId) {
}

@Override
public Status deleteClient(String userId) {
public Status deleteClient(String userId) {
HttpUrl url = baseUrl.newBuilder()
.addPathSegment("api")
.addPathSegment("wireguard")
Expand Down Expand Up @@ -269,7 +282,7 @@ public Client getClientByName(String name) throws IllegalStateException, NullPoi
}

@Override
public Client[] getClients() {
public Client[] getClients() {
HttpUrl url = baseUrl.newBuilder()
.addPathSegment("api")
.addPathSegment("wireguard")
Expand All @@ -284,7 +297,6 @@ public Session getSession() {
HttpUrl url = baseUrl.newBuilder()
.addPathSegment("api")
.addPathSegment("session")
.addQueryParameter("password", password)
.build();

return get(url, new DefaultResponseTransformer<>(Session.class, gson));
Expand Down Expand Up @@ -326,12 +338,14 @@ private <E> E post(HttpUrl url, JSONObject jsonBody, ResponseTransformer<E> resp

HttpEntity stringEntity = new StringEntity(jsonBody.toString(), ContentType.APPLICATION_JSON);
request.setEntity(stringEntity);

return execute(request, responseTransformer);
}

private <E> E delete(HttpUrl url, ResponseTransformer<E> responseTransformer) {
HttpDelete request = new HttpDelete(url.uri());
request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");

return execute(request, responseTransformer);
}

Expand All @@ -348,7 +362,11 @@ private <E> E put(HttpUrl url, JSONObject jsonBody, ResponseTransformer<E> respo
private void setCookie(Cookie cookies) {
BasicCookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookies);
httpClient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();

httpClient = HttpClientBuilder
.create()
.setDefaultCookieStore(cookieStore)
.build();
}

private File writeToFile(String text, String fileName) {
Expand Down Expand Up @@ -400,6 +418,11 @@ private void execute(ClassicHttpRequest request) {
CookieStore cookieStore = context.getCookieStore();
List<Cookie> cookie = cookieStore.getCookies();

if (devMode) {
System.out.println("cookie: " + context.getResponse().getVersion());
System.out.println(Arrays.toString(context.getResponse().getHeaders()));
}

if (cookie.isEmpty()) throw new RuntimeException("Cookie is null");
else setCookie(cookie.get(0));
}
Expand All @@ -410,7 +433,7 @@ private void execute(ClassicHttpRequest request) {

private File execute(ClassicHttpRequest request, String fileName, FileExtension fileExtension) {
try {
return httpClient.execute(request, new BasicHttpContext(), httpResponse -> {
return httpClient.execute(request, basicHttpContext, httpResponse -> {
HttpEntity entity = httpResponse.getEntity();
String body = EntityUtils.toString(entity);
if (httpResponse.getCode() == 200 && fileExtension.equals(FileExtension.CONFIG)) {
Expand All @@ -433,7 +456,7 @@ private File execute(ClassicHttpRequest request, String fileName, FileExtension
@Nullable
private <E> E execute(ClassicHttpRequest request, ResponseTransformer<E> responseTransformer) {
try {
return httpClient.execute(request, new BasicHttpContext(), httpResponse -> {
return httpClient.execute(request, basicHttpContext, httpResponse -> {
HttpEntity entity = httpResponse.getEntity();
String body = "{}";
if (entity != null) {
Expand Down Expand Up @@ -495,6 +518,9 @@ private void logResponse(ClassicHttpResponse response, String body) {
return;
}

System.out.println("Response: " + response.getVersion());
System.out.println(Arrays.toString(response.getHeaders()));

String status = String.format(
"StatusCode: %s Reason: %s",
response.getCode(),
Expand Down

0 comments on commit 96ce329

Please sign in to comment.