From: Kazuhiro Fujieda Date: Wed, 14 Nov 2018 12:26:16 +0000 (+0900) Subject: 通信ログからトークン、提督名、艦隊名を削除する X-Git-Tag: v11.10~2 X-Git-Url: http://git.osdn.net/view?p=kancollesniffer%2FKancolleSniffer.git;a=commitdiff_plain;h=383d4a0e79f45ab2d3ac428d6cbdc27c68163ee8 通信ログからトークン、提督名、艦隊名を削除する --- diff --git a/KancolleSniffer.Test/ErrorLogTest.cs b/KancolleSniffer.Test/ErrorLogTest.cs deleted file mode 100644 index fb9e509..0000000 --- a/KancolleSniffer.Test/ErrorLogTest.cs +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (C) 2017 Kazuhiro Fujieda -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -using ExpressionToCodeLib; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace KancolleSniffer.Test -{ - [TestClass] - public class ErrorLogTest - { - [TestMethod] - public void RemoveTokenFromRequest() - { - var request = "api%5Fverno=1&api%5Ftoken=0123456abcdef&api%5Fport=0123456789"; - var response = ""; - ErrorLog.RemoveUnwantedInformation(ref request, ref response); - PAssert.That(() => request == "api%5Fverno=1&api%5Fport=0123456789", "トークンが中間"); - var request2 = "api%5Fverno=1&api%5Ftoken=0123456abcdef"; - ErrorLog.RemoveUnwantedInformation(ref request2, ref response); - PAssert.That(() => request2 == @"api%5Fverno=1", "トークンが末尾"); - var request3 = "api%5Ftoken=0123456abcdef&api%5Fverno=1"; - ErrorLog.RemoveUnwantedInformation(ref request3, ref response); - PAssert.That(() => request3 == @"api%5Fverno=1", "トークンが先頭"); - var request4 = "api%5Ftoken=0123456abcdef"; - ErrorLog.RemoveUnwantedInformation(ref request4, ref response); - PAssert.That(() => request4 == "", "トークン単独"); - var request5 = "api%5Fbtime=83026279&api%5Ftoken=0123456abcdef&api%5Fverno=1"; - ErrorLog.RemoveUnwantedInformation(ref request5, ref response); - PAssert.That(() => request5 == "api%5Fverno=1", "戦闘APIの時刻印を削除"); - } - - /// - /// 二期は%エンコードされていない - /// - [TestMethod] - public void RemoveTokenFromRequest2() - { - var request = "api_verno=1&api_token=0123456abcdef&api_port=0123456789"; - var response = ""; - ErrorLog.RemoveUnwantedInformation(ref request, ref response); - PAssert.That(() => request == "api_verno=1&api_port=0123456789", "トークンが中間"); - var request5 = "api_btime=83026279&api_token=0123456abcdef&api_verno=1"; - ErrorLog.RemoveUnwantedInformation(ref request5, ref response); - PAssert.That(() => request5 == "api_verno=1", "戦闘APIの時刻印を削除"); - } - - [TestMethod] - public void RemoveUnwantedInformationFromResponse() - { - var request = ""; - var response1 = @"{""api_result"":1,""api_result_msg"":""成功"",""api_data"":" + - @"{""api_basic"":{""api_member_id"":""123456""," + - @"""api_nickname"":""ぱんなこった"",""api_nickname_id"":""12345678"",""api_active_flag"":1}}}"; - ErrorLog.RemoveUnwantedInformation(ref request, ref response1); - PAssert.That(() => response1 == - @"{""api_result"":1,""api_result_msg"":""成功"",""api_data"":{""api_basic"":{""api_active_flag"":1}}}"); - var response2 = - @"{""api_deck_data"":[{""api_member_id"":123456,""api_id"":1,""api_name"":""第一艦隊"",""api_name_id"":""123456"",""api_mission"":[0,0,0,0],""api_flagship"":""0""}]}"; - ErrorLog.RemoveUnwantedInformation(ref request, ref response2); - PAssert.That(() => response2 == @"{""api_deck_data"":[{""api_id"":1,""api_name"":"""",""api_mission"":[0,0,0,0],""api_flagship"":""0""}]}"); - } - } -} \ No newline at end of file diff --git a/KancolleSniffer.Test/KancolleSniffer.Test.csproj b/KancolleSniffer.Test/KancolleSniffer.Test.csproj index 70cdf0f..b620049 100644 --- a/KancolleSniffer.Test/KancolleSniffer.Test.csproj +++ b/KancolleSniffer.Test/KancolleSniffer.Test.csproj @@ -78,7 +78,7 @@ - + diff --git a/KancolleSniffer.Test/PrivacyTest.cs b/KancolleSniffer.Test/PrivacyTest.cs new file mode 100644 index 0000000..ebd53bb --- /dev/null +++ b/KancolleSniffer.Test/PrivacyTest.cs @@ -0,0 +1,75 @@ +// Copyright (C) 2017 Kazuhiro Fujieda +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using ExpressionToCodeLib; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace KancolleSniffer.Test +{ + [TestClass] + public class PrivacyTest + { + [ClassInitialize] + public static void Initialize(TestContext context) + { + SnifferTest.Initialize(context); + } + + [TestMethod] + public void RemoveToken() + { + const string request = "api%5Fverno=1&api%5Ftoken=0123456abcdef&api%5Fport=0123456789"; + PAssert.That(() => Privacy.RemoveToken(request) == "api%5Fverno=1&api%5Fport=0123456789", "トークンが中間"); + const string request2 = "api%5Fverno=1&api%5Ftoken=0123456abcdef"; + PAssert.That(() => Privacy.RemoveToken(request2) == @"api%5Fverno=1", "トークンが末尾"); + const string request3 = "api%5Ftoken=0123456abcdef&api%5Fverno=1"; + PAssert.That(() => Privacy.RemoveToken(request3) == @"api%5Fverno=1", "トークンが先頭"); + const string request4 = "api%5Ftoken=0123456abcdef"; + PAssert.That(() => Privacy.RemoveToken(request4) == "", "トークン単独"); + const string request5 = "api%5Fbtime=83026279&api%5Ftoken=0123456abcdef&api%5Fverno=1"; + PAssert.That(() => Privacy.RemoveToken(request5) == "api%5Fverno=1", "戦闘APIの時刻印を削除"); + } + + /// + /// 二期は%エンコードされていない + /// + [TestMethod] + public void RemoveToken2() + { + var request = "api_verno=1&api_token=0123456abcdef&api_port=0123456789"; + PAssert.That(() => Privacy.RemoveToken(request) == "api_verno=1&api_port=0123456789", "トークンが中間"); + var request5 = "api_btime=83026279&api_token=0123456abcdef&api_verno=1"; + PAssert.That(() => Privacy.RemoveToken(request5) == "api_verno=1", "戦闘APIの時刻印を削除"); + } + + [TestMethod] + public void RemoveName() + { + const string response1 = + @"{""api_result"":1,""api_result_msg"":""成功"",""api_data"":{""api_basic"":{""api_member_id"":""123456""," + + @"""api_nickname"":""ぱんなこった"",""api_nickname_id"":""12345678"",""api_active_flag"":1}}}"; + const string result1 = + @"{""api_result"":1,""api_result_msg"":""成功"",""api_data"":{""api_basic"":{""api_active_flag"":1}}}"; + PAssert.That(() => Privacy.RemoveName(response1) == result1); + const string response2 = + @"{""api_deck_data"":[{""api_member_id"":123456,""api_id"":1,""api_name"":""第一艦隊"",""api_name_id"":""123456"",""api_mission"":[0,0,0,0],""api_flagship"":""0""}]}"; + const string result2 = + @"{""api_deck_data"":[{""api_id"":1,""api_mission"":[0,0,0,0],""api_flagship"":""0""}]}"; + PAssert.That(() => Privacy.RemoveName(response2) == result2); + const string response3 = + @"{""api_deck_data"":[{""api_member_id"":123456,""api_id"":1,""api_name"":""第\\/一艦\\""隊\\"""",""api_name_id"":""123456"",""api_mission"":[0,0,0,0],""api_flagship"":""0""}]}"; + PAssert.That(() => Privacy.RemoveName(response2) == result2); + } + } +} \ No newline at end of file diff --git a/KancolleSniffer/ErrorLog.cs b/KancolleSniffer/ErrorLog.cs index d2c5c9b..5bb606a 100644 --- a/KancolleSniffer/ErrorLog.cs +++ b/KancolleSniffer/ErrorLog.cs @@ -69,7 +69,7 @@ namespace KancolleSniffer public string GenerateBattleErrorLog() { foreach (var logs in _battleApiLog) - RemoveUnwantedInformation(ref logs[1], ref logs[2]); + Privacy.Remove(ref logs[0], ref logs[1], ref logs[2]); var version = string.Join(".", Application.ProductVersion.Split('.').Take(2)); var api = CompressApi(string.Join("\r\n", new[] {BattleStartSlots()}.Concat(_battleApiLog.SelectMany(logs => logs)))); @@ -104,7 +104,7 @@ namespace KancolleSniffer public string GenerateErrorLog(string url, string request, string response, string exception) { - RemoveUnwantedInformation(ref request, ref response); + Privacy.Remove(ref url, ref request, ref response); var version = string.Join(".", Application.ProductVersion.Split('.').Take(2)); var api = CompressApi($"{url}\r\n{request}\r\n{response}"); var result = $"{{{{{{\r\n{DateTime.Now:g} {version}\r\n{exception}\r\n{api}\r\n}}}}}}"; @@ -112,16 +112,6 @@ namespace KancolleSniffer return result; } - public static void RemoveUnwantedInformation(ref string request, ref string response) - { - var token = new Regex(@"&api(?:%5F|_)token=.+?(?=&|$)|api(?:%5F|_)token=.+?(?:&|$)|api(?:%5F|_)btime=\d+&?"); - request = token.Replace(request, ""); - var id = new Regex(@"""api_member_id"":""?\d+""?,?|""api_nickname"":"".+?"",?|""api_nickname_id"":""\d+"",?|""api_name_id"":"".+?"",?|"); - response = id.Replace(response, ""); - var name = new Regex(@"""api_name"":"".+?"""); - response = name.Replace(response, @"""api_name"":"""""); - } - private string CompressApi(string api) { var output = new MemoryStream(); diff --git a/KancolleSniffer/KancolleSniffer.csproj b/KancolleSniffer/KancolleSniffer.csproj index bf137d0..7bd50c0 100644 --- a/KancolleSniffer/KancolleSniffer.csproj +++ b/KancolleSniffer/KancolleSniffer.csproj @@ -54,6 +54,7 @@ + diff --git a/KancolleSniffer/MainForm.cs b/KancolleSniffer/MainForm.cs index be88edc..810d826 100644 --- a/KancolleSniffer/MainForm.cs +++ b/KancolleSniffer/MainForm.cs @@ -181,6 +181,7 @@ namespace KancolleSniffer return; var request = session.Request.BodyAsString; var response = session.Response.BodyAsString; + Privacy.Remove(ref url, ref request, ref response); if (response == null || !response.StartsWith("svdata=")) { WriteDebugLog(url, request, response); diff --git a/KancolleSniffer/Privacy.cs b/KancolleSniffer/Privacy.cs new file mode 100644 index 0000000..29bb027 --- /dev/null +++ b/KancolleSniffer/Privacy.cs @@ -0,0 +1,50 @@ +// Copyright (C) 2018 Kazuhiro Fujieda +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Text.RegularExpressions; + +namespace KancolleSniffer +{ + public static class Privacy + { + public static void Remove(ref string url, ref string request, ref string response) + { + if (url != null) + url = RemoveToken(url); + if (request != null) + request = RemoveToken(request); + if (response != null && !(url != null && url.Contains("start2"))) + response = RemoveName(response); + } + + private static readonly Regex NameRegex = new Regex( + @"""api_member_id"":""?\d*""?,|""api_(?:nick)?name"":""(?:[^\""]|\\.)*"",""api_(?:nick)?name_id"":""\d*"",", + RegexOptions.Compiled); + + public static string RemoveName(string response) + { + var result = NameRegex.Replace(response, ""); + return result; + } + + public static string RemoveToken(string query) + { + var result = new Regex(@"api(?:%5F|_)token=\w+|api(?:%5F|_)btime=\w+").Replace(query, ""); + result = result.Replace("&&", "&"); + result = result.Replace("?&", "?"); + result = result.Trim('&', '?'); + return result; + } + } +} \ No newline at end of file