Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Jan 14, 2023
2 parents 2dc21b4 + 47785ad commit 24c9aa1
Show file tree
Hide file tree
Showing 10 changed files with 473 additions and 59 deletions.
61 changes: 10 additions & 51 deletions src/main/java/net/raphimc/viaproxy/ViaProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package net.raphimc.viaproxy;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
Expand All @@ -36,20 +34,18 @@
import net.raphimc.viaproxy.cli.ConsoleHandler;
import net.raphimc.viaproxy.cli.options.Options;
import net.raphimc.viaproxy.injection.Java17ToJava8;
import net.raphimc.viaproxy.plugins.PluginManager;
import net.raphimc.viaproxy.protocolhack.ProtocolHack;
import net.raphimc.viaproxy.proxy.ProxyConnection;
import net.raphimc.viaproxy.proxy.client2proxy.Client2ProxyChannelInitializer;
import net.raphimc.viaproxy.proxy.client2proxy.Client2ProxyHandler;
import net.raphimc.viaproxy.saves.SaveManager;
import net.raphimc.viaproxy.tasks.AccountRefreshTask;
import net.raphimc.viaproxy.tasks.LoaderTask;
import net.raphimc.viaproxy.tasks.UpdatedCheckTask;
import net.raphimc.viaproxy.ui.ViaProxyUI;
import net.raphimc.viaproxy.util.logging.Logger;

