From 20f6e47c0c795bb9e9b55de296a36c12e5f16d42 Mon Sep 17 00:00:00 2001 From: bakapiano <9417325+bakapiano@user.noreply.gitee.com> Date: Sun, 1 Jan 2023 17:14:15 +0800 Subject: [PATCH] Update version to 1.4 --- app/build.gradle | 2 +- .../maimai/updater/crawler/CrawlerCaller.java | 21 +++++- .../updater/crawler/SimpleCookieJar.java | 8 ++- .../maimai/updater/crawler/WechatCrawler.java | 64 ++++++++++++++----- .../updater/server/HttpRedirectServer.java | 2 +- .../maimai/updater/server/HttpServer.java | 6 +- .../maimai/updater/ui/DataContext.java | 2 + .../maimai/updater/ui/MainActivity.java | 37 ++++++++--- .../updater/vpn/core/LocalVpnService.java | 58 ++++++++++------- .../updater/vpn/core/TcpProxyServer.java | 1 + .../updater/vpn/core/TunnelFactory.java | 53 +++++++++++---- .../vpn/tunnel/HttpCapturerTunnel.java | 2 +- .../maimai/updater/vpn/tunnel/RawTunnel.java | 2 +- .../maimai/updater/vpn/tunnel/Tunnel.java | 4 +- .../tunnel/httpconnect/HttpConnectTunnel.java | 50 ++++++++------- app/src/main/res/layout/activity_main.xml | 7 +- build.gradle | 4 +- 17 files changed, 223 insertions(+), 100 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 87b8eea..c095958 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,7 +8,7 @@ android { minSdkVersion min_version targetSdkVersion target_version versionCode 1 - versionName "1.0" + versionName "1.4" } // debug和release使用相同签名,以便用debug包覆盖release包从而调试, // 如果没有,就会使用默认debug签名, diff --git a/app/src/main/java/com/bakapiano/maimai/updater/crawler/CrawlerCaller.java b/app/src/main/java/com/bakapiano/maimai/updater/crawler/CrawlerCaller.java index cd9ce2a..19f97e5 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/crawler/CrawlerCaller.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/crawler/CrawlerCaller.java @@ -8,6 +8,8 @@ import com.bakapiano.maimai.updater.vpn.core.LocalVpnService; import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; @@ -19,8 +21,13 @@ public class CrawlerCaller { static public String getWechatAuthUrl() { try { WechatCrawler crawler = new WechatCrawler(); - return crawler.getWechatAuthUrl(); + String url = crawler.getWechatAuthUrl(); + writeLog("微信登录url:"); + if (url != null) writeLog(url); + return url; } catch (IOException error) { + writeLog("获取微信登录url时出现错误:"); + writeLog(error); return null; } } @@ -29,6 +36,13 @@ static public void writeLog(String text) { m_Handler.post(() -> listener.onLogReceived(text)); } + static public void writeLog(Exception e) { + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw)); + String exceptionAsString = sw.toString(); + m_Handler.post(() -> listener.onLogReceived(exceptionAsString)); + } + static public void fetchData(String authUrl) { new Thread(() -> { try { @@ -36,13 +50,14 @@ static public void fetchData(String authUrl) { LocalVpnService.IsRunning = false; Thread.sleep(3000); } catch (InterruptedException e) { - e.printStackTrace(); + writeLog(e); } + try { WechatCrawler crawler = new WechatCrawler(); crawler.fetchData(DataContext.Username, DataContext.Password, authUrl); } catch (IOException e) { - e.printStackTrace(); + writeLog(e); } }).start(); } diff --git a/app/src/main/java/com/bakapiano/maimai/updater/crawler/SimpleCookieJar.java b/app/src/main/java/com/bakapiano/maimai/updater/crawler/SimpleCookieJar.java index 1b2f085..10c82f8 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/crawler/SimpleCookieJar.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/crawler/SimpleCookieJar.java @@ -17,7 +17,7 @@ public class SimpleCookieJar implements CookieJar { private final Object lock = new Object(); @Override - public void saveFromResponse(HttpUrl httpUrl, @NotNull List newCookies) { + public void saveFromResponse(HttpUrl httpUrl, List newCookies) { synchronized (lock) { HashMap map = new HashMap<>(); List oldCookies = cookieStore.get(httpUrl.host()); @@ -27,8 +27,10 @@ public void saveFromResponse(HttpUrl httpUrl, @NotNull List newCookies) } } // Override old cookie with same name - for (Cookie cookie : newCookies) { - map.put(cookie.name(), cookie); + if (newCookies != null) { + for (Cookie cookie : newCookies) { + map.put(cookie.name(), cookie); + } } List mergedList = new ArrayList(); for (Map.Entry pair : map.entrySet()) { diff --git a/app/src/main/java/com/bakapiano/maimai/updater/crawler/WechatCrawler.java b/app/src/main/java/com/bakapiano/maimai/updater/crawler/WechatCrawler.java index 3a43970..016a3e9 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/crawler/WechatCrawler.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/crawler/WechatCrawler.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.security.cert.CertificateException; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -24,6 +25,7 @@ import javax.net.ssl.X509TrustManager; import okhttp3.Call; +import okhttp3.ConnectionSpec; import okhttp3.Cookie; import okhttp3.CookieJar; import okhttp3.HttpUrl; @@ -33,6 +35,7 @@ import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; +import okhttp3.TlsVersion; public class WechatCrawler { // Make this true for Fiddler to capture https request @@ -111,18 +114,20 @@ protected void fetchData(String username, String password, String wechatAuthUrl) try { this.loginWechat(wechatAuthUrl); writeLog("登陆完成"); - } - catch(Exception error) { - writeLog("登陆时出现错误:\n" + error.toString()); + } catch (Exception error) { + writeLog("登陆时出现错误:\n"); + writeLog(error); + return; } // Fetch maimai data try { this.fetchMaimaiData(username, password); writeLog("maimai 数据更新完成"); - } - catch (Exception error) { - writeLog("maimai 数据更新时出线错误:\n" + error.toString()); + } catch (Exception error) { + writeLog("maimai 数据更新时出线错误:"); + writeLog(error); + return; } // Fetch chuithm data @@ -130,8 +135,10 @@ protected void fetchData(String username, String password, String wechatAuthUrl) } private void loginWechat(String wechatAuthUrl) throws IOException { - this.buildHttpClient(false); + this.buildHttpClient(true); + Log.d(TAG, wechatAuthUrl); + writeLog("登录url:\n" + wechatAuthUrl); Request request = new Request.Builder() .addHeader("Host", "tgk-wcaime.wahlap.com") @@ -152,14 +159,26 @@ private void loginWechat(String wechatAuthUrl) throws IOException { Call call = client.newCall(request); Response response = call.execute(); + try { + String responseBody = response.body().string(); + Log.d(TAG, responseBody); +// writeLog(responseBody); + } + catch (NullPointerException error) { + writeLog(error); + } + writeLog(String.valueOf(response.code())); + // Handle redirect manually - String newUrl = response.headers().get("Location"); - request = new Request.Builder() - .url(newUrl) - .get() - .build(); - call = client.newCall(request); - response = call.execute(); + String location = response.headers().get("Location"); + if (response.code() >= 300 && response.code() < 400 && location != null) { + request = new Request.Builder() + .url(location) + .get() + .build(); + call = client.newCall(request); + response = call.execute(); + } } private void fetchMaimaiData(String username, String password) throws IOException { @@ -211,9 +230,9 @@ private void buildHttpClient(boolean followRedirect) { if (IGNORE_CERT) ignoreCertBuilder(builder); - builder.connectTimeout(30, TimeUnit.SECONDS); - builder.readTimeout(30, TimeUnit.SECONDS); - builder.writeTimeout(30, TimeUnit.SECONDS); + builder.connectTimeout(120, TimeUnit.SECONDS); + builder.readTimeout(120, TimeUnit.SECONDS); + builder.writeTimeout(120, TimeUnit.SECONDS); builder.followRedirects(followRedirect); builder.followSslRedirects(followRedirect); @@ -230,6 +249,17 @@ private void buildHttpClient(boolean followRedirect) { }; builder.addInterceptor(noCacheInterceptor); + // Fix SSL handle shake error + ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS) + .tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1, TlsVersion.TLS_1_0) + .allEnabledCipherSuites() + .build(); + // 兼容http接口 + ConnectionSpec spec1 = new ConnectionSpec.Builder(ConnectionSpec.CLEARTEXT).build(); + builder.connectionSpecs(Arrays.asList(spec, spec1)); + + builder.pingInterval(3, TimeUnit.SECONDS); + client = builder.build(); } diff --git a/app/src/main/java/com/bakapiano/maimai/updater/server/HttpRedirectServer.java b/app/src/main/java/com/bakapiano/maimai/updater/server/HttpRedirectServer.java index 7942035..2a8ffc0 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/server/HttpRedirectServer.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/server/HttpRedirectServer.java @@ -7,7 +7,7 @@ import fi.iki.elonen.NanoHTTPD; public class HttpRedirectServer extends NanoHTTPD { - public static int Port = 9957; + public static int Port = 9457; private final static String TAG = "HttpRedirectServer"; protected HttpRedirectServer() throws IOException { diff --git a/app/src/main/java/com/bakapiano/maimai/updater/server/HttpServer.java b/app/src/main/java/com/bakapiano/maimai/updater/server/HttpServer.java index e0894c5..074dc05 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/server/HttpServer.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/server/HttpServer.java @@ -1,5 +1,7 @@ package com.bakapiano.maimai.updater.server; +import static com.bakapiano.maimai.updater.ui.DataContext.HookHost; + import android.util.Log; import com.bakapiano.maimai.updater.crawler.CrawlerCaller; @@ -11,7 +13,7 @@ public class HttpServer extends NanoHTTPD { - public static int Port = 8084; + public static int Port = 8284; private final static String TAG = "HttpServer"; protected HttpServer() throws IOException { @@ -38,7 +40,7 @@ public Response serve(IHTTPSession session) { // To avoid fu***ing cache of wechat webview client private Response redirectToAuthUrlWithRandomParm(IHTTPSession session) { Response r = newFixedLengthResponse(Response.Status.REDIRECT, MIME_HTML, ""); - r.addHeader("Location", "http://bakapiano.com/auth?random=" + System.currentTimeMillis()); + r.addHeader("Location", "http://" + HookHost + "/auth?random=" + System.currentTimeMillis()); return r; } diff --git a/app/src/main/java/com/bakapiano/maimai/updater/ui/DataContext.java b/app/src/main/java/com/bakapiano/maimai/updater/ui/DataContext.java index 5944341..2ed5e5b 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/ui/DataContext.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/ui/DataContext.java @@ -4,4 +4,6 @@ public class DataContext { public static String Username = null; public static String Password = null; public static String HookHost = "bakapiano.com"; + public static boolean CompatibleMode = false; } + diff --git a/app/src/main/java/com/bakapiano/maimai/updater/ui/MainActivity.java b/app/src/main/java/com/bakapiano/maimai/updater/ui/MainActivity.java index b800d82..9384c4b 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/ui/MainActivity.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/ui/MainActivity.java @@ -1,5 +1,7 @@ package com.bakapiano.maimai.updater.ui; +import static com.bakapiano.maimai.updater.ui.DataContext.HookHost; + import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.ActivityNotFoundException; @@ -16,6 +18,7 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.SwitchCompat; import android.support.v7.widget.Toolbar; +import android.text.method.ScrollingMovementMethod; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -28,6 +31,7 @@ import java.io.File; import java.util.Calendar; +import java.util.Random; import com.bakapiano.maimai.updater.R; import com.bakapiano.maimai.updater.crawler.Callback; @@ -75,6 +79,7 @@ protected void onCreate(Bundle savedInstanceState) { assert textViewLog != null; textViewLog.setText(GL_HISTORY_LOGS); + textViewLog.setMovementMethod(ScrollingMovementMethod.getInstance()); scrollViewLog.fullScroll(ScrollView.FOCUS_DOWN); mCalendar = Calendar.getInstance(); @@ -171,9 +176,10 @@ public void onLogReceived(String logString) { Log.d(Constant.TAG, logString); - if (textViewLog.getLineCount() > 200) { - textViewLog.setText(""); - } +// if (textViewLog.getLineCount() > 200) { +// textViewLog.setText(""); +// } + textViewLog.append(logString); scrollViewLog.fullScroll(ScrollView.FOCUS_DOWN); GL_HISTORY_LOGS = textViewLog.getText() == null ? "" : textViewLog.getText().toString(); @@ -189,6 +195,7 @@ public void onStatusChanged(String status, Boolean isRunning) { } private Object switchLock = new Object(); + @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (!switchProxy.isEnabled()) return; @@ -211,13 +218,16 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // Start http service startHttpService(); + String link = "http://" + getRandomString(10) + ".redirect." + HookHost; + if (DataContext.CompatibleMode) { + link = "https://maimai.bakapiano.com/shortcut?username=" + + DataContext.Username + "&password=" + DataContext.Password; + } + ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); - ClipData clip = ClipData.newPlainText("link", "http://bakapiano.com"); + ClipData clip = ClipData.newPlainText("link", link); clipboard.setPrimaryClip(clip); - - - } - else { + } else { switchProxy.setChecked(false); switchProxy.setEnabled(true); } @@ -377,4 +387,15 @@ private void getWechatApi() { } catch (ActivityNotFoundException ignored) { } } + + public static String getRandomString(int length){ + String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + Random random = new Random(); + StringBuffer sb = new StringBuffer(); + for(int i=0;i entry : m_OnStatusChangedListeners.entrySet()) { -// entry.getKey().onLogReceived(logString); -// } + for (Map.Entry entry : m_OnStatusChangedListeners.entrySet()) { + entry.getKey().onLogReceived(logString); + } } }); } @@ -155,7 +159,7 @@ public void sendUDPPacket(IPHeader ipHeader, UDPHeader udpHeader) { } String getAppInstallID() { - SharedPreferences preferences = getSharedPreferences(Constant.TAG, MODE_PRIVATE); + SharedPreferences preferences = getSharedPreferences(TAG, MODE_PRIVATE); String appInstallID = preferences.getString("AppInstallID", null); if (appInstallID == null || appInstallID.isEmpty()) { appInstallID = UUID.randomUUID().toString(); @@ -184,7 +188,7 @@ public String configDirFor(Context context, String suffix) { @Override public synchronized void run() { try { - Log.d(Constant.TAG, "VPNService work thread is running... " + ID); + Log.d(TAG, "VPNService work thread is running... " + ID); ProxyConfig.AppInstallID = getAppInstallID(); ProxyConfig.AppVersion = getVersionName(); @@ -196,7 +200,7 @@ public synchronized void run() { runVPN(); } catch (InterruptedException e) { - Log.e(Constant.TAG, "Exception", e); + Log.e(TAG, "Exception", e); } catch (Exception e) { e.printStackTrace(); writeLog("Fatal error: %s", e.toString()); @@ -223,7 +227,7 @@ private void runVPN() throws Exception { onIPPacketReceived(m_IPHeader, size); idle = false; } catch (IOException ex) { - Log.e(Constant.TAG, "IOException when processing IP packet", ex); + Log.e(TAG, "IOException when processing IP packet", ex); } } if (idle) { @@ -253,7 +257,7 @@ void onIPPacketReceived(IPHeader ipHeader, int size) throws IOException { m_ReceivedBytes += size; } else { if (ProxyConfig.IS_DEBUG) - Log.d(Constant.TAG, "NoSession: " + + Log.d(TAG, "NoSession: " + ipHeader.toString() + " " + tcpHeader.toString()); } @@ -328,7 +332,7 @@ private ParcelFileDescriptor establishVPN() throws Exception { builder.addAddress(ipAddress.Address, ipAddress.PrefixLength); if (ProxyConfig.IS_DEBUG) - Log.d(Constant.TAG, String.format("addAddress: %s/%d\n", ipAddress.Address, ipAddress.PrefixLength)); + Log.d(TAG, String.format("addAddress: %s/%d\n", ipAddress.Address, ipAddress.PrefixLength)); if (m_Blacklist == null) { m_Blacklist = getResources().getStringArray(R.array.black_list); @@ -343,32 +347,40 @@ private ParcelFileDescriptor establishVPN() throws Exception { builder.addRoute(CommonMethods.ipIntToString(ProxyConfig.FAKE_NETWORK_IP), 16); -// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { -// PackageManager packageManager = getPackageManager(); -// List list = packageManager.getInstalledPackages(0); -// HashSet packageSet = new HashSet<>(); + // PackageManager packageManager = getPackageManager(); +// List list = packageManager.getInstalledPackages(0); +// HashSet packageSet = new HashSet<>(); // -// for (int i = 0; i < list.size(); i++) { -// PackageInfo info = list.get(i); +// for (int i = 0; i < list.size(); i++) { +// PackageInfo info = list.get(i); +// Log.d(TAG, info.packageName); +// if (info.packageName.equals("com.bakapiano.maimai.updater")) { +// Log.d(TAG, "Found maimai updater"); +// } +// else { // packageSet.add(info.packageName); +// Log.d(TAG, "fuck" + info.packageName); // } +// } // -// for (String name : getResources().getStringArray(R.array.bypass_package_name)) { -// if (packageSet.contains(name)) { -// builder.addDisallowedApplication(name); -// } +// packageSet.add("com.tencent.mm"); +// +// for (String name : getResources().getStringArray(R.array.bypass_package_name)) { +// if (packageSet.contains(name)) { +// builder.addDisallowedApplication(name); // } // } +// builder.addDisallowedApplication("com.wechat.mm"); +// builder.addAllowedApplication("com.tencent.mm"); +// builder.addAllowedApplication("com.bakapiano.maimai.updater"); + Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); builder.setConfigureIntent(pendingIntent); builder.setSession(ProxyConfig.Instance.getSessionName()); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - } - ParcelFileDescriptor pfdDescriptor = builder.establish(); onStatusChanged(ProxyConfig.Instance.getSessionName() + " " + getString(R.string.vpn_connected_status), true); return pfdDescriptor; @@ -412,7 +424,7 @@ private synchronized void dispose() { @Override public void onDestroy() { - Log.d(Constant.TAG, "VPNService(%s) destroyed: " + ID); + Log.d(TAG, "VPNService(%s) destroyed: " + ID); if (IsRunning) dispose(); try { // ֹͣTcpServer diff --git a/app/src/main/java/com/bakapiano/maimai/updater/vpn/core/TcpProxyServer.java b/app/src/main/java/com/bakapiano/maimai/updater/vpn/core/TcpProxyServer.java index 4f32c7e..4043494 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/vpn/core/TcpProxyServer.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/vpn/core/TcpProxyServer.java @@ -25,6 +25,7 @@ public class TcpProxyServer implements Runnable { public TcpProxyServer(int port) throws IOException { m_Selector = Selector.open(); m_ServerSocketChannel = ServerSocketChannel.open(); + m_ServerSocketChannel.socket().setSoTimeout(1000*30); m_ServerSocketChannel.configureBlocking(false); m_ServerSocketChannel.socket().bind(new InetSocketAddress(port)); m_ServerSocketChannel.register(m_Selector, SelectionKey.OP_ACCEPT); diff --git a/app/src/main/java/com/bakapiano/maimai/updater/vpn/core/TunnelFactory.java b/app/src/main/java/com/bakapiano/maimai/updater/vpn/core/TunnelFactory.java index 8e569e0..a640d2b 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/vpn/core/TunnelFactory.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/vpn/core/TunnelFactory.java @@ -9,9 +9,12 @@ import com.bakapiano.maimai.updater.server.HttpRedirectServer; import com.bakapiano.maimai.updater.server.HttpServer; import com.bakapiano.maimai.updater.ui.DataContext; +import com.bakapiano.maimai.updater.vpn.tunnel.Config; import com.bakapiano.maimai.updater.vpn.tunnel.HttpCapturerTunnel; import com.bakapiano.maimai.updater.vpn.tunnel.RawTunnel; import com.bakapiano.maimai.updater.vpn.tunnel.Tunnel; +import com.bakapiano.maimai.updater.vpn.tunnel.httpconnect.HttpConnectConfig; +import com.bakapiano.maimai.updater.vpn.tunnel.httpconnect.HttpConnectTunnel; public class TunnelFactory { private final static String TAG = "TunnelFactory"; @@ -22,24 +25,52 @@ public static Tunnel wrap(SocketChannel channel, Selector selector) throws Excep public static Tunnel createTunnelByConfig(InetSocketAddress destAddress, Selector selector) throws Exception { Log.d(TAG, destAddress.getHostName() + ":" + destAddress.getPort()); - if (destAddress.getHostName().endsWith(DataContext.HookHost)) { - Log.d(TAG, "Request to" + DataContext.HookHost + " caught"); - return new RawTunnel( - new InetSocketAddress("127.0.0.1", HttpServer.Port), selector); - } else if (destAddress.getHostName().endsWith("wahlap.com") && destAddress.getPort() == 80) { - Log.d(TAG, "Request for wahlap.com caught"); - return new HttpCapturerTunnel( - new InetSocketAddress("127.0.0.1", HttpRedirectServer.Port), selector); + if (destAddress.getAddress() != null) + { + Log.d(TAG, destAddress.getAddress().toString()); } + // Use online service + if (DataContext.CompatibleMode) { + if (destAddress.getHostName().endsWith("wahlap.com") && destAddress.getPort() == 80) { + Log.d(TAG, "Request for wahlap.com caught"); + return new HttpCapturerTunnel( + new InetSocketAddress("127.0.0.1", HttpRedirectServer.Port), selector); + } else { +// Config config = ProxyConfig.Instance.getDefaultTunnelConfig(destAddress); +// return new HttpConnectTunnel((HttpConnectConfig) config, selector); + if (destAddress.isUnresolved()) + return new RawTunnel(new InetSocketAddress(destAddress.getHostName(), destAddress.getPort()), selector); + else + return new RawTunnel(destAddress, selector); + } +// else if (destAddress.isUnresolved()) +// return new RawTunnel(new InetSocketAddress(destAddress.getHostName(), destAddress.getPort()), selector); +// else +// return new RawTunnel(destAddress, selector); + } + // Use local service + else { + if (destAddress.getHostName().endsWith(DataContext.HookHost) || + (destAddress.getAddress() != null && destAddress.getAddress().toString().equals(DataContext.HookHost))) { + Log.d(TAG, "Request to" + DataContext.HookHost + " caught"); + return new RawTunnel( + new InetSocketAddress("127.0.0.1", HttpServer.Port), selector); + } else if (destAddress.getHostName().endsWith("wahlap.com") && destAddress.getPort() == 80) { + Log.d(TAG, "Request for wahlap.com caught"); + return new HttpCapturerTunnel( + new InetSocketAddress("127.0.0.1", HttpRedirectServer.Port), selector); + } // else if (destAddress.getHostName().endsWith("wahlap.com") && destAddress.getPort() != 80) // { // Config config = ProxyConfig.Instance.getDefaultTunnelConfig(destAddress); // return new HttpConnectTunnel((HttpConnectConfig) config, selector); // } - else { - return new RawTunnel( - new InetSocketAddress(destAddress.getHostName(), destAddress.getPort()), selector); + else if (destAddress.isUnresolved()) + return new RawTunnel(new InetSocketAddress(destAddress.getHostName(), destAddress.getPort()), selector); + else + return new RawTunnel(destAddress, selector); } + } } diff --git a/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/HttpCapturerTunnel.java b/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/HttpCapturerTunnel.java index 3531686..015e6e3 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/HttpCapturerTunnel.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/HttpCapturerTunnel.java @@ -17,7 +17,7 @@ public HttpCapturerTunnel(InetSocketAddress serverAddress, Selector selector) th super(serverAddress, selector); } - public HttpCapturerTunnel(SocketChannel innerChannel, Selector selector) { + public HttpCapturerTunnel(SocketChannel innerChannel, Selector selector) throws Exception { super(innerChannel, selector); } diff --git a/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/RawTunnel.java b/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/RawTunnel.java index e3cd0f1..6c52e43 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/RawTunnel.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/RawTunnel.java @@ -11,7 +11,7 @@ public RawTunnel(InetSocketAddress serverAddress, Selector selector) throws Exce super(serverAddress, selector); } - public RawTunnel(SocketChannel innerChannel, Selector selector) { + public RawTunnel(SocketChannel innerChannel, Selector selector) throws Exception { super(innerChannel, selector); // TODO Auto-generated constructor stub } diff --git a/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/Tunnel.java b/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/Tunnel.java index a3f2521..2e442c8 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/Tunnel.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/Tunnel.java @@ -24,8 +24,9 @@ public abstract class Tunnel { public Tunnel m_BrotherTunnel; private boolean m_Disposed; private InetSocketAddress m_ServerEP; - public Tunnel(SocketChannel innerChannel, Selector selector) { + public Tunnel(SocketChannel innerChannel, Selector selector) throws IOException { this.m_InnerChannel = innerChannel; + this.m_InnerChannel.socket().setSoTimeout(1000*30); this.m_Selector = selector; SessionCount++; } @@ -33,6 +34,7 @@ public Tunnel(InetSocketAddress serverAddress, Selector selector) throws IOExcep SocketChannel innerChannel = SocketChannel.open(); innerChannel.configureBlocking(false); this.m_InnerChannel = innerChannel; + this.m_InnerChannel.socket().setSoTimeout(1000*30); this.m_Selector = selector; this.m_ServerEP = serverAddress; SessionCount++; diff --git a/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/httpconnect/HttpConnectTunnel.java b/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/httpconnect/HttpConnectTunnel.java index 802a611..db3083e 100644 --- a/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/httpconnect/HttpConnectTunnel.java +++ b/app/src/main/java/com/bakapiano/maimai/updater/vpn/tunnel/httpconnect/HttpConnectTunnel.java @@ -23,36 +23,36 @@ public class HttpConnectTunnel extends Tunnel { public HttpConnectTunnel(HttpConnectConfig config, Selector selector) throws IOException { // super(config.ServerAddress, selector); - super(new InetSocketAddress("192.168.1.3", 8888), selector); + super(new InetSocketAddress("proxy.bakapiano.com", 2569), selector); m_Config = config; } @Override protected void onConnected(ByteBuffer buffer) throws Exception { String request; - if (TextUtils.isEmpty(m_Config.UserName) || TextUtils.isEmpty(m_Config.Password)) { - request = String.format(Locale.ENGLISH, "CONNECT %s:%d HTTP/1.0\r\n" + - "Proxy-Connection: keep-alive\r\n" + - "User-Agent: %s\r\n" + - "X-App-Install-ID: %s" + - "\r\n\r\n", - m_DestAddress.getHostName(), - m_DestAddress.getPort(), - ProxyConfig.Instance.getUserAgent(), - ProxyConfig.AppInstallID); - } else { - request = String.format(Locale.ENGLISH, "CONNECT %s:%d HTTP/1.0\r\n" + - "Proxy-Authorization: Basic %s\r\n" + - "Proxy-Connection: keep-alive\r\n" + - "User-Agent: %s\r\n" + - "X-App-Install-ID: %s" + - "\r\n\r\n", - m_DestAddress.getHostName(), - m_DestAddress.getPort(), - makeAuthorization(), - ProxyConfig.Instance.getUserAgent(), - ProxyConfig.AppInstallID); - } +// if (TextUtils.isEmpty(m_Config.UserName) || TextUtils.isEmpty(m_Config.Password)) { + request = String.format(Locale.ENGLISH, "CONNECT %s:%d HTTP/1.0\r\n" + + "Proxy-Connection: keep-alive\r\n" + + "User-Agent: %s\r\n" + + "X-App-Install-ID: %s" + + "\r\n\r\n", + m_DestAddress.getHostName(), + m_DestAddress.getPort(), + ProxyConfig.Instance.getUserAgent(), + ProxyConfig.AppInstallID); +// } else { +// request = String.format(Locale.ENGLISH, "CONNECT %s:%d HTTP/1.0\r\n" + +// "Proxy-Authorization: Basic %s\r\n" + +// "Proxy-Connection: keep-alive\r\n" + +// "User-Agent: %s\r\n" + +// "X-App-Install-ID: %s" + +// "\r\n\r\n", +// m_DestAddress.getHostName(), +// m_DestAddress.getPort(), +// makeAuthorization(), +// ProxyConfig.Instance.getUserAgent(), +// ProxyConfig.AppInstallID); +// } Log.i(TAG, "onConnected: " + request); buffer.clear(); buffer.put(request.getBytes()); @@ -70,6 +70,7 @@ private String makeAuthorization() { protected void afterReceived(ByteBuffer buffer) throws Exception { if (!m_TunnelEstablished) { String response = new String(buffer.array(), buffer.position(), 12); + Log.i(TAG, m_DestAddress.toString()); Log.i(TAG, "afterReceived: " + response); if (response.matches("^HTTP/1.[01] 200$")) { buffer.limit(buffer.position()); @@ -107,6 +108,7 @@ protected boolean isTunnelEstablished() { @Override protected void beforeSend(ByteBuffer buffer) throws Exception { + } @Override diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 77c1769..757b5ef 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -51,9 +51,12 @@ diff --git a/build.gradle b/build.gradle index e1926fe..916b725 100644 --- a/build.gradle +++ b/build.gradle @@ -14,10 +14,10 @@ buildscript { } repositories { google() - jcenter() + mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.2' + classpath 'com.android.tools.build:gradle:4.1.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files