Skip to content

Commit e2f2cef

Browse files
authored
CF turnstile 대응 (#64)
1 parent 13598ba commit e2f2cef

11 files changed

Lines changed: 93 additions & 79 deletions

File tree

app/src/androidTest/java/ml/melun/mangaview/ExampleInstrumentedTest.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/src/main/java/ml/melun/mangaview/Utils.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,15 @@ public static boolean checkConnection(Context context){
276276

277277

278278

279-
public static void showCaptchaPopup(Context context, int code, Exception e, boolean force_close, Fragment fragment, Preference p){
279+
public static void showCaptchaPopup(String url, Context context, int code, Exception e, boolean force_close, Fragment fragment, Preference p){
280280
if(context != null) {
281281
if (!checkConnection(context)) {
282282
//no internet
283283
//showErrorPopup(context, "네트워크 연결이 없습니다.", e, force_close);
284284
Toast.makeText(context, "네트워크 연결이 없습니다.", Toast.LENGTH_LONG).show();
285285
if (force_close) ((Activity) context).finish();
286286
} else if (captchaCount == 0) {
287-
startCaptchaActivity(context, code, fragment);
287+
startCaptchaActivity(context, code, fragment, url);
288288
} else {
289289
AlertDialog.Builder builder;
290290
String title = "오류";
@@ -297,7 +297,7 @@ public static void showCaptchaPopup(Context context, int code, Exception e, bool
297297
.setNeutralButton("확인", (dialogInterface, i) -> {
298298
if (force_close) ((Activity) context).finish();
299299
})
300-
.setPositiveButton("CAPTCHA 인증", (dialog, which) -> startCaptchaActivity(context, code, fragment))
300+
.setPositiveButton("CAPTCHA 인증", (dialog, which) -> startCaptchaActivity(context, code, fragment, url))
301301
.setNegativeButton("URL 설정", (dialogInterface, i) -> urlSettingPopup(context, p))
302302
.setOnCancelListener(dialogInterface -> {
303303
if (force_close) ((Activity) context).finish();
@@ -315,6 +315,16 @@ public static void showCaptchaPopup(Context context, int code, Exception e, bool
315315
}
316316
}
317317

318+
static void startCaptchaActivity(Context context, int code, Fragment fragment, String url){
319+
Intent captchaIntent = new Intent(context, CaptchaActivity.class);
320+
System.out.println("ppppsend " + url);
321+
captchaIntent.putExtra("url", url);
322+
if(fragment == null)
323+
((Activity)context).startActivityForResult(captchaIntent, code);
324+
else
325+
fragment.startActivityForResult(captchaIntent, code);
326+
}
327+
318328
static void startCaptchaActivity(Context context, int code, Fragment fragment){
319329
Intent captchaIntent = new Intent(context, CaptchaActivity.class);
320330
if(fragment == null)
@@ -323,30 +333,40 @@ static void startCaptchaActivity(Context context, int code, Fragment fragment){
323333
fragment.startActivityForResult(captchaIntent, code);
324334
}
325335

326-
public static void showCaptchaPopup(Context context, int code, Exception e, boolean force_close, Preference p) {
327-
showCaptchaPopup(context,code,e,force_close,null, p);
336+
public static void showCaptchaPopup(String url, Context context, int code, Exception e, boolean force_close, Preference p) {
337+
showCaptchaPopup(url, context,code,e,force_close,null, p);
328338
}
329339

330-
public static void showCaptchaPopup(Context context, Exception e, Preference p) {
340+
public static void showCaptchaPopup(String url, Context context, Exception e, Preference p) {
331341
// viewer call
332-
showCaptchaPopup(context, REQUEST_CAPTCHA, e, true, p);
342+
showCaptchaPopup(url, context, REQUEST_CAPTCHA, e, true, p);
333343
}
334344

335-
public static void showCaptchaPopup(Context context, int code, Preference p){
345+
public static void showCaptchaPopup(String url, Context context, int code, Preference p){
336346
// menu call
337-
showCaptchaPopup(context, code, null, false, p);
347+
showCaptchaPopup(url, context, code, null, false, p);
348+
}
349+
350+
public static void showCaptchaPopup(String url, Context context, int code, Fragment fragment, Preference p){
351+
// menu call
352+
showCaptchaPopup(url, context, code, null, false, fragment, p);
338353
}
339354

340355
public static void showCaptchaPopup(Context context, int code, Fragment fragment, Preference p){
341356
// menu call
342-
showCaptchaPopup(context, code, null, false, fragment, p);
357+
showCaptchaPopup(null, context, code, null, false, fragment, p);
343358
}
344359

360+
public static void showCaptchaPopup(String url, Context context, Preference p){
361+
// viewer call
362+
showCaptchaPopup(url, context, 0, null, true, p);
363+
}
345364
public static void showCaptchaPopup(Context context, Preference p){
346365
// viewer call
347-
showCaptchaPopup(context, 0, null, true, p);
366+
showCaptchaPopup(null, context, 0, null, true, p);
348367
}
349368

369+
350370
public static void showTokiCaptchaPopup(Context context, Preference p){
351371
AlertDialog.Builder builder;
352372
String title = "캡차 인증";

app/src/main/java/ml/melun/mangaview/activity/CaptchaActivity.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.view.MotionEvent;
1414
import android.view.View;
1515
import android.webkit.CookieManager;
16+
import android.webkit.WebChromeClient;
1617
import android.webkit.WebResourceError;
1718
import android.webkit.WebResourceRequest;
1819
import android.webkit.WebResourceResponse;
@@ -23,6 +24,7 @@
2324

2425
import java.net.MalformedURLException;
2526
import java.net.URL;
27+
import java.util.Map;
2628

2729
import ml.melun.mangaview.R;
2830
import ml.melun.mangaview.Utils;
@@ -45,8 +47,13 @@ protected void onCreate(Bundle savedInstanceState) {
4547
super.onCreate(savedInstanceState);
4648
Context context = this;
4749
setContentView(R.layout.activity_captcha);
50+
4851
String purl = p.getUrl();
4952

53+
Intent intent = getIntent();
54+
String path = intent.getStringExtra("url");
55+
String url = purl + (path == null ? "" : path);
56+
5057
TextView infoText = this.findViewById(R.id.infoText);
5158
try {
5259
URL u = new URL(purl);
@@ -62,29 +69,29 @@ protected void onCreate(Bundle savedInstanceState) {
6269
webView = this.findViewById(R.id.captchaWebView);
6370

6471
WebSettings settings = webView.getSettings();
65-
settings.setUserAgentString("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
6672
settings.setJavaScriptEnabled(true);
6773
settings.setDomStorageEnabled(true);
6874
CookieManager cookiem = CookieManager.getInstance();
69-
cookiem.removeAllCookie();
75+
cookiem.removeAllCookies(null);
7076

7177
WebViewClient client = new WebViewClient() {
7278

73-
@Nullable
74-
@Override
75-
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
76-
return null;
77-
}
78-
7979
@Override
8080
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
8181
//super.onReceivedError(view, request, error);
8282
showPopup(context, "오류", "연결에 실패했습니다. URL을 확인해 주세요");
8383
}
8484

85+
@Nullable
86+
@Override
87+
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
88+
httpClient.agent = request.getRequestHeaders().get("User-Agent");
89+
return super.shouldInterceptRequest(view, request);
90+
}
91+
8592
@Override
8693
public void onLoadResource(WebView view, String url) {
87-
System.out.println(url);
94+
8895
if(url.contains("bootstrap") || url.contains("jquery")){
8996
// read cookies and finish
9097
try {
@@ -93,7 +100,6 @@ public void onLoadResource(WebView view, String url) {
93100
for (String s : cookieStr.split("; ")) {
94101
String k = s.substring(0, s.indexOf("="));
95102
String v = s.substring(s.indexOf("=") + 1);
96-
//System.out.println(k + " : " + v);
97103
httpClient.setCookie(k, v);
98104
}
99105
}
@@ -104,23 +110,22 @@ public void onLoadResource(WebView view, String url) {
104110
Utils.showErrorPopup(context, "인증 도중 오류가 발생했습니다. 네트워크 연결 상태를 확인해주세요.", e, true);
105111
}
106112

107-
} else {
108-
super.onLoadResource(view, url);
109113
}
114+
super.onLoadResource(view, url);
110115
}
111116
};
112117

113118
webView.setWebViewClient(client);
114119

115-
webView.setOnTouchListener((view, motionEvent) -> true);
120+
// webView.setOnTouchListener((view, motionEvent) -> true);
116121

117-
Login login = p.getLogin();
118-
if(login != null && login.getCookie() !=null && login.getCookie().length()>0){
119-
//session exists
120-
cookiem.setCookie(purl, login.getCookie(true));
121-
}
122+
// Login login = p.getLogin();
123+
// if(login != null && login.getCookie() !=null && login.getCookie().length()>0){
124+
// //session exists
125+
// cookiem.setCookie(purl, login.getCookie(true));
126+
// }
122127

123-
webView.loadUrl(purl);
128+
webView.loadUrl(url);
124129

125130
new Handler(Looper.getMainLooper()).postDelayed(() -> {
126131
//Do something after 100ms

app/src/main/java/ml/melun/mangaview/activity/EpisodeActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ protected void onPostExecute(Integer res) {
392392
showTokiCaptchaPopup(context, p);
393393
return;
394394
}else if(episodes == null || episodes.size()==0){
395-
showCaptchaPopup(context, p);
395+
showCaptchaPopup(title.getUrl(), context, p);
396396
return;
397397
}else {
398398
afterLoad();

app/src/main/java/ml/melun/mangaview/activity/ViewerActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public void setManga(Manga m){
307307
try {
308308
lockUi(false);
309309
if(m.getImgs(context) == null || m.getImgs(context).size()==0) {
310-
showCaptchaPopup(context, p);
310+
showCaptchaPopup(m.getUrl(), context, p);
311311
return;
312312
}
313313
stripAdapter = new StripAdapter(context, m, autoCut, width,title, infiniteScrollCallback);
@@ -318,7 +318,7 @@ public void setManga(Manga m){
318318
updateIntent(m);
319319

320320
}catch (Exception e){
321-
Utils.showCaptchaPopup(context, e, p);
321+
Utils.showCaptchaPopup(m.getUrl(), context, e, p);
322322
e.printStackTrace();
323323
}
324324
}

app/src/main/java/ml/melun/mangaview/activity/ViewerActivity2.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ public void onLoadFailed(@Nullable Drawable errorDrawable) {
527527
});
528528
}catch(Exception e) {
529529
e.printStackTrace();
530-
Utils.showCaptchaPopup(context, e, p);
530+
Utils.showCaptchaPopup(manga.getUrl(), context, e, p);
531531
}
532532
//일단 왼쪽거 냅두다가, 오른쪽이 landscape일 경우, GONE 처리
533533
}
@@ -687,7 +687,7 @@ public void onLoadFailed(@Nullable Drawable errorDrawable) {
687687
});
688688
}catch(Exception e) {
689689
e.printStackTrace();
690-
Utils.showCaptchaPopup(context, e, p);
690+
Utils.showCaptchaPopup(manga.getUrl(), context, e, p);
691691
}
692692
}
693693

@@ -849,7 +849,7 @@ public void reloadManga(){
849849
lockUi(false);
850850
imgs = manga.getImgs(context);
851851
if(imgs == null || imgs.size()==0) {
852-
showCaptchaPopup(context, p);
852+
showCaptchaPopup(manga.getUrl(), context, p);
853853
return;
854854
}
855855
d = new Decoder(manga.getSeed(), manga.getId());

app/src/main/java/ml/melun/mangaview/activity/ViewerActivity3.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ public void reloadManga(){
403403
lockUi(false);
404404
imgs = manga.getImgs(context);
405405
if(imgs == null || imgs.size()==0) {
406-
showCaptchaPopup(context, p);
406+
showCaptchaPopup(manga.getUrl(), context, p);
407407
return;
408408
}
409409
refreshAdapter();
@@ -418,7 +418,7 @@ public void reloadManga(){
418418
for(StackTraceElement s : stack){
419419
message.append(s.toString()).append('\n');
420420
}
421-
Utils.showCaptchaPopup(context, e, p);
421+
Utils.showCaptchaPopup(manga.getUrl(), context, e, p);
422422
}
423423
}
424424

app/src/main/java/ml/melun/mangaview/mangaview/CustomHttpClient.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
public class CustomHttpClient {
2828
public OkHttpClient client;
2929
Map<String, String> cookies;
30+
public String agent = "Mozilla/5.0 (Linux; Android 13; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36";
3031

3132
public CustomHttpClient(){
3233
System.out.println("http client create");
@@ -112,12 +113,14 @@ public String getUrl(){
112113
public Response mget(String url, Boolean doLogin, Map<String, String> customCookie){
113114
if(customCookie==null)
114115
customCookie = new HashMap<>();
115-
if(doLogin && p.getLogin() != null && p.getLogin().cookie != null && p.getLogin().cookie.length()>0){
116-
customCookie.put("PHPSESSID", p.getLogin().cookie);
117-
}
116+
// if(doLogin && p.getLogin() != null && p.getLogin().cookie != null && p.getLogin().cookie.length()>0){
117+
// customCookie.put("PHPSESSID", p.getLogin().cookie);
118+
// }
118119
Map<String, String> cookie = new HashMap<>(this.cookies);
119120
cookie.putAll(customCookie);
120121

122+
123+
121124
StringBuilder cbuilder = new StringBuilder();
122125
for(String key : cookie.keySet()){
123126
cbuilder.append(key);
@@ -128,10 +131,12 @@ public Response mget(String url, Boolean doLogin, Map<String, String> customCook
128131
if(cbuilder.length()>2)
129132
cbuilder.delete(cbuilder.length()-2,cbuilder.length());
130133

134+
System.out.println("ppppcookie: "+cbuilder.toString());
135+
131136
Map<String, String> headers = new HashMap<>();
132137
headers.put("Cookie", cbuilder.toString());
133-
headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36");
134-
//headers.put("Referer",p.getUrl());
138+
headers.put("User-Agent", agent);
139+
headers.put("Referer",p.getUrl());
135140

136141
return get(p.getUrl()+url, headers);
137142
}

app/src/main/java/ml/melun/mangaview/mangaview/Manga.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public int fetch(CustomHttpClient client, boolean doLogin, Map<String, String> c
9494
int tries = 0;
9595

9696
while (imgs.size() == 0 && tries < 2) {
97-
Response r = client.mget('/' + baseModeStr(baseMode) + '/' + id, false, cookies);
97+
Response r = client.mget( baseModeStr(baseMode) + '/' + id, false, cookies);
9898
try {
9999
if (r.code() == 302 && r.header("location").contains("captcha.php")) {
100100
return LOAD_CAPTCHA;
@@ -110,6 +110,8 @@ public int fetch(CustomHttpClient client, boolean doLogin, Map<String, String> c
110110

111111
Document d = Jsoup.parse(body);
112112

113+
System.out.println(body);
114+
113115
//name
114116
name = d.selectFirst("div.toon-title").ownText();
115117

@@ -340,7 +342,7 @@ public void setMode(int mode) {
340342
}
341343

342344
public String getUrl() {
343-
return "/manga/" + id;
345+
return '/' + baseModeStr(baseMode) + '/' + id;
344346
}
345347

346348
public boolean useBookmark() {

app/src/main/java/ml/melun/mangaview/mangaview/Title.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import okhttp3.RequestBody;
1818
import okhttp3.Response;
1919

20+
import static ml.melun.mangaview.MainApplication.p;
2021
import static ml.melun.mangaview.Utils.getNumberFromString;
2122

2223

@@ -40,6 +41,10 @@ public Title(String n, String t, String a, List<String> tg, String r, int id, in
4041
super(n, id, t, a, tg, r, baseMode);
4142
}
4243

44+
public String getUrl(){
45+
return '/'+baseModeStr(baseMode)+'/'+ id;
46+
}
47+
4348

4449
public Title(MTitle title){
4550
super(title.getName(), title.getId(), title.getThumb(), title.getAuthor(), title.getTags(), title.getRelease(), title.getBaseMode());

0 commit comments

Comments
 (0)