Skip to content

Commit

Permalink
js实现重定向拦截
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Oct 16, 2020
1 parent e718e2c commit f6e8255
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 72 deletions.
84 changes: 84 additions & 0 deletions app/src/main/java/com/kunfei/bookshelf/help/JsExtensions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.kunfei.bookshelf.help;

import com.kunfei.bookshelf.base.BaseModelImpl;
import com.kunfei.bookshelf.model.analyzeRule.AnalyzeUrl;
import com.kunfei.bookshelf.utils.StringUtils;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import retrofit2.Response;

@SuppressWarnings({"unused", "WeakerAccess"})
public class JsExtensions {

/**
* js实现跨域访问,不能删
*/
public String ajax(String urlStr) {
try {
AnalyzeUrl analyzeUrl = new AnalyzeUrl(urlStr);
Response<String> response = BaseModelImpl.getInstance().getResponseO(analyzeUrl)
.blockingFirst();
return response.body();
} catch (Exception e) {
return e.getLocalizedMessage();
}
}

/**
* js实现解码,不能删
*/
public String base64Decoder(String base64) {
return StringUtils.base64Decode(base64);
}

/**
* 章节数转数字
*/
public String toNumChapter(String s) {
if (s == null) {
return null;
}
Pattern pattern = Pattern.compile("(第)(.+?)(章)");
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
return matcher.group(1) + StringUtils.stringToInt(matcher.group(2)) + matcher.group(3);
}
return s;
}

/**
* js实现重定向拦截,不能删
*/
public Connection.Response get(String urlStr, Map<String, String> headers) throws IOException {
return Jsoup.connect(urlStr)
.sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
.ignoreContentType(true)
.followRedirects(false)
.headers(headers)
.method(Connection.Method.GET)
.execute();
}

/**
* js实现重定向拦截,不能删
*/
public Connection.Response post(String urlStr, String body, Map<String, String> headers) throws IOException {
return Jsoup.connect(urlStr)
.sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
.ignoreContentType(true)
.followRedirects(false)
.requestBody(body)
.headers(headers)
.method(Connection.Method.POST)
.execute();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
import androidx.annotation.Keep;

import com.google.gson.Gson;
import com.kunfei.bookshelf.base.BaseModelImpl;
import com.kunfei.bookshelf.bean.BaseBookBean;
import com.kunfei.bookshelf.help.SSLSocketClient;
import com.kunfei.bookshelf.help.JsExtensions;
import com.kunfei.bookshelf.utils.NetworkUtils;
import com.kunfei.bookshelf.utils.StringUtils;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Entities;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -25,8 +21,6 @@

import javax.script.SimpleBindings;

import retrofit2.Response;

import static android.text.TextUtils.isEmpty;
import static com.kunfei.bookshelf.constant.AppConstant.EXP_PATTERN;
import static com.kunfei.bookshelf.constant.AppConstant.JS_PATTERN;
Expand All @@ -41,7 +35,7 @@
*/
@Keep
@SuppressWarnings({"unused", "WeakerAccess"})
public class AnalyzeRule {
public class AnalyzeRule extends JsExtensions {
private static final Pattern putPattern = Pattern.compile("@put:(\\{[^}]+?\\})", Pattern.CASE_INSENSITIVE);
private static final Pattern getPattern = Pattern.compile("@get:\\{([^}]+?)\\}", Pattern.CASE_INSENSITIVE);

Expand Down Expand Up @@ -542,66 +536,4 @@ private Object evalJS(String jsStr, Object result) throws Exception {
return SCRIPT_ENGINE.eval(jsStr, bindings);
}

/**
* js实现跨域访问,不能删
*/
public String ajax(String urlStr) {
try {
AnalyzeUrl analyzeUrl = new AnalyzeUrl(urlStr);
Response<String> response = BaseModelImpl.getInstance().getResponseO(analyzeUrl)
.blockingFirst();
return response.body();
} catch (Exception e) {
return e.getLocalizedMessage();
}
}

/**
* js实现解码,不能删
*/
public String base64Decoder(String base64) {
return StringUtils.base64Decode(base64);
}

/**
* 章节数转数字
*/
public String toNumChapter(String s) {
if (s == null) {
return null;
}
Pattern pattern = Pattern.compile("(第)(.+?)(章)");
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
return matcher.group(1) + StringUtils.stringToInt(matcher.group(2)) + matcher.group(3);
}
return s;
}

/**
* js实现重定向拦截,不能删
*/
public Connection.Response get(String urlStr, Map<String, String> headers) throws IOException {
return Jsoup.connect(urlStr)
.sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
.ignoreContentType(true)
.followRedirects(false)
.headers(headers)
.method(Connection.Method.GET)
.execute();
}

/**
* js实现重定向拦截,不能删
*/
public Connection.Response post(String urlStr, String body, Map<String, String> headers) throws IOException {
return Jsoup.connect(urlStr)
.sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
.ignoreContentType(true)
.followRedirects(false)
.requestBody(body)
.headers(headers)
.method(Connection.Method.POST)
.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import androidx.annotation.Keep;

import com.google.gson.Gson;
import com.kunfei.bookshelf.help.JsExtensions;
import com.kunfei.bookshelf.utils.NetworkUtils;
import com.kunfei.bookshelf.utils.StringUtils;
import com.kunfei.bookshelf.utils.UrlEncoderUtils;
Expand Down Expand Up @@ -33,7 +34,7 @@
* 搜索URL规则解析
*/
@Keep
public class AnalyzeUrl {
public class AnalyzeUrl extends JsExtensions {
private static final Pattern pagePattern = Pattern.compile("\\{(.*?)\\}");
private String baseUrl;
private String url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.kunfei.bookshelf.DbHelper;
import com.kunfei.bookshelf.bean.BookSourceBean;
import com.kunfei.bookshelf.bean.SearchBookBean;
import com.kunfei.bookshelf.help.JsExtensions;
import com.kunfei.bookshelf.model.WebBookModel;
import com.kunfei.bookshelf.model.analyzeRule.AnalyzeRule;
import com.kunfei.bookshelf.service.CheckSourceService;
Expand All @@ -23,7 +24,7 @@

import static com.kunfei.bookshelf.constant.AppConstant.SCRIPT_ENGINE;

public class CheckSourceTask {
public class CheckSourceTask extends JsExtensions {

private BookSourceBean sourceBean;
private Scheduler scheduler;
Expand Down

0 comments on commit f6e8255

Please sign in to comment.