Рефакторинг кода; при наведении на иконку трея теперь выводится текущий прокси; теперь при остановке применения прокси программа в установленный период проверяет текущий прокси и выводит его при наведении на иконку трея
This commit is contained in:
parent
53fc98502b
commit
2ccaf250da
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 7.0 KiB |
Binary file not shown.
After Width: | Height: | Size: 7.0 KiB |
Binary file not shown.
After Width: | Height: | Size: 6.9 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -2,11 +2,11 @@ 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.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.*;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
@ -32,16 +32,37 @@ public class Main {
|
|||
}
|
||||
|
||||
public static Thread currentThread = null;
|
||||
public static String currentProxyPub = " proxy is off";
|
||||
|
||||
public static void main(String[] args) throws IOException, ParseException {
|
||||
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();
|
||||
}
|
||||
|
||||
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");
|
||||
|
@ -71,35 +92,25 @@ public class Main {
|
|||
trayFont = new Font("Arial", Font.PLAIN, 12);
|
||||
}
|
||||
List<String> proxyList = getProxyList();
|
||||
int counter = 1;
|
||||
PopupMenu trayMenu = new PopupMenu();
|
||||
for (String prox : proxyList){
|
||||
MenuItem proxy = new MenuItem(prox);
|
||||
counter++;
|
||||
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 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;
|
||||
if (isLinux){
|
||||
icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon.png");
|
||||
} else {
|
||||
icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon.png");
|
||||
}
|
||||
trayIcon = new TrayIcon(icon, "Current proxy: proxy is off", trayMenu);
|
||||
if (isLinux){
|
||||
trayIcon.setImageAutoSize(false);
|
||||
} else {
|
||||
trayIcon.setImageAutoSize(true);
|
||||
}
|
||||
icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon off.png");
|
||||
trayIcon = new TrayIcon(icon, "Current proxy: " + currentProxyPub, trayMenu);
|
||||
trayIcon.setImageAutoSize(!isLinux);
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
try {
|
||||
tray.add(trayIcon);
|
||||
|
@ -110,31 +121,41 @@ public class Main {
|
|||
}
|
||||
|
||||
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);
|
||||
}
|
||||
closeThread();
|
||||
System.exit(0);
|
||||
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);
|
||||
};
|
||||
return listener;
|
||||
}
|
||||
public static ActionListener stopListener() {
|
||||
ActionListener listener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
closeThread();
|
||||
trayIcon.setToolTip("Current proxy: proxy is off");
|
||||
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 {
|
||||
currentProxyPub = getCurrentProxy();
|
||||
trayIcon.setToolTip("Current proxy: " + currentProxyPub);
|
||||
Thread.sleep(repeatTime);
|
||||
} catch (InterruptedException | IOException ex) {
|
||||
exeptionActions("Stop Listener", ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
currentThread.start();
|
||||
};
|
||||
return listener;
|
||||
}
|
||||
public static void closeThread(){
|
||||
if (currentThread != null){
|
||||
|
@ -144,36 +165,31 @@ public class Main {
|
|||
}
|
||||
|
||||
public static ActionListener proxyListener(String proxyStr) {
|
||||
ActionListener listener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (currentThread != null){
|
||||
currentThread.stop();
|
||||
}
|
||||
currentThread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while(true){
|
||||
try {
|
||||
setProxy(proxyStr);
|
||||
Thread.sleep(repeatTime);
|
||||
} catch (InterruptedException | IOException ex) {
|
||||
exeptionActions(proxyStr, ex);
|
||||
}
|
||||
}
|
||||
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);
|
||||
currentProxyPub = getCurrentProxy();
|
||||
trayIcon.setToolTip("Current proxy: " + currentProxyPub);
|
||||
Thread.sleep(repeatTime);
|
||||
} catch (InterruptedException | IOException ex) {
|
||||
exeptionActions( "Set proxy: " + proxyStr, ex);
|
||||
}
|
||||
});
|
||||
currentThread.start();
|
||||
}
|
||||
}
|
||||
});
|
||||
currentThread.start();
|
||||
};
|
||||
return listener;
|
||||
}
|
||||
|
||||
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("setProxy", "Proxy is set on: " + proxyStr, true, false);
|
||||
trayIcon.setToolTip("Current proxy: " + proxyStr);
|
||||
setLog("Set Proxy", "Proxy is set on: " + proxyStr, true, false);
|
||||
}
|
||||
|
||||
public static void execute(String command) throws IOException, InterruptedException {
|
||||
|
|
Loading…
Reference in New Issue