Рефакторинг кода; при наведении на иконку трея теперь выводится текущий прокси; теперь при остановке применения прокси программа в установленный период проверяет текущий прокси и выводит его при наведении на иконку трея
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 org.json.simple.parser.ParseException;
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
@ -32,16 +32,37 @@ public class Main {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Thread currentThread = null;
|
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");
|
setLogDir("logs");
|
||||||
setLogName("startup");
|
setLogName("startup");
|
||||||
logCleaner(getDeleteTime());
|
logCleaner(getDeleteTime());
|
||||||
bindSocket();
|
bindSocket();
|
||||||
repeatTime = getRepeatTime();
|
repeatTime = getRepeatTime();
|
||||||
|
currentProxyPub = getCurrentProxy();
|
||||||
|
setLog("Proxy Checker", "Current proxy: " + currentProxyPub, true, false);
|
||||||
systemTray();
|
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 {
|
public static void bindSocket() throws IOException, ParseException {
|
||||||
String context = "BindSocket";
|
String context = "BindSocket";
|
||||||
InetAddress inetAddress = InetAddress.getByName("localhost");
|
InetAddress inetAddress = InetAddress.getByName("localhost");
|
||||||
|
@ -71,35 +92,25 @@ public class Main {
|
||||||
trayFont = new Font("Arial", Font.PLAIN, 12);
|
trayFont = new Font("Arial", Font.PLAIN, 12);
|
||||||
}
|
}
|
||||||
List<String> proxyList = getProxyList();
|
List<String> proxyList = getProxyList();
|
||||||
int counter = 1;
|
|
||||||
PopupMenu trayMenu = new PopupMenu();
|
PopupMenu trayMenu = new PopupMenu();
|
||||||
for (String prox : proxyList){
|
for (String prox : proxyList){
|
||||||
MenuItem proxy = new MenuItem(prox);
|
MenuItem proxy = new MenuItem(prox);
|
||||||
counter++;
|
|
||||||
proxy.setFont(trayFont);
|
proxy.setFont(trayFont);
|
||||||
proxy.addActionListener(proxyListener(prox));
|
proxy.addActionListener(proxyListener(prox));
|
||||||
trayMenu.add(proxy);
|
trayMenu.add(proxy);
|
||||||
}
|
}
|
||||||
MenuItem stopbutton = new MenuItem("Stop");
|
MenuItem stopButton = new MenuItem("Stop");
|
||||||
stopbutton.setFont(trayFont);
|
stopButton.setFont(trayFont);
|
||||||
stopbutton.addActionListener(stopListener());
|
stopButton.addActionListener(stopListener());
|
||||||
trayMenu.add(stopbutton);
|
trayMenu.add(stopButton);
|
||||||
MenuItem trayExit = new MenuItem("Exit");
|
MenuItem trayExit = new MenuItem("Exit");
|
||||||
trayExit.setFont(trayFont);
|
trayExit.setFont(trayFont);
|
||||||
trayExit.addActionListener(trayExitListener());
|
trayExit.addActionListener(trayExitListener());
|
||||||
trayMenu.add(trayExit);
|
trayMenu.add(trayExit);
|
||||||
Image icon;
|
Image icon;
|
||||||
if (isLinux){
|
icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon off.png");
|
||||||
icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon.png");
|
trayIcon = new TrayIcon(icon, "Current proxy: " + currentProxyPub, trayMenu);
|
||||||
} else {
|
trayIcon.setImageAutoSize(!isLinux);
|
||||||
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);
|
|
||||||
}
|
|
||||||
SystemTray tray = SystemTray.getSystemTray();
|
SystemTray tray = SystemTray.getSystemTray();
|
||||||
try {
|
try {
|
||||||
tray.add(trayIcon);
|
tray.add(trayIcon);
|
||||||
|
@ -110,31 +121,41 @@ public class Main {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ActionListener trayExitListener() {
|
public static ActionListener trayExitListener() {
|
||||||
ActionListener listener = new ActionListener() {
|
return e -> {
|
||||||
@Override
|
try {
|
||||||
public void actionPerformed(ActionEvent e) {
|
setLog("Program","Closing server socket...", true, false);
|
||||||
try {
|
setLog( "Program","Exiting program", true, false);
|
||||||
setLog("Program","Closing server socket...", true, false);
|
serverSocket.close();
|
||||||
setLog( "Program","Exiting program", true, false);
|
} catch (IOException ex) {
|
||||||
serverSocket.close();
|
exeptionActions("TrayExitListener", ex);
|
||||||
} catch (IOException ex) {
|
|
||||||
exeptionActions("TrayExitListener", ex);
|
|
||||||
}
|
|
||||||
closeThread();
|
|
||||||
System.exit(0);
|
|
||||||
}
|
}
|
||||||
|
closeThread();
|
||||||
|
System.exit(0);
|
||||||
};
|
};
|
||||||
return listener;
|
|
||||||
}
|
}
|
||||||
public static ActionListener stopListener() {
|
public static ActionListener stopListener() {
|
||||||
ActionListener listener = new ActionListener() {
|
return e -> {
|
||||||
@Override
|
closeThread();
|
||||||
public void actionPerformed(ActionEvent e) {
|
try {
|
||||||
closeThread();
|
setLog("Stop Listener", "Proxy setter now is off", true, false);
|
||||||
trayIcon.setToolTip("Current proxy: proxy is off");
|
} 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(){
|
public static void closeThread(){
|
||||||
if (currentThread != null){
|
if (currentThread != null){
|
||||||
|
@ -144,36 +165,31 @@ public class Main {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ActionListener proxyListener(String proxyStr) {
|
public static ActionListener proxyListener(String proxyStr) {
|
||||||
ActionListener listener = new ActionListener() {
|
return e -> {
|
||||||
@Override
|
closeThread();
|
||||||
public void actionPerformed(ActionEvent e) {
|
currentThread = new Thread(() -> {
|
||||||
if (currentThread != null){
|
while(true){
|
||||||
currentThread.stop();
|
try {
|
||||||
}
|
currentProxyPub = getCurrentProxy();
|
||||||
currentThread = new Thread(new Runnable() {
|
if (!currentProxyPub.equals(proxyStr)) setProxy(proxyStr);
|
||||||
@Override
|
Image icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon on blue.png");
|
||||||
public void run() {
|
trayIcon.setImage(icon);
|
||||||
while(true){
|
currentProxyPub = getCurrentProxy();
|
||||||
try {
|
trayIcon.setToolTip("Current proxy: " + currentProxyPub);
|
||||||
setProxy(proxyStr);
|
Thread.sleep(repeatTime);
|
||||||
Thread.sleep(repeatTime);
|
} catch (InterruptedException | IOException ex) {
|
||||||
} catch (InterruptedException | IOException ex) {
|
exeptionActions( "Set proxy: " + proxyStr, ex);
|
||||||
exeptionActions(proxyStr, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
currentThread.start();
|
});
|
||||||
}
|
currentThread.start();
|
||||||
};
|
};
|
||||||
return listener;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setProxy(String proxyStr) throws IOException, InterruptedException {
|
public static void setProxy(String proxyStr) throws IOException, InterruptedException {
|
||||||
execute("reg delete \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" /v AutoConfigURL /f");
|
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 + "\"");
|
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);
|
setLog("Set Proxy", "Proxy is set on: " + proxyStr, true, false);
|
||||||
trayIcon.setToolTip("Current proxy: " + proxyStr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void execute(String command) throws IOException, InterruptedException {
|
public static void execute(String command) throws IOException, InterruptedException {
|
||||||
|
|
Loading…
Reference in New Issue