1717 */
1818package net .raphimc .viaproxy ;
1919
20- import com .google .gson .JsonObject ;
21- import com .google .gson .JsonParser ;
2220import io .netty .channel .Channel ;
2321import io .netty .channel .group .ChannelGroup ;
2422import io .netty .channel .group .DefaultChannelGroup ;
3634import net .raphimc .viaproxy .cli .ConsoleHandler ;
3735import net .raphimc .viaproxy .cli .options .Options ;
3836import net .raphimc .viaproxy .injection .Java17ToJava8 ;
39- import net .raphimc .viaproxy .plugins .PluginManager ;
40- import net .raphimc .viaproxy .protocolhack .ProtocolHack ;
4137import net .raphimc .viaproxy .proxy .ProxyConnection ;
4238import net .raphimc .viaproxy .proxy .client2proxy .Client2ProxyChannelInitializer ;
4339import net .raphimc .viaproxy .proxy .client2proxy .Client2ProxyHandler ;
4440import 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 ;
4544import net .raphimc .viaproxy .ui .ViaProxyUI ;
4645import net .raphimc .viaproxy .util .logging .Logger ;
4746
4847import javax .swing .*;
4948import java .awt .*;
50- import java .io .InputStream ;
51- import java .net .HttpURLConnection ;
52- import java .net .URL ;
5349
5450public 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!\n Current version: " + VERSION + "\n Latest 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 }
0 commit comments