diff --git a/app/src/main/java/com/github/catvod/api/TianYiHandler.java b/app/src/main/java/com/github/catvod/api/TianYiHandler.java index e7534502..f45dd64b 100644 --- a/app/src/main/java/com/github/catvod/api/TianYiHandler.java +++ b/app/src/main/java/com/github/catvod/api/TianYiHandler.java @@ -8,6 +8,7 @@ import android.view.Gravity; import android.widget.FrameLayout; import android.widget.ImageView; import com.github.catvod.bean.uc.Cache; +import com.github.catvod.bean.uc.User; import com.github.catvod.crawler.SpiderDebug; import com.github.catvod.net.OkHttp; import com.github.catvod.net.OkResult; @@ -20,7 +21,6 @@ import okhttp3.Request; import okhttp3.Response; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.commons.lang3.time.DateUtils; import java.io.File; import java.io.IOException; @@ -30,8 +30,6 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import static org.apache.commons.lang3.time.DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT; - public class TianYiHandler { private ScheduledExecutorService service; @@ -44,92 +42,14 @@ public class TianYiHandler { private String indexUrl = ""; + private String reqId; + private String lt; + public TianYiHandler() { cache = Cache.objectFrom(Path.read(getCache())); } - public JsonObject getUUID() throws IOException { - Map params = new HashMap<>(); - params.put("appId", "E_189"); - Map headers = new HashMap<>(); - headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"); - - OkResult okResult = OkHttp.post("https://open.e.189.cn/api/logbox/oauth2/getUUID.do", params, headers); - if (okResult.getCode() == 200) { - return Json.safeObject(okResult.getBody()); - } - - return null; - } - - public byte[] downloadQRCode(String uuid, String reqId,String cookie) throws IOException { - - - Map headers = new HashMap<>(); - headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"); - - headers.put("referer", indexUrl); - headers.put("cookie", cookie); - // OkResult okResult = OkHttp.get("https://open.e.189.cn/api/logbox/oauth2/image.do", params, headers); - - HttpUrl url = HttpUrl.parse("https://open.e.189.cn/api/logbox/oauth2/image.do").newBuilder().addQueryParameter("uuid", uuid).addQueryParameter("REQID", reqId).build(); - - Request request = new Request.Builder().url(url).headers(Headers.of(headers)).build(); - Response response = OkHttp.newCall(request); - if (response.code() == 200) { - return response.body().bytes(); - } - return null; - } - - - private Map checkLoginStatus(String uuid, String encryuuid, String reqId, String lt, String paramId, String returnUrl, String secondCookie) throws IOException { - Map params = new HashMap<>(); - params.put("appId", "E_189"); - params.put("encryuuid", encryuuid); - params.put("uuid", uuid); - params.put("date", DateFormatUtils.format(new Date(),"yyyy-MM-ddHH:mm:ss")+new Random().nextInt(24));; - params.put("returnUrl", URLEncoder.encode(returnUrl, "UTF-8")); - params.put("clientType", "1"); - params.put("timeStamp", String.valueOf(System.currentTimeMillis())); - params.put("cb_SaveName", "0"); - params.put("isOauth2", "false"); - params.put("state", ""); - params.put("paramId", paramId); - Map headers = new HashMap<>(); - headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"); - headers.put("lt", lt); - headers.put("origin", "https,//open.e.189.cn"); - headers.put("referer", indexUrl); - headers.put("Reqid", reqId); - OkResult okResult = OkHttp.post("https://open.e.189.cn/api/logbox/oauth2/qrcodeLoginState.do", params, headers); - JsonObject obj = Json.safeObject(okResult.getBody()).getAsJsonObject(); - if (okResult.getCode() == 200 && obj.get("status").getAsInt() == 0) { - - String redirectUrl = obj.get("redirectUrl").getAsString(); - Map result = new HashMap<>(); - fetchUserInfo(redirectUrl, secondCookie, result); - } - - - return null; - } - - private void fetchUserInfo(String redirectUrl, String secondCookie, Map result) throws IOException { - - Map params = new HashMap<>(); - Map headers = new HashMap<>(); - headers.put("Cookie", secondCookie); - Map> okResult = OkHttp.getLocationHeader(redirectUrl, headers); - /* if (okResult.getCode() == 200) { - okResult.getBody(); - }*/ - return ; - - } - - public byte[] startScan() throws Exception { OkResult okResult1 = OkHttp.get("https://ux.21cn.com/api/htmlReportRest/getJs.js?pid=25577E0DEEDF48ADBD4459911F5825E4", new HashMap<>(), new HashMap<>()); @@ -140,10 +60,13 @@ public class TianYiHandler { String cookie1 = split[0]; cookieList.add(cookie1); } - String firstCookie = StringUtils.join(cookieList, ";"); - String index = OkHttp.getLocation("https://cloud.189.cn/api/portal/loginUrl.action?redirectURL=https://cloud.189.cn/web/redirect.html&defaultSaveName=3&defaultSaveNameCheck=uncheck&browserId=8d38da4344fba4699d13d6e6854319d7", Map.of("Cookie", firstCookie)); + //String firstCookie = StringUtils.join(cookieList, ";"); + SpiderDebug.log("index ori: " + "https://cloud.189.cn/api/portal/loginUrl.action?redirectURL=https%3A%2F%2Fcloud.189.cn%2Fweb%2Fredirect.html&defaultSaveName=3&defaultSaveNameCheck=uncheck&browserId=dff95dced0b03d9d972d920f03ddd05e"); + String index = OkHttp.getLocation("https://cloud.189.cn/api/portal/loginUrl.action?redirectURL=https://cloud.189.cn/web/redirect.html&defaultSaveName=3&defaultSaveNameCheck=uncheck&browserId=8d38da4344fba4699d13d6e6854319d7", Map.of("Cookie", "")); + SpiderDebug.log("index red: " + index); Map> resHeaderMap = OkHttp.getLocationHeader(index, new HashMap<>()); - indexUrl =resHeaderMap.get("Location").get(0); + indexUrl = resHeaderMap.get("Location").get(0); + SpiderDebug.log("indexUrl red: " + indexUrl); cookieList.clear(); for (String s : resHeaderMap.get("Set-Cookie")) { @@ -152,11 +75,13 @@ public class TianYiHandler { cookieList.add(cookie1); } String secondCookie = StringUtils.join(cookieList, ";"); - HttpUrl httpParams = HttpUrl.parse(indexUrl); - String reqId = httpParams.queryParameter("reqId"); - String lt = httpParams.queryParameter("lt"); - Map tHeaders = new HashMap<>(); + SpiderDebug.log("secondCookie: " + secondCookie); + HttpUrl httpParams = HttpUrl.parse(indexUrl); + reqId = httpParams.queryParameter("reqId"); + lt = httpParams.queryParameter("lt"); + + Map tHeaders = new HashMap<>(); tHeaders.put("Content-Type", "application/x-www-form-urlencoded"); tHeaders.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/76.0"); tHeaders.put("Referer", indexUrl); @@ -178,20 +103,22 @@ public class TianYiHandler { paramId = ""; returnUrl = ""; } - + SpiderDebug.log("paramId: " + paramId); + SpiderDebug.log("returnUrl: " + returnUrl); // Step 1: Get UUID JsonObject uuidInfo = getUUID(); String uuid = uuidInfo.get("uuid").getAsString(); String encryuuid = uuidInfo.get("encryuuid").getAsString(); + String encodeuuid = uuidInfo.get("encodeuuid").getAsString(); // Step 2: Get QR Code - byte[] byteStr = downloadQRCode(encryuuid, reqId,secondCookie); + byte[] byteStr = downloadQRCode(encodeuuid, reqId, secondCookie); Init.run(() -> showQRCode(byteStr)); // Step 3: Check login status // return - Init.execute(() -> startService(uuid, encryuuid, reqId, lt, paramId, returnUrl, firstCookie)); + Init.execute(() -> startService(uuid, encryuuid, reqId, lt, paramId, returnUrl, secondCookie)); /*Map result = new HashMap<>(); result.put("qrcode", "data:image/png;base64," + qrCode); result.put("status", "NEW");*/ @@ -199,6 +126,108 @@ public class TianYiHandler { } + public JsonObject getUUID() { + Map params = new HashMap<>(); + params.put("appId", "cloud"); + Map headers = new HashMap<>(); + headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"); + headers.put("lt", lt); + headers.put("reqId", reqId); + headers.put("referer", indexUrl); + + OkResult okResult = OkHttp.post("https://open.e.189.cn/api/logbox/oauth2/getUUID.do", params, headers); + if (okResult.getCode() == 200) { + return Json.safeObject(okResult.getBody()); + } + return null; + } + + public byte[] downloadQRCode(String uuid, String reqId, String cookie) throws IOException { + + + Map headers = new HashMap<>(); + headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"); + + headers.put("referer", indexUrl); + headers.put("cookie", cookie); + // OkResult okResult = OkHttp.get("https://open.e.189.cn/api/logbox/oauth2/image.do", params, headers); +//.addQueryParameter("uuid", uuid).addQueryParameter("REQID", reqId) + HttpUrl url = HttpUrl.parse("https://open.e.189.cn/api/logbox/oauth2/image.do?uuid=" + uuid + "&REQID=" + reqId).newBuilder().build(); + + Request request = new Request.Builder().url(url).headers(Headers.of(headers)).build(); + Response response = OkHttp.newCall(request); + if (response.code() == 200) { + return response.body().bytes(); + } + return null; + } + + + private Map checkLoginStatus(String uuid, String encryuuid, String reqId, String lt, String paramId, String returnUrl, String secondCookie) throws Exception { + Map params = new HashMap<>(); + params.put("appId", "cloud"); + params.put("encryuuid", encryuuid); + params.put("uuid", uuid); + params.put("date", DateFormatUtils.format(new Date(), "yyyy-MM-ddHH:mm:ss") + new Random().nextInt(24)); + params.put("returnUrl", URLEncoder.encode(returnUrl, "UTF-8")); + params.put("clientType", "1"); + params.put("timeStamp", (System.currentTimeMillis() / 1000 + 1) + "000"); + params.put("cb_SaveName", "0"); + params.put("isOauth2", "false"); + params.put("state", ""); + params.put("paramId", paramId); + Map headers = new HashMap<>(); + headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"); + headers.put("referer", indexUrl); + headers.put("Reqid", reqId); + headers.put("cookie", secondCookie); + OkResult okResult = OkHttp.post("https://open.e.189.cn/api/logbox/oauth2/qrcodeLoginState.do", params, headers); + JsonObject obj = Json.safeObject(okResult.getBody()).getAsJsonObject(); + if (okResult.getCode() == 200 && Objects.nonNull(obj.get("status")) && obj.get("status").getAsInt() == 0) { + SpiderDebug.log("扫码成功------" + obj.get("redirectUrl").getAsString()); + String redirectUrl = obj.get("redirectUrl").getAsString(); + Map result = new HashMap<>(); + + fetchUserInfo(redirectUrl, secondCookie, result); + + + } else { + SpiderDebug.log("扫码失败------" + okResult.getBody()); + } + + + return null; + } + + private void fetchUserInfo(String redirectUrl, String secondCookie, Map result) throws IOException { + + Map params = new HashMap<>(); + Map headers = new HashMap<>(); + headers.put("Cookie", secondCookie); + Map> okResult = OkHttp.getLocationHeader(redirectUrl, headers); + SpiderDebug.log("扫码返回数据:" + Json.toJson(okResult)); + if (okResult.containsKey("set-cookie")) { + List cookieList = new ArrayList<>(); + for (String s : okResult.get("set-cookie")) { + + String[] split = s.split(";"); + String cookie1 = split[0]; + cookieList.add(cookie1); + + } + cache.setTokenUser(User.objectFrom(StringUtils.join(cookieList, ";"))); + SpiderDebug.log("获取cookie成功:" + StringUtils.join(cookieList, ";")); + } + + //停止检验线程,关闭弹窗 + stopService(); + /* if (okResult.getCode() == 200) { + okResult.getBody(); + }*/ + return; + + } + /** * 显示qrcode @@ -247,7 +276,8 @@ public class TianYiHandler { SpiderDebug.log("----checkLoginStatus ing...."); try { checkLoginStatus(uuid, encryuuid, reqId, lt, paramId, returnUrl, secondCookie); - } catch (IOException e) { + } catch (Exception e) { + SpiderDebug.log("----checkLoginStatus error" + e.getMessage()); throw new RuntimeException(e); } diff --git a/app/src/main/java/com/github/catvod/spider/Introduce.java b/app/src/main/java/com/github/catvod/spider/Introduce.java index 6bc443d6..8e4105c1 100644 --- a/app/src/main/java/com/github/catvod/spider/Introduce.java +++ b/app/src/main/java/com/github/catvod/spider/Introduce.java @@ -1,6 +1,7 @@ package com.github.catvod.spider; import android.content.Context; +import com.github.catvod.api.TianYiHandler; import com.github.catvod.api.UCTokenHandler; import com.github.catvod.bean.Class; import com.github.catvod.bean.Result; @@ -24,6 +25,7 @@ public class Introduce extends Spider { public String homeContent(boolean filter) throws Exception { List classes = new ArrayList<>(); classes.add(new Class("1", "UC")); + classes.add(new Class("2", "天翼")); List list = new ArrayList<>(); String pic = "https://androidcatvodspider.netlify.app/wechat.png"; String name = "关注公众号"; @@ -44,6 +46,12 @@ public class Introduce extends Spider { String name = "点击设置Token"; vodList.add(new Vod("UCToken", name, pic)); } + //天翼 + if (tid.equals("2")) { + String pic = "https://androidcatvodspider.netlify.app/wechat.png"; + String name = "点击设置cookie"; + vodList.add(new Vod("天翼cookie", name, pic)); + } return Result.get().vod(vodList).string(); } @@ -56,6 +64,10 @@ public class Introduce extends Spider { UCTokenHandler qrCodeHandler = new UCTokenHandler(); qrCodeHandler.startUC_TOKENScan(); } + if (vodId.equals("天翼cookie")) { + TianYiHandler qrCodeHandler = new TianYiHandler(); + qrCodeHandler.startScan(); + } Vod item = new Vod(); item.setVodId(vodId); item.setVodName("公众号"); diff --git a/app/src/test/java/com/github/catvod/api/TianYiHandlerTest.java b/app/src/test/java/com/github/catvod/api/TianYiHandlerTest.java index d1f0c6ae..53acc63f 100644 --- a/app/src/test/java/com/github/catvod/api/TianYiHandlerTest.java +++ b/app/src/test/java/com/github/catvod/api/TianYiHandlerTest.java @@ -7,6 +7,8 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; +import java.net.URLEncoder; + @RunWith(RobolectricTestRunner.class) public class TianYiHandlerTest { @@ -54,6 +56,11 @@ public class TianYiHandlerTest { FileUtil.writeBytes(qrCode, "c://qrcode.png"); System.out.println(uuid);*/ + + String url = "https://cloud.189.cn/api/portal/callbackUnify.action?browserId=dff95dced0b03d9d972d920f03ddd05e&redirectURL=https%3A%2F%2Fcloud.189.cn%2Fweb%2Fredirect.html"; + + String encode = "https%3A%2F%2Fcloud.189.cn%2Fapi%2Fportal%2FcallbackUnify.action%3FbrowserId%3Ddff95dced0b03d9d972d920f03ddd05e%26redirectURL%3Dhttps%253A%252F%252Fcloud.189.cn%252Fweb%252Fredirect.html"; + assert URLEncoder.encode(url, "UTF-8").equals(encode); } diff --git a/jar/custom_spider.jar b/jar/custom_spider.jar index 6e8e86c9..423e7f83 100644 Binary files a/jar/custom_spider.jar and b/jar/custom_spider.jar differ diff --git a/jar/custom_spider.jar.md5 b/jar/custom_spider.jar.md5 index a531c367..bc1cf8ad 100644 --- a/jar/custom_spider.jar.md5 +++ b/jar/custom_spider.jar.md5 @@ -1 +1 @@ -5367a33a11684f580d80ee0a77aa1cbb +f52b8d1c93ad33ad192b2be11d219e05 diff --git a/json/index.json b/json/index.json index 2ca68afe..ad3715d5 100644 --- a/json/index.json +++ b/json/index.json @@ -1,5 +1,5 @@ { - "spider": "https://androidcatvodspider.netlify.app/jar/custom_spider.jar;md5;5367a33a11684f580d80ee0a77aa1cbb", + "spider": "https://androidcatvodspider.netlify.app/jar/custom_spider.jar;md5;f52b8d1c93ad33ad192b2be11d219e05", "lives": [ { "name": "电视直播", diff --git a/json/index1.json b/json/index1.json index 8afa3d71..d6b2f6fe 100644 --- a/json/index1.json +++ b/json/index1.json @@ -1,5 +1,5 @@ { - "spider": "https://androidcatvodspider.netlify.app/jar/custom_spider.jar;md5;5367a33a11684f580d80ee0a77aa1cbb", + "spider": "https://androidcatvodspider.netlify.app/jar/custom_spider.jar;md5;f52b8d1c93ad33ad192b2be11d219e05", "lives": [ { "name": "直播ipv6", diff --git a/json/index2.json b/json/index2.json index 3c782914..3066ef14 100644 --- a/json/index2.json +++ b/json/index2.json @@ -1,5 +1,5 @@ { - "spider": "https://androidcatvodspider.netlify.app/jar/custom_spider.jar;md5;5367a33a11684f580d80ee0a77aa1cbb", + "spider": "https://androidcatvodspider.netlify.app/jar/custom_spider.jar;md5;f52b8d1c93ad33ad192b2be11d219e05", "lives": [ { "name": "直播ipv6", diff --git a/json/test.json b/json/test.json index 701e7c3b..f5cefa5a 100644 --- a/json/test.json +++ b/json/test.json @@ -1,5 +1,5 @@ { - "spider": "https://androidcatvodspider.netlify.app/jar/custom_spider.jar;md5;5367a33a11684f580d80ee0a77aa1cbb", + "spider": "https://androidcatvodspider.netlify.app/jar/custom_spider.jar;md5;f52b8d1c93ad33ad192b2be11d219e05", "lives": [ { "name": "直播",