Skip to content

Commit 24c9aa1

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 2dc21b4 + 47785ad commit 24c9aa1

File tree

10 files changed

+473
-59
lines changed

10 files changed

+473
-59
lines changed

src/main/java/net/raphimc/viaproxy/ViaProxy.java

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
*/
1818
package net.raphimc.viaproxy;
1919

20-
import com.google.gson.JsonObject;
21-
import com.google.gson.JsonParser;
2220
import io.netty.channel.Channel;
2321
import io.netty.channel.group.ChannelGroup;
2422
import io.netty.channel.group.DefaultChannelGroup;
@@ -36,20 +34,18 @@
3634
import net.raphimc.viaproxy.cli.ConsoleHandler;
3735
import net.raphimc.viaproxy.cli.options.Options;
3836
import net.raphimc.viaproxy.injection.Java17ToJava8;
39-
import net.raphimc.viaproxy.plugins.PluginManager;
40-
import net.raphimc.viaproxy.protocolhack.ProtocolHack;
4137
import net.raphimc.viaproxy.proxy.ProxyConnection;
4238
import net.raphimc.viaproxy.proxy.client2proxy.Client2ProxyChannelInitializer;
4339
import net.raphimc.viaproxy.proxy.client2proxy.Client2ProxyHandler;
4440
import net.raphimc.viaproxy.saves.SaveManager;
41+
import net.raphimc.viaproxy.tasks.AccountRefreshTask;
42+
import net.raphimc.viaproxy.tasks.LoaderTask;
43+
import net.raphimc.viaproxy.tasks.UpdatedCheckTask;
4544
import net.raphimc.viaproxy.ui.ViaProxyUI;
4645
import net.raphimc.viaproxy.util.logging.Logger;
4746

4847
import javax.swing.*;
4948
import java.awt.*;
50-
import java.io.InputStream;
51-
import java.net.HttpURLConnection;
52-
import java.net.URL;
5349

