From 2e24dc4534e28b4dd34682ae7a9eea1db2a426f9 Mon Sep 17 00:00:00 2001 From: Mishiranu Date: Wed, 30 Sep 2020 12:20:12 +0300 Subject: [PATCH] Use mobile User-Agent --- .../content/net/UserAgentProvider.java | 115 ++++++++++++++---- 1 file changed, 89 insertions(+), 26 deletions(-) diff --git a/src/com/mishiranu/dashchan/content/net/UserAgentProvider.java b/src/com/mishiranu/dashchan/content/net/UserAgentProvider.java index 26143e19..ee9f7164 100644 --- a/src/com/mishiranu/dashchan/content/net/UserAgentProvider.java +++ b/src/com/mishiranu/dashchan/content/net/UserAgentProvider.java @@ -1,6 +1,7 @@ package com.mishiranu.dashchan.content.net; import android.net.Uri; +import android.os.Build; import android.util.Base64; import chan.content.ChanLocator; import chan.http.HttpException; @@ -22,23 +23,41 @@ public static UserAgentProvider getInstance() { return INSTANCE; } + private static final String PREFIX = "chromium.v1:"; + private static final String DEFAULT_REFERENCE = PREFIX + "74.0.3729.169.537.36"; + + private String userAgent = "Mozilla/5.0"; + private UserAgentProvider() { - String reference = Preferences.getUserAgentReference(); - if (reference == null) { - reference = "chromium:74.0.3729.169"; + String userAgent = formatUserAgentForReference(Preferences.getUserAgentReference()); + if (userAgent == null) { + userAgent = formatUserAgentForReference(DEFAULT_REFERENCE); } - if (reference.startsWith("chromium:")) { - String version = reference.substring(9); - userAgent = formatChromiumUserAgent(version); + if (userAgent != null) { + this.userAgent = userAgent; } loadChromiumUserAgentReference(); } - private String userAgent = "Mozilla/5.0"; + private String formatUserAgentForReference(String reference) { + if (reference != null && reference.startsWith(PREFIX)) { + String version = reference.substring(PREFIX.length()); + return formatChromiumUserAgent(version); + } + return null; + } private String formatChromiumUserAgent(String version) { - return String.format(Locale.US, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + - "(KHTML, like Gecko) Chrome/%s Safari/537.36", version); + try { + String[] numbers = version.split("\\."); + String chromiumVersion = numbers[0] + "." + numbers[1] + "." + numbers[2] + "." + numbers[3]; + String webKitVersion = numbers[4] + "." + numbers[5]; + return String.format(Locale.US, "Mozilla/5.0 (Linux; Android %s; wv) AppleWebKit/%s " + + "(KHTML, like Gecko) Version/4.0 Chrome/%s Mobile Safari/%s", Build.VERSION.RELEASE, + webKitVersion, chromiumVersion, webKitVersion); + } catch (Exception e) { + return null; + } } private void loadChromiumUserAgentReference() { @@ -60,43 +79,84 @@ protected String doInBackground(HttpHolder holder, Void... params) { String commit = object != null ? object.optString("cr-git-commit") : null; if (commit != null) { - String response; + String chromeVersionResponse; try { Uri uri = ChanLocator.getDefault().buildQueryWithHost("chromium.googlesource.com", "chromium/src/+/" + commit + "/chrome/VERSION", "format", "TEXT"); - response = new HttpRequest(uri, holder).read().getString(); + chromeVersionResponse = new HttpRequest(uri, holder).read().getString(); + } catch (HttpException e) { + return null; + } + String webKitVersionResponse; + try { + Uri uri = ChanLocator.getDefault().buildQueryWithHost("chromium.googlesource.com", + "chromium/src/+/" + commit + "/build/util/webkit_version.h.in", "format", "TEXT"); + webKitVersionResponse = new HttpRequest(uri, holder).read().getString(); } catch (HttpException e) { return null; } - if (response != null) { - String versionData; + if (chromeVersionResponse != null && webKitVersionResponse != null) { + String chromiumVersionData; try { - versionData = new String(Base64.decode(response, Base64.DEFAULT)); + chromiumVersionData = new String(Base64.decode(chromeVersionResponse, Base64.DEFAULT)); } catch (Exception e) { Log.persistent().stack(e); return null; } - HashMap map = new HashMap<>(); - for (String line : versionData.split("\n")) { + String webKitVersionData; + try { + webKitVersionData = new String(Base64.decode(webKitVersionResponse, Base64.DEFAULT)); + } catch (Exception e) { + Log.persistent().stack(e); + return null; + } + HashMap chromeMap = new HashMap<>(); + for (String line : chromiumVersionData.split("\n")) { int index = line.indexOf('='); if (index >= 0) { String key = line.substring(0, index); String valueString = line.substring(index + 1); try { int value = Integer.parseInt(valueString); - map.put(key.toLowerCase(Locale.US), value); + chromeMap.put(key.toLowerCase(Locale.US), value); } catch (NumberFormatException e) { // Ignore } } } - Integer major = map.get("major"); - Integer minor = map.get("minor"); - Integer build = map.get("build"); - Integer patch = map.get("patch"); - if (major != null && minor != null) { - return major + "." + minor + "." + (build != null ? build : 0) + "." + - (patch != null ? patch : 0); + HashMap webKitMap = new HashMap<>(); + for (String line : webKitVersionData.split("\n")) { + if (line.startsWith("#define")) { + String[] nameValue = line.substring(7).trim().split(" "); + if (nameValue.length == 2 && nameValue[0].startsWith("WEBKIT_VERSION_")) { + try { + webKitMap.put(nameValue[0].substring(15).toLowerCase(Locale.US), + Integer.parseInt(nameValue[1])); + } catch (NumberFormatException e) { + // Ignore + } + } + } + } + Integer chromeMajor = chromeMap.get("major"); + Integer chromeMinor = chromeMap.get("minor"); + Integer chromeBuild = chromeMap.get("build"); + Integer chromePatch = chromeMap.get("patch"); + Integer webKitMajor = webKitMap.get("major"); + Integer webKitMinor = webKitMap.get("minor"); + if (chromeMajor != null && chromeMinor != null) { + if (chromeBuild == null) { + chromeBuild = 0; + } + if (chromePatch == 0) { + chromePatch = 0; + } + if (webKitMajor == null || webKitMinor == null) { + webKitMajor = 537; + webKitMinor = 36; + } + return chromeMajor + "." + chromeMinor + "." + chromeBuild + "." + chromePatch + "." + + webKitMajor + "." + webKitMinor; } } } @@ -106,8 +166,11 @@ protected String doInBackground(HttpHolder holder, Void... params) { @Override protected void onPostExecute(String version) { if (version != null) { - Preferences.setUserAgentReference("chromium:" + version); - userAgent = formatChromiumUserAgent(version); + Preferences.setUserAgentReference(PREFIX + version); + String userAgent = formatChromiumUserAgent(version); + if (userAgent != null) { + UserAgentProvider.this.userAgent = userAgent; + } } } }.executeOnExecutor(ConcurrentUtils.SEPARATE_EXECUTOR);