Рефакторинг кода, прокси теперь применяется в установленный промежуток времени, при наведении на иконку в трее выводится текущий прокси (который устанавливает программа)

This commit is contained in:
BelPE 2023-04-21 15:17:21 +08:00
parent 384bc34d3a
commit e7cafe0705
20 changed files with 138 additions and 54 deletions

View File

@ -3,5 +3,6 @@
"proxyList" : [
"http://ideco.mcs.br/wpad.dat",
"http://proxy.mcs.br:8080/array.dll?Get.Routing.Script"
]
],
"setRepeatTimeMs" : "60000"
}

View File

@ -0,0 +1,9 @@
[2023-04-13 15:25:44] [Config Loading] Port loaded: 7458
[2023-04-13 15:25:44] [BindSocket] Socket binded
[2023-04-13 15:25:44] [BindSocket] Local Socket Address: localhost/127.0.0.1:7458
[2023-04-13 15:25:44] [Config Loading] Proxy list loaded. Items count: 2
[Config Loading] Proxy list item: http://ideco.mcs.br/wpad.dat
[Config Loading] Proxy list item: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:25:44] [SystemTray] System tray launched
[2023-04-13 15:26:10] [Program] Closing server socket...
[2023-04-13 15:26:10] [Program] Exiting program

View File

@ -0,0 +1,7 @@
[2023-04-13 15:26:50] [Config Loading] Port loaded: 7458
[2023-04-13 15:26:50] [BindSocket] Socket binded
[2023-04-13 15:26:50] [BindSocket] Local Socket Address: localhost/127.0.0.1:7458
[2023-04-13 15:26:50] [Config Loading] Proxy list loaded. Items count: 2
[Config Loading] Proxy list item: http://ideco.mcs.br/wpad.dat
[Config Loading] Proxy list item: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:26:50] [SystemTray] System tray launched

View File

@ -0,0 +1,7 @@
[2023-04-13 15:27:02] [Config Loading] Port loaded: 7458
[2023-04-13 15:27:02] [BindSocket] Socket binded
[2023-04-13 15:27:02] [BindSocket] Local Socket Address: localhost/127.0.0.1:7458
[2023-04-13 15:27:02] [Config Loading] Proxy list loaded. Items count: 2
[Config Loading] Proxy list item: http://ideco.mcs.br/wpad.dat
[Config Loading] Proxy list item: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:27:03] [SystemTray] System tray launched

View File

@ -0,0 +1,9 @@
[2023-04-13 15:28:22] [Config Loading] Port loaded: 7458
[2023-04-13 15:28:22] [BindSocket] Socket binded
[2023-04-13 15:28:22] [BindSocket] Local Socket Address: localhost/127.0.0.1:7458
[2023-04-13 15:28:22] [Config Loading] Proxy list loaded. Items count: 2
[Config Loading] Proxy list item: http://ideco.mcs.br/wpad.dat
[Config Loading] Proxy list item: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:28:22] [SystemTray] System tray launched
[2023-04-13 15:31:01] [Program] Closing server socket...
[2023-04-13 15:31:01] [Program] Exiting program

View File

@ -0,0 +1,7 @@
[2023-04-13 15:36:25] [Config Loading] Port loaded: 7458
[2023-04-13 15:36:25] [BindSocket] Socket binded
[2023-04-13 15:36:25] [BindSocket] Local Socket Address: localhost/127.0.0.1:7458
[2023-04-13 15:36:25] [Config Loading] Proxy list loaded. Items count: 2
[Config Loading] Proxy list item: http://ideco.mcs.br/wpad.dat
[Config Loading] Proxy list item: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:36:25] [SystemTray] System tray launched

View File

@ -0,0 +1,18 @@
[2023-04-13 15:38:36] [Config Loading] Port loaded: 7458
[2023-04-13 15:38:36] [BindSocket] Socket binded
[2023-04-13 15:38:36] [BindSocket] Local Socket Address: localhost/127.0.0.1:7458
[2023-04-13 15:38:36] [Config Loading] Repeat time loaded: 10000
[2023-04-13 15:38:36] [Config Loading] Proxy list loaded. Items count: 2
[Config Loading] Proxy list item: http://ideco.mcs.br/wpad.dat
[Config Loading] Proxy list item: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:38:36] [SystemTray] System tray launched
[2023-04-13 15:38:50] [setProxy] Proxy is set on: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:39:01] [setProxy] Proxy is set on: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:39:11] [setProxy] Proxy is set on: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:39:21] [setProxy] Proxy is set on: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:39:47] [setProxy] Proxy is set on: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:39:57] [setProxy] Proxy is set on: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:40:07] [setProxy] Proxy is set on: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:40:18] [setProxy] Proxy is set on: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:41:04] [Program] Closing server socket...
[2023-04-13 15:41:04] [Program] Exiting program

View File

