Skip to content

Commit

Permalink
Update version to 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
bakapiano committed Jan 1, 2023
1 parent 3699610 commit 20f6e47
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 100 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion min_version
targetSdkVersion target_version
versionCode 1
versionName "1.0"
versionName "1.4"
}
// debug和release使用相同签名,以便用debug包覆盖release包从而调试,
// 如果没有,就会使用默认debug签名,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
Expand All @@ -29,20 +36,28 @@ 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 {
Thread.sleep(3000);
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SimpleCookieJar implements CookieJar {
private final Object lock = new Object();

@Override
public void saveFromResponse(HttpUrl httpUrl, @NotNull List<Cookie> newCookies) {
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> newCookies) {
synchronized (lock) {
HashMap<String, Cookie> map = new HashMap<>();
List<Cookie> oldCookies = cookieStore.get(httpUrl.host());
Expand All @@ -27,8 +27,10 @@ public void saveFromResponse(HttpUrl httpUrl, @NotNull List<Cookie> 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<Cookie> mergedList = new ArrayList<Cookie>();
for (Map.Entry<String, Cookie> pair : map.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,7 @@
import javax.net.ssl.X509TrustManager;

import okhttp3.Call;
import okhttp3.ConnectionSpec;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
Expand All @@ -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
Expand Down Expand Up @@ -111,27 +114,31 @@ 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
this.fetchChunithmData(username, password);
}

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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<length;i++){
int number=random.nextInt(62);
sb.append(str.charAt(number));
}
return sb.toString();
}
}
Loading

0 comments on commit 20f6e47

Please sign in to comment.