Skip to content

Commit bc9198f

Browse files
committed
🤓 Modflared now prefers using the cloudflared version installed in PATH
1 parent 25433df commit bc9198f

File tree

7 files changed

+116
-38
lines changed

7 files changed

+116
-38
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package de.rafael.modflared.binary;
2+
3+
import de.rafael.modflared.Modflared;
4+
import de.rafael.modflared.binary.download.DownloadedCloudflared;
5+
import de.rafael.modflared.binary.local.LocalCloudflared;
6+
import de.rafael.modflared.tunnel.RunningTunnel;
7+
8+
import java.util.concurrent.CompletableFuture;
9+
10+
public abstract class Cloudflared {
11+
12+
protected volatile String version;
13+
14+
public Cloudflared(String version) {
15+
this.version = version;
16+
}
17+
18+
public abstract CompletableFuture<Void> prepare();
19+
20+
public abstract String[] buildCommand(RunningTunnel.Access access);
21+
22+
public static CompletableFuture<Cloudflared> create() {
23+
var local = LocalCloudflared.tryCreate();
24+
if(local != null) return CompletableFuture.completedFuture(local);
25+
26+
return DownloadedCloudflared.tryCreate();
27+
}
28+
29+
public RunningTunnel createTunnel(RunningTunnel.Access access) {
30+
try {
31+
return RunningTunnel.createTunnel(this, access).get();
32+
} catch (Exception exception) {
33+
Modflared.LOGGER.error("Failed to create tunnel", exception);
34+
return null;
35+
}
36+
}
37+
38+
}

common/src/main/java/de/rafael/modflared/download/CloudflaredDownload.java renamed to common/src/main/java/de/rafael/modflared/binary/download/CloudflaredDownload.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package de.rafael.modflared.download;
1+
package de.rafael.modflared.binary.download;
22

33
import org.jetbrains.annotations.NotNull;
44

common/src/main/java/de/rafael/modflared/download/CloudflaredVersion.java renamed to common/src/main/java/de/rafael/modflared/binary/download/DownloadedCloudflared.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package de.rafael.modflared.download;
1+
package de.rafael.modflared.binary.download;
22

33
//------------------------------
44
//
@@ -9,6 +9,7 @@
99
//------------------------------
1010

1111
import de.rafael.modflared.Modflared;
12+
import de.rafael.modflared.binary.Cloudflared;
1213
import de.rafael.modflared.github.GithubAPI;
1314
import de.rafael.modflared.tunnel.RunningTunnel;
1415
import de.rafael.modflared.tunnel.manager.TunnelManager;
@@ -22,43 +23,35 @@
2223
import java.net.URI;
2324
import java.nio.charset.StandardCharsets;
2425
import java.nio.file.Files;
26+
import java.util.Arrays;
2527
import java.util.concurrent.CompletableFuture;
2628

