diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml index 18a9980..b8fb171 100644 --- a/.idea/dbnavigator.xml +++ b/.idea/dbnavigator.xml @@ -2,7 +2,7 @@ - + @@ -27,6 +27,9 @@ + + + diff --git a/out/artifacts/proxy/config/config.json b/out/artifacts/proxy/config/config.json index 991e120..b51fb44 100644 --- a/out/artifacts/proxy/config/config.json +++ b/out/artifacts/proxy/config/config.json @@ -1,9 +1,11 @@ { "port" : "7458", "proxyList" : [ + "https://notaproxy.ru", + "http://proxy.com", "http://ideco.mcs.br/wpad.dat", "http://proxy.mcs.br:8080/array.dll?Get.Routing.Script" ], "setRepeatTimeMs" : "60000", "deleteBySevenDays" : "true" -} \ No newline at end of file +} diff --git a/out/artifacts/proxy/proxy.jar b/out/artifacts/proxy/proxy.jar index 5fa5d19..ad9f002 100644 Binary files a/out/artifacts/proxy/proxy.jar and b/out/artifacts/proxy/proxy.jar differ diff --git a/out/production/proxy/org/proxy/Main.class b/out/production/proxy/org/proxy/Main.class index 658a574..c98e9bc 100644 Binary files a/out/production/proxy/org/proxy/Main.class and b/out/production/proxy/org/proxy/Main.class differ diff --git a/src/org/proxy/Main.java b/src/org/proxy/Main.java index d3e62a0..b3a315f 100644 --- a/src/org/proxy/Main.java +++ b/src/org/proxy/Main.java @@ -2,14 +2,15 @@ package org.proxy; import org.json.simple.parser.ParseException; +import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.net.*; +import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.Scanner; import static javax.swing.JOptionPane.showMessageDialog; import static org.dhaverdLogs.DhaverdLogs.*; @@ -23,6 +24,18 @@ public class Main { public static TrayIcon trayIcon = null; + public static List currentProxyList = null; + + public static SystemTray tray; + public static Font trayFont; + static { + if (isLinux){ + trayFont = new Font("Tempora LGC Uni", Font.PLAIN, 16); + } else { + trayFont = new Font("Arial", Font.PLAIN, 12); + } + } + static { try { serverSocket = new ServerSocket(); @@ -85,14 +98,14 @@ public class Main { 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); - } + List proxyList = getProxyList(); + currentProxyList = proxyList; PopupMenu trayMenu = new PopupMenu(); + MenuItem addButton = new MenuItem("Add proxy..."); + addButton.setFont(trayFont); + addButton.addActionListener(addListener()); + trayMenu.add(addButton); for (String prox : proxyList){ MenuItem proxy = new MenuItem(prox); proxy.setFont(trayFont); @@ -111,7 +124,7 @@ public class Main { icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon off.png"); trayIcon = new TrayIcon(icon, "Current proxy: " + currentProxyPub, trayMenu); trayIcon.setImageAutoSize(!isLinux); - SystemTray tray = SystemTray.getSystemTray(); + tray = SystemTray.getSystemTray(); try { tray.add(trayIcon); } catch (AWTException e) { @@ -133,6 +146,143 @@ public class Main { System.exit(0); }; } + + public static void addNewProxy(String proxy) throws IOException { + File config = getConfigFile(); + FileReader reader = new FileReader(config); + Scanner sc = new Scanner(reader); + List propsList = new ArrayList<>(); + while (sc.hasNext()){ + String line = sc.nextLine(); + propsList.add(line); + } + sc.close(); + reader.close(); + List newProps = new ArrayList<>(); + for (String prop : propsList){ + newProps.add(prop); + if (prop.contains("\"proxyList\" : [")){ + newProps.add("\t\t\"" + proxy + "\","); + } + } + /* + setLog("Add new proxy function", "New proxy list:", true, false); + for (String prop : newProps) { + setLog("Add new proxy function", prop, false, true); + } + */ + FileWriter writer = new FileWriter(config, false); + for (String prop : newProps) { + writer.write(prop + "\n"); + } + writer.close(); + } + + public static void reloadMenu() throws IOException, ParseException { + String context = "Reload tray menu"; + tray.remove(trayIcon); + List proxyList = getProxyList(); + currentProxyList = proxyList; + PopupMenu trayMenu = new PopupMenu(); + MenuItem addButton = new MenuItem("Add proxy..."); + addButton.setFont(trayFont); + addButton.addActionListener(addListener()); + trayMenu.add(addButton); + for (String prox : proxyList){ + MenuItem proxy = new MenuItem(prox); + proxy.setFont(trayFont); + proxy.addActionListener(proxyListener(prox)); + trayMenu.add(proxy); + } + MenuItem stopButton = new MenuItem("Stop"); + stopButton.setFont(trayFont); + stopButton.addActionListener(stopListener()); + trayMenu.add(stopButton); + MenuItem trayExit = new MenuItem("Exit"); + trayExit.setFont(trayFont); + trayExit.addActionListener(trayExitListener()); + trayMenu.add(trayExit); + Image icon; + icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon off.png"); + trayIcon = new TrayIcon(icon, "Current proxy: " + currentProxyPub, trayMenu); + trayIcon.setImageAutoSize(!isLinux); + try { + tray.add(trayIcon); + } catch (AWTException e) { + exeptionActions(context, e); + } + } + + public static ActionListener addListener(){ + return e -> { + // frame + JFrame frame = new JFrame("Add proxy"); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + frame.setSize(400,200); + frame.setLocationRelativeTo(null); + + //main panel + JPanel mainPanel = new JPanel(); + GridBagLayout layout = new GridBagLayout(); + mainPanel.setLayout(layout); + GridBagConstraints gbc = new GridBagConstraints(); + + //label + JLabel label = new JLabel("Proxy address:"); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.weightx = 0.3; + gbc.gridx = 0; + gbc.gridy = 0; + gbc.insets = new Insets(10, 10, 10, 2); + mainPanel.add(label, gbc); + + //text field + JTextField proxyField = new JTextField(); + proxyField.setSize(300, 50); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.weightx = 0.7; + gbc.gridx = 1; + gbc.gridy = 0; + gbc.gridwidth = 2; + gbc.insets = new Insets(10, 2, 10, 10); + mainPanel.add(proxyField, gbc); + + // add button + JButton addButton = new JButton("Add"); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.gridx = 1; + gbc.gridy = 1; + gbc.gridwidth = 1; + gbc.insets = new Insets(0, 10, 10, 10); + addButton.addActionListener(al -> { + String newProxy = proxyField.getText(); + try { + setLog("Add proxy", "New proxy: " + newProxy, true, false); + addNewProxy(newProxy); + reloadMenu(); + frame.dispose(); + } catch (IOException | ParseException ex) { + exeptionActions("Add proxy", ex); + } + }); + mainPanel.add(addButton, gbc); + + //close button + JButton closeButton = new JButton("Close"); + closeButton.addActionListener(al -> { + frame.dispose(); + }); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.gridx = 2; + gbc.gridy = 1; + gbc.gridwidth = 1; + gbc.insets = new Insets(0, 10, 10, 10); + mainPanel.add(closeButton, gbc); + frame.getContentPane().add(mainPanel); + frame.setResizable(false); + frame.setVisible(true); + }; + } public static ActionListener stopListener() { return e -> { closeThread();