2023-03-16 05:28:14 +01:00
|
|
|
package org.proxy;
|
|
|
|
|
|
|
|
|
|
import org.json.simple.parser.ParseException;
|
|
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionListener;
|
2023-04-25 09:00:22 +02:00
|
|
|
import java.io.BufferedReader;
|
2023-03-16 05:28:14 +01:00
|
|
|
import java.io.IOException;
|
2023-04-25 09:00:22 +02:00
|
|
|
import java.io.InputStreamReader;
|
2023-03-16 05:28:14 +01:00
|
|
|
import java.net.*;
|
2023-03-29 11:12:35 +02:00
|
|
|
import java.util.List;
|
2023-03-16 05:28:14 +01:00
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
|
|
import static javax.swing.JOptionPane.showMessageDialog;
|
|
|
|
|
import static org.dhaverdLogs.DhaverdLogs.*;
|
2023-04-21 09:17:21 +02:00
|
|
|
import static org.proxy.loadConfig.*;
|
2023-03-16 05:28:14 +01:00
|
|
|
|
|
|
|
|
public class Main {
|
|
|
|
|
|
|
|
|
|
public static boolean isLinux = System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux");
|
|
|
|
|
public static ServerSocket serverSocket;
|
2023-04-21 09:17:21 +02:00
|
|
|
public static int repeatTime;
|
|
|
|
|
|
|
|
|
|
public static TrayIcon trayIcon = null;
|
2023-03-16 05:28:14 +01:00
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
try {
|
|
|
|
|
serverSocket = new ServerSocket();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 09:17:21 +02:00
|
|
|
public static Thread currentThread = null;
|
2023-04-25 09:00:22 +02:00
|
|
|
public static String currentProxyPub = " proxy is off";
|
2023-03-16 05:28:14 +01:00
|
|
|
|
2023-04-25 09:00:22 +02:00
|
|
|
public static void main(String[] args) throws IOException, ParseException, InterruptedException {
|
2023-03-16 05:28:14 +01:00
|
|
|
setLogDir("logs");
|
|
|
|
|
setLogName("startup");
|
2023-04-21 10:38:52 +02:00
|
|
|
logCleaner(getDeleteTime());
|
2023-03-16 05:28:14 +01:00
|
|
|
bindSocket();
|
2023-04-21 09:17:21 +02:00
|
|
|
repeatTime = getRepeatTime();
|
2023-04-25 09:00:22 +02:00
|
|
|
currentProxyPub = getCurrentProxy();
|
|
|
|
|
setLog("Proxy Checker", "Current proxy: " + currentProxyPub, true, false);
|
2023-03-16 05:28:14 +01:00
|
|
|
systemTray();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 09:00:22 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 05:28:14 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 11:12:35 +02:00
|
|
|
public static void systemTray() throws IOException, ParseException {
|
2023-03-16 05:28:14 +01:00
|
|
|
String context = "SystemTray";
|
|
|
|
|
if(! SystemTray.isSupported() ) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
2023-03-29 11:12:35 +02:00
|
|
|
List<String> proxyList = getProxyList();
|
|
|
|
|
PopupMenu trayMenu = new PopupMenu();
|
|
|
|
|
for (String prox : proxyList){
|
|
|
|
|
MenuItem proxy = new MenuItem(prox);
|
|
|
|
|
proxy.setFont(trayFont);
|
|
|
|
|
proxy.addActionListener(proxyListener(prox));
|
|
|
|
|
trayMenu.add(proxy);
|
|
|
|
|
}
|
2023-04-25 09:00:22 +02:00
|
|
|
MenuItem stopButton = new MenuItem("Stop");
|
|
|
|
|
stopButton.setFont(trayFont);
|
|
|
|
|
stopButton.addActionListener(stopListener());
|
|
|
|
|
trayMenu.add(stopButton);
|
2023-03-16 05:28:14 +01:00
|
|
|
MenuItem trayExit = new MenuItem("Exit");
|
|
|
|
|
trayExit.setFont(trayFont);
|
|
|
|
|
trayExit.addActionListener(trayExitListener());
|
|
|
|
|
trayMenu.add(trayExit);
|
|
|
|
|
Image icon;
|
2023-04-25 09:00:22 +02:00
|
|
|
icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon off.png");
|
|
|
|
|
trayIcon = new TrayIcon(icon, "Current proxy: " + currentProxyPub, trayMenu);
|
|
|
|
|
trayIcon.setImageAutoSize(!isLinux);
|
2023-03-16 05:28:14 +01:00
|
|
|
SystemTray tray = SystemTray.getSystemTray();
|
|
|
|
|
try {
|
|
|
|
|
tray.add(trayIcon);
|
|
|
|
|
} catch (AWTException e) {
|
|
|
|
|
exeptionActions(context, e);
|
|
|
|
|
}
|
|
|
|
|
setLog(context,"System tray launched", true, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ActionListener trayExitListener() {
|
2023-04-25 09:00:22 +02:00
|
|
|
return e -> {
|
|
|
|
|
try {
|
|
|
|
|
setLog("Program","Closing server socket...", true, false);
|
|
|
|
|
setLog( "Program","Exiting program", true, false);
|
|
|
|
|
serverSocket.close();
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
exeptionActions("TrayExitListener", ex);
|
2023-03-16 05:28:14 +01:00
|
|
|
}
|
2023-04-25 09:00:22 +02:00
|
|
|
closeThread();
|
|
|
|
|
System.exit(0);
|
2023-03-16 05:28:14 +01:00
|
|
|
};
|
|
|
|
|
}
|
2023-04-21 09:17:21 +02:00
|
|
|
public static ActionListener stopListener() {
|
2023-04-25 09:00:22 +02:00
|
|
|
return e -> {
|
|
|
|
|
closeThread();
|
|
|
|
|
try {
|
|
|
|
|
setLog("Stop Listener", "Proxy setter now is off", true, false);
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
exeptionActions("Stop Listener", ex);
|
2023-03-16 05:28:14 +01:00
|
|
|
}
|
2023-04-25 09:00:22 +02:00
|
|
|
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();
|
2023-03-16 05:28:14 +01:00
|
|
|
};
|
|
|
|
|
}
|
2023-04-21 09:17:21 +02:00
|
|
|
public static void closeThread(){
|
|
|
|
|
if (currentThread != null){
|
|
|
|
|
currentThread.stop();
|
|
|
|
|
}
|
|
|
|
|
currentThread = null;
|
2023-03-29 11:12:35 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-21 09:17:21 +02:00
|
|
|
public static ActionListener proxyListener(String proxyStr) {
|
2023-04-25 09:00:22 +02:00
|
|
|
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);
|
2023-04-21 09:17:21 +02:00
|
|
|
}
|
2023-04-25 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
currentThread.start();
|
2023-03-16 05:28:14 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 11:12:35 +02:00
|
|
|
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 + "\"");
|
2023-04-25 09:00:22 +02:00
|
|
|
setLog("Set Proxy", "Proxy is set on: " + proxyStr, true, false);
|
2023-03-16 05:28:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void execute(String command) throws IOException, InterruptedException {
|
|
|
|
|
Process proc = Runtime.getRuntime().exec(command);
|
|
|
|
|
proc.waitFor();
|
|
|
|
|
proc.destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|