Добавлена функция добавления нового прокси
This commit is contained in:
		
							parent
							
								
									2ccaf250da
								
							
						
					
					
						commit
						744bca6839
					
				|  | @ -2,7 +2,7 @@ | |||
| <project version="4"> | ||||
|   <component name="DBNavigator.Project.DataEditorManager"> | ||||
|     <record-view-column-sorting-type value="BY_INDEX" /> | ||||
|     <value-preview-text-wrapping value="true" /> | ||||
|     <value-preview-text-wrapping value="false" /> | ||||
|     <value-preview-pinned value="false" /> | ||||
|   </component> | ||||
|   <component name="DBNavigator.Project.DataExportManager"> | ||||
|  | @ -27,6 +27,9 @@ | |||
|     <show-object-properties value="true" /> | ||||
|     <loaded-nodes /> | ||||
|   </component> | ||||
|   <component name="DBNavigator.Project.DatabaseEditorStateManager"> | ||||
|     <last-used-providers /> | ||||
|   </component> | ||||
|   <component name="DBNavigator.Project.DatabaseFileManager"> | ||||
|     <open-files /> | ||||
|   </component> | ||||
|  |  | |||
|  | @ -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" | ||||
| } | ||||
| } | ||||
|  |  | |||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							|  | @ -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<String> 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<String> 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<String> propsList = new ArrayList<>(); | ||||
|         while (sc.hasNext()){ | ||||
|             String line = sc.nextLine(); | ||||
|             propsList.add(line); | ||||
|         } | ||||
|         sc.close(); | ||||
|         reader.close(); | ||||
|         List<String> 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<String> 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(); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue