197 lines
7.1 KiB
Java
197 lines
7.1 KiB
Java
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.IOException;
|
|
import java.net.*;
|
|
import java.util.List;
|
|
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;
|
|
|
|
public class Main {
|
|
|
|
public static boolean isLinux = System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux");
|
|
public static ServerSocket serverSocket;
|
|
|
|
static {
|
|
try {
|
|
serverSocket = new ServerSocket();
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public Main() throws IOException {
|
|
}
|
|
|
|
public static void main(String[] args) throws IOException, ParseException {
|
|
setLogDir("logs");
|
|
setLogName("startup");
|
|
bindSocket();
|
|
systemTray();
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
public static void systemTray() throws IOException, ParseException {
|
|
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);
|
|
}
|
|
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);
|
|
}
|
|
/*
|
|
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 trayExit = new MenuItem("Exit");
|
|
trayExit.setFont(trayFont);
|
|
trayExit.addActionListener(trayExitListener());
|
|
/*
|
|
trayMenu.add(proxyOn);
|
|
trayMenu.add(proxyOff);
|
|
*/
|
|
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 trayIcon = new TrayIcon(icon, "Proxy", trayMenu);
|
|
if (isLinux){
|
|
trayIcon.setImageAutoSize(false);
|
|
} else {
|
|
trayIcon.setImageAutoSize(true);
|
|
}
|
|
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() {
|
|
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);
|
|
}
|
|
System.exit(0);
|
|
}
|
|
};
|
|
return listener;
|
|
}
|
|
|
|
public static ActionListener proxyOnListener() {
|
|
ActionListener listener = new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
try {
|
|
setProxyOn();
|
|
} catch (IOException | InterruptedException ex) {
|
|
exeptionActions("ProxyOn", ex);
|
|
}
|
|
}
|
|
};
|
|
return listener;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
};
|
|
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 + "\"");
|
|
}
|
|
|
|
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\"");
|
|
}
|
|
|
|
public static void execute(String command) throws IOException, InterruptedException {
|
|
Process proc = Runtime.getRuntime().exec(command);
|
|
proc.waitFor();
|
|
proc.destroy();
|
|
}
|
|
} |