373 lines
12 KiB
Java
373 lines
12 KiB
Java
|
package top.octopusyan.controller;
|
||
|
|
||
|
import com.jfoenix.controls.*;
|
||
|
import javafx.application.Platform;
|
||
|
import javafx.beans.value.ChangeListener;
|
||
|
import javafx.beans.value.ObservableValue;
|
||
|
import javafx.collections.ObservableList;
|
||
|
import javafx.fxml.FXML;
|
||
|
import javafx.fxml.FXMLLoader;
|
||
|
import javafx.fxml.Initializable;
|
||
|
import javafx.scene.control.*;
|
||
|
import javafx.scene.input.MouseEvent;
|
||
|
import javafx.scene.layout.HBox;
|
||
|
import javafx.scene.layout.StackPane;
|
||
|
import org.apache.commons.lang3.StringUtils;
|
||
|
import org.jetbrains.annotations.NotNull;
|
||
|
import top.octopusyan.base.BaseController;
|
||
|
import top.octopusyan.config.ProxyConfig;
|
||
|
import top.octopusyan.config.ProxyConfig.ProxyServer;
|
||
|
import top.octopusyan.config.ProxyConfig.ProxyType;
|
||
|
import top.octopusyan.http.request.ProxySetup;
|
||
|
import top.octopusyan.utils.AlertUtil;
|
||
|
import top.octopusyan.utils.ApplicatonStore;
|
||
|
import top.octopusyan.utils.ProxyUtil;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Arrays;
|
||
|
import java.util.List;
|
||
|
|
||
|
/**
|
||
|
* @author : octopus yan
|
||
|
* @email : octopus_yan@foxmail.com
|
||
|
* @description :
|
||
|
* @create : 2022-4-4 22:32
|
||
|
*/
|
||
|
public class MainController extends BaseController<StackPane> implements Initializable {
|
||
|
|
||
|
public static final String PROXY_LIST_ITEM_CLASS = "proxyListItem";
|
||
|
public static final String PROXY_LIST_ITEM_STOP_CLASS = "proxyListItem-stop";
|
||
|
public static final String PROXY_LIST_ITEM_RUN_CLASS = "proxyListItem-run";
|
||
|
public static final String PROXY_LIST_ITEM_CLOSE_CLASS = "proxyListItem-close";
|
||
|
|
||
|
@FXML
|
||
|
public StackPane root;
|
||
|
|
||
|
@FXML
|
||
|
public JFXButton closeBtn, minimizeBtn;
|
||
|
|
||
|
@FXML
|
||
|
public JFXButton startProxyBtn;
|
||
|
@FXML
|
||
|
public JFXButton addProxyBtn;
|
||
|
@FXML
|
||
|
public JFXComboBox<Label> proxyProtocolCombox;
|
||
|
@FXML
|
||
|
public JFXButton customizeDomainBtn;
|
||
|
@FXML
|
||
|
public JFXRadioButton openProxyRBtn, closeProxyRBtn;
|
||
|
@FXML
|
||
|
public JFXTextField domainTextField, domainSuffixTextField;
|
||
|
@FXML
|
||
|
public JFXTextField proxyNameTextField, localHostTextField, localPortTextField;
|
||
|
@FXML
|
||
|
public JFXComboBox<Label> proxyServerCB;
|
||
|
@FXML
|
||
|
public JFXTabPane tabPane;
|
||
|
@FXML
|
||
|
public JFXListView<Label> proxyListView;
|
||
|
|
||
|
private ToggleGroup openProxyGroup = new ToggleGroup();
|
||
|
private boolean customizeDomain = false;
|
||
|
private List<ProxySetup> proxyList;
|
||
|
private int selectProxy = 0;
|
||
|
private ProxySetup proxySetup;
|
||
|
|
||
|
@Override
|
||
|
public boolean dragWindow() {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public StackPane getRootPanel() {
|
||
|
return root;
|
||
|
}
|
||
|
|
||
|
@NotNull
|
||
|
@Override
|
||
|
public String getRootFxmlPath() {
|
||
|
return "/fxml/main.fxml";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public Button getClooseBtn() {
|
||
|
return closeBtn;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public Button getMinimizeBtn() {
|
||
|
return minimizeBtn;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public JFXButton getFirstBtn() {
|
||
|
return minimizeBtn;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void initData() {
|
||
|
|
||
|
// 隧道类型
|
||
|
Arrays.asList(ProxyType.values()).forEach((type) -> proxyProtocolCombox.getItems().add(new Label(type.name())));
|
||
|
|
||
|
// 服务器
|
||
|
Arrays.asList(ProxyServer.values()).forEach((server) -> proxyServerCB.getItems().add(new Label(server.name())));
|
||
|
|
||
|
// 获取用户隧道列表
|
||
|
ProxyUtil.getList(setupList -> {
|
||
|
proxyList = setupList;
|
||
|
|
||
|
// 主线程执行
|
||
|
Platform.runLater(() -> {
|
||
|
// 设置隧道列表
|
||
|
ApplicatonStore.setProxySetupList(proxyList);
|
||
|
|
||
|
// 初始化隧道设置
|
||
|
if (proxyList != null && proxyList.size() > 0) {
|
||
|
// 显示隧道列表
|
||
|
initProxyList(proxyList);
|
||
|
// 显示隧道设置
|
||
|
proxySetup = proxyList.get(0);
|
||
|
// 默认选中第一个
|
||
|
proxyListView.getSelectionModel().select(0);
|
||
|
} else {
|
||
|
proxySetup = ProxyUtil.initProxy();
|
||
|
proxyList = new ArrayList<>();
|
||
|
proxyList.add(proxySetup);
|
||
|
initProxySetupView(proxySetup);
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void initViewStyle() {
|
||
|
// 启用链接
|
||
|
openProxyRBtn.setToggleGroup(openProxyGroup);
|
||
|
openProxyRBtn.setSelected(true);
|
||
|
closeProxyRBtn.setToggleGroup(openProxyGroup);
|
||
|
openProxyRBtn.setUserData(true);
|
||
|
closeProxyRBtn.setUserData(false);
|
||
|
|
||
|
// 默认展示 常见问题
|
||
|
tabPane.getSelectionModel().select(1);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 设置外网访问域名
|
||
|
*/
|
||
|
private void setDomain(ProxySetup setup) {
|
||
|
|
||
|
if (setup != null) {
|
||
|
String serverName = ProxyServer.values()[setup.getNode()].name();
|
||
|
String setupDomain = setup.getDomain();
|
||
|
String domain = setupDomain;
|
||
|
String serverPath = ProxyConfig.getServerPath(serverName);
|
||
|
// 是否自定义
|
||
|
customizeDomain = setupDomain.contains(serverPath);
|
||
|
if (customizeDomain) domain = setupDomain.substring(0, setupDomain.indexOf("." + serverPath));
|
||
|
domainSuffixTextField.setText("." + serverPath);
|
||
|
domainTextField.setText(domain);
|
||
|
}
|
||
|
|
||
|
if (!customizeDomain) {
|
||
|
domainTextField.styleProperty().set("-fx-border-radius: 5");
|
||
|
domainSuffixTextField.setVisible(false);
|
||
|
domainTextField.setText("");
|
||
|
customizeDomainBtn.setText("系统分配");
|
||
|
} else {
|
||
|
domainTextField.styleProperty().set("-fx-border-radius: 5 0 0 5");
|
||
|
domainSuffixTextField.setVisible(true);
|
||
|
customizeDomainBtn.setText("自定义");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void initViewAction() {
|
||
|
|
||
|
// 是否启用
|
||
|
openProxyGroup.selectedToggleProperty().addListener(
|
||
|
(ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) -> {
|
||
|
Toggle toggle = openProxyGroup.getSelectedToggle();
|
||
|
if (toggle != null) {
|
||
|
closeProxy(!(Boolean) toggle.getUserData());
|
||
|
}
|
||
|
}
|
||
|
);
|
||
|
|
||
|
// 隧道类型
|
||
|
proxyProtocolCombox.valueProperty().addListener((observable, oldValue, newValue) -> {
|
||
|
String newType = StringUtils.lowerCase(newValue.getText());
|
||
|
String port;
|
||
|
if (!newType.equals(proxySetup.getProxy_type()))
|
||
|
port = ProxyConfig.getTypePort(newType).toString();
|
||
|
else
|
||
|
port = proxySetup.getLocal_port().toString();
|
||
|
|
||
|
// 设置默认端口
|
||
|
localPortTextField.setText(port);
|
||
|
// 设置隧道类型
|
||
|
proxySetup.setProxy_type(newType);
|
||
|
});
|
||
|
|
||
|
// 自定义访问域名
|
||
|
customizeDomainBtn.setOnMouseClicked((event) -> {
|
||
|
setDomain(proxySetup != null ? proxySetup : null);
|
||
|
});
|
||
|
|
||
|
// 点击隧道列表
|
||
|
proxyListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
|
||
|
if (newValue != null) {
|
||
|
for (ProxySetup setup : proxyList) {
|
||
|
if (setup.getProxy_name().equals(newValue.getText()))
|
||
|
initProxySetupView(setup);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// 添加隧道
|
||
|
addProxyBtn.setOnMouseClicked(event -> {
|
||
|
// 获取默认隧道设置
|
||
|
proxySetup = ProxyUtil.initProxy();
|
||
|
// 加入列表
|
||
|
proxyList.add(proxySetup);
|
||
|
selectProxy = proxyList.indexOf(proxySetup);
|
||
|
// 展示
|
||
|
initProxySetupView(proxySetup);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 显示隧道设置
|
||
|
*/
|
||
|
private void initProxySetupView(ProxySetup setup) {
|
||
|
|
||
|
int select = proxyList.indexOf(setup);
|
||
|
//
|
||
|
if (setup.getLocal_ip() == null && ProxyUtil.info(setup) == null) {
|
||
|
AlertUtil.error("隧道设置获取失败!").show();
|
||
|
if (select != selectProxy) {
|
||
|
proxyListView.getSelectionModel().select(select);
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// 选中的隧道下标
|
||
|
selectProxy = select;
|
||
|
|
||
|
// 隧道类型
|
||
|
for (Label label : proxyProtocolCombox.getItems()) {
|
||
|
// 获取用户设置
|
||
|
if (StringUtils.lowerCase(label.getText()).equals(setup.getProxy_type()))
|
||
|
proxyProtocolCombox.getSelectionModel().select(proxyProtocolCombox.getItems().indexOf(label));
|
||
|
}
|
||
|
|
||
|
// 服务器
|
||
|
String serverName = ProxyServer.values()[setup.getNode()].name();
|
||
|
proxyServerCB.getItems().forEach((label -> {
|
||
|
if (label.getText().equals(serverName))
|
||
|
proxyServerCB.getSelectionModel().select(proxyServerCB.getItems().indexOf(label));
|
||
|
}));
|
||
|
|
||
|
// 隧道名称
|
||
|
proxyNameTextField.setText(setup.getProxy_name());
|
||
|
|
||
|
// 本地服务地址
|
||
|
localPortTextField.setText(setup.getLocal_port().toString());
|
||
|
localHostTextField.setText(setup.getLocal_ip());
|
||
|
|
||
|
// 公网访问地址
|
||
|
setDomain(setup);
|
||
|
|
||
|
// 启动按钮
|
||
|
if (setup.isRuning() && !setup.isClose()) {
|
||
|
startProxyBtn.setText("停止");
|
||
|
startProxyBtn.getStyleClass().remove("startProxyBtn");
|
||
|
startProxyBtn.getStyleClass().add("stopProxyBtn");
|
||
|
} else {
|
||
|
startProxyBtn.setText("启动");
|
||
|
startProxyBtn.getStyleClass().remove("stoptProxyBtn");
|
||
|
startProxyBtn.getStyleClass().add("startProxyBtn");
|
||
|
}
|
||
|
|
||
|
// 是否停用
|
||
|
closeProxy(setup.isClose());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 显示隧道列表
|
||
|
*/
|
||
|
private void initProxyList(List<ProxySetup> proxyList) {
|
||
|
// 清空列表
|
||
|
proxyListView.getItems().clear();
|
||
|
|
||
|
for (ProxySetup proxy : proxyList) {
|
||
|
try {
|
||
|
Label label = FXMLLoader.load(getClass().getResource("/fxml/proxyItem.fxml"));
|
||
|
label.setText(proxy.getProxy_name());
|
||
|
label.getStyleClass().addAll(PROXY_LIST_ITEM_CLASS, PROXY_LIST_ITEM_STOP_CLASS);
|
||
|
proxyListView.getItems().add(label);
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 是否启用当前隧道
|
||
|
*/
|
||
|
private void closeProxy(boolean close) {
|
||
|
if (close && proxySetup.isRuning()) stopProxy();
|
||
|
|
||
|
proxyNameTextField.setDisable(close);// 隧道名称
|
||
|
proxyProtocolCombox.setDisable(close);// 隧道类型
|
||
|
localHostTextField.setDisable(close);// 本地地址
|
||
|
localPortTextField.setDisable(close);// 本地端口
|
||
|
domainTextField.setDisable(close); // 外网子域名
|
||
|
startProxyBtn.setDisable(close); // 启动按钮
|
||
|
|
||
|
ObservableList<String> styleClass = proxyListView.getItems().get(selectProxy).getStyleClass();
|
||
|
if (close) {
|
||
|
styleClass.remove(PROXY_LIST_ITEM_STOP_CLASS);
|
||
|
styleClass.remove(PROXY_LIST_ITEM_RUN_CLASS);
|
||
|
styleClass.add(PROXY_LIST_ITEM_CLOSE_CLASS);
|
||
|
} else {
|
||
|
styleClass.remove(PROXY_LIST_ITEM_CLOSE_CLASS);
|
||
|
styleClass.add(proxySetup.isClose() ? PROXY_LIST_ITEM_CLOSE_CLASS : PROXY_LIST_ITEM_STOP_CLASS);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* TODO 启动代理
|
||
|
*/
|
||
|
private void startProxy() {
|
||
|
|
||
|
// 删除旧隧道
|
||
|
if (proxySetup.getId() != null) ProxyUtil.delete(proxySetup.getId());
|
||
|
|
||
|
// 添加隧道
|
||
|
if (!ProxyUtil.add(result -> {
|
||
|
}, proxySetup)) return;
|
||
|
|
||
|
// TODO 修改设置
|
||
|
// TODO 调用cmd
|
||
|
|
||
|
ObservableList<String> styleClass = proxyListView.getItems().get(selectProxy).getStyleClass();
|
||
|
styleClass.remove(PROXY_LIST_ITEM_STOP_CLASS);
|
||
|
styleClass.add(PROXY_LIST_ITEM_RUN_CLASS);
|
||
|
proxySetup.setRuning(true);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* TODO 暂停代理
|
||
|
*/
|
||
|
private void stopProxy() {
|
||
|
// 关闭CMD
|
||
|
|
||
|
proxySetup.setRuning(false);
|
||
|
}
|
||
|
}
|