This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package tech.nevets.tvpn;
|
||||
|
||||
import com.formdev.flatlaf.FlatDarkLaf;
|
||||
import tech.nevets.jconf.JsonConfig;
|
||||
import tech.nevets.tvpn.cli.CommandBrigadier;
|
||||
import tech.nevets.tvpn.ui.UIManager;
|
||||
import tech.nevets.tvpn.wg.WireGuardJNI;
|
||||
|
||||
@@ -9,11 +11,16 @@ import java.io.File;
|
||||
|
||||
public class Main {
|
||||
private static final Logger LOGGER = new Logger(Main.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
LogOutputStream logOut = new LogOutputStream();
|
||||
Logger.loadLogOutStream(logOut);
|
||||
|
||||
JsonConfig conf = new JsonConfig("");
|
||||
|
||||
if (args.length > 0) {
|
||||
new CommandBrigadier().execute(args);
|
||||
}
|
||||
|
||||
try {
|
||||
javax.swing.UIManager.setLookAndFeel(new FlatDarkLaf());
|
||||
} catch (UnsupportedLookAndFeelException e) {
|
||||
@@ -26,9 +33,8 @@ public class Main {
|
||||
UIManager uim = new UIManager(logOut);
|
||||
uim.startLoginFrame();
|
||||
|
||||
WireGuardJNI wgjni = new WireGuardJNI();
|
||||
//WireGuardJNI wgjni = new WireGuardJNI();
|
||||
//System.out.println(wgjni.createTunnel("test"));
|
||||
System.out.println(wgjni.deleteTunnel("test"));
|
||||
//System.out.println(wgjni.removeTunnel("test"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package tech.nevets.tvpn;
|
||||
|
||||
import tech.nevets.jconf.JsonConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
@@ -10,14 +12,24 @@ import java.util.Map;
|
||||
|
||||
public class Utils {
|
||||
private static final Logger LOGGER = new Logger(Utils.class);
|
||||
public static final File APP_DIR = new File("./tvpn/");
|
||||
|
||||
public static final String WG_CONF_DIR = "C:\\Program Files\\WireGuard\\Data\\Configurations\\";
|
||||
public static final String API_ENDPOINT_BASE = "http://tvpn.lan/testing/";
|
||||
public static final File APP_DIR = new File("C:\\Program Files\\TVPN\\");
|
||||
public static final JsonConfig CONFIG = new JsonConfig(APP_DIR + "/config.json", getConfStr());
|
||||
public static final String API_ENDPOINT_BASE = CONFIG.getString("apiEndpoint");
|
||||
|
||||
static {
|
||||
if (!APP_DIR.exists()) APP_DIR.mkdirs();
|
||||
}
|
||||
|
||||
private static String getConfStr() {
|
||||
return """
|
||||
{
|
||||
"apiEndpoint":"http://tvpn.lan"
|
||||
}
|
||||
""";
|
||||
}
|
||||
|
||||
public static String get(URI url, Map<String, String> headers) {
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest.Builder reqBuilder = HttpRequest.newBuilder()
|
||||
|
||||
12
src/main/java/tech/nevets/tvpn/cli/Command.java
Normal file
12
src/main/java/tech/nevets/tvpn/cli/Command.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package tech.nevets.tvpn.cli;
|
||||
|
||||
public interface Command {
|
||||
|
||||
void execute(String[] args);
|
||||
|
||||
String getName();
|
||||
|
||||
String getUsage();
|
||||
|
||||
String getDescription();
|
||||
}
|
||||
49
src/main/java/tech/nevets/tvpn/cli/CommandBrigadier.java
Normal file
49
src/main/java/tech/nevets/tvpn/cli/CommandBrigadier.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package tech.nevets.tvpn.cli;
|
||||
|
||||
import tech.nevets.tvpn.Logger;
|
||||
import tech.nevets.tvpn.cli.commands.HelpCmd;
|
||||
import tech.nevets.tvpn.cli.commands.InstallTunnelServiceCmd;
|
||||
import tech.nevets.tvpn.cli.commands.UninstallTunnelServiceCmd;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandBrigadier {
|
||||
private static final Logger LOGGER = new Logger(CommandBrigadier.class);
|
||||
|
||||
private final List<Command> registeredCommands;
|
||||
private final HelpCmd helpCmd;
|
||||
|
||||
public CommandBrigadier() {
|
||||
this.registeredCommands = new ArrayList<>();
|
||||
helpCmd = new HelpCmd(this);
|
||||
|
||||
registerCommand(helpCmd);
|
||||
registerCommand(new InstallTunnelServiceCmd());
|
||||
registerCommand(new UninstallTunnelServiceCmd());
|
||||
}
|
||||
|
||||
public void registerCommand(Command cmd) {
|
||||
for (Command c : registeredCommands) {
|
||||
if (c.getName().equals(cmd.getName())) {
|
||||
LOGGER.warn("Command with name " + cmd.getName() + " already registered, skipping.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
registeredCommands.add(cmd);
|
||||
}
|
||||
|
||||
public void execute(String[] args) {
|
||||
for (Command c : registeredCommands) {
|
||||
if (c.getName().equals(args[0])) {
|
||||
c.execute(args);
|
||||
return;
|
||||
}
|
||||
}
|
||||
helpCmd.execute(new String[0]);
|
||||
}
|
||||
|
||||
public List<Command> getCommands() {
|
||||
return registeredCommands;
|
||||
}
|
||||
}
|
||||
52
src/main/java/tech/nevets/tvpn/cli/commands/HelpCmd.java
Normal file
52
src/main/java/tech/nevets/tvpn/cli/commands/HelpCmd.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package tech.nevets.tvpn.cli.commands;
|
||||
|
||||
import tech.nevets.tvpn.cli.Command;
|
||||
import tech.nevets.tvpn.cli.CommandBrigadier;
|
||||
|
||||
public class HelpCmd implements Command {
|
||||
private final CommandBrigadier cb;
|
||||
|
||||
public HelpCmd(CommandBrigadier cb) {
|
||||
this.cb = cb;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String[] args) {
|
||||
if (args.length > 0) {
|
||||
for (Command c : cb.getCommands()) {
|
||||
if (!(args.length == 1) && args[1].equals(c.getName())) {
|
||||
System.out.println("TVPN - Usage Menu [" + c.getName() + "]");
|
||||
System.out.println(c.getName() + " " + c.getUsage() + ": " + c.getDescription());
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("TVPN - Help Menu\n");
|
||||
for (Command c : cb.getCommands()) {
|
||||
sb.append(" - ").append(c.getName())
|
||||
.append(" ").append(c.getUsage())
|
||||
.append(": ").append(c.getDescription())
|
||||
.append("\n");
|
||||
}
|
||||
|
||||
System.out.println(sb);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "help";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Displays the help menu";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package tech.nevets.tvpn.cli.commands;
|
||||
|
||||
import tech.nevets.tvpn.Utils;
|
||||
import tech.nevets.tvpn.cli.Command;
|
||||
import tech.nevets.tvpn.wg.WireGuardJNI;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class InstallTunnelServiceCmd implements Command {
|
||||
@Override
|
||||
public void execute(String[] args) {
|
||||
|
||||
|
||||
System.out.println("Executing InstallTunnelServiceCmd");
|
||||
String path = Utils.APP_DIR + "/configurations/" + args[1] + ".conf";
|
||||
System.out.println(new WireGuardJNI().installTunnel(args[1], path));
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "/installtunnelservice";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "(/path/to/tunnel/config)";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Creates and starts a WireGuard Tunnel Service";
|
||||
}
|
||||
|
||||
String[] reservedNames = new String[]{
|
||||
"CON", "PRN", "AUX", "NUL",
|
||||
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
|
||||
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
|
||||
};
|
||||
|
||||
|
||||
private boolean isReserved(String name) {
|
||||
if (name.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (String s : reservedNames) {
|
||||
if (name.contains(s)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static final String[] SPECIAL_CHARS = new String[]{"$", "\\", "/", ":", "*", "?", "\"", "'", "<", ">", "|", "\t", "\00"};
|
||||
private boolean hasSpecialChars(String name) {
|
||||
for (String s : SPECIAL_CHARS) {
|
||||
if (name.contains(s)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static final Pattern PATTERN = Pattern.compile("^[a-zA-Z0-9_=+.-]{1,32}$");
|
||||
private boolean tunnelNameIsValid(String name) {
|
||||
if (isReserved(name) || hasSpecialChars(name)) {
|
||||
return false;
|
||||
}
|
||||
return PATTERN.matcher(name).hasMatch();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package tech.nevets.tvpn.cli.commands;
|
||||
|
||||
import tech.nevets.tvpn.cli.Command;
|
||||
import tech.nevets.tvpn.wg.WireGuardJNI;
|
||||
|
||||
public class UninstallTunnelServiceCmd implements Command {
|
||||
@Override
|
||||
public void execute(String[] args) {
|
||||
System.out.println("Executing UnInstallTunnelServiceCmd");
|
||||
System.out.println(new WireGuardJNI().removeTunnel(args[1]));
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "/uninstalltunnelservice";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "(tunnel name)";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Stops and removes WireGuard Tunnel Service";
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ public class UIManager {
|
||||
|
||||
public UIManager(LogOutputStream logOut) {
|
||||
frame = new JFrame();
|
||||
tabPane = new JTabbedPane();
|
||||
tabPane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
|
||||
|
||||
homePanel = new HomePanel();
|
||||
tabPane.addTab("Home", homePanel);
|
||||
|
||||
@@ -10,10 +10,14 @@ public class WireGuardJNI {
|
||||
public native int installAdapter();
|
||||
public native int removeAdapter();
|
||||
|
||||
public native int startTunnel(String tunnelName);
|
||||
public native int stopTunnel(String tunnelName);
|
||||
public native int createTunnel(String tunnelName);
|
||||
public native int deleteTunnel(String tunnelName);
|
||||
/**
|
||||
* Creates and starts the WireGuard service
|
||||
* @param tunnelName Name of the tunnel
|
||||
* @param pathToConfig Full path to the tunnel's configuration file
|
||||
* @return 0 if successful, 1 if failed
|
||||
*/
|
||||
public native int installTunnel(String tunnelName, String pathToConfig);
|
||||
public native int removeTunnel(String tunnelName);
|
||||
|
||||
public native String getConfig(String tunnelName);
|
||||
public native int updateConfig(String tunnelName, String newConfig);
|
||||
|
||||
Reference in New Issue
Block a user