import javax.swing.*;
import java.awt.*;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ViaProxy {

Expand All @@ -58,6 +54,7 @@ public class ViaProxy {
public static SaveManager saveManager;
public static NetServer currentProxyServer;
public static ChannelGroup c2pChannels;
public static ViaProxyUI ui;

public static void main(String[] args) throws Throwable {
final IClassProvider classProvider = new GuavaClassPathProvider();
Expand All @@ -81,60 +78,22 @@ public static void injectedMain(String[] args) throws InterruptedException {
setNettyParameters();
MCPipeline.useOptimizedPipeline();
c2pChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
Thread loaderThread = new Thread(() -> {
ProtocolHack.init();
PluginManager.loadPlugins();
}, "ViaProtocolHack-Loader");
Thread accountRefreshThread = new Thread(() -> {
saveManager.accountsSave.refreshAccounts();
saveManager.save();
}, "AccountRefresh");
Thread updateCheckThread = new Thread(() -> {
if (VERSION.startsWith("$")) return; // Dev env check
try {
URL url = new URL("https://api.github.com/repos/RaphiMC/ViaProxy/releases/latest");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "ViaProxy/" + VERSION);
con.setConnectTimeout(5000);
con.setReadTimeout(5000);

InputStream in = con.getInputStream();
byte[] bytes = new byte[1024];
int read;
StringBuilder builder = new StringBuilder();
while ((read = in.read(bytes)) != -1) builder.append(new String(bytes, 0, read));
con.disconnect();

JsonObject object = JsonParser.parseString(builder.toString()).getAsJsonObject();
String latestVersion = object.get("tag_name").getAsString().substring(1);
if (!VERSION.equals(latestVersion)) {
Logger.LOGGER.warn("You are running an outdated version of ViaProxy! Latest version: " + latestVersion);
if (hasUI) {
SwingUtilities.invokeLater(() -> {
JFrame frontFrame = new JFrame();
frontFrame.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(frontFrame, "You are running an outdated version of ViaProxy!\nCurrent version: " + VERSION + "\nLatest version: " + latestVersion, "ViaProxy", JOptionPane.WARNING_MESSAGE);
});
}
}
} catch (Throwable ignored) {
}
}, "UpdateCheck");
Thread loaderThread = new Thread(new LoaderTask(), "ViaProtocolHack-Loader");
Thread accountRefreshThread = new Thread(new AccountRefreshTask(saveManager), "AccountRefresh");
Thread updateCheckThread = new Thread(new UpdatedCheckTask(hasUI), "UpdateCheck");

if (hasUI) {
loaderThread.start();
accountRefreshThread.start();
final ViaProxyUI[] ui = new ViaProxyUI[1];
SwingUtilities.invokeLater(() -> ui[0] = new ViaProxyUI());
SwingUtilities.invokeLater(() -> ui = new ViaProxyUI());
updateCheckThread.start();
loaderThread.join();
accountRefreshThread.join();
while (ui[0] == null) {
while (ui == null) {
Logger.LOGGER.info("Waiting for UI to be initialized...");
Thread.sleep(1000);
}
ui[0].setReady();
ui.setReady();
Logger.LOGGER.info("ViaProxy started successfully!");
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/raphimc/viaproxy/saves/SaveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.gson.JsonObject;
import net.lenni0451.reflect.stream.RStream;
import net.raphimc.viaproxy.saves.impl.AccountsSave;
import net.raphimc.viaproxy.saves.impl.UISave;
import net.raphimc.viaproxy.util.logging.Logger;

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

public final AccountsSave accountsSave = new AccountsSave();
public final UISave uiSave = new UISave();

public SaveManager() {
this.load();
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/net/raphimc/viaproxy/saves/impl/UISave.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.saves.impl;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.raphimc.viaproxy.saves.AbstractSave;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

public class UISave extends AbstractSave {

private final Map<String, String> values;

public UISave() {
super("ui");

this.values = new HashMap<>();
}

@Override
public void load(JsonElement jsonElement) {
this.values.clear();
for (Map.Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) this.values.put(entry.getKey(), entry.getValue().getAsString());
}

@Override
public JsonElement save() {
JsonObject jsonObject = new JsonObject();
for (Map.Entry<String, String> entry : this.values.entrySet()) jsonObject.addProperty(entry.getKey(), entry.getValue());
return jsonObject;
}

public void put(final String key, final String value) {
this.values.put(key, value);
}

public void loadTextField(final String key, final JTextField textField) {
try {
String value = this.values.get(key);
if (value != null) textField.setText(value);
} catch (Throwable ignored) {
}
}

public void loadComboBox(final String key, final JComboBox<?> comboBox) {
try {
int index = Integer.parseInt(this.values.get(key));
if (index >= 0 && index < comboBox.getItemCount()) comboBox.setSelectedIndex(index);
} catch (Throwable ignored) {
}
}

public void loadSpinner(final String key, final JSpinner spinner) {
try {
Integer value = Integer.valueOf(this.values.get(key));
if (spinner.getModel() instanceof SpinnerNumberModel) {
SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel();
Comparable<Integer> minimum = (Comparable<Integer>) model.getMinimum();
Comparable<Integer> maximum = (Comparable<Integer>) model.getMaximum();
if (minimum.compareTo(value) <= 0 && maximum.compareTo(value) >= 0) spinner.setValue(value);
} else {
spinner.setValue(value);
}
} catch (Throwable ignored) {
}
}

public void loadCheckBox(final String key, final JCheckBox checkBox) {
try {
boolean value = Boolean.parseBoolean(this.values.get(key));
checkBox.setSelected(value);
} catch (Throwable ignored) {
}
}

}
36 changes: 36 additions & 0 deletions src/main/java/net/raphimc/viaproxy/tasks/AccountRefreshTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.tasks;

import net.raphimc.viaproxy.saves.SaveManager;

public class AccountRefreshTask implements Runnable {

private final SaveManager saveManager;

public AccountRefreshTask(final SaveManager saveManager) {
this.saveManager = saveManager;
}

@Override
public void run() {
this.saveManager.accountsSave.refreshAccounts();
this.saveManager.save();
}

}
31 changes: 31 additions & 0 deletions src/main/java/net/raphimc/viaproxy/tasks/LoaderTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.tasks;

import net.raphimc.viaproxy.plugins.PluginManager;
import net.raphimc.viaproxy.protocolhack.ProtocolHack;

public class LoaderTask implements Runnable {

@Override
public void run() {
ProtocolHack.init();
PluginManager.loadPlugins();
}

}
Loading

0 comments on commit 24c9aa1

Please sign in to comment.