diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9ac7e1707..fa3ffc7c3 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -83,6 +83,8 @@ kotlin-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", kotlin-platform = { module = "org.jetbrains.kotlin:kotlin-bom", version.ref = "kotlin" } kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" } netty-http = { module = "io.netty:netty-codec-http", version.ref = "netty" } +netty-socks = { module = "io.netty:netty-codec-socks", version.ref = "netty" } +netty-proxy = { module = "io.netty:netty-handler-proxy", version.ref = "netty" } nightConfig-core = { module = "com.electronwill.night-config:core", version.ref = "nightConfig" } nightConfig-toml = { module = "com.electronwill.night-config:toml", version.ref = "nightConfig" } slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/config/AddressRuleConfig.java b/projects/common/src/main/java/dan200/computercraft/shared/config/AddressRuleConfig.java index 5c6cd20a2..8974c9185 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/config/AddressRuleConfig.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/config/AddressRuleConfig.java @@ -45,6 +45,9 @@ The maximum size (in bytes) that a computer can upload in a single request. This config.setComment("max_websocket_message", "The maximum size (in bytes) that a computer can send or receive in one websocket packet."); config.set("max_websocket_message", AddressRule.WEBSOCKET_MESSAGE); + + config.setComment("use_proxy", "Enable use of the HTTP/SOCKS proxy if it is configured."); + config.set("use_proxy", false); } return config; @@ -58,6 +61,7 @@ && check(builder, "port", Number.class) && check(builder, "max_upload", Number.class) && check(builder, "max_download", Number.class) && check(builder, "websocket_message", Number.class) + && check(builder, "use_proxy", Boolean.class) && AddressRule.parse(hostObj, port, PartialOptions.DEFAULT) != null; } @@ -71,12 +75,14 @@ public static AddressRule parseRule(UnmodifiableConfig builder) { var maxUpload = unboxOptLong(get(builder, "max_upload", Number.class).map(Number::longValue)); var maxDownload = unboxOptLong(get(builder, "max_download", Number.class).map(Number::longValue)); var websocketMessage = unboxOptInt(get(builder, "websocket_message", Number.class).map(Number::intValue)); + var useProxy = get(builder, "use_proxy", Boolean.class); var options = new PartialOptions( action, maxUpload, maxDownload, - websocketMessage + websocketMessage, + useProxy ); return AddressRule.parse(hostObj, port, options); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigFile.java b/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigFile.java index 346fb5e49..afb239e9a 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigFile.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigFile.java @@ -8,6 +8,7 @@ import javax.annotation.Nullable; import javax.annotation.OverridingMethodsMustInvokeSuper; +import java.nio.file.Path; import java.util.ArrayDeque; import java.util.Deque; import java.util.List; @@ -141,6 +142,18 @@ public void pop() { * @param onChange The function to run on change. * @return The built config file. */ - public abstract ConfigFile build(Runnable onChange); + public abstract ConfigFile build(ConfigListener onChange); + } + + @FunctionalInterface + interface ConfigListener { + /** + * The function called then a config file is changed. + * + * @param path The path to the config file. This will be {@code null} when the config file does not exist on + * disk, such as when synced from a server to the client. + * @see Builder#build(ConfigListener) + */ + void onConfigChanged(@Nullable Path path); } } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigSpec.java b/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigSpec.java index 9f751ce30..fdd603ade 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigSpec.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigSpec.java @@ -5,10 +5,12 @@ package dan200.computercraft.shared.config; import com.electronwill.nightconfig.core.UnmodifiableConfig; +import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.core.CoreConfig; import dan200.computercraft.core.Logging; import dan200.computercraft.core.apis.http.NetworkUtils; import dan200.computercraft.core.apis.http.options.Action; +import dan200.computercraft.core.apis.http.options.ProxyType; import dan200.computercraft.core.computer.mainthread.MainThreadConfig; import dan200.computercraft.shared.peripheral.monitor.MonitorRenderer; import dan200.computercraft.shared.platform.PlatformHelper; @@ -16,6 +18,8 @@ import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.filter.MarkerFilter; +import javax.annotation.Nullable; +import java.nio.file.Path; import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -49,6 +53,10 @@ public final class ConfigSpec { public static final ConfigFile.Value httpDownloadBandwidth; public static final ConfigFile.Value httpUploadBandwidth; + public static final ConfigFile.Value httpProxyType; + public static final ConfigFile.Value httpProxyHost; + public static final ConfigFile.Value httpProxyPort; + public static final ConfigFile.Value commandBlockEnabled; public static final ConfigFile.Value modemRange; public static final ConfigFile.Value modemHighAltitudeRange; @@ -222,6 +230,30 @@ CIDR notation ("127.0.0.0/8"). builder.pop(); + builder + .comment(""" + Tunnels HTTP and websocket requests through a proxy server. Only affects HTTP + rules with "use_proxy" set to true (off by default). + If authentication is required for the proxy, create a "computercraft-proxy.pw" + file in the same directory as "computercraft-server.toml", containing the + username and password separated by a colon, e.g. "myuser:mypassword". For + SOCKS4 proxies only the username is required.""") + .push("proxy"); + + httpProxyType = builder + .comment("The type of proxy to use.") + .defineEnum("type", CoreConfig.httpProxyType); + + httpProxyHost = builder + .comment("The hostname or IP address of the proxy server.") + .define("host", CoreConfig.httpProxyHost); + + httpProxyPort = builder + .comment("The port of the proxy server.") + .defineInRange("port", CoreConfig.httpProxyPort, 1, 65536); + + builder.pop(); + builder.pop(); } @@ -339,7 +371,7 @@ CIDR notation ("127.0.0.0/8"). clientSpec = clientBuilder.build(ConfigSpec::syncClient); } - public static void syncServer() { + public static void syncServer(@Nullable Path path) { // General Config.computerSpaceLimit = computerSpaceLimit.get(); Config.floppySpaceLimit = floppySpaceLimit.get(); @@ -370,6 +402,13 @@ public static void syncServer() { CoreConfig.httpMaxWebsockets = httpMaxWebsockets.get(); CoreConfig.httpDownloadBandwidth = httpDownloadBandwidth.get(); CoreConfig.httpUploadBandwidth = httpUploadBandwidth.get(); + + CoreConfig.httpProxyType = httpProxyType.get(); + CoreConfig.httpProxyHost = httpProxyHost.get(); + CoreConfig.httpProxyPort = httpProxyPort.get(); + + if (path != null) ProxyPasswordConfig.init(path.resolveSibling(ComputerCraftAPI.MOD_ID + "-proxy.pw")); + NetworkUtils.reloadConfig(); // Peripheral @@ -396,7 +435,7 @@ public static void syncServer() { Config.monitorHeight = monitorHeight.get(); } - public static void syncClient() { + public static void syncClient(@Nullable Path path) { Config.monitorRenderer = monitorRenderer.get(); Config.monitorDistance = monitorDistance.get(); Config.uploadNagDelay = uploadNagDelay.get(); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/config/ProxyPasswordConfig.java b/projects/common/src/main/java/dan200/computercraft/shared/config/ProxyPasswordConfig.java new file mode 100644 index 000000000..3c45f64f4 --- /dev/null +++ b/projects/common/src/main/java/dan200/computercraft/shared/config/ProxyPasswordConfig.java @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers +// +// SPDX-License-Identifier: MPL-2.0 + +package dan200.computercraft.shared.config; + +import dan200.computercraft.core.CoreConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +record ProxyPasswordConfig(String username, String password) { + private static final Logger LOG = LoggerFactory.getLogger(ProxyPasswordConfig.class); + + @Nullable + private static ProxyPasswordConfig loadFromFile(@Nullable Path path) { + if (path == null || !path.toFile().exists()) return null; + + try (var br = Files.newBufferedReader(path, StandardCharsets.UTF_8)) { + var line = br.readLine(); + if (line == null) return null; + + var parts = line.trim().split(":", 2); + if (parts.length == 0) return null; + + return new ProxyPasswordConfig(parts[0], parts.length == 2 ? parts[1] : ""); + } catch (IOException e) { + LOG.error("Failed to load proxy password from {}.", path, e); + return null; + } + } + + static void init(@Nullable Path path) { + var config = loadFromFile(path); + if (config == null) { + CoreConfig.httpProxyUsername = ""; + CoreConfig.httpProxyPassword = ""; + } else { + CoreConfig.httpProxyUsername = config.username; + CoreConfig.httpProxyPassword = config.password; + } + } +} diff --git a/projects/core/build.gradle.kts b/projects/core/build.gradle.kts index ba5e8ef9b..fc84c1771 100644 --- a/projects/core/build.gradle.kts +++ b/projects/core/build.gradle.kts @@ -21,6 +21,8 @@ dependencies { implementation(libs.guava) implementation(libs.jzlib) implementation(libs.netty.http) + implementation(libs.netty.socks) + implementation(libs.netty.proxy) implementation(libs.slf4j) implementation(libs.asm) diff --git a/projects/core/src/main/java/dan200/computercraft/core/CoreConfig.java b/projects/core/src/main/java/dan200/computercraft/core/CoreConfig.java index 01dee60b2..46cff11c7 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/CoreConfig.java +++ b/projects/core/src/main/java/dan200/computercraft/core/CoreConfig.java @@ -6,6 +6,7 @@ import dan200.computercraft.core.apis.http.options.Action; import dan200.computercraft.core.apis.http.options.AddressRule; +import dan200.computercraft.core.apis.http.options.ProxyType; import java.util.List; import java.util.OptionalInt; @@ -34,4 +35,9 @@ private CoreConfig() { public static int httpMaxWebsockets = 4; public static int httpDownloadBandwidth = 32 * 1024 * 1024; public static int httpUploadBandwidth = 32 * 1024 * 1024; + public static ProxyType httpProxyType = ProxyType.HTTP; + public static String httpProxyHost = ""; + public static int httpProxyPort = 8080; + public static String httpProxyUsername = ""; + public static String httpProxyPassword = ""; } diff --git a/projects/core/src/main/java/dan200/computercraft/core/apis/http/NetworkUtils.java b/projects/core/src/main/java/dan200/computercraft/core/apis/http/NetworkUtils.java index 3d0aa167b..517e67790 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/apis/http/NetworkUtils.java +++ b/projects/core/src/main/java/dan200/computercraft/core/apis/http/NetworkUtils.java @@ -4,6 +4,7 @@ package dan200.computercraft.core.apis.http; +import com.google.common.base.Strings; import dan200.computercraft.core.CoreConfig; import dan200.computercraft.core.apis.http.options.Action; import dan200.computercraft.core.apis.http.options.AddressRule; @@ -17,11 +18,16 @@ import io.netty.handler.codec.DecoderException; import io.netty.handler.codec.TooLongFrameException; import io.netty.handler.codec.http.websocketx.WebSocketHandshakeException; +import io.netty.handler.proxy.HttpProxyHandler; +import io.netty.handler.proxy.Socks4ProxyHandler; +import io.netty.handler.proxy.Socks5ProxyHandler; import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.SslHandler; import io.netty.handler.timeout.ReadTimeoutException; import io.netty.handler.traffic.AbstractTrafficShapingHandler; import io.netty.handler.traffic.GlobalTrafficShapingHandler; +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,6 +38,7 @@ import java.net.URI; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; /** * Just a shared object for executing simple HTTP related tasks. @@ -57,8 +64,7 @@ private NetworkUtils() { private static @Nullable SslContext sslContext; private static boolean triedSslContext = false; - @Nullable - private static SslContext makeSslContext() { + private static @Nullable SslContext makeSslContext() { if (triedSslContext) return sslContext; synchronized (sslLock) { if (triedSslContext) return sslContext; @@ -133,6 +139,66 @@ public static Options getOptions(String host, InetSocketAddress address) throws return options; } + /** + * Creates a proxy handler for a specific domain. Returns null if a proxy is not required for this HTTP rule, or + * throws if it is required but is not configured correctly. + *

+ * Note, this may require a DNS lookup, and so should not be executed on the main CC thread. + * + * @param options The options for the host to be proxied. + * @param timeout The timeout for this connection. Currently only used for establishing the SSL initialisation. + * @return A consumer that takes a {@link SocketChannel} and injects the proxy handler.. + * @throws HTTPRequestException If a proxy is required but not configured correctly. + */ + public static @Nullable Consumer getProxyHandler(Options options, int timeout) throws HTTPRequestException { + if (!options.useProxy) return null; + + var type = CoreConfig.httpProxyType; + var host = CoreConfig.httpProxyHost; + var port = CoreConfig.httpProxyPort; + var username = CoreConfig.httpProxyUsername; + var password = CoreConfig.httpProxyPassword; + + if (Strings.isNullOrEmpty(host)) { + throw new HTTPRequestException("Proxy host not configured"); + } + + var proxyAddress = new InetSocketAddress(host, port); + if (proxyAddress.isUnresolved()) throw new HTTPRequestException("Unknown proxy host"); + + return switch (type) { + case HTTP -> ch -> ch.pipeline().addLast(new HttpProxyHandler(proxyAddress, username, password)); + case HTTPS -> { + var sslContext = getSslContext(); + yield ch -> { + var p = ch.pipeline(); + // If we're using an HTTPS proxy, we need to add an SSL handler for the proxy too. + p.addLast(makeSslHandler(ch, sslContext, timeout, host, port)); + p.addLast(new HttpProxyHandler(proxyAddress, username, password)); + }; + } + case SOCKS4 -> ch -> ch.pipeline().addLast(new Socks4ProxyHandler(proxyAddress, username)); + case SOCKS5 -> ch -> ch.pipeline().addLast(new Socks5ProxyHandler(proxyAddress, username, password)); + }; + } + + /** + * Make an SSL handler for the remote host. + * + * @param ch The channel the handler will be added to. + * @param sslContext The SSL context, if present. + * @param timeout The timeout on this channel. + * @param peerHost The host to connect to. + * @param peerPort The port to connect to. + * @return The SSL handler. + * @see io.netty.handler.ssl.SslHandler + */ + private static SslHandler makeSslHandler(SocketChannel ch, @NotNull SslContext sslContext, int timeout, String peerHost, int peerPort) { + var handler = sslContext.newHandler(ch.alloc(), peerHost, peerPort); + if (timeout > 0) handler.setHandshakeTimeoutMillis(timeout); + return handler; + } + /** * Set up some basic properties of the channel. This adds a timeout, the traffic shaping handler, and the SSL * handler. @@ -141,19 +207,20 @@ public static Options getOptions(String host, InetSocketAddress address) throws * @param uri The URI to connect to. * @param socketAddress The address of the socket to connect to. * @param sslContext The SSL context, if present. + * @param proxy The proxy handler, if present. * @param timeout The timeout on this channel. * @see io.netty.channel.ChannelInitializer */ - public static void initChannel(SocketChannel ch, URI uri, InetSocketAddress socketAddress, @Nullable SslContext sslContext, int timeout) { + public static void initChannel(SocketChannel ch, URI uri, InetSocketAddress socketAddress, @Nullable SslContext sslContext, @Nullable Consumer proxy, int timeout) { if (timeout > 0) ch.config().setConnectTimeoutMillis(timeout); var p = ch.pipeline(); p.addLast(SHAPING_HANDLER); + if (proxy != null) proxy.accept(ch); + if (sslContext != null) { - var handler = sslContext.newHandler(ch.alloc(), uri.getHost(), socketAddress.getPort()); - if (timeout > 0) handler.setHandshakeTimeoutMillis(timeout); - p.addLast(handler); + p.addLast(makeSslHandler(ch, sslContext, timeout, uri.getHost(), socketAddress.getPort())); } } diff --git a/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/Action.java b/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/Action.java index 7b8483121..ffdcfb3b1 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/Action.java +++ b/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/Action.java @@ -4,6 +4,7 @@ package dan200.computercraft.core.apis.http.options; +import java.util.Optional; import java.util.OptionalInt; import java.util.OptionalLong; @@ -12,7 +13,7 @@ public enum Action { DENY; private final PartialOptions partial = new PartialOptions( - this, OptionalLong.empty(), OptionalLong.empty(), OptionalInt.empty() + this, OptionalLong.empty(), OptionalLong.empty(), OptionalInt.empty(), Optional.empty() ); public PartialOptions toPartial() { diff --git a/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/Options.java b/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/Options.java index 6205e1ded..ddc3cab0a 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/Options.java +++ b/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/Options.java @@ -13,11 +13,13 @@ public final class Options { public final long maxUpload; public final long maxDownload; public final int websocketMessage; + public final boolean useProxy; - Options(Action action, long maxUpload, long maxDownload, int websocketMessage) { + Options(Action action, long maxUpload, long maxDownload, int websocketMessage, boolean useProxy) { this.action = action; this.maxUpload = maxUpload; this.maxDownload = maxDownload; this.websocketMessage = websocketMessage; + this.useProxy = useProxy; } } diff --git a/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/PartialOptions.java b/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/PartialOptions.java index 9717f936b..1d4d6824f 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/PartialOptions.java +++ b/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/PartialOptions.java @@ -7,28 +7,31 @@ import com.google.errorprone.annotations.Immutable; import javax.annotation.Nullable; +import java.util.Optional; import java.util.OptionalInt; import java.util.OptionalLong; @Immutable public final class PartialOptions { public static final PartialOptions DEFAULT = new PartialOptions( - null, OptionalLong.empty(), OptionalLong.empty(), OptionalInt.empty() + null, OptionalLong.empty(), OptionalLong.empty(), OptionalInt.empty(), Optional.empty() ); private final @Nullable Action action; private final OptionalLong maxUpload; private final OptionalLong maxDownload; private final OptionalInt websocketMessage; + private final Optional useProxy; @SuppressWarnings("Immutable") // Lazily initialised, so this mutation is invisible in the public API private @Nullable Options options; - public PartialOptions(@Nullable Action action, OptionalLong maxUpload, OptionalLong maxDownload, OptionalInt websocketMessage) { + public PartialOptions(@Nullable Action action, OptionalLong maxUpload, OptionalLong maxDownload, OptionalInt websocketMessage, Optional useProxy) { this.action = action; this.maxUpload = maxUpload; this.maxDownload = maxDownload; this.websocketMessage = websocketMessage; + this.useProxy = useProxy; } Options toOptions() { @@ -38,7 +41,8 @@ Options toOptions() { action == null ? Action.DENY : action, maxUpload.orElse(AddressRule.MAX_UPLOAD), maxDownload.orElse(AddressRule.MAX_DOWNLOAD), - websocketMessage.orElse(AddressRule.WEBSOCKET_MESSAGE) + websocketMessage.orElse(AddressRule.WEBSOCKET_MESSAGE), + useProxy.orElse(false) ); } @@ -56,7 +60,8 @@ PartialOptions merge(PartialOptions other) { action == null && other.action != null ? other.action : action, maxUpload.isPresent() ? maxUpload : other.maxUpload, maxDownload.isPresent() ? maxDownload : other.maxDownload, - websocketMessage.isPresent() ? websocketMessage : other.websocketMessage + websocketMessage.isPresent() ? websocketMessage : other.websocketMessage, + useProxy.isPresent() ? useProxy : other.useProxy ); } } diff --git a/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/ProxyType.java b/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/ProxyType.java new file mode 100644 index 000000000..cbbfa35a3 --- /dev/null +++ b/projects/core/src/main/java/dan200/computercraft/core/apis/http/options/ProxyType.java @@ -0,0 +1,18 @@ +// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers +// +// SPDX-License-Identifier: MPL-2.0 + +package dan200.computercraft.core.apis.http.options; + +/** + * The type of proxy to use for HTTP requests. + * + * @see dan200.computercraft.core.apis.http.NetworkUtils#getProxyHandler(Options, int) + * @see dan200.computercraft.core.CoreConfig#httpProxyType + */ +public enum ProxyType { + HTTP, + HTTPS, + SOCKS4, + SOCKS5 +} diff --git a/projects/core/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java b/projects/core/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java index d7d40c1c0..33fdab50e 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java +++ b/projects/core/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java @@ -124,6 +124,7 @@ private void doRequest(URI uri, HttpMethod method) { var socketAddress = NetworkUtils.getAddress(uri, ssl); var options = NetworkUtils.getOptions(uri.getHost(), socketAddress); var sslContext = ssl ? NetworkUtils.getSslContext() : null; + var proxy = NetworkUtils.getProxyHandler(options, timeout); // getAddress may have a slight delay, so let's perform another cancellation check. if (isClosed()) return; @@ -145,7 +146,7 @@ private void doRequest(URI uri, HttpMethod method) { .handler(new ChannelInitializer() { @Override protected void initChannel(SocketChannel ch) { - NetworkUtils.initChannel(ch, uri, socketAddress, sslContext, timeout); + NetworkUtils.initChannel(ch, uri, socketAddress, sslContext, proxy, timeout); var p = ch.pipeline(); if (timeout > 0) p.addLast(new ReadTimeoutHandler(timeout, TimeUnit.MILLISECONDS)); diff --git a/projects/core/src/main/java/dan200/computercraft/core/apis/http/websocket/Websocket.java b/projects/core/src/main/java/dan200/computercraft/core/apis/http/websocket/Websocket.java index 282b04c79..3dfb3fd8e 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/apis/http/websocket/Websocket.java +++ b/projects/core/src/main/java/dan200/computercraft/core/apis/http/websocket/Websocket.java @@ -117,6 +117,7 @@ private void doConnect() { var socketAddress = NetworkUtils.getAddress(uri, ssl); var options = NetworkUtils.getOptions(uri.getHost(), socketAddress); var sslContext = ssl ? NetworkUtils.getSslContext() : null; + var proxy = NetworkUtils.getProxyHandler(options, timeout); // getAddress may have a slight delay, so let's perform another cancellation check. if (isClosed()) return; @@ -127,7 +128,7 @@ private void doConnect() { .handler(new ChannelInitializer() { @Override protected void initChannel(SocketChannel ch) { - NetworkUtils.initChannel(ch, uri, socketAddress, sslContext, timeout); + NetworkUtils.initChannel(ch, uri, socketAddress, sslContext, proxy, timeout); var subprotocol = headers.get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL); var handshaker = new NoOriginWebSocketHandshaker( diff --git a/projects/fabric/build.gradle.kts b/projects/fabric/build.gradle.kts index f9e8fb6b0..576bac769 100644 --- a/projects/fabric/build.gradle.kts +++ b/projects/fabric/build.gradle.kts @@ -54,6 +54,8 @@ dependencies { include(libs.cobalt) include(libs.jzlib) include(libs.netty.http) + include(libs.netty.socks) + include(libs.netty.proxy) include(libs.nightConfig.core) include(libs.nightConfig.toml) @@ -65,6 +67,8 @@ dependencies { // in our POM, and this is the easiest way. runtimeOnly(libs.cobalt) runtimeOnly(libs.netty.http) + runtimeOnly(libs.netty.socks) + runtimeOnly(libs.netty.proxy) annotationProcessorEverywhere(libs.autoService) diff --git a/projects/fabric/src/main/java/dan200/computercraft/shared/platform/FabricConfigFile.java b/projects/fabric/src/main/java/dan200/computercraft/shared/platform/FabricConfigFile.java index 1115f9b95..1294e3428 100644 --- a/projects/fabric/src/main/java/dan200/computercraft/shared/platform/FabricConfigFile.java +++ b/projects/fabric/src/main/java/dan200/computercraft/shared/platform/FabricConfigFile.java @@ -34,11 +34,11 @@ public class FabricConfigFile implements ConfigFile { private final ConfigSpec spec; private final Trie entries; - private final Runnable onChange; + private final ConfigListener onChange; private @Nullable CommentedFileConfig config; - public FabricConfigFile(ConfigSpec spec, Trie entries, Runnable onChange) { + public FabricConfigFile(ConfigSpec spec, Trie entries, ConfigListener onChange) { this.spec = spec; this.entries = entries; this.onChange = onChange; @@ -95,7 +95,7 @@ private synchronized boolean loadConfig() { LOG.warn("Incorrect key {} was corrected from {} to {}", String.join(".", entryPath), oldValue, newValue); }); - onChange.run(); + onChange.onConfigChanged(config.getNioPath()); return corrected > 0; } @@ -204,7 +204,7 @@ public > Value defineEnum(String path, V defaultValue) { } @Override - public ConfigFile build(Runnable onChange) { + public ConfigFile build(ConfigListener onChange) { return new FabricConfigFile(spec, entries, onChange); } } diff --git a/projects/forge/build.gradle.kts b/projects/forge/build.gradle.kts index bb32e08fe..589e44f0d 100644 --- a/projects/forge/build.gradle.kts +++ b/projects/forge/build.gradle.kts @@ -147,6 +147,14 @@ dependencies { jarJar.ranged(this, "[${libs.versions.netty.get()},)") isTransitive = false } + minecraftEmbed(libs.netty.socks) { + jarJar.ranged(this, "[${libs.versions.netty.get()},)") + isTransitive = false + } + minecraftEmbed(libs.netty.proxy) { + jarJar.ranged(this, "[${libs.versions.netty.get()},)") + isTransitive = false + } testFixturesApi(libs.bundles.test) testFixturesApi(libs.bundles.kotlin) diff --git a/projects/forge/src/main/java/dan200/computercraft/ComputerCraft.java b/projects/forge/src/main/java/dan200/computercraft/ComputerCraft.java index fded03ddb..b47f39772 100644 --- a/projects/forge/src/main/java/dan200/computercraft/ComputerCraft.java +++ b/projects/forge/src/main/java/dan200/computercraft/ComputerCraft.java @@ -4,6 +4,7 @@ package dan200.computercraft; +import com.electronwill.nightconfig.core.file.FileConfig; import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.api.ForgeComputerCraftAPI; import dan200.computercraft.api.detail.ForgeDetailRegistries; @@ -94,10 +95,12 @@ public static void sync(ModConfigEvent.Reloading event) { private static void syncConfig(ModConfig config) { if (!config.getModId().equals(ComputerCraftAPI.MOD_ID)) return; + var path = config.getConfigData() instanceof FileConfig fileConfig ? fileConfig.getNioPath() : null; + if (config.getType() == ModConfig.Type.SERVER && ((ForgeConfigFile) ConfigSpec.serverSpec).spec().isLoaded()) { - ConfigSpec.syncServer(); + ConfigSpec.syncServer(path); } else if (config.getType() == ModConfig.Type.CLIENT) { - ConfigSpec.syncClient(); + ConfigSpec.syncClient(path); } } } diff --git a/projects/forge/src/main/java/dan200/computercraft/shared/platform/ForgeConfigFile.java b/projects/forge/src/main/java/dan200/computercraft/shared/platform/ForgeConfigFile.java index eba7500ce..2f44b2e73 100644 --- a/projects/forge/src/main/java/dan200/computercraft/shared/platform/ForgeConfigFile.java +++ b/projects/forge/src/main/java/dan200/computercraft/shared/platform/ForgeConfigFile.java @@ -117,7 +117,7 @@ public > ConfigFile.Value defineEnum(String path, V default } @Override - public ConfigFile build(Runnable onChange) { + public ConfigFile build(ConfigListener onChange) { var spec = builder.build(); entries.stream().forEach(x -> { if (x instanceof ValueImpl value) value.owner = spec;