完善刷新cookie功能
修复ffmpeg下载一大堆的问题 尝试处理了SQLite的内存泄露问题(待确定)
This commit is contained in:
@@ -37,4 +37,11 @@ public interface LoginApi {
|
||||
, @Field("source") String source
|
||||
, @Field("refresh_token") String refresh_token
|
||||
);
|
||||
@POST("/x/passport-login/web/confirm/refresh")
|
||||
@FormUrlEncoded
|
||||
Call<HttpBody<LoginInfoBean>> confirmRefreshCookie(
|
||||
@Field("csrf") String refresh_csrf
|
||||
, @Field("refresh_token") String old_token
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,26 +1,32 @@
|
||||
package com.yutou.biliapi.net;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.yutou.biliapi.api.LoginApi;
|
||||
import com.yutou.biliapi.bean.login.LoginCookieDatabaseBean;
|
||||
import com.yutou.biliapi.bean.login.LoginInfoBean;
|
||||
import com.yutou.biliapi.databases.BiliBiliLoginDatabase;
|
||||
import com.yutou.common.okhttp.FileBody;
|
||||
import com.yutou.common.okhttp.HttpBody;
|
||||
import com.yutou.common.okhttp.HttpLoggingInterceptor;
|
||||
import com.yutou.common.utils.ConfigTools;
|
||||
import com.yutou.common.utils.Log;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import retrofit2.Response;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.OAEPParameterSpec;
|
||||
import javax.crypto.spec.PSource;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.MGF1ParameterSpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.Base64;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class BiliCookieManager {
|
||||
public static final int COOKIE_INVALID = -101;
|
||||
@@ -66,7 +72,7 @@ public class BiliCookieManager {
|
||||
}
|
||||
|
||||
private static String getRefreshCsrf(String htmlContent) {
|
||||
htmlContent=htmlContent.split("<div id=\"1-name\">")[1].split("</div>")[0];
|
||||
htmlContent = htmlContent.split("<div id=\"1-name\">")[1].split("</div>")[0];
|
||||
return htmlContent;
|
||||
}
|
||||
|
||||
@@ -74,28 +80,65 @@ public class BiliCookieManager {
|
||||
try {
|
||||
LoginApi api = BiliLoginNetApiManager.getInstance().getLoginApi(cookie.getDedeUserID());
|
||||
String correspondPath = getCorrespondPath(String.format("refresh_%d", System.currentTimeMillis()));
|
||||
System.out.println("correspondPath = " + correspondPath);
|
||||
Thread.sleep(300);
|
||||
Response<FileBody<String>> body = api.getCorrespond(correspondPath).execute();
|
||||
if(body.code()==404){
|
||||
System.out.println("返回404");
|
||||
return false;
|
||||
}
|
||||
System.out.println("body.code() = " + body.code());
|
||||
String string = IOUtils.toString(body.body().getInputStream(), StandardCharsets.UTF_8);
|
||||
String refreshCsrf = getRefreshCsrf(string);
|
||||
System.out.println("body = " + refreshCsrf);
|
||||
Log.i("correspondPath = " + correspondPath);
|
||||
var body = okhttp(correspondPath, cookie);
|
||||
String refreshCsrf = getRefreshCsrf(body);
|
||||
Log.i("body = " + refreshCsrf);
|
||||
String oldCsrf = cookie.getRefreshToken();
|
||||
Response<HttpBody<LoginInfoBean>> newCookie = api.refreshCookie(cookie.getBiliJct(), refreshCsrf, "main_web", cookie.getRefreshToken()).execute();
|
||||
LoginCookieDatabaseBean nc = BiliLoginNetApiManager.getInstance().getCookie(newCookie.headers(), newCookie.body());
|
||||
nc.setGourl(cookie.getGourl());
|
||||
nc.setSql_time(cookie.getSql_time());
|
||||
BiliBiliLoginDatabase.getInstance().updateLoginCookie(nc);
|
||||
System.out.println("返回正确");
|
||||
api.confirmRefreshCookie(refreshCsrf, oldCsrf).execute();
|
||||
Log.i("返回正确");
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
Log.e(e);
|
||||
System.out.println("返回错误");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String okhttp(String correspondPath, LoginCookieDatabaseBean cookie) {
|
||||
JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(cookie));
|
||||
StringBuilder ck = new StringBuilder();
|
||||
json.remove("sql_time");
|
||||
json.remove("gourl");
|
||||
json.remove("Expires");
|
||||
json.remove("path");
|
||||
json.remove("refresh_token");
|
||||
json.put("SESSDATA", URLDecoder.decode(json.getString("SESSDATA"), StandardCharsets.UTF_8));
|
||||
|
||||
// 构建Cookie字符串,去掉最后一个分号
|
||||
Iterator<String> keys = json.keySet().iterator();
|
||||
while (keys.hasNext()) {
|
||||
String key = keys.next();
|
||||
ck.append(key).append("=").append(json.getString(key));
|
||||
if (keys.hasNext()) {
|
||||
ck.append(";");
|
||||
}
|
||||
}
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url("https://www.bilibili.com/correspond/1/" + correspondPath)
|
||||
.method("GET", null) // GET请求不带body
|
||||
.addHeader("Cookie", ck.toString())
|
||||
.addHeader("User-Agent", ConfigTools.getUserAgent())
|
||||
.addHeader("Accept", "*/*")
|
||||
.addHeader("Host", "www.bilibili.com")
|
||||
.addHeader("Connection", "keep-alive")
|
||||
.build();
|
||||
|
||||
try {
|
||||
okhttp3.Response execute = client.newCall(request).execute();
|
||||
while (execute.code() == 404) {
|
||||
execute = client.newCall(request).execute();
|
||||
}
|
||||
return IOUtils.toString(execute.body().byteStream(), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to execute HTTP request", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,11 +23,9 @@ public class BiliLoginNetApiManager extends BaseApi {
|
||||
public static final int LOGIN_SUCCESS = 101;
|
||||
|
||||
private static BiliLoginNetApiManager instance;
|
||||
LoginApi loginApi;
|
||||
|
||||
private BiliLoginNetApiManager() {
|
||||
super("https://passport.bilibili.com");
|
||||
loginApi = createApi(LoginApi.class);
|
||||
}
|
||||
|
||||
public static BiliLoginNetApiManager getInstance() {
|
||||
@@ -48,11 +46,11 @@ public class BiliLoginNetApiManager extends BaseApi {
|
||||
useCookie(JSONObject.parseObject(JSONObject.toJSONString(cookie)));
|
||||
}
|
||||
}
|
||||
return loginApi;
|
||||
return createApi(LoginApi.class);
|
||||
}
|
||||
|
||||
public void login(HttpCallback<LoginCookieDatabaseBean> callback) {
|
||||
loginApi.getQRCodeGenerate().enqueue(new HttpCallback<QRCodeGenerateBean>() {
|
||||
getLoginApi().getQRCodeGenerate().enqueue(new HttpCallback<QRCodeGenerateBean>() {
|
||||
@Override
|
||||
public void onResponse(Headers headers, int code, String status, QRCodeGenerateBean response, String rawResponse) {
|
||||
String oauthKey = response.getQrcode_key();
|
||||
@@ -81,7 +79,7 @@ public class BiliLoginNetApiManager extends BaseApi {
|
||||
}
|
||||
|
||||
try {
|
||||
Response<HttpBody<LoginInfoBean>> response = loginApi.loginQRCode(oauthKey).execute();
|
||||
Response<HttpBody<LoginInfoBean>> response = getLoginApi().loginQRCode(oauthKey).execute();
|
||||
LoginCookieDatabaseBean cookie = getCookie(response.headers(), response.body());
|
||||
if (cookie != null) {
|
||||
cookie.setGourl(bd);
|
||||
|
||||
Reference in New Issue
Block a user