package org.proxy; import org.json.simple.parser.ParseException; import javax.swing.*; import java.awt.*; import java.awt.event.*; 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.*; import static org.proxy.loadConfig.*; public class Main { public static boolean isLinux = System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux"); public static ServerSocket serverSocket; public static int repeatTime; 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(); } catch (IOException e) { throw new RuntimeException(e); } } public static Thread currentThread = null; public static String currentProxyPub = " proxy is off"; public static void main(String[] args) throws IOException, ParseException, InterruptedException { setLogDir("logs"); setLogName("startup"); logCleaner(getDeleteTime()); bindSocket(); repeatTime = getRepeatTime(); currentProxyPub = getCurrentProxy(); setLog("Proxy Checker", "Current proxy: " + currentProxyPub, true, false); systemTray(); if (args.length != 0){ setProxyOnLaunch(args[0]); updateIcon(); } } public static void setProxyOnLaunch(String proxyStr) throws IOException, InterruptedException { setLog("Setting proxy on launch", "Proxy will be set on: " + proxyStr, true, false); closeThread(); currentThread = new Thread(() -> { while(true){ try { currentProxyPub = getCurrentProxy(); if (!currentProxyPub.equals(proxyStr)) setProxy(proxyStr); Image icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon on blue.png"); trayIcon.setImage(icon); updateIcon(); Thread.sleep(repeatTime); } catch (InterruptedException | IOException ex) { exeptionActions( "Set proxy: " + proxyStr, ex); } } }); currentThread.start(); } public static String getCurrentProxy() throws InterruptedException, IOException { String currentProxy = " proxy is off"; Process proc = Runtime.getRuntime().exec("reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\""); proc.waitFor(); BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); String s; String proxyLine = ""; while ((s = stdInput.readLine()) != null) { if (s.contains("AutoConfigURL")){ proxyLine = s.replace(" ", ";"); } } String[] abc = proxyLine.split(";"); if (abc.length == 4 && abc[3] != null) currentProxy = abc[3]; proc.destroy(); return currentProxy; } 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, ParseException { String context = "SystemTray"; if(! SystemTray.isSupported() ) { setLog(context,"System tray is unsupported!", true, false); return; } tray = SystemTray.getSystemTray(); loadMenu(); setLog(context,"System tray launched", true, false); } public static ActionListener trayExitListener() { return e -> { try { setLog("Program","Closing server socket...", true, false); setLog( "Program","Exiting program", true, false); serverSocket.close(); } catch (IOException ex) { exeptionActions("TrayExitListener", ex); } closeThread(); 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 + "\","); } } FileWriter writer = new FileWriter(config, false); for (String prop : newProps) { writer.write(prop + "\n"); } writer.close(); } public static void reloadMenu() throws IOException, ParseException { tray.remove(trayIcon); loadMenu(); } public static void loadMenu() throws IOException, ParseException { String context = "Load tray menu"; List proxyList = getProxyList(); currentProxyList = proxyList; PopupMenu trayMenu = new PopupMenu(); MenuItem addButton = new MenuItem("Add proxy..."); addButton.setFont(trayFont); addButton.addActionListener(addListener()); trayMenu.add(addButton); Menu proxyMenu = new Menu("Proxy"); trayMenu.add(proxyMenu); for (String prox : proxyList){ MenuItem proxy = new MenuItem(prox); proxy.setFont(trayFont); proxy.addActionListener(proxyListener(prox)); proxyMenu.add(proxy); } MenuItem deleteButton = new MenuItem("Delete proxy..."); deleteButton.setFont(trayFont); deleteButton.addActionListener(deleteListener()); trayMenu.add(deleteButton); 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.addMouseMotionListener(trayMouseListener()); trayIcon.setImageAutoSize(!isLinux); try { tray.add(trayIcon); } catch (AWTException e) { exeptionActions(context, e); } } public static MouseMotionListener trayMouseListener(){ return new MouseMotionListener() { @Override public void mouseDragged(MouseEvent e) { } @Override public void mouseMoved(MouseEvent e) { try { updateIcon(); } catch (InterruptedException | IOException ex) { exeptionActions("Mouse Entered Event", ex); } } }; } 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 deleteListener(){ return e -> { String context = "Delete Listener"; //frame JFrame frame = new JFrame("Add proxy"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setSize(300,200); frame.setLocationRelativeTo(null); //main panel JPanel mainPanel = new JPanel(); GridBagLayout layout = new GridBagLayout(); mainPanel.setLayout(layout); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; gbc.insets = new Insets(5, 5, 5, 5); try { List proxyList = getProxyList(); List checkBoxList = new ArrayList<>(); for (String proxy : proxyList){ JCheckBox checkBox = new JCheckBox(proxy); checkBoxList.add(checkBox); mainPanel.add(checkBox, gbc); gbc.gridy++; } gbc.anchor = GridBagConstraints.CENTER; gbc.insets = new Insets(5, 10, 5, 10); JButton deleteButton = new JButton("Delete"); deleteButton.addActionListener(al ->{ List proxyToDelete = new ArrayList<>(); for (JCheckBox checkBox : checkBoxList){ if (checkBox.isSelected()){ proxyToDelete.add(checkBox.getText()); } } try { deleteProxy(proxyToDelete); reloadMenu(); } catch (IOException | ParseException ex) { exeptionActions(context, ex); } frame.dispose(); }); gbc.weightx = 0.5; mainPanel.add(deleteButton, gbc); JButton closeButton = new JButton("Close"); gbc.gridx = 1; closeButton.addActionListener(al -> frame.dispose()); mainPanel.add(closeButton, gbc); frame.getContentPane().add(mainPanel); frame.pack(); frame.setResizable(false); frame.setVisible(true); } catch (IOException | ParseException ex) { exeptionActions(context, ex); } }; } public static void deleteProxy(List proxyToDelete) 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){ boolean isToDelete = false; for (String deleteProxy : proxyToDelete){ if (prop.contains(deleteProxy)){ isToDelete = true; setLog("Delete Proxy", "Proxy is deleted: " + deleteProxy, true, false); break; } } if (!isToDelete){ newProps.add(prop); } } FileWriter writer = new FileWriter(config, false); for (String prop : newProps) { writer.write(prop + "\n"); } writer.close(); } public static ActionListener stopListener() { return e -> { closeThread(); try { setLog("Stop Listener", "Proxy setter now is off", true, false); } catch (IOException ex) { exeptionActions("Stop Listener", ex); } Image icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon off.png"); trayIcon.setImage(icon); currentThread = new Thread(() -> { while(true){ try { updateIcon(); Thread.sleep(repeatTime); } catch (InterruptedException | IOException ex) { exeptionActions("Stop Listener", ex); } } }); currentThread.start(); }; } public static void updateIcon() throws IOException, InterruptedException { currentProxyPub = getCurrentProxy(); trayIcon.setToolTip("Current proxy: " + currentProxyPub); } public static void closeThread(){ if (currentThread != null){ currentThread.stop(); } currentThread = null; } public static ActionListener proxyListener(String proxyStr) { return e -> { closeThread(); currentThread = new Thread(() -> { while(true){ try { currentProxyPub = getCurrentProxy(); if (!currentProxyPub.equals(proxyStr)) setProxy(proxyStr); Image icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon on blue.png"); trayIcon.setImage(icon); updateIcon(); Thread.sleep(repeatTime); } catch (InterruptedException | IOException ex) { exeptionActions( "Set proxy: " + proxyStr, ex); } } }); currentThread.start(); }; } public static void setProxy(String proxyStr) 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 \"" + proxyStr + "\""); setLog("Set Proxy", "Proxy is set on: " + proxyStr, true, false); } public static void execute(String command) throws IOException, InterruptedException { Process proc = Runtime.getRuntime().exec(command); proc.waitFor(); proc.destroy(); } }