Skip to content

Commit 02cdf5d

Browse files
committed
Added option to hide ip addresses in console and logs
Closes #416
1 parent 6158229 commit 02cdf5d

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

src/main/java/net/raphimc/viaproxy/protocoltranslator/viaproxy/ViaProxyConfig.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ public class ViaProxyConfig {
211211
})
212212
private boolean sendConnectionDetails = false;
213213

214+
@Option("log-ips")
215+
@Description({
216+
"Disable this if you want to hide IP addresses in the console and log files.",
217+
})
218+
private boolean logIps = true;
219+
214220
@Option("log-client-status-requests")
215221
@Description({
216222
"Enable this if you want to see client status requests in the console and log files.",
@@ -548,6 +554,15 @@ public void setSendConnectionDetails(final boolean sendConnectionDetails) {
548554
this.save();
549555
}
550556

557+
public boolean shouldLogIps() {
558+
return this.logIps;
559+
}
560+
561+
public void setLogIps(final boolean logIps) {
562+
this.logIps = logIps;
563+
this.save();
564+
}
565+
551566
public boolean shouldLogClientStatusRequests() {
552567
return this.logClientStatusRequests;
553568
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
3+
* Copyright (C) 2021-2025 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.viaproxy.util.logging;
19+
20+
import net.raphimc.viaproxy.ViaProxy;
21+
import org.apache.logging.log4j.core.LogEvent;
22+
import org.apache.logging.log4j.core.config.plugins.Plugin;
23+
import org.apache.logging.log4j.core.pattern.ConverterKeys;
24+
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
25+
import org.apache.logging.log4j.core.pattern.PatternConverter;
26+
27+
import java.util.regex.Pattern;
28+
29+
@Plugin(name = "ip_redactor", category = PatternConverter.CATEGORY)
30+
@ConverterKeys({"ip_redactor"})
31+
public class IpRedactor extends LogEventPatternConverter {
32+
33+
private final Pattern IPV4_REGEX = Pattern.compile("(\\b25[0-5]|\\b2[0-4][0-9]|\\b[01]?[0-9][0-9]?)(\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}");
34+
private final Pattern IPV6_REGEX = Pattern.compile("(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))");
35+
36+
public static IpRedactor newInstance(final String[] options) {
37+
return new IpRedactor();
38+
}
39+
40+
private IpRedactor() {
41+
super("IpRedactor", null);
42+
}
43+
44+
@Override
45+
public void format(final LogEvent event, final StringBuilder toAppendTo) {
46+
if (ViaProxy.getConfig() != null && !ViaProxy.getConfig().shouldLogIps()) {
47+
String message = toAppendTo.toString();
48+
message = IPV4_REGEX.matcher(message).replaceAll("REDACTED_IP");
49+
message = IPV6_REGEX.matcher(message).replaceAll("REDACTED_IP");
50+
toAppendTo.setLength(0);
51+
toAppendTo.append(message);
52+
}
53+
}
54+
55+
}

src/main/java/net/raphimc/viaproxy/util/logging/Logger.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import net.raphimc.viaproxy.util.AddressUtil;
2323
import org.apache.logging.log4j.Level;
2424
import org.apache.logging.log4j.LogManager;
25+
import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
2526
import org.jline.jansi.AnsiConsole;
2627

2728
import java.io.PrintStream;
@@ -30,6 +31,10 @@
3031

3132
public class Logger {
3233

34+
static {
35+
PluginManager.addPackage("net.raphimc.viaproxy.util.logging");
36+
}
37+
3338
public static final org.apache.logging.log4j.Logger LOGGER = LogManager.getLogger("ViaProxy");
3439

3540
public static final PrintStream SYSOUT = System.out;

src/main/resources/log4j2.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
<Appenders>
44
<!-- System out -->
55
<Console name="SysOut" target="SYSTEM_OUT">
6-
<PatternLayout pattern="%style{[%d{HH:mm:ss}]}{blue} %highlight{[%t/%level]}{FATAL=red, ERROR=red, WARN=yellow, INFO=green, DEBUG=green, TRACE=blue} %style{(%logger{1})}{cyan} %highlight{%msg%n}{FATAL=red, ERROR=red, WARN=normal, INFO=normal, DEBUG=normal, TRACE=normal}" disableAnsi="false"/>
6+
<PatternLayout pattern="%style{[%d{HH:mm:ss}]}{blue} %highlight{[%t/%level]}{FATAL=red, ERROR=red, WARN=yellow, INFO=green, DEBUG=green, TRACE=blue} %style{(%logger{1})}{cyan} %highlight{%msg{nolookups}%ip_redactor%n}{FATAL=red, ERROR=red, WARN=normal, INFO=normal, DEBUG=normal, TRACE=normal}" disableAnsi="false"/>
77
</Console>
88

99
<!-- latest.log same as vanilla -->
1010
<RollingRandomAccessFile name="LatestFile" fileName="${sys:user.dir}/logs/latest.log" filePattern="${sys:user.dir}/logs/%d{yyyy-MM-dd}-%i.log.gz">
11-
<PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level] (%logger{1}) %msg{nolookups}%n"/>
11+
<PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level] (%logger{1}) %msg{nolookups}%ip_redactor%n"/>
1212
<Policies>
1313
<TimeBasedTriggeringPolicy />
1414
<OnStartupTriggeringPolicy />
@@ -17,7 +17,7 @@
1717

1818
<!-- Debug log file -->
1919
<RollingRandomAccessFile name="DebugFile" fileName="${sys:user.dir}/logs/debug.log" filePattern="${sys:user.dir}/logs/debug-%i.log.gz">
20-
<PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level] (%logger) %msg{nolookups}%n"/>
20+
<PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level] (%logger) %msg{nolookups}%ip_redactor%n"/>
2121

2222
<!-- Keep 5 files max -->
2323
<DefaultRolloverStrategy max="5" fileIndex="min"/>

0 commit comments

Comments
 (0)