2023-03-02 08:42:34 +08:00
|
|
|
package coloryr.allmusic.core.music.api;
|
2020-02-20 23:22:58 +08:00
|
|
|
|
2023-03-02 08:42:34 +08:00
|
|
|
import coloryr.allmusic.core.AllMusic;
|
2024-04-17 22:15:12 +08:00
|
|
|
import coloryr.allmusic.core.objs.enums.EncryptType;
|
2023-03-02 08:42:34 +08:00
|
|
|
import coloryr.allmusic.core.objs.HttpResObj;
|
|
|
|
|
import coloryr.allmusic.core.objs.api.EncResObj;
|
2021-06-09 01:38:40 +08:00
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
|
import com.google.gson.JsonObject;
|
2021-04-03 15:55:25 +08:00
|
|
|
import okhttp3.*;
|
2020-02-20 23:22:58 +08:00
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.InputStream;
|
2020-03-02 12:15:58 +08:00
|
|
|
import java.net.URLEncoder;
|
2020-02-20 23:22:58 +08:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2022-01-03 15:27:38 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
2021-04-03 15:55:25 +08:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2020-02-20 23:22:58 +08:00
|
|
|
|
2021-03-19 15:32:47 +08:00
|
|
|
public class HttpClientUtil {
|
2020-12-23 23:40:34 +08:00
|
|
|
|
2021-04-03 15:55:25 +08:00
|
|
|
private static final int CONNECT_TIMEOUT = 5;
|
|
|
|
|
private static final int READ_TIMEOUT = 7;
|
2023-01-08 20:55:21 +08:00
|
|
|
private static OkHttpClient client;
|
2021-06-08 23:28:03 +08:00
|
|
|
|
2022-01-03 15:27:38 +08:00
|
|
|
public static void init() {
|
2021-01-23 12:43:40 +08:00
|
|
|
try {
|
2021-04-03 15:55:25 +08:00
|
|
|
synchronized (OkHttpClient.class) {
|
2022-01-03 15:27:38 +08:00
|
|
|
client = new OkHttpClient.Builder().cookieJar(new MyCookieJar())
|
2021-04-03 15:55:25 +08:00
|
|
|
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
|
|
|
|
|
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
|
|
|
|
|
.build();
|
2021-01-23 12:43:40 +08:00
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2020-12-23 23:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
2023-01-06 18:15:10 +08:00
|
|
|
public static HttpResObj get(String path, String data) {
|
2020-02-20 23:22:58 +08:00
|
|
|
try {
|
2020-03-02 12:15:58 +08:00
|
|
|
data = URLEncoder.encode(data, "UTF-8");
|
2021-04-03 15:55:25 +08:00
|
|
|
Request request = new Request.Builder().url(path + data)
|
2021-06-08 23:28:03 +08:00
|
|
|
.addHeader("referer", "https://music.163.com")
|
|
|
|
|
.addHeader("content-type", "application/json;charset=UTF-8")
|
|
|
|
|
.addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41")
|
|
|
|
|
.get()
|
2021-04-03 15:55:25 +08:00
|
|
|
.build();
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
int httpCode = response.code();
|
2021-06-08 23:28:03 +08:00
|
|
|
ResponseBody body = response.body();
|
|
|
|
|
if (body == null) {
|
2024-04-17 22:15:12 +08:00
|
|
|
AllMusic.log.warning("§d[AllMusic3]§c获取网页错误");
|
2021-06-08 23:28:03 +08:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
InputStream inputStream = body.byteStream();
|
|
|
|
|
boolean ok = httpCode == 200;
|
|
|
|
|
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
|
int length;
|
|
|
|
|
while ((length = inputStream.read(buffer)) != -1) {
|
|
|
|
|
result.write(buffer, 0, length);
|
|
|
|
|
}
|
|
|
|
|
String data1 = result.toString(StandardCharsets.UTF_8.name());
|
|
|
|
|
if (!ok) {
|
2024-04-17 22:15:12 +08:00
|
|
|
AllMusic.log.warning("§d[AllMusic3]§c服务器返回错误:" + data1);
|
2021-06-08 23:28:03 +08:00
|
|
|
}
|
2023-01-06 18:15:10 +08:00
|
|
|
return new HttpResObj(data1, ok);
|
2021-06-08 23:28:03 +08:00
|
|
|
} catch (Exception e) {
|
2024-04-17 22:15:12 +08:00
|
|
|
AllMusic.log.warning("§d[AllMusic3]§c获取网页错误");
|
2021-06-08 23:28:03 +08:00
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 18:15:10 +08:00
|
|
|
public static HttpResObj post(String url, JsonObject data, EncryptType type, String ourl) {
|
2021-06-08 23:28:03 +08:00
|
|
|
try {
|
|
|
|
|
RequestBody formBody;
|
|
|
|
|
Request.Builder request = new Request.Builder();
|
|
|
|
|
request = request.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
|
request = request.addHeader("Referer", "https://music.163.com");
|
2023-01-06 18:15:10 +08:00
|
|
|
EncResObj res;
|
2021-06-08 23:28:03 +08:00
|
|
|
List<Cookie> cookies = new ArrayList<>();
|
2021-11-30 13:37:43 +08:00
|
|
|
if (AllMusic.cookie.cookieStore.containsKey("music.163.com")) {
|
|
|
|
|
cookies = AllMusic.cookie.cookieStore.get("music.163.com");
|
2021-06-08 23:28:03 +08:00
|
|
|
}
|
2022-01-03 15:27:38 +08:00
|
|
|
if (type == EncryptType.WEAPI) {
|
2021-06-08 23:28:03 +08:00
|
|
|
request = request.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/13.10586");
|
|
|
|
|
String csrfToken = "";
|
2022-01-03 15:27:38 +08:00
|
|
|
for (Cookie item : cookies) {
|
|
|
|
|
if (item.name().equalsIgnoreCase("__csrf")) {
|
|
|
|
|
csrfToken = item.value();
|
2021-06-08 23:28:03 +08:00
|
|
|
}
|
2022-01-03 15:27:38 +08:00
|
|
|
}
|
2021-06-09 01:38:40 +08:00
|
|
|
data.addProperty("csrf_token", csrfToken);
|
|
|
|
|
res = CryptoUtil.weapiEncrypt(AllMusic.gson.toJson(data));
|
2021-06-08 23:28:03 +08:00
|
|
|
url = url.replaceFirst("\\w*api", "weapi");
|
|
|
|
|
request = request.url(url);
|
|
|
|
|
formBody = new FormBody.Builder()
|
|
|
|
|
.add("params", res.params)
|
|
|
|
|
.add("encSecKey", res.encSecKey)
|
|
|
|
|
.build();
|
2022-01-03 15:27:38 +08:00
|
|
|
} else if (type == EncryptType.EAPI) {
|
2021-06-08 23:28:03 +08:00
|
|
|
request = request.addHeader("User-Agent", "Mozilla/5.0 (Linux; Android 9; PCT-AL10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.311 Mobile Safari/537.36");
|
2021-06-09 01:38:40 +08:00
|
|
|
JsonObject header = new JsonObject();
|
2023-11-23 21:08:17 +08:00
|
|
|
header.addProperty("appver", "8.10.90");
|
2021-06-09 01:38:40 +08:00
|
|
|
header.addProperty("versioncode", "140");
|
|
|
|
|
header.addProperty("buildver", new Date().toString().substring(0, 10));
|
|
|
|
|
header.addProperty("resolution", "1920x1080");
|
|
|
|
|
header.addProperty("os", "android");
|
2021-06-08 23:28:03 +08:00
|
|
|
String requestId = "0000" + (new Date() + "_" + Math.floor(Math.random() * 1000));
|
2021-06-09 01:38:40 +08:00
|
|
|
header.addProperty("requestId", requestId);
|
2021-06-08 23:28:03 +08:00
|
|
|
for (Cookie item : cookies) {
|
|
|
|
|
if (item.name().equalsIgnoreCase("MUSIC_U")) {
|
2021-06-09 01:38:40 +08:00
|
|
|
header.addProperty("MUSIC_U", item.value());
|
2021-06-08 23:28:03 +08:00
|
|
|
} else if (item.name().equalsIgnoreCase("MUSIC_A")) {
|
2021-06-09 01:38:40 +08:00
|
|
|
header.addProperty("MUSIC_A", item.value());
|
2021-06-08 23:28:03 +08:00
|
|
|
} else if (item.name().equalsIgnoreCase("channel")) {
|
2021-06-09 01:38:40 +08:00
|
|
|
header.addProperty("channel", item.value());
|
2021-06-08 23:28:03 +08:00
|
|
|
} else if (item.name().equalsIgnoreCase("mobilename")) {
|
2021-06-09 01:38:40 +08:00
|
|
|
header.addProperty("mobilename", item.value());
|
2021-06-08 23:28:03 +08:00
|
|
|
} else if (item.name().equalsIgnoreCase("osver")) {
|
2021-06-09 01:38:40 +08:00
|
|
|
header.addProperty("osver", item.value());
|
2021-06-08 23:28:03 +08:00
|
|
|
} else if (item.name().equalsIgnoreCase("__csrf")) {
|
2021-06-09 01:38:40 +08:00
|
|
|
header.addProperty("__csrf", item.value());
|
2021-06-08 23:28:03 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 01:38:40 +08:00
|
|
|
data.add("header", header);
|
2021-06-08 23:28:03 +08:00
|
|
|
res = CryptoUtil.eapi(ourl, data);
|
|
|
|
|
url = url.replaceFirst("\\w*api", "eapi");
|
|
|
|
|
request = request.url(url);
|
|
|
|
|
formBody = new FormBody.Builder()
|
|
|
|
|
.add("params", res.params)
|
|
|
|
|
.build();
|
2022-01-03 15:27:38 +08:00
|
|
|
} else {
|
2021-06-08 23:28:03 +08:00
|
|
|
request = request.url(url);
|
|
|
|
|
FormBody.Builder builder = new FormBody.Builder();
|
2021-06-09 01:38:40 +08:00
|
|
|
for (Map.Entry<String, JsonElement> item : data.entrySet()) {
|
|
|
|
|
builder = builder.add(item.getKey(), item.getValue().getAsString());
|
2021-06-08 23:28:03 +08:00
|
|
|
}
|
|
|
|
|
formBody = builder.build();
|
|
|
|
|
}
|
|
|
|
|
request = request.post(formBody);
|
|
|
|
|
Response response = client.newCall(request.build()).execute();
|
|
|
|
|
int httpCode = response.code();
|
2021-04-03 15:55:25 +08:00
|
|
|
ResponseBody body = response.body();
|
|
|
|
|
if (body == null) {
|
2024-04-17 22:15:12 +08:00
|
|
|
AllMusic.log.warning("§d[AllMusic3]§c获取网页错误");
|
2021-04-03 15:55:25 +08:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
InputStream inputStream = body.byteStream();
|
|
|
|
|
boolean ok = httpCode == 200;
|
2020-02-20 23:22:58 +08:00
|
|
|
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
2021-03-19 15:32:47 +08:00
|
|
|
byte[] buffer = new byte[4096];
|
2020-02-20 23:22:58 +08:00
|
|
|
int length;
|
|
|
|
|
while ((length = inputStream.read(buffer)) != -1) {
|
|
|
|
|
result.write(buffer, 0, length);
|
|
|
|
|
}
|
2020-03-11 18:10:14 +08:00
|
|
|
String data1 = result.toString(StandardCharsets.UTF_8.name());
|
|
|
|
|
if (!ok) {
|
2024-04-17 22:15:12 +08:00
|
|
|
AllMusic.log.warning("§d[AllMusic3]§c服务器返回错误:" + data1);
|
2020-03-11 18:10:14 +08:00
|
|
|
}
|
2023-01-06 18:15:10 +08:00
|
|
|
return new HttpResObj(data1, ok);
|
2020-03-02 12:15:58 +08:00
|
|
|
} catch (Exception e) {
|
2024-04-17 22:15:12 +08:00
|
|
|
AllMusic.log.warning("§d[AllMusic3]§c获取网页错误");
|
2020-02-20 23:22:58 +08:00
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|