commit 9b3e1182ab56a82442595db49fe618b429d631a4 Author: BelPE Date: Thu Mar 16 12:28:14 2023 +0800 initial-commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..466e248 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +out/ \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/artifacts/proxy.xml b/.idea/artifacts/proxy.xml new file mode 100644 index 0000000..677bf83 --- /dev/null +++ b/.idea/artifacts/proxy.xml @@ -0,0 +1,12 @@ + + + $PROJECT_DIR$/out/artifacts/proxy + + + + + + + + + \ No newline at end of file diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml new file mode 100644 index 0000000..18a9980 --- /dev/null +++ b/.idea/dbnavigator.xml @@ -0,0 +1,463 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/dhaverdLogs.xml b/.idea/libraries/dhaverdLogs.xml new file mode 100644 index 0000000..c632e3f --- /dev/null +++ b/.idea/libraries/dhaverdLogs.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/json_simple_1_1_1.xml b/.idea/libraries/json_simple_1_1_1.xml new file mode 100644 index 0000000..23fd83c --- /dev/null +++ b/.idea/libraries/json_simple_1_1_1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0ffafee --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/dhaverdLogs.jar b/lib/dhaverdLogs.jar new file mode 100644 index 0000000..e17d051 Binary files /dev/null and b/lib/dhaverdLogs.jar differ diff --git a/lib/json-simple-1.1.1.jar b/lib/json-simple-1.1.1.jar new file mode 100644 index 0000000..dfd5856 Binary files /dev/null and b/lib/json-simple-1.1.1.jar differ diff --git a/proxy.iml b/proxy.iml new file mode 100644 index 0000000..d59139e --- /dev/null +++ b/proxy.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..abb9c37 --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: org.proxy.Main + diff --git a/src/org/proxy/Main.java b/src/org/proxy/Main.java new file mode 100644 index 0000000..d52c7e6 --- /dev/null +++ b/src/org/proxy/Main.java @@ -0,0 +1,162 @@ +package org.proxy; + +import org.json.simple.parser.ParseException; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; +import java.net.*; +import java.util.Locale; + +import static javax.swing.JOptionPane.showMessageDialog; +import static org.dhaverdLogs.DhaverdLogs.*; +import static org.proxy.loadConfig.getSocketPort; + +public class Main { + + public static boolean isLinux = System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux"); + public static ServerSocket serverSocket; + + static { + try { + serverSocket = new ServerSocket(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public Main() throws IOException { + } + + public static void main(String[] args) throws IOException, ParseException { + setLogDir("logs"); + setLogName("startup"); + bindSocket(); + systemTray(); + } + + public static void bindSocket() throws IOException, ParseException { + String context = "BindSocket"; + InetAddress inetAddress = InetAddress.getByName("localhost"); + int port = getSocketPort(); + SocketAddress endPoint = new InetSocketAddress(inetAddress, port); + try { + serverSocket.bind(endPoint); + } catch (BindException e) { + showMessageDialog(null, "Приложение уже запущено"); + exeptionActions(context, e); + System.exit(0); + } + setLog(context, "Socket binded", true, false); + setLog(context, "Local Socket Address: "+serverSocket.getLocalSocketAddress(), true, false); + } + + public static void systemTray() throws IOException { + String context = "SystemTray"; + if(! SystemTray.isSupported() ) { + setLog(context,"System tray is unsupported!", true, false); + return; + } + Font trayFont; + if (isLinux){ + trayFont = new Font("Tempora LGC Uni", Font.PLAIN, 16); + } else { + trayFont = new Font("Arial", Font.PLAIN, 12); + } + PopupMenu trayMenu = new PopupMenu(); + MenuItem proxyOn = new MenuItem("Proxy On"); + proxyOn.setFont(trayFont); + proxyOn.addActionListener(proxyOnListener()); + MenuItem proxyOff = new MenuItem("Proxy Off"); + proxyOff.setFont(trayFont); + proxyOff.addActionListener(proxyOffListener()); + MenuItem trayExit = new MenuItem("Exit"); + trayExit.setFont(trayFont); + trayExit.addActionListener(trayExitListener()); + trayMenu.add(proxyOn); + trayMenu.add(proxyOff); + trayMenu.add(trayExit); + Image icon; + if (isLinux){ + icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon.png"); + } else { + icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon.png"); + } + TrayIcon trayIcon = new TrayIcon(icon, "Proxy", trayMenu); + if (isLinux){ + trayIcon.setImageAutoSize(false); + } else { + trayIcon.setImageAutoSize(true); + } + SystemTray tray = SystemTray.getSystemTray(); + try { + tray.add(trayIcon); + } catch (AWTException e) { + exeptionActions(context, e); + } + setLog(context,"System tray launched", true, false); + } + + public static ActionListener trayExitListener() { + ActionListener listener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + setLog("Program","Closing server socket...", true, false); + setLog( "Program","Exiting program", true, false); + serverSocket.close(); + } catch (IOException ex) { + exeptionActions("TrayExitListener", ex); + } + System.exit(0); + } + }; + return listener; + } + + public static ActionListener proxyOnListener() { + ActionListener listener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + setProxyOn(); + } catch (IOException | InterruptedException ex) { + exeptionActions("ProxyOn", ex); + } + } + }; + return listener; + } + + public static ActionListener proxyOffListener() { + ActionListener listener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + setProxyOff(); + } catch (IOException | InterruptedException ex) { + exeptionActions("ProxyOff", ex); + } + } + }; + return listener; + } + + public static void setProxyOn() throws IOException, InterruptedException { + execute("reg delete \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" /v AutoConfigURL /f"); + execute("reg add \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" /v AutoConfigURL /t REG_SZ /d \"http://proxy.mcs.br:8080/array.dll?Get.Routing.Script\""); + } + + public static void setProxyOff() throws IOException, InterruptedException { + execute("reg delete \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" /v AutoConfigURL /f"); + execute("reg add \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" /v AutoConfigURL /t REG_SZ /d \"http://ideco.mcs.br/wpad.dat\""); + } + + public static void execute(String command) throws IOException, InterruptedException { + Process proc = Runtime.getRuntime().exec(command); + proc.waitFor(); + proc.destroy(); + } +} \ No newline at end of file diff --git a/src/org/proxy/loadConfig.java b/src/org/proxy/loadConfig.java new file mode 100644 index 0000000..c108ba4 --- /dev/null +++ b/src/org/proxy/loadConfig.java @@ -0,0 +1,56 @@ +package org.proxy; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import static org.dhaverdLogs.DhaverdLogs.setLog; + +public class loadConfig { + + public static JSONObject getJsonObjConfig() throws IOException, ParseException { + File config = getConfigFile(); + FileReader reader = new FileReader(config); + Object obj = new JSONParser().parse(reader); + JSONObject jo = (JSONObject) obj; + reader.close(); + return jo; + } + + public static File getConfigFile() throws IOException { + File config = new File("config/config.json"); + if (config.exists()) { + return config; + } else { + createConfig(config); + config = new File("config/config.json"); + return config; + } + } + + public static void createConfig(File config) throws IOException { + String[] props = { + "{", + "\t\"port\" : \"7458\"", + "}" + }; + FileWriter writer = new FileWriter(config, true); + for (int i = 0; i < props.length; i++){ + writer.write(props[i]+"\n"); + } + writer.close(); + } + + public static int getSocketPort() throws IOException, ParseException { + JSONObject jo = getJsonObjConfig(); + String result = String.valueOf(jo.get("port")); + setLog("Config Loading", "Port loaded: " + result, true, false); + return Integer.parseInt(result); + } + +}