|
| 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 | +} |
0 commit comments