diff --git a/src/com/hippo/ehviewer/AppContext.java b/src/com/hippo/ehviewer/AppContext.java index 83acdd377..68f3d4b9f 100644 --- a/src/com/hippo/ehviewer/AppContext.java +++ b/src/com/hippo/ehviewer/AppContext.java @@ -31,6 +31,7 @@ import com.hippo.ehviewer.data.Data; import com.hippo.ehviewer.ehclient.EhClient; import com.hippo.ehviewer.ehclient.EhInfo; import com.hippo.ehviewer.ehclient.ExDownloaderManager; +import com.hippo.ehviewer.network.HttpHelper; import com.hippo.ehviewer.util.Config; import com.hippo.ehviewer.util.Crash; import com.hippo.ehviewer.util.EhUtils; @@ -88,6 +89,9 @@ public class AppContext extends Application implements UncaughtExceptionHandler } catch (IOException e) {} } + // Update proxy urls + HttpHelper.updateProxyUrls(this); + // Fix <=22 login error if (Config.getVersionCode() <= 22) EhInfo.getInstance(this).logout(); diff --git a/src/com/hippo/ehviewer/UpdateHelper.java b/src/com/hippo/ehviewer/UpdateHelper.java index e4f13a0f7..13ee639f1 100644 --- a/src/com/hippo/ehviewer/UpdateHelper.java +++ b/src/com/hippo/ehviewer/UpdateHelper.java @@ -31,6 +31,7 @@ import android.net.Uri; import android.os.Process; import android.support.v4.app.NotificationCompat; +import com.hippo.ehviewer.ehclient.EhClient; import com.hippo.ehviewer.network.HttpHelper; import com.hippo.ehviewer.util.Config; import com.hippo.ehviewer.util.Utils; @@ -42,7 +43,6 @@ public class UpdateHelper implements Runnable { @SuppressWarnings("unused") private static final String TAG = "UpdateHelper"; - private static final String UPDATE_API = "http://www.ehviewer.com/API"; private static final String HEADER = "Ehviewer-"; private static final String FOOTER = ".apk"; @@ -114,7 +114,7 @@ public class UpdateHelper implements Runnable { jo.put("detail", detailJO); - _package.body = hp.postJson(UPDATE_API, jo); + _package.body = hp.postJson(EhClient.API_EHVIEWER, jo); _package.emesg = hp.getEMsg(); send(_package); } catch (NameNotFoundException e) { diff --git a/src/com/hippo/ehviewer/ehclient/EhClient.java b/src/com/hippo/ehviewer/ehclient/EhClient.java index 02f584048..92936c950 100644 --- a/src/com/hippo/ehviewer/ehclient/EhClient.java +++ b/src/com/hippo/ehviewer/ehclient/EhClient.java @@ -69,7 +69,7 @@ public class EhClient { public static final String HEADER_EX = "http://exhentai.org/"; public static final String HEADER_LOFI = "http://lofi.e-hentai.org/"; - private static final String API_EHVIEWER = "http://www.ehviewer.com/API"; + public static final String API_EHVIEWER = "http://www.ehviewer.com/API"; /* Avatar get code */ public static final int GET_AVATAR_OK = 0x0; diff --git a/src/com/hippo/ehviewer/network/HttpHelper.java b/src/com/hippo/ehviewer/network/HttpHelper.java index c5ef321cd..bcb10b27e 100644 --- a/src/com/hippo/ehviewer/network/HttpHelper.java +++ b/src/com/hippo/ehviewer/network/HttpHelper.java @@ -38,6 +38,7 @@ import java.util.Map; import java.util.zip.GZIPInputStream; import org.apache.http.conn.ConnectTimeoutException; +import org.json.JSONArray; import org.json.JSONObject; import android.content.Context; @@ -51,6 +52,7 @@ import com.hippo.ehviewer.R; import com.hippo.ehviewer.ehclient.EhClient; import com.hippo.ehviewer.ehclient.EhInfo; import com.hippo.ehviewer.exception.StopRequestException; +import com.hippo.ehviewer.util.BgThread; import com.hippo.ehviewer.util.Config; import com.hippo.ehviewer.util.Constants; import com.hippo.ehviewer.util.FastByteArrayOutputStream; @@ -81,25 +83,8 @@ public class HttpHelper { private static final String CHARSET_KEY = "charset="; // TODO Get proxy list from server - private static final String[] PROXY_URLS = { - "http://proxyy0000.appsp0t.com/proxy", - "http://proxyy0001.appsp0t.com/proxy", - "http://proxyy0002.appsp0t.com/proxy", - "http://proxyy0003.appsp0t.com/proxy", - "http://proxyy0004.appsp0t.com/proxy", - "http://proxyy0005.appsp0t.com/proxy", - "http://proxyy0006.appsp0t.com/proxy", - "http://proxyy0007.appsp0t.com/proxy", - "http://proxyy0008.appsp0t.com/proxy", - "http://proxyy0009.appsp0t.com/proxy", - "http://proxyy000a.appsp0t.com/proxy", - "http://proxyy000b.appsp0t.com/proxy", - "http://proxyy000c.appsp0t.com/proxy", - "http://proxyy000d.appsp0t.com/proxy", - "http://proxyy000e.appsp0t.com/proxy", - "http://proxyy000f.appsp0t.com/proxy" - }; - private static volatile int sProxyIndex = MathUtils.random(0, PROXY_URLS.length); + private static String[] sProxyUrls = Config.DEFAULT_PROXY_URLS; + private static volatile int sProxyIndex = MathUtils.random(0, sProxyUrls.length); private static Object sProxyIndexLock = new Object(); public class Package { @@ -125,11 +110,43 @@ public class HttpHelper { void onFailure(String eMsg); } + public static void updateProxyUrls(final Context context) { + new BgThread() { + @Override + public void run() { + try { + HttpHelper hh = new HttpHelper(context); + JSONObject json = new JSONObject(); + json.put("method", "proxy_urls"); + String body = hh.postJson(EhClient.API_EHVIEWER, json); + JSONObject jo = new JSONObject(body); + JSONArray ja = jo.getJSONObject("proxy_urls").getJSONArray("urls"); + String[] newUrls = new String[ja.length()]; + if (newUrls.length < 1) + throw new Throwable(); + for (int i = 0; i < ja.length(); i++) + newUrls[i] = ja.getString(i); + synchronized(sProxyIndexLock) { + sProxyUrls = newUrls; + sProxyIndex = MathUtils.random(0, sProxyUrls.length); + } + // Put proxy into config + Config.setProxyUrls(sProxyUrls); + } catch (Throwable e) { + synchronized(sProxyIndexLock) { + sProxyUrls = Config.getProxyUrls(); + sProxyIndex = MathUtils.random(0, sProxyUrls.length); + } + } + } + }.start(); + } + public String getProxyUrl() { synchronized(sProxyIndexLock) { - if (sProxyIndex < 0 || sProxyIndex >= PROXY_URLS.length) + if (sProxyIndex < 0 || sProxyIndex >= sProxyUrls.length) sProxyIndex = 0; - return PROXY_URLS[sProxyIndex++]; + return sProxyUrls[sProxyIndex++]; } } diff --git a/src/com/hippo/ehviewer/util/Config.java b/src/com/hippo/ehviewer/util/Config.java index 99cbf41b1..2e8244672 100644 --- a/src/com/hippo/ehviewer/util/Config.java +++ b/src/com/hippo/ehviewer/util/Config.java @@ -17,6 +17,9 @@ package com.hippo.ehviewer.util; import java.io.File; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import android.content.Context; import android.content.SharedPreferences; @@ -597,4 +600,38 @@ public class Config { return 0; } } + + + // Proxy urls + private static final String PROXY_URLS_KEY = "proxy_urls"; + public static final String[] DEFAULT_PROXY_URLS = { + "http://proxyy0000.appsp0t.com/proxy", + "http://proxyy0001.appsp0t.com/proxy", + "http://proxyy0002.appsp0t.com/proxy", + "http://proxyy0003.appsp0t.com/proxy", + "http://proxyy0004.appsp0t.com/proxy", + "http://proxyy0005.appsp0t.com/proxy", + "http://proxyy0006.appsp0t.com/proxy", + "http://proxyy0007.appsp0t.com/proxy", + "http://proxyy0008.appsp0t.com/proxy", + "http://proxyy0009.appsp0t.com/proxy", + "http://proxyy000a.appsp0t.com/proxy", + "http://proxyy000b.appsp0t.com/proxy", + "http://proxyy000c.appsp0t.com/proxy", + "http://proxyy000d.appsp0t.com/proxy", + "http://proxyy000e.appsp0t.com/proxy", + "http://proxyy000f.appsp0t.com/proxy" + }; + + public static String[] getProxyUrls() { + Set re = mConfigPre.getStringSet(PROXY_URLS_KEY, null); + if (re != null && re.size() > 0) + return re.toArray(new String[re.size()]); + else + return DEFAULT_PROXY_URLS; + } + + public static void setProxyUrls(String[] urls) { + mConfigPre.edit().putStringSet(PROXY_URLS_KEY, new HashSet(Arrays.asList(urls))).apply(); + } }