mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-24 10:27:38 +00:00
Document HTTP rules a little better
It turns out we don't document the "port" option anywhere, so probably worth doing a bit of an overhaul here. - Expand the top-level HTTP rules comment, clarifying how things are matched and describing each field. - Improve the comments on the default HTTP rule. We now also describe the $private rule and its motivation. - Don't drop/ignore invalid rules. This gets written back to the original config file, so is very annoying! Instead we now log an error and convert the rule into a "deny all" rule, which should make it obvious something is wrong.
This commit is contained in:
@@ -5,10 +5,7 @@
|
||||
package dan200.computercraft.core.apis.http.options;
|
||||
|
||||
import com.google.common.net.InetAddresses;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -19,8 +16,6 @@ import java.util.regex.Pattern;
|
||||
* @see AddressRule#apply(Iterable, String, InetSocketAddress) for the actual handling of this rule.
|
||||
*/
|
||||
interface AddressPredicate {
|
||||
Logger LOG = LoggerFactory.getLogger(AddressPredicate.class);
|
||||
|
||||
default boolean matches(String domain) {
|
||||
return false;
|
||||
}
|
||||
@@ -51,28 +46,25 @@ interface AddressPredicate {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static HostRange parse(String addressStr, String prefixSizeStr) {
|
||||
int prefixSize;
|
||||
try {
|
||||
prefixSize = Integer.parseInt(prefixSizeStr);
|
||||
} catch (NumberFormatException e) {
|
||||
LOG.error(
|
||||
"Malformed http whitelist/blacklist entry '{}': Cannot extract size of CIDR mask from '{}'.",
|
||||
throw new InvalidRuleException(String.format(
|
||||
"Invalid host host '%s': Cannot extract size of CIDR mask from '%s'.",
|
||||
addressStr + '/' + prefixSizeStr, prefixSizeStr
|
||||
);
|
||||
return null;
|
||||
));
|
||||
}
|
||||
|
||||
InetAddress address;
|
||||
try {
|
||||
address = InetAddresses.forString(addressStr);
|
||||
} catch (IllegalArgumentException e) {
|
||||
LOG.error(
|
||||
"Malformed http whitelist/blacklist entry '{}': Cannot extract IP address from '{}'.",
|
||||
throw new InvalidRuleException(String.format(
|
||||
"Invalid host '%s': Cannot extract IP address from '%s'.",
|
||||
addressStr + '/' + prefixSizeStr, addressStr
|
||||
);
|
||||
return null;
|
||||
));
|
||||
}
|
||||
|
||||
// Mask the bytes of the IP address.
|
||||
@@ -112,7 +104,6 @@ interface AddressPredicate {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
final class PrivatePattern implements AddressPredicate {
|
||||
static final PrivatePattern INSTANCE = new PrivatePattern();
|
||||
|
||||
|
||||
@@ -35,14 +35,13 @@ public final class AddressRule {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static AddressRule parse(String filter, OptionalInt port, PartialOptions partial) {
|
||||
var cidr = filter.indexOf('/');
|
||||
if (cidr >= 0) {
|
||||
var addressStr = filter.substring(0, cidr);
|
||||
var prefixSizeStr = filter.substring(cidr + 1);
|
||||
var range = HostRange.parse(addressStr, prefixSizeStr);
|
||||
return range == null ? null : new AddressRule(range, port, partial);
|
||||
return new AddressRule(range, port, partial);
|
||||
} else if (filter.equalsIgnoreCase("$private")) {
|
||||
return new AddressRule(PrivatePattern.INSTANCE, port, partial);
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
|
||||
//
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package dan200.computercraft.core.apis.http.options;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
/**
|
||||
* Throw when a {@link AddressRule} cannot be parsed.
|
||||
*
|
||||
* @see AddressRule#parse(String, OptionalInt, PartialOptions)
|
||||
* @see AddressPredicate.HostRange#parse(String, String)
|
||||
*/
|
||||
public class InvalidRuleException extends RuntimeException {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1303376302865132758L;
|
||||
|
||||
public InvalidRuleException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -90,9 +90,8 @@ while running do
|
||||
|
||||
local results = table.pack(exception.try(func))
|
||||
if results[1] then
|
||||
local n = 1
|
||||
while n < results.n do
|
||||
local value = results[n + 1]
|
||||
for i = 2, results.n do
|
||||
local value = results[i]
|
||||
local ok, serialised = pcall(pretty.pretty, value, {
|
||||
function_args = settings.get("lua.function_args"),
|
||||
function_source = settings.get("lua.function_source"),
|
||||
@@ -102,7 +101,6 @@ while running do
|
||||
else
|
||||
print(tostring(value))
|
||||
end
|
||||
n = n + 1
|
||||
end
|
||||
else
|
||||
printError(results[2])
|
||||
|
||||
Reference in New Issue
Block a user