5450
public class ViaProxy {
5551

@@ -58,6 +54,7 @@ public class ViaProxy {
5854
public static SaveManager saveManager;
5955
public static NetServer currentProxyServer;
6056
public static ChannelGroup c2pChannels;
57+
public static ViaProxyUI ui;
6158

6259
public static void main(String[] args) throws Throwable {
6360
final IClassProvider classProvider = new GuavaClassPathProvider();
@@ -81,60 +78,22 @@ public static void injectedMain(String[] args) throws InterruptedException {
8178
setNettyParameters();
8279
MCPipeline.useOptimizedPipeline();
8380
c2pChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
84-
Thread loaderThread = new Thread(() -> {
85-
ProtocolHack.init();
86-
PluginManager.loadPlugins();
87-
}, "ViaProtocolHack-Loader");
88-
Thread accountRefreshThread = new Thread(() -> {
89-
saveManager.accountsSave.refreshAccounts();
90-
saveManager.save();
91-
}, "AccountRefresh");
92-
Thread updateCheckThread = new Thread(() -> {
93-
if (VERSION.startsWith("$")) return; // Dev env check
94-
try {
95-
URL url = new URL("https://api.github.com/repos/RaphiMC/ViaProxy/releases/latest");
96-
HttpURLConnection con = (HttpURLConnection) url.openConnection();
97-
con.setRequestMethod("GET");
98-
con.setRequestProperty("User-Agent", "ViaProxy/" + VERSION);
99-
con.setConnectTimeout(5000);
100-
con.setReadTimeout(5000);
101-
102-
InputStream in = con.getInputStream();
103-
byte[] bytes = new byte[1024];
104-
int read;
105-
StringBuilder builder = new StringBuilder();
106-
while ((read = in.read(bytes)) != -1) builder.append(new String(bytes, 0, read));
107-
con.disconnect();
108-
109-
JsonObject object = JsonParser.parseString(builder.toString()).getAsJsonObject();
110-
String latestVersion = object.get("tag_name").getAsString().substring(1);
111-
if (!VERSION.equals(latestVersion)) {
112-
Logger.LOGGER.warn("You are running an outdated version of ViaProxy! Latest version: " + latestVersion);
113-
if (hasUI) {
114-
SwingUtilities.invokeLater(() -> {
115-
JFrame frontFrame = new JFrame();
116-
frontFrame.setAlwaysOnTop(true);
117-
JOptionPane.showMessageDialog(frontFrame, "You are running an outdated version of ViaProxy!\nCurrent version: " + VERSION + "\nLatest version: " + latestVersion, "ViaProxy", JOptionPane.WARNING_MESSAGE);
118-
});
119-
}
120-
}
121-
} catch (Throwable ignored) {
122-
}
123-
}, "UpdateCheck");
81+
Thread loaderThread = new Thread(new LoaderTask(), "ViaProtocolHack-Loader");
82+
Thread accountRefreshThread = new Thread(new AccountRefreshTask(saveManager), "AccountRefresh");
83+
Thread updateCheckThread = new Thread(new UpdatedCheckTask(hasUI), "UpdateCheck");
12484

12585
if (hasUI) {
12686
loaderThread.start();
12787
accountRefreshThread.start();
128-
final ViaProxyUI[] ui = new ViaProxyUI[1];
129-
SwingUtilities.invokeLater(() -> ui[0] = new ViaProxyUI());
88+
SwingUtilities.invokeLater(() -> ui = new ViaProxyUI());
13089
updateCheckThread.start();
13190
loaderThread.join();
13291
accountRefreshThread.join();
133-
while (ui[0] == null) {
92+
while (ui == null) {
13493
Logger.LOGGER.info("Waiting for UI to be initialized...");
13594
Thread.sleep(1000);
13695
}
137-
ui[0].setReady();
96+
ui.setReady();
13897
Logger.LOGGER.info("ViaProxy started successfully!");
13998
return;
14099
}

src/main/java/net/raphimc/viaproxy/saves/SaveManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.gson.JsonObject;
2222
import net.lenni0451.reflect.stream.RStream;
2323
import net.raphimc.viaproxy.saves.impl.AccountsSave;
24+
import net.raphimc.viaproxy.saves.impl.UISave;
2425
import net.raphimc.viaproxy.util.logging.Logger;
2526

2627
import java.io.File;
@@ -33,6 +34,7 @@ public class SaveManager {
3334
private static final Gson GSON = new Gson();
3435

3536
public final AccountsSave accountsSave = new AccountsSave();
37+
public final UISave uiSave = new UISave();
3638

3739
public SaveManager() {
3840
this.load();
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
3+
* Copyright (C) 2023 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.saves.impl;
19+
20+
import com.google.gson.JsonElement;
21+
import com.google.gson.JsonObject;
22+
import net.raphimc.viaproxy.saves.AbstractSave;
23+
24+
import javax.swing.*;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
public class UISave extends AbstractSave {
29+
30+
private final Map<String, String> values;
31+
32+
public UISave() {
33+
super("ui");
34+
35+
this.values = new HashMap<>();
36+
}
37+
38+
@Override
39+
public void load(JsonElement jsonElement) {
40+
this.values.clear();
41+
for (Map.Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) this.values.put(entry.getKey(), entry.getValue().getAsString());
42+
}
43+
44+
@Override
45+
public JsonElement save() {
46+
JsonObject jsonObject = new JsonObject();
47+
for (Map.Entry<String, String> entry : this.values.entrySet()) jsonObject.addProperty(entry.getKey(), entry.getValue());
48+
return jsonObject;
49+
}
50+
51+
public void put(final String key, final String value) {
52+
this.values.put(key, value);
53+
}
54+
55+
public void loadTextField(final String key, final JTextField textField) {
56+
try {
57+
String value = this.values.get(key);
58+
if (value != null) textField.setText(value);
59+
} catch (Throwable ignored) {
60+
}
61+
}
62+
63+
public void loadComboBox(final String key, final JComboBox<?> comboBox) {
64+
try {
65+
int index = Integer.parseInt(this.values.get(key));
66+
if (index >= 0 && index < comboBox.getItemCount()) comboBox.setSelectedIndex(index);
67+
} catch (Throwable ignored) {
68+
}
69+
}
70+
71+
public void loadSpinner(final String key, final JSpinner spinner) {
72+
try {
73+
Integer value = Integer.valueOf(this.values.get(key));
74+
if (spinner.getModel() instanceof SpinnerNumberModel) {
75+
SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel();
76+
Comparable<Integer> minimum = (Comparable<Integer>) model.getMinimum();
77+
Comparable<Integer> maximum = (Comparable<Integer>) model.getMaximum();
78+
if (minimum.compareTo(value) <= 0 && maximum.compareTo(value) >= 0) spinner.setValue(value);
79+
} else {
80+
spinner.setValue(value);
81+
}
82+
} catch (Throwable ignored) {
83+
}
84+
}
85+
86+
public void loadCheckBox(final String key, final JCheckBox checkBox) {
87+
try {
88+
boolean value = Boolean.parseBoolean(this.values.get(key));
89+
checkBox.setSelected(value);
90+
} catch (Throwable ignored) {
91+
}
92+
}
93+
94+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
3+
* Copyright (C) 2023 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.tasks;
19+
20+
import net.raphimc.viaproxy.saves.SaveManager;
21+
22+
public class AccountRefreshTask implements Runnable {
23+
24+
private final SaveManager saveManager;
25+
26+
public AccountRefreshTask(final SaveManager saveManager) {
27+
this.saveManager = saveManager;
28+
}
29+
30+
@Override
31+
public void run() {
32+
this.saveManager.accountsSave.refreshAccounts();
33+
this.saveManager.save();
34+
}
35+
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
3+
* Copyright (C) 2023 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.tasks;
19+
20+
import net.raphimc.viaproxy.plugins.PluginManager;
21+
import net.raphimc.viaproxy.protocolhack.ProtocolHack;
22+
23+
public class LoaderTask implements Runnable {
24+
25+
@Override
26+
public void run() {
27+
ProtocolHack.init();
28+
PluginManager.loadPlugins();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)