From 36e4424749a311a5367b5879d1532ee26d070354 Mon Sep 17 00:00:00 2001 From: iFish Date: Tue, 2 Dec 2014 21:13:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95=E7=89=88=E6=9C=AC=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RwTicketAssistantV2/app/12306/mobileproxy.js | 61 +++++++++++----- RwTicketAssistantV2/app/background.js | 73 ++++++++++++++++++- RwTicketAssistantV2/app/manifest.json | 2 +- TrainInfomationProviderService/Program.cs | 5 +- .../Web/Controllers/TransitController.cs | 2 +- Web12306/Web.config | 8 +- Web12306/js/otn/trainstationsuggest.js | 2 +- Web12306/js/platform/webRequest.js | 3 +- build.cmd | 1 + 9 files changed, 130 insertions(+), 27 deletions(-) diff --git a/RwTicketAssistantV2/app/12306/mobileproxy.js b/RwTicketAssistantV2/app/12306/mobileproxy.js index e6f18b6..d5b7193 100644 --- a/RwTicketAssistantV2/app/12306/mobileproxy.js +++ b/RwTicketAssistantV2/app/12306/mobileproxy.js @@ -94,6 +94,9 @@ })(); $(function () { + var enableDirectAccess = false; + var directAccessCounter = 0; + //监听ajax事件 document.addEventListener("ajaxproxy", function (e) { var detail = e.detail; @@ -125,26 +128,40 @@ $(function () { if (!data.headers["Fish-Referer"]) data.headers["Fish-Referer"] = null; + if (!enableDirectAccess || data.url.indexOf("leftTicket/query") === -1 || (directAccessCounter++) % 3 === 0) { + $.ajax(data).done(function (result, status, xhr) { + handle.success = true; + handle.status = xhr.status; + handle.statusText = xhr.statusText; + handle.text = xhr.responseText; + handle.model = result; + handle.headers = xhr.getAllResponseHeaders(); - $.ajax(data).done(function (result, status, xhr) { - handle.success = true; - handle.status = xhr.status; - handle.statusText = xhr.statusText; - handle.text = xhr.responseText; - handle.model = result; - handle.headers = xhr.getAllResponseHeaders(); + notifyAjaxComplete(handle); + }).fail(function (xhr) { + handle.success = false; + handle.status = xhr.status; + handle.statusText = xhr.statusText || ""; + handle.text = xhr.responseText || ""; + handle.model = null; + handle.headers = xhr.getAllResponseHeaders(); - notifyAjaxComplete(handle); - }).fail(function (xhr) { - handle.success = false; - handle.status = xhr.status; - handle.statusText = xhr.statusText || ""; - handle.text = xhr.responseText || ""; - handle.model = null; - handle.headers = xhr.getAllResponseHeaders(); + notifyAjaxComplete(handle); + }); + } else { + chrome.runtime.sendMessage({ action: "directAccess", data: data }, function(args) { + (function(success, status, statusText, text, headers, model) { + handle.success = success; + handle.status = status; + handle.statusText = statusText || ""; + handle.text = text || ""; + handle.model = model; + handle.headers = headers; - notifyAjaxComplete(handle); - }); + notifyAjaxComplete(handle); + }).apply(this, args); + }); + } e.preventDefault && e.preventDefault(); e.stopPropagation && e.stopPropagation(); }); @@ -205,4 +222,14 @@ $(function () { document.body.dataset["browserVersion"] = m.bv; document.dispatchEvent(new CustomEvent("mobileSupportInitialized")); }); + chrome.runtime.sendMessage({ action: "servervalid" }, function (vm) { + console.log("liebao detected: " + vm.valid); + if (!vm.valid) + return; + + chrome.runtime.sendMessage({ action: "getServerStatus" }, function (m) { + enableDirectAccess = m.enableDirectAccess; + console.log("direct access: " + enableDirectAccess); + }); + }); }); \ No newline at end of file diff --git a/RwTicketAssistantV2/app/background.js b/RwTicketAssistantV2/app/background.js index e273a15..3e22109 100644 --- a/RwTicketAssistantV2/app/background.js +++ b/RwTicketAssistantV2/app/background.js @@ -651,6 +651,7 @@ window.cbl = function (u, h) { (function () { /// 服务器IP var valid = window.external.LieBaoLookupDnsAddress ? 1 : 0; + var enableDirectAccess = typeof (chrome.tabs.httpRequest) === "function"; chrome.runtime.onMessage.addListener(function (m, s, r) { if (m && m.action === "servervalid") @@ -674,7 +675,8 @@ window.cbl = function (u, h) { failed: 0, lastUpdate: null, validList: [], - status: 0 + status: 0, + enableDirectAccess: enableDirectAccess }; var serverlist = [], testedServer = [], @@ -1012,6 +1014,7 @@ window.cbl = function (u, h) { var checkAndStart = function () { if (localStorage['serverStorage']) { serverStorage = JSON.parse(localStorage['serverStorage']); + serverStorage.enableDirectAccess = enableDirectAccess; } if (!serverStorage.lastUpdate || (new Date().getTime() / 3600000) - new Date(serverStorage.lastUpdate).getTime() / 3600000 >= 12 || serverStorage.validList.length < 5) { inupdateMode = true; @@ -1055,8 +1058,74 @@ window.cbl = function (u, h) { entry12306 = true; } chrome.runtime.onMessage.addListener(function (m, s, r) { - if (m && m.action === 'triggerUpdate') { + if (!m || !m.action) + return; + + if (m.action === 'triggerUpdate') { triggerUpdate(); + } else if (m.action === "directAccess") { + var data = m.data; + var host = /^https?:\/\/([^\/]+)\//.exec(data.url) && RegExp.$1;; + var hostip = null; + var hostindex = 0; + if (enableDirectAccess && host && serverStorage.validList && serverStorage.validList[host] && serverStorage.validList[host].length) { + var hosts = serverStorage.validList[host]; + hostindex = Math.floor(Math.random() * hosts.length); + hostip = hosts[hostindex]; + data.url = data.url.replace("//" + host, "//" + hostip.ip); + } + + if (hostip) { + //build header + var headers = []; + if (data.headers) { + for (var i in data.headers) { + var key = i; + if (key.indexOf("Fish-") === 0) + key = key.substr(5); + headers.push(key + ": " + data.headers[i]); + } + } + headers.push("Host: " + host); + //using direct access + if (data.data) { + data.url += "?" + $.param(data.data); + } + chrome.tabs.httpRequest( + data.url, + headers.join("\r\n"), + 20000, + function (response) { + (function (success, status, text, rHeader, base64) { + if (!success || status !== 200) { + //failed. + r([false, status, status, text, rHeader || "", null]); + + //block error ip + hostip.failedCount = (hostip.failedCount || 0) + 1; + console.log("bad response for host [" + hostip.ip + "], increase failed counter. current at " + hostip.failedCount); + if (hostip.failedCount > 3) { + //block error ip + serverStorage.validList.splice(hostindex, 1); + console.log("bad response for host [" + hostip.ip + "] failed too many times, removed from validlist."); + localStorage["serverStorage"] = JSON.stringify(serverStorage); + } + } else { + r([true, status, status, text, rHeader || "", JSON.parse(text)]); + } + }).apply(this, JSON.parse(response)); + } + ); + } else { + //doing a regular access + $.ajax(data).done(function (result, status, xhr) { + r([true, xhr.status, xhr.statusText, xhr.responseText, xhr.getAllResponseHeaders(), result]); + }).fail(function (xhr) { + r([false, xhr.status, xhr.statusText, xhr.responseText, "", null]); + }); + } + + return true; } }); chrome.runtime.onMessageExternal.addListener(function (m, s, r) { diff --git a/RwTicketAssistantV2/app/manifest.json b/RwTicketAssistantV2/app/manifest.json index ba89e9d..de0bdce 100644 --- a/RwTicketAssistantV2/app/manifest.json +++ b/RwTicketAssistantV2/app/manifest.json @@ -56,7 +56,7 @@ "description": "12306订票助手 v7: 帮您订票的小助手 by 木鱼,全力为您的车票购买献计献策!", "key": "7k6gnXVACvUPU2DfslJgSrWJTHqIg5uwd+Kgl/5zSg==", "name": "猎豹抢票党/12306订票助手 V7", - "version": "7.2.1", + "version": "7.3.1", "manifest_version": 2, "icons": { "16": "icons/icon_16.png", diff --git a/TrainInfomationProviderService/Program.cs b/TrainInfomationProviderService/Program.cs index d2c8a62..928b8c2 100644 --- a/TrainInfomationProviderService/Program.cs +++ b/TrainInfomationProviderService/Program.cs @@ -29,7 +29,10 @@ namespace TrainInfomationProviderService public static void WebLoader() { - RouteTable.Routes.MapRoute("traintransit", "tt/{action}/{id}", new { area = "", controller = "Transit", id = UrlParameter.Optional }, new { action = "(keepalive|ls|ss|tor|dt)" }, new[] { "TrainInfomationProviderService.Web" }); + if (ConfigurationManager.AppSettings["local_disable_train_provider"] == "1") + return; + + RouteTable.Routes.MapRoute("traintransit", "tt/{action}/{id}", new { area = "", controller = "Transit", id = UrlParameter.Optional }, new { action = "(keepalive|ls|ss|tor|dt|ls2)" }, new[] { "TrainInfomationProviderService.Web" }); Task.Run(() => { try diff --git a/TrainInfomationProviderService/Web/Controllers/TransitController.cs b/TrainInfomationProviderService/Web/Controllers/TransitController.cs index 9a44915..a2a1ba1 100644 --- a/TrainInfomationProviderService/Web/Controllers/TransitController.cs +++ b/TrainInfomationProviderService/Web/Controllers/TransitController.cs @@ -36,7 +36,7 @@ namespace TrainInfomationProviderService.Web.Controllers if (string.IsNullOrEmpty(data)) return null; - return LogSameStation(Encoding.UTF8.GetString(Convert.FromBase64String(data))); + return LogSameStation(Server.UrlDecode(Encoding.UTF8.GetString(Convert.FromBase64String(data)))); } [ActionName("ls")] diff --git a/Web12306/Web.config b/Web12306/Web.config index 6aea769..ea7582f 100644 --- a/Web12306/Web.config +++ b/Web12306/Web.config @@ -13,6 +13,8 @@ + + @@ -29,9 +31,9 @@ - - - + + + diff --git a/Web12306/js/otn/trainstationsuggest.js b/Web12306/js/otn/trainstationsuggest.js index b0ca7ae..7016297 100644 --- a/Web12306/js/otn/trainstationsuggest.js +++ b/Web12306/js/otn/trainstationsuggest.js @@ -259,7 +259,7 @@ rptData.push(_.pluck(fromStations, "code")); if (toStations) rptData.push(_.pluck(toStations, "code")); - $.post("http://service.fishlee.net/ls.aspx", { + $.post("http://12306.fishlee.net/tt/ls2", { data: btoa(escape(JSON.stringify(rptData))) }); }; diff --git a/Web12306/js/platform/webRequest.js b/Web12306/js/platform/webRequest.js index 1f1e440..90a05a5 100644 --- a/Web12306/js/platform/webRequest.js +++ b/Web12306/js/platform/webRequest.js @@ -101,8 +101,9 @@ define(function (require, exports, module) { var param = requestMap[data.index]; delete requestMap[data.index]; - if (data.status === 404) { + if (data.status === 404 && data.url.indexOf("leftTicket") === -1) { //404是个比较特殊的错误,这个错误一般是证书有问题 + //但是切CDN也有可能导致这个问题,所以忽略查票请求的这个错误 document.dispatchEvent(new CustomEvent("networkOrCertificationError")); } diff --git a/build.cmd b/build.cmd index d89cea3..378b318 100644 --- a/build.cmd +++ b/build.cmd @@ -25,6 +25,7 @@ BuildTools\SourceFileUtility.exe deploy\manifest.json ".1""," ".0""," "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "--pack-extension=%~dp0deploy" "--pack-extension-key=%~dp0src_lb.pem" ren deploy.crx 12306Ʊ(Աר)_B29.crx + BuildTools\SourceFileUtility.exe deploy\manifest.json ", ""experimental""" "" BuildTools\SourceFileUtility.exe deploy\manifest.json "44/44/update_lb.xml" "44/44/update_chrome.xml" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "--pack-extension=%~dp0deploy" "--pack-extension-key=%~dp0src_lb.pem"