OSDN Git Service

通信ログからトークン、提督名、艦隊名を削除する
authorKazuhiro Fujieda <fujieda@users.osdn.me>
Wed, 14 Nov 2018 12:26:16 +0000 (21:26 +0900)
committerKazuhiro Fujieda <fujieda@users.osdn.me>
Wed, 14 Nov 2018 12:27:00 +0000 (21:27 +0900)
KancolleSniffer.Test/ErrorLogTest.cs [deleted file]
KancolleSniffer.Test/KancolleSniffer.Test.csproj
KancolleSniffer.Test/PrivacyTest.cs [new file with mode: 0644]
KancolleSniffer/ErrorLog.cs
KancolleSniffer/KancolleSniffer.csproj
KancolleSniffer/MainForm.cs
KancolleSniffer/Privacy.cs [new file with mode: 0644]

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