forked from HMCL-dev/HMCL
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9edfe29
commit 63d50ae
Showing
5 changed files
with
193 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/TokenFence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.jackhuang.hmcl.util.logging; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.Writer; | ||
|
||
final class TokenFence { | ||
private TokenFence() { | ||
} | ||
|
||
public static String filter(String[] accessTokens, String message) { | ||
for (String token : accessTokens) | ||
message = message.replace(token, "<access token>"); | ||
return message; | ||
} | ||
|
||
public static void filter(String[] accessTokens, Reader reader, Writer out) throws IOException { | ||
char[] buffer = new char[1024]; | ||
for (String token : accessTokens) { | ||
if (token.length() > buffer.length) { | ||
buffer = new char[token.length() + 16]; | ||
} | ||
filter(token, reader, out, buffer); | ||
} | ||
} | ||
|
||
private static void filter(String token, Reader reader, Writer out, char[] buffer) throws IOException { | ||
char first = token.charAt(0); | ||
int start = 0, length; | ||
while ((length = reader.read(buffer, start, buffer.length - start)) != -1) { | ||
int fi = TokenFence.indexOf(buffer, 0, length - token.length(), first); | ||
if (fi == -1 || !TokenFence.isToken(buffer, fi, token)) { | ||
int fi2 = TokenFence.indexOf(buffer, length - token.length(), buffer.length, first); | ||
if (fi2 == -1) { | ||
out.write(buffer, 0, length); | ||
start = 0; | ||
} else { | ||
out.write(buffer, 0, fi2); | ||
System.arraycopy(buffer, fi2, buffer, 0, length - fi2); | ||
start = length - fi2; | ||
} | ||
} else { | ||
out.write(buffer, 0, fi); | ||
out.write("<access token>"); | ||
System.arraycopy(buffer, fi + token.length(), buffer, 0, token.length()); | ||
start = 0; | ||
} | ||
} | ||
} | ||
|
||
|
||
private static int indexOf(char[] buffer, int start, int length, char target) { | ||
for (int i = start; i < length; i++) { | ||
if (buffer[i] == target) { | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
private static boolean isToken(char[] buffer, int start, String token) { | ||
for (int i = 1; i < token.length(); i++) { | ||
if (buffer[start + i] != token.charAt(i)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
HMCLCore/src/test/java/org/jackhuang/hmcl/util/logger/LoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Hello Minecraft! Launcher | ||
* Copyright (C) 2020 huangyuhui <[email protected]> and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package org.jackhuang.hmcl.util.logger; | ||
|
||
import org.jackhuang.hmcl.util.logging.Logger; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
||
public class LoggerTest { | ||
private static final String TOKEN = "the_token"; | ||
|
||
@Test | ||
public void checkTokenFence() throws IOException { | ||
test("001122334455the_token667788", TOKEN); | ||
|
||
{ | ||
char[] data = new char[1050]; | ||
TOKEN.getChars(0, TOKEN.length(), data, 1020); | ||
test(data, TOKEN); | ||
} | ||
} | ||
|
||
private void test(String data, String token) throws IOException { | ||
test0(new StringReader(data), token); | ||
} | ||
|
||
private void test(char[] data, String token) throws IOException { | ||
test0(new CharArrayReader(data), token); | ||
} | ||
|
||
private void test0(Reader reader, String token) throws IOException { | ||
Logger.registerAccessToken(token); | ||
|
||
try (StringWriter writer = new StringWriter()) { | ||
Logger.filterForbiddenToken(reader, writer); | ||
|
||
assertEquals(writer.getBuffer().indexOf(token), -1); | ||
assertNotEquals(writer.getBuffer().indexOf("<access token>"), -1); | ||
} | ||
} | ||
} |