27-
public class CloudflaredVersion {
29+
public class DownloadedCloudflared extends Cloudflared {
2830

2931
private final CloudflaredDownload download;
30-
private volatile String version;
3132

3233
private static final File VERSION_FILE = new File(TunnelManager.DATA_FOLDER, "version.json");
3334

3435
private static final String GITHUB_DOWNLOAD_ENDPOINT = "https://github.com/cloudflare/cloudflared/releases/download/";
3536

36-
public CloudflaredVersion(CloudflaredDownload download, String version) {
37-
this.version = version;
37+
public DownloadedCloudflared(CloudflaredDownload download, String version) {
38+
super(version);
3839
this.download = download;
3940
}
4041

41-
public static CompletableFuture<CloudflaredVersion> create() {
42+
public static CompletableFuture<Cloudflared> tryCreate() {
4243
if(VERSION_FILE.exists()) {
4344
try {
44-
var version = Modflared.GSON.fromJson(new InputStreamReader(new FileInputStream(VERSION_FILE)), CloudflaredVersion.class);
45+
var version = Modflared.GSON.fromJson(new InputStreamReader(new FileInputStream(VERSION_FILE)), DownloadedCloudflared.class);
4546
if(version != null) return CompletableFuture.completedFuture(version);
4647
} catch (Throwable throwable) {
4748
Modflared.LOGGER.error("Failed to load existing version file creating new one...", throwable);
4849
}
4950
}
50-
return GithubAPI.requestLatestVersion().thenApply(latestVersion -> new CloudflaredVersion(CloudflaredDownload.find(), latestVersion));
51-
}
52-
53-
public @Nullable RunningTunnel createTunnel(RunningTunnel.Access access) {
54-
try {
55-
return RunningTunnel.createTunnel(this, access).get();
56-
} catch (Exception exception) {
57-
Modflared.LOGGER.error("Failed to create tunnel", exception);
58-
return null;
59-
}
51+
return GithubAPI.requestLatestVersion().thenApply(latestVersion -> new DownloadedCloudflared(CloudflaredDownload.find(), latestVersion));
6052
}
6153

54+
@Override
6255
public CompletableFuture<Void> prepare() {
6356
if(isInstalled()) {
6457
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
@@ -88,6 +81,16 @@ public CompletableFuture<Void> prepare() {
8881
}
8982
}
9083

84+
@Override
85+
public String[] buildCommand(RunningTunnel.@NotNull Access access) {
86+
var command = access.command(createBinaryRef().getName(), true);
87+
Modflared.LOGGER.info(Arrays.toString(command).replace(",",""));
88+
if (Platform.get() == Platform.WINDOWS) {
89+
command[0] = "\"" + TunnelManager.DATA_FOLDER.getAbsolutePath() + "\\" + command[0] + "\"";
90+
}
91+
return command;
92+
}
93+
9194
private CompletableFuture<Void> downloadAndSaveInfo() {
9295
return downloadFile().thenAccept(unused -> {
9396
try {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package de.rafael.modflared.binary.local;
2+
3+
import de.rafael.modflared.Modflared;
4+
import de.rafael.modflared.binary.Cloudflared;
5+
import de.rafael.modflared.tunnel.RunningTunnel;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.io.BufferedReader;
10+
import java.io.InputStreamReader;
11+
import java.util.concurrent.CompletableFuture;
12+
13+
public class LocalCloudflared extends Cloudflared {
14+
15+
public LocalCloudflared(String version) {
16+
super(version);
17+
}
18+
19+
@Override
20+
public CompletableFuture<Void> prepare() {
21+
return CompletableFuture.completedFuture(null);
22+
}
23+
24+
@Override
25+
public String[] buildCommand(RunningTunnel.@NotNull Access access) {
26+
return access.command("cloudflared", false);
27+
}
28+
29+
public static @Nullable Cloudflared tryCreate() {
30+
// Check if cloudflared is already installed on the system
31+
try {
32+
var builder = new ProcessBuilder("cloudflared", "--version");
33+
var process = builder.start();
34+
var reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
35+
String versionString = reader.readLine();
36+
String version = versionString.split(" ")[2];
37+
Modflared.LOGGER.info("Cloudflared output: {}", versionString);
38+
Modflared.LOGGER.info("Cloudflared version {} is already installed on the system", version);
39+
return new LocalCloudflared(version);
40+
} catch (Throwable ignored) {
41+
Modflared.LOGGER.info("Cloudflared is not installed on the system. Downloading it if necessary...");
42+
}
43+
return null;
44+
}
45+
46+
}

common/src/main/java/de/rafael/modflared/tunnel/RunningTunnel.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,31 @@
99
//------------------------------
1010

1111
import de.rafael.modflared.Modflared;
12-
import de.rafael.modflared.download.CloudflaredVersion;
12+
import de.rafael.modflared.binary.Cloudflared;
1313
import de.rafael.modflared.tunnel.manager.TunnelManager;
1414
import org.jetbrains.annotations.Contract;
1515
import org.jetbrains.annotations.NotNull;
1616
import org.lwjgl.system.Platform;
1717

1818
import java.io.*;
1919
import java.net.InetSocketAddress;
20-
import java.util.Arrays;
2120
import java.util.concurrent.CompletableFuture;
2221

2322
public record RunningTunnel(Access access, Process process) {
2423

25-
public static @NotNull CompletableFuture<RunningTunnel> createTunnel(@NotNull CloudflaredVersion binary, @NotNull Access access) {
24+
public static @NotNull CompletableFuture<RunningTunnel> createTunnel(@NotNull Cloudflared binary, @NotNull Access access) {
2625
var future = new CompletableFuture<RunningTunnel>();
2726
Modflared.EXECUTOR.execute(() -> {
2827
try {
29-
var command = access.command(binary.createBinaryRef());
30-
Modflared.LOGGER.info(Arrays.toString(command).replace(",",""));
31-
if (Platform.get() == Platform.WINDOWS) {
32-
command[0] = "\"" + TunnelManager.DATA_FOLDER.getAbsolutePath() + "\\" + command[0] + "\"";
33-
}
34-
ProcessBuilder processBuilder = new ProcessBuilder(command);
28+
ProcessBuilder processBuilder = new ProcessBuilder(binary.buildCommand(access));
3529
// Since LINUX, MACOSX, and WINDOWS are the only options, this will work to only set the directory for Linux and MacOS
3630
if (Platform.get() != Platform.WINDOWS) {
3731
processBuilder.directory(TunnelManager.DATA_FOLDER);
3832
}
3933
processBuilder.redirectErrorStream(true);
40-
Process process = processBuilder.start();
34+
var process = processBuilder.start();
4135

42-
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
36+
var reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
4337

4438
String line;
4539
while ((line = reader.readLine()) != null) {
@@ -68,9 +62,8 @@ public record Access(String protocol, String hostname, InetSocketAddress tunnelA
6862
return new Access("tcp", host, new InetSocketAddress("127.0.0.1", (int) (Math.random() * 10000 + 25565)));
6963
}
7064

71-
@Contract("_ -> new")
72-
public String @NotNull [] command(@NotNull File executable) {
73-
return new String[] {(Platform.get() != Platform.WINDOWS ? "./" : "") + executable.getName(), "access", protocol, "--hostname", hostname, "--url", tunnelAddress.getHostString() + ":" + tunnelAddress.getPort()};
65+
public String @NotNull [] command(@NotNull String fileName, boolean prefix) {
66+
return new String[] {(prefix && Platform.get() != Platform.WINDOWS ? "./" : "") + fileName, "access", protocol, "--hostname", hostname, "--url", tunnelAddress.getHostString() + ":" + tunnelAddress.getPort()};
7467
}
7568
}
7669

common/src/main/java/de/rafael/modflared/tunnel/manager/TunnelManager.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.google.gson.JsonParser;
66
import de.rafael.modflared.Modflared;
77
import de.rafael.modflared.ModflaredPlatform;
8-
import de.rafael.modflared.download.CloudflaredVersion;
8+
import de.rafael.modflared.binary.Cloudflared;
99
import de.rafael.modflared.interfaces.mixin.IClientConnection;
1010
import de.rafael.modflared.tunnel.RunningTunnel;
1111
import de.rafael.modflared.tunnel.TunnelStatus;
@@ -28,11 +28,9 @@
2828
import java.net.InetSocketAddress;
2929
import java.net.UnknownHostException;
3030
import java.util.ArrayList;
31-
import java.util.Arrays;
3231
import java.util.List;
3332
import java.util.Properties;
3433
import java.util.concurrent.atomic.AtomicReference;
35-
import java.util.stream.Collectors;
3634

3735
public class TunnelManager {
3836

@@ -42,7 +40,7 @@ public class TunnelManager {
4240

4341
public static final Logger CLOUDFLARE_LOGGER = LoggerFactory.getLogger("Cloudflared");
4442

45-
private final AtomicReference<CloudflaredVersion> cloudflared = new AtomicReference<>();
43+
private final AtomicReference<Cloudflared> cloudflared = new AtomicReference<>();
4644
private final List<ServerAddress> forcedTunnels = new ArrayList<>();
4745

4846
private final List<RunningTunnel> runningTunnels = new ArrayList<>();
@@ -184,7 +182,7 @@ public TunnelStatus handleConnect(@NotNull InetSocketAddress address) {
184182
}
185183

186184
public void prepareBinary() {
187-
CloudflaredVersion.create().whenComplete((version, throwable) -> {
185+
Cloudflared.create().whenComplete((version, throwable) -> {
188186
if (throwable != null) {
189187
Modflared.LOGGER.error(throwable.getMessage(), throwable);
190188
} else {

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ enabled_platforms=fabric,neoforge
1010

1111
# Mod Properties
1212
archives_base_name=modflared
13-
mod_version=1.2.1
13+
mod_version=1.2.2
1414
maven_group=de.rafael
1515
mod_minecraft_version=1.21
1616

0 commit comments

Comments
 (0)