This commit is contained in:
aliveoutside
2022-03-24 19:37:43 +03:00
commit aa972fa5b3
37643 changed files with 6680801 additions and 0 deletions
@@ -0,0 +1,87 @@
package ru.f0x1d.net;
import android.os.AsyncTask;
import com.vk.core.network.Network;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import ru.vtosters.lite.utils.Helper;
public class F0x1dRequest {
public interface F0x1dRequestCallback {
void onResponse(String str);
}
public static void makeRequest(byte[] bArr, F0x1dRequestCallback f0x1dRequestCallback) {
new MakeRequestTask(bArr, f0x1dRequestCallback).execute();
}
public static void makeRequest(String str, F0x1dRequestCallback f0x1dRequestCallback) {
new MakeRequestTask(str.getBytes(), f0x1dRequestCallback).execute();
}
public static void makeRequestToaster(String str, F0x1dRequestCallback f0x1dRequestCallback, boolean z) {
new MakeRequestTask(str.getBytes(), f0x1dRequestCallback, true, !z).execute();
}
private static class MakeRequestTask extends AsyncTask<Void, Void, String> {
private F0x1dRequestCallback callback;
private boolean mIsPut;
private boolean mIsVkToasterRequest;
private byte[] url;
public MakeRequestTask(byte[] bArr, F0x1dRequestCallback f0x1dRequestCallback) {
this.mIsVkToasterRequest = false;
this.mIsPut = false;
this.callback = f0x1dRequestCallback;
this.url = bArr;
}
public MakeRequestTask(byte[] bArr, F0x1dRequestCallback f0x1dRequestCallback, boolean z, boolean z2) {
this.mIsVkToasterRequest = false;
this.mIsPut = false;
this.callback = f0x1dRequestCallback;
this.url = bArr;
this.mIsVkToasterRequest = z;
this.mIsPut = z2;
}
/* JADX INFO: Access modifiers changed from: protected */
public String doInBackground(Void... voidArr) {
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(new String(this.url)).openConnection();
httpURLConnection.setRequestMethod(this.mIsPut ? "PUT" : "GET");
if (this.mIsVkToasterRequest) {
httpURLConnection.setRequestProperty("Token", Helper.GetUserToken());
}
if (!this.mIsVkToasterRequest) {
httpURLConnection.setRequestProperty("User-Agent", Network.a.a().a());
}
httpURLConnection.setRequestProperty("Content-Type", this.mIsVkToasterRequest ? "Content-Type" : "application/x-www-form-urlencoded");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
while (true) {
String readLine = bufferedReader.readLine();
if (readLine != null) {
stringBuffer.append(readLine);
} else {
bufferedReader.close();
return stringBuffer.toString();
}
}
} catch (Exception e) {
return e.getLocalizedMessage();
}
}
public void onPostExecute(String str) {
super.onPostExecute(str);
this.callback.onResponse(str);
}
}
}
@@ -0,0 +1,96 @@
package ru.f0x1d.net;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.TreeMap;
import android.util.Base64;
public final class NetCall {
private NetRequest req;
private NetClient cl;
protected NetCall(NetClient cl, NetRequest req) {
this.req = req;
this.cl = cl;
}
public NetRequest request() {
return req;
}
public NetResponse execute() throws IOException {
Proxy proxy = cl.getProxy();
PasswordAuthentication auth = cl.getAuthenticator();
HttpURLConnection con = (HttpURLConnection) new URL(req.url()).openConnection(proxy != null ? proxy : Proxy.NO_PROXY);
if (proxy != null && auth != null) {
String authStr = "Basic " + Base64.encodeToString((auth.getUserName() + new String(auth.getPassword())).getBytes(), Base64.NO_WRAP);
con.setRequestProperty("Proxy-Connection", "Keep-Alive");
con.setRequestProperty("Proxy-Authorization", authStr);
}
con.setConnectTimeout((int) cl.getTimeout());
con.setRequestMethod(req.getRequestMethod());
if (req.getRequestMethod().equals("POST")) {
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
}
Map<String, String> params = req.getRequestParams();
if (params != null) {
String strArgs = constructArgs(params);
con.setRequestProperty("Content-Length", String.valueOf(strArgs.getBytes(StandardCharsets.UTF_8).length));
con.setDoOutput(true);
con.getOutputStream().write(strArgs.getBytes(StandardCharsets.UTF_8));
con.getOutputStream().close();
}
con.connect();
InputStream in;
if (con.getResponseCode() == 200) in = con.getInputStream();
else in = con.getErrorStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 1024];
int c;
while ((c = in.read(buffer)) != -1) bos.write(buffer, 0, c);
in.close();
bos.close();
NetResponse r = new NetResponse(bos.toByteArray());
r.setCode(con.getResponseCode());
return r;
}
private static String constructArgs(Map<String, String> args) throws UnsupportedEncodingException {
args = new TreeMap<>(args);
String str = "";
for (Map.Entry<String, String> en : args.entrySet()) {
if (!str.isEmpty()) str += "&";
str += URLEncoder.encode(en.getKey(), "UTF-8") + "=" + URLEncoder.encode(en.getValue(), "UTF-8");
}
return str;
}
public void enqueue(NetCallback cb) {
new Thread() {
@Override
public void run() {
try {
NetResponse resp = execute();
cb.onResponse(NetCall.this, resp);
} catch (IOException e) {
cb.onFailure(NetCall.this, e);
}
try {
interrupt();
join();
} catch (Exception e) {
}
}
}.start();
}
}
@@ -0,0 +1,9 @@
package ru.f0x1d.net;
import java.io.IOException;
public interface NetCallback {
void onFailure(NetCall netCall, IOException iOException);
void onResponse(NetCall netCall, NetResponse netResponse) throws IOException;
}
@@ -0,0 +1,87 @@
package ru.f0x1d.net;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;
public class NetClient {
private long timeout;
private Proxy proxy;
private PasswordAuthentication authenticator;
public static final class Builder {
private long timeout;
private Proxy proxy;
private PasswordAuthentication auth;
public Builder connectTimeout(long l, TimeUnit unit) {
int m;
switch (unit) {
case SECONDS:
m = 1000;
break;
case MILLISECONDS:
m = 1;
break;
case HOURS:
m = 1000 * 60 * 60;
break;
case DAYS:
m = 1000 * 60 * 60 * 24;
break;
case MINUTES:
m = 1000 * 60;
break;
default:
m = 1;
break;
}
timeout = l * m;
return this;
}
public Builder proxyAuthenticator(PasswordAuthentication auth) {
this.auth = auth;
return this;
}
public Builder proxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
public NetClient build() {
return new NetClient(this);
}
}
public Builder newBuilder() {
Builder b = new Builder();
b.timeout = timeout;
b.proxy = proxy;
b.auth = authenticator;
return b;
}
protected NetClient(Builder b) {
timeout = b.timeout;
proxy = b.proxy;
authenticator = b.auth;
}
public NetCall newCall(NetRequest req) {
return new NetCall(this, req);
}
public PasswordAuthentication getAuthenticator() {
return authenticator;
}
public Proxy getProxy() {
return proxy;
}
public long getTimeout() {
return timeout;
}
}
+64
View File
@@ -0,0 +1,64 @@
package ru.f0x1d.net;
import java.util.Map;
import java.util.TreeMap;
public class NetRequest {
private String url;
private String requestMethod;
private Map<String, String> params;
public static final class Builder {
private String url;
private String req = "GET";
private Map<String, String> params;
public NetRequest build() {
return new NetRequest(this);
}
public Builder url(String url) {
this.url = url;
return this;
}
public Builder get() {
req = "GET";
return this;
}
public Builder post() {
req = "POST";
return this;
}
public Builder params(Map<String, String> params) {
this.params = params;
return this;
}
public Builder param(String param, String value) {
if (params == null) params = new TreeMap<>();
params.put(param, value);
return this;
}
}
protected NetRequest(Builder b) {
url = b.url;
requestMethod = b.req;
params = b.params;
}
public Map<String, String> getRequestParams() {
return params;
}
public String url() {
return url;
}
public String getRequestMethod() {
return requestMethod;
}
}
+38
View File
@@ -0,0 +1,38 @@
package ru.f0x1d.net;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public final class NetResponse {
private byte[] data;
private int code;
protected NetResponse(byte[] data) {
this.data = data;
}
protected void setCode(int code) {
this.code = code;
}
public boolean isSuccessful() {
return code() == 200;
}
public int code() {
return code;
}
public byte[] getData() {
return data;
}
public InputStream getDataStream() {
return new ByteArrayInputStream(data);
}
public String getDataString() {
return new String(data, StandardCharsets.UTF_8);
}
}