@ -0,0 +1,10 @@
[2023-04-13 15:41:12] [Config Loading] Port loaded: 7458
[2023-04-13 15:41:12] [BindSocket] Socket binded
[2023-04-13 15:41:12] [BindSocket] Local Socket Address: localhost/127.0.0.1:7458
[2023-04-13 15:41:12] [Config Loading] Repeat time loaded: 60000
[2023-04-13 15:41:12] [Config Loading] Proxy list loaded. Items count: 2
[Config Loading] Proxy list item: http://ideco.mcs.br/wpad.dat
[Config Loading] Proxy list item: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:41:12] [SystemTray] System tray launched
[2023-04-13 15:41:21] [Program] Closing server socket...
[2023-04-13 15:41:21] [Program] Exiting program

View File

@ -0,0 +1,12 @@
[2023-04-13 15:49:33] [Config Loading] Port loaded: 7458
[2023-04-13 15:49:33] [BindSocket] Socket binded
[2023-04-13 15:49:33] [BindSocket] Local Socket Address: localhost/127.0.0.1:7458
[2023-04-13 15:49:33] [Config Loading] Repeat time loaded: 60000
[2023-04-13 15:49:33] [Config Loading] Proxy list loaded. Items count: 2
[Config Loading] Proxy list item: http://ideco.mcs.br/wpad.dat
[Config Loading] Proxy list item: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:49:33] [SystemTray] System tray launched
[2023-04-13 15:49:37] [setProxy] Proxy is set on: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:50:37] [setProxy] Proxy is set on: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-13 15:50:42] [Program] Closing server socket...
[2023-04-13 15:50:42] [Program] Exiting program

View File

@ -0,0 +1,11 @@
[2023-04-21 15:02:47] [Config Loading] Port loaded: 7458
[2023-04-21 15:02:47] [BindSocket] Socket binded
[2023-04-21 15:02:47] [BindSocket] Local Socket Address: localhost/127.0.0.1:7458
[2023-04-21 15:02:47] [Config Loading] Repeat time loaded: 60000
[2023-04-21 15:02:47] [Config Loading] Proxy list loaded. Items count: 2
[Config Loading] Proxy list item: http://ideco.mcs.br/wpad.dat
[Config Loading] Proxy list item: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-21 15:02:47] [SystemTray] System tray launched
[2023-04-21 15:02:55] [setProxy] Proxy is set on: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script
[2023-04-21 15:03:01] [setProxy] Proxy is set on: http://ideco.mcs.br/wpad.dat
[2023-04-21 15:03:09] [setProxy] Proxy is set on: http://proxy.mcs.br:8080/array.dll?Get.Routing.Script

Binary file not shown.

Binary file not shown.

View File

@ -13,13 +13,15 @@ import java.util.Locale;
import static javax.swing.JOptionPane.showMessageDialog;
import static org.dhaverdLogs.DhaverdLogs.*;
import static org.proxy.loadConfig.getProxyList;
import static org.proxy.loadConfig.getSocketPort;
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;
static {
try {
@ -29,13 +31,13 @@ public class Main {
}
}
public Main() throws IOException {
}
public static Thread currentThread = null;
public static void main(String[] args) throws IOException, ParseException {
setLogDir("logs");
setLogName("startup");
bindSocket();
repeatTime = getRepeatTime();
systemTray();
}
@ -77,22 +79,13 @@ public class Main {
proxy.addActionListener(proxyListener(prox));
trayMenu.add(proxy);
}
/*
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 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(proxyOn);
trayMenu.add(proxyOff);
*/
trayMenu.add(trayExit);
Image icon;
if (isLinux){
@ -100,7 +93,7 @@ public class Main {
} else {
icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon.png");
}
TrayIcon trayIcon = new TrayIcon(icon, "Proxy", trayMenu);
trayIcon = new TrayIcon(icon, "Current proxy: proxy is off", trayMenu);
if (isLinux){
trayIcon.setImageAutoSize(false);
} else {
@ -126,49 +119,50 @@ public class Main {
} catch (IOException ex) {
exeptionActions("TrayExitListener", ex);
}
closeThread();
System.exit(0);
}
};
return listener;
}
public static ActionListener proxyOnListener() {
public static ActionListener stopListener() {
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
setProxyOn();
} catch (IOException | InterruptedException ex) {
exeptionActions("ProxyOn", ex);
}
closeThread();
trayIcon.setToolTip("Current proxy: proxy is off");
}
};
return listener;
}
public static void closeThread(){
if (currentThread != null){
currentThread.stop();
}
currentThread = null;
}
public static ActionListener proxyListener(String proxyStr) {
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
setProxy(proxyStr);
} 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);
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);
}
}
}
});
currentThread.start();
}
};
return listener;
@ -177,16 +171,8 @@ public class Main {
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 + "\"");
}
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\"");
setLog("setProxy", "Proxy is set on: " + proxyStr, true, false);
trayIcon.setToolTip("Current proxy: " + proxyStr);
}
public static void execute(String command) throws IOException, InterruptedException {

View File

@ -64,4 +64,11 @@ public class loadConfig {
return result;
}
public static int getRepeatTime() throws IOException, ParseException {
JSONObject jo = getJsonObjConfig();
String result = String.valueOf(jo.get("setRepeatTimeMs"));
setLog("Config Loading", "Repeat time loaded: " + result, true, false);
return Integer.parseInt(result);
}
}