- 嗅探规则配置相关

pull/60/head
okjackcaptain 4 years ago
parent 032a299a00
commit 9b0ff83d61
  1. 38
      app/src/main/java/com/github/tvbox/osc/ui/activity/PlayActivity.java
  2. 38
      app/src/main/java/com/github/tvbox/osc/ui/fragment/PlayFragment.java
  3. 10
      app/src/main/java/com/github/tvbox/osc/util/DefaultConfig.java
  4. 56
      app/src/main/java/com/github/tvbox/osc/util/VideoParseRuler.java

@ -1430,31 +1430,16 @@ public class PlayActivity extends BaseActivity {
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
LOG.i("shouldInterceptRequest url:" + url);
//jpg等无效资源避免请求远程直接返回response
String uselessmMimeType = null;
if (url.contains(".jpg")) {
uselessmMimeType = "image/jpeg";
} else if (url.contains(".png")) {
uselessmMimeType = "image/png";
} else if (url.contains(".gif")) {
uselessmMimeType = "image/gif";
}
if (uselessmMimeType != null && !uselessmMimeType.isEmpty()) {
return new WebResourceResponse(uselessmMimeType, "UTF-8", null);
}
HashMap<String, String> webHeaders = new HashMap<>();
try {
Map<String, String> hds = request.getRequestHeaders();
Map<String, String> hds = request.getRequestHeaders();
if (hds != null && hds.keySet().size() > 0) {
for (String k : hds.keySet()) {
if (k.equalsIgnoreCase("user-agent")
|| k.equalsIgnoreCase("referer")
|| k.equalsIgnoreCase("accept")
|| k.equalsIgnoreCase("origin")) {
webHeaders.put(k, hds.get(k));
}
}
} catch (Throwable th) {
}
WebResourceResponse response = checkIsVideo(url, webHeaders);
return response;
@ -1570,18 +1555,6 @@ public class PlayActivity extends BaseActivity {
}
return null;
}
//jpg等无效资源避免请求远程直接返回response
String uselessmMimeType = null;
if (url.contains(".jpg")) {
uselessmMimeType = "image/jpeg";
} else if (url.contains(".png")) {
uselessmMimeType = "image/png";
} else if (url.contains(".gif")) {
uselessmMimeType = "image/gif";
}
if (uselessmMimeType != null && !uselessmMimeType.isEmpty()) {
return createXWalkWebResourceResponse(uselessmMimeType, "UTF-8", null);
}
boolean ad;
if (!loadedUrls.containsKey(url)) {
ad = AdBlocker.isAd(url);
@ -1592,18 +1565,15 @@ public class PlayActivity extends BaseActivity {
if (!ad ) {
if (checkVideoFormat(url)) {
HashMap<String, String> webHeaders = new HashMap<>();
try {
Map<String, String> hds = request.getRequestHeaders();
Map<String, String> hds = request.getRequestHeaders();
if (hds != null && hds.keySet().size() > 0) {
for (String k : hds.keySet()) {
if (k.equalsIgnoreCase("user-agent")
|| k.equalsIgnoreCase("referer")
|| k.equalsIgnoreCase("accept")
|| k.equalsIgnoreCase("origin")) {
webHeaders.put(k, hds.get(k));
}
}
} catch (Throwable th) {
}
loadFoundVideoUrls.add(url);
loadFoundVideoUrlsHeader.put(url, webHeaders);

@ -1454,31 +1454,16 @@ public class PlayFragment extends BaseLazyFragment {
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
LOG.i("shouldInterceptRequest url:" + url);
//jpg等无效资源避免请求远程直接返回response
String uselessmMimeType = null;
if (url.contains(".jpg")) {
uselessmMimeType = "image/jpeg";
} else if (url.contains(".png")) {
uselessmMimeType = "image/png";
} else if (url.contains(".gif")) {
uselessmMimeType = "image/gif";
}
if (uselessmMimeType != null && !uselessmMimeType.isEmpty()) {
return new WebResourceResponse(uselessmMimeType, "UTF-8", null);
}
HashMap<String, String> webHeaders = new HashMap<>();
try {
Map<String, String> hds = request.getRequestHeaders();
Map<String, String> hds = request.getRequestHeaders();
if (hds != null && hds.keySet().size() > 0) {
for (String k : hds.keySet()) {
if (k.equalsIgnoreCase("user-agent")
|| k.equalsIgnoreCase("referer")
|| k.equalsIgnoreCase("accept")
|| k.equalsIgnoreCase("origin")) {
webHeaders.put(k, hds.get(k));
}
}
} catch (Throwable th) {
}
WebResourceResponse response = checkIsVideo(url, webHeaders);
return response;
@ -1594,18 +1579,6 @@ public class PlayFragment extends BaseLazyFragment {
}
return null;
}
//jpg等无效资源避免请求远程直接返回response
String uselessmMimeType = null;
if (url.contains(".jpg")) {
uselessmMimeType = "image/jpeg";
} else if (url.contains(".png")) {
uselessmMimeType = "image/png";
} else if (url.contains(".gif")) {
uselessmMimeType = "image/gif";
}
if (uselessmMimeType != null && !uselessmMimeType.isEmpty()) {
return createXWalkWebResourceResponse(uselessmMimeType, "UTF-8", null);
}
boolean ad;
if (!loadedUrls.containsKey(url)) {
ad = AdBlocker.isAd(url);
@ -1616,18 +1589,15 @@ public class PlayFragment extends BaseLazyFragment {
if (!ad ) {
if (checkVideoFormat(url)) {
HashMap<String, String> webHeaders = new HashMap<>();
try {
Map<String, String> hds = request.getRequestHeaders();
Map<String, String> hds = request.getRequestHeaders();
if (hds != null && hds.keySet().size() > 0) {
for (String k : hds.keySet()) {
if (k.equalsIgnoreCase("user-agent")
|| k.equalsIgnoreCase("referer")
|| k.equalsIgnoreCase("accept")
|| k.equalsIgnoreCase("origin")) {
webHeaders.put(k, hds.get(k));
}
}
} catch (Throwable th) {
}
loadFoundVideoUrls.add(url);
loadFoundVideoUrlsHeader.put(url, webHeaders);

@ -106,10 +106,7 @@ public class DefaultConfig {
return start > -1 ? fileName.substring(0, start) : fileName;
}
//增加对flv|avi|mkv|rm|wmv|mpg等几种视频格式的支持
//private static final Pattern snifferMatch = Pattern.compile("http((?!http).){26,}?\\.(m3u8|mp4)\\?.*|http((?!http).){26,}\\.(m3u8|mp4)|http((?!http).){26,}?/m3u8\\?pt=m3u8.*|http((?!http).)*?default\\.ixigua\\.com/.*|http((?!http).)*?cdn-tos[^\\?]*|http((?!http).)*?/obj/tos[^\\?]*|http.*?/player/m3u8play\\.php\\?url=.*|http.*?/player/.*?[pP]lay\\.php\\?url=.*|http.*?/playlist/m3u8/\\?vid=.*|http.*?\\.php\\?type=m3u8&.*|http.*?/download.aspx\\?.*|http.*?/api/up_api.php\\?.*|https.*?\\.66yk\\.cn.*|http((?!http).)*?netease\\.com/file/.*");
private static final Pattern snifferMatch = Pattern.compile("http((?!http).)*?default\\.365yg\\.com/.*|http((?!http).){20,}?/m3u8\\?pt=m3u8.*|http((?!http).)*?default\\.ixigua\\.com/.*|http((?!http).)*?dycdn-tos\\.pstatp[^\\?]*|http.*?/player/m3u8play\\.php\\?url=.*|http.*?/playlist/m3u8/\\?vid=.*|http.*?\\.php\\?type=m3u8&.*|http.*?/download.aspx\\?.*|http.*?/api/up_api.php\\?.*|https.*?\\.66yk\\.cn.*|http((?!http).)*?netease\\.com/file/.*");
private static final String[] videoSubfix = {"m3u8","mp4","flv","avi","mkv","rm","wmv","mpg"};
private static final String[] videoSuffix = {"m3u8","mp4","flv","avi","mkv","rm","wmv","mpg"};
public static boolean isVideoFormat(String url) {
if (url.contains("=http")) {
return false;
@ -126,14 +123,11 @@ public class DefaultConfig {
if (query != null && query.startsWith("http")) {
return false;
}
for(String oneSubfix : videoSubfix) {
for(String oneSubfix : videoSuffix) {
if (path.endsWith("." + oneSubfix)) {
return true;
}
}
if (snifferMatch.matcher(url).find()) {
return true;
}
return false;
}

@ -1,22 +1,23 @@
package com.github.tvbox.osc.util;
import android.net.Uri;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
public class VideoParseRuler {
private static final HashMap<String, ArrayList<String>> HOSTS_RULE = new HashMap<>();
private static final HashMap<String, ArrayList<ArrayList<String>>> HOSTS_RULE = new HashMap<>();
public static void addHostRule(String host, ArrayList<String> rule) {
HOSTS_RULE.put(host, rule);
ArrayList<ArrayList<String>> rules = new ArrayList<>();
if (HOSTS_RULE.get(host) != null && HOSTS_RULE.get(host).size() > 0) {
rules = HOSTS_RULE.get(host);
}
rules.add(rule);
HOSTS_RULE.put(host, rules);
}
@Nullable
public static ArrayList<String> getHostRule(String host) {
public static ArrayList<ArrayList<String>> getHostRules(String host) {
if (HOSTS_RULE.containsKey(host)) {
return HOSTS_RULE.get(host);
}
@ -31,18 +32,9 @@ public class VideoParseRuler {
}
if (!isVideo) {
Uri uri = Uri.parse(webUrl);
ArrayList<String> hostRule = getHostRule(uri.getHost());
if (hostRule != null && hostRule.size() > 0) {
boolean checkIsVideo = true;
for(int i=0; i<hostRule.size(); i++) {
if (!url.contains(hostRule.get(i))) {
checkIsVideo = false;
break;
}
}
if (checkIsVideo) {
isVideo = true;
}
isVideo = checkVideoForOneHostRules(uri.getHost(), url);
if (!isVideo) {
isVideo = checkVideoForOneHostRules("*", url);
}
}
return isVideo;
@ -52,4 +44,30 @@ public class VideoParseRuler {
return false;
}
private static boolean checkVideoForOneHostRules(String host, String url) {
boolean isVideo = false;
ArrayList<ArrayList<String>> hostRules = getHostRules(host);
if (hostRules != null && hostRules.size() > 0) {
boolean isVideoRuleCheck = false;
for(int i=0; i<hostRules.size(); i++) {
boolean checkIsVideo = true;
for(int j=0; j<hostRules.get(i).size(); j++) {
if (!url.contains(hostRules.get(i).get(j))) {
checkIsVideo = false;
break;
}
}
if (checkIsVideo) {
isVideoRuleCheck = true;
break;
}
}
if (isVideoRuleCheck) {
isVideo = true;
}
}
return isVideo;
}
}

Loading…
Cancel
Save