From 21045b6e1aec10fa354dd5282bfd496f82fda2e1 Mon Sep 17 00:00:00 2001 From: octopusYan <935540320@qq.com> Date: Mon, 11 Apr 2022 16:14:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E9=9A=A7=E9=81=93=E6=97=B6=EF=BC=8C=E9=9A=A7=E9=81=93=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E8=AE=BE=E7=BD=AE=E7=9A=84bug=20=E5=8E=BB=E9=99=A4?= =?UTF-8?q?=E6=97=A0=E7=94=A8import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../octopusyan/controller/MainController.java | 108 +++++++++--------- src/main/java/top/octopusyan/http/Api.java | 2 +- .../top/octopusyan/manager/FrpManager.java | 89 ++++++++++++--- .../top/octopusyan/manager/ProxyManager.java | 65 ++--------- .../top/octopusyan/manager/http/EasyHttp.java | 10 +- .../octopusyan/manager/http/EasyUtils.java | 2 +- .../octopusyan/manager/http/HttpConfig.java | 10 +- .../manager/http/callback/BaseCallback.java | 7 +- .../manager/http/callback/NormalCallback.java | 10 +- .../manager/http/config/LogStrategy.java | 2 +- .../manager/http/model/UpdateBody.java | 2 +- .../manager/http/request/IRequestHandler.java | 6 +- .../manager/http/request/RequestHandler.java | 11 +- .../top/octopusyan/model/ApplicatonStore.java | 10 +- .../java/top/octopusyan/utils/FxmlUtil.java | 2 - .../top/octopusyan/utils/KaptchaUtil.java | 3 +- 17 files changed, 171 insertions(+), 170 deletions(-) diff --git a/pom.xml b/pom.xml index 1bbac05..a3d32ef 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ top.octopusyan YanFrp - 0.1.2-SNAPSHOT + 1.0.3-SNAPSHOT YanFrp diff --git a/src/main/java/top/octopusyan/controller/MainController.java b/src/main/java/top/octopusyan/controller/MainController.java index 33af128..5af8725 100644 --- a/src/main/java/top/octopusyan/controller/MainController.java +++ b/src/main/java/top/octopusyan/controller/MainController.java @@ -52,7 +52,6 @@ public class MainController extends BaseController implements Initial public static final String PROXY_LIST_ITEM_RUN_CLASS = "proxyListItem-run"; public static final String PROXY_LIST_ITEM_CLOSE_CLASS = "proxyListItem-close"; public static final String PROXY_LIST_ITEM_SELECT_CLASS = "proxyListItem-select"; - public static final String INPUT_CLASS = "inputText"; @FXML public StackPane root; @@ -93,12 +92,13 @@ public class MainController extends BaseController implements Initial @FXML public JFXButton logoutBtn; - private ToggleGroup openProxyGroup = new ToggleGroup(); - private SimpleBooleanProperty customizeDomain = new SimpleBooleanProperty(false); - private Map frpUtilMap = new HashMap<>(); + private final ToggleGroup openProxyGroup = new ToggleGroup(); + private final SimpleBooleanProperty customizeDomain = new SimpleBooleanProperty(false); + private final Map frpUtilMap = new HashMap<>(); + private final Map userProxy = new HashMap<>(); private ProxySetupModel proxySetupModel; private ProxySetup proxySetup; - private SimpleBooleanProperty setup = new SimpleBooleanProperty(false); + private final SimpleBooleanProperty setup = new SimpleBooleanProperty(false); @Override public boolean dragWindow() { @@ -156,6 +156,20 @@ public class MainController extends BaseController implements Initial // 如果用户隧道列表不为空 if (result != null && result.size() > 0) { + + // 上次关闭时选择的隧道 + if (selectProxyName != null) + for (int i = 0; i < result.size(); i++) { + if (result.get(i).getProxy_name().equals(selectProxyName)) + selectProxy(i); + selectProxyName = null; + } + + // 保存用户隧道信息 + for (ProxySetup setup : result) { + userProxy.put(setup.getId(), setup); + } + proxySetup = result.get(selectProxy()); proxySetupModel.set(result.get(selectProxy())); // 初始化用户隧道列表 @@ -163,10 +177,7 @@ public class MainController extends BaseController implements Initial // 重置隧道列表视图 initProxyListView(); } -// else { -// } -// -// bindDataView(); + }); } @@ -264,8 +275,7 @@ public class MainController extends BaseController implements Initial // 是否切换为自定义域名 if (newValue) { // 隐藏系统域名 - if (parent.getChildren().contains(domainSuffixTextField)) - parent.getChildren().remove(domainSuffixTextField); + parent.getChildren().remove(domainSuffixTextField); // 用户域名是否自定义 if (!DomainUtil.isCustomize(proxySetupModel.get())) { proxySetupModel.setDomain(""); @@ -298,7 +308,9 @@ public class MainController extends BaseController implements Initial tabPane.getSelectionModel().select(1); } - /** 设置访问链接 */ + /** + * 设置访问链接 + */ private void setDomainLink() { // 外网访问连接 if (DomainUtil.isHttp(proxySetupModel)) { @@ -315,15 +327,16 @@ public class MainController extends BaseController implements Initial public void initViewAction() { // 重置 resetProxyBtn.setOnMouseClicked(event -> { - proxySetup.setRuning(false); + proxySetup = userProxy.get(proxySetup.getId()); + proxySetup.setRuning(proxySetupModel.isRunning()); proxySetupModel.set(proxySetup); domainTextField.resetValidation(); }); // 日志清理 clearLogBtn.setOnMouseClicked(event -> { - if (frpUtilMap.get(proxySetupModel.getProxyName()) != null) - frpUtilMap.get(proxySetupModel.getProxyName()).clearLog(); + if (frpUtilMap.get(proxySetup.getId()) != null) + frpUtilMap.get(proxySetup.getId()).clearLog(); }); // 更换服务器地址 @@ -356,16 +369,12 @@ public class MainController extends BaseController implements Initial // 本地IP检查 localHostTextField.getValidators().add(TextValidate.IpRequired); localHostTextField.getValidators().add(TextValidate.IpFormat); - localHostTextField.textProperty().addListener((observable, oldValue, newValue) -> { - localHostTextField.validate(); - }); + localHostTextField.textProperty().addListener((observable, oldValue, newValue) -> localHostTextField.validate()); // 端口检查 localPortTextField.getValidators().add(TextValidate.PortRequired); localPortTextField.getValidators().add(TextValidate.PortFormat); - localPortTextField.textProperty().addListener((observable, oldValue, newValue) -> { - localPortTextField.validate(); - }); + localPortTextField.textProperty().addListener((observable, oldValue, newValue) -> localPortTextField.validate()); // 域名检查 setup.addListener((observable, oldValue, newValue) -> { @@ -403,7 +412,7 @@ public class MainController extends BaseController implements Initial Platform.runLater(() -> { setup.set(false); proxySetupModel.set(proxySetup); - FrpManager frpManager = frpUtilMap.get(proxySetup.getProxy_name()); + FrpManager frpManager = frpUtilMap.get(proxySetup.getId()); proxyLogPane.contentProperty().set(frpManager == null ? null : frpManager.getConsole()); setup.set(true); setDomainLink(); @@ -430,9 +439,7 @@ public class MainController extends BaseController implements Initial }); // 链接跳转 - domainLink.setOnMouseClicked(event -> { - getApplication().getHostServices().showDocument(domainLink.getText()); - }); + domainLink.setOnMouseClicked(event -> getApplication().getHostServices().showDocument(domainLink.getText())); // 复制外网访问地址 copyDomainBtn.setOnMouseClicked(event -> { @@ -448,7 +455,7 @@ public class MainController extends BaseController implements Initial // 退出登录 logoutBtn.setOnMouseClicked(event -> { - for (String key : frpUtilMap.keySet()) { + for (Integer key : frpUtilMap.keySet()) { frpUtilMap.get(key).stop(); } ApplicatonStore.logout(); @@ -516,16 +523,15 @@ public class MainController extends BaseController implements Initial } catch (IOException e) { e.printStackTrace(); } - - proxyListView.getSelectionModel().select(items.size() - 1); } /** * 启动代理 */ private void startProxy() { + // 验证 - if(!domainTextField.validate() || !localHostTextField.validate() || !localPortTextField.validate()) + if (!domainTextField.validate() || !localHostTextField.validate() || !localPortTextField.validate()) return; // 非 http 隧道系统随机分配端口 @@ -533,45 +539,37 @@ public class MainController extends BaseController implements Initial proxySetupModel.setRemotePort(String.valueOf(ProxyManager.randomPort())); } - // 是用户隧道 - if (proxySetupModel.getId() != null && !proxySetupModel.get().equals(proxySetup)) { - // 删除旧隧道 - ProxyManager.delete(Integer.parseInt(proxySetupModel.getId())); - + // 是否有修改 + if (!proxySetupModel.get().equals(userProxy.get(proxySetup.getId()))) { // 添加隧道 ProxyManager.add(new OnHttpListener() { @Override public void onSucceed(ProxySetup result) { - result.setRuning(frpUtilMap.containsKey(result.getProxy_name())); - proxySetupModel.set(result); + // 如果添加成功 + if (result != null) { + userProxy.put(result.getId(), result); + start(result); + } } @Override public void onFail(Exception e) { - - } - }, proxySetupModel.get()); - } else if(proxySetupModel.getId() == null) { - - // 添加隧道 - ProxyManager.add(new OnHttpListener() { - @Override - public void onSucceed(ProxySetup result) { - result.setRuning(frpUtilMap.containsKey(result.getProxy_name())); - proxySetupModel.set(result); - } - - @Override - public void onFail(Exception e) { - } }, proxySetupModel.get()); + } else { + start(proxySetup); } + } + + private void start(ProxySetup setup) { + // 添加成功,设置运行状态 + setup.setRuning(frpUtilMap.containsKey(setup.getId())); + proxySetupModel.set(setup); // 初始化frputil FrpManager frpManager; - if ((frpManager = frpUtilMap.get(proxySetupModel.getProxyName())) == null) - frpUtilMap.put(proxySetupModel.getProxyName(), frpManager = FrpManager.init(proxySetupModel)); + if ((frpManager = frpUtilMap.get(setup.getId())) == null) + frpUtilMap.put(proxySetup.getId(), frpManager = FrpManager.init(proxySetupModel)); // 设置文本域对象 if (proxyLogPane.getContent() == null) proxyLogPane.contentProperty().set(frpManager.getConsole()); @@ -588,7 +586,7 @@ public class MainController extends BaseController implements Initial */ private void stopProxy() { // 关闭CMD - FrpManager frpManager = frpUtilMap.get(proxySetupModel.getProxyName()); + FrpManager frpManager = frpUtilMap.get(proxySetup.getId()); if (frpManager != null) frpManager.stop(); } diff --git a/src/main/java/top/octopusyan/http/Api.java b/src/main/java/top/octopusyan/http/Api.java index 4b2d7c5..8428905 100644 --- a/src/main/java/top/octopusyan/http/Api.java +++ b/src/main/java/top/octopusyan/http/Api.java @@ -1,12 +1,12 @@ package top.octopusyan.http; import top.octopusyan.http.request.*; +import top.octopusyan.manager.ProxyManager; import top.octopusyan.manager.http.api.NotParamApi; import top.octopusyan.manager.http.api.ParamApi; import top.octopusyan.manager.http.api.PathParamApi; import top.octopusyan.manager.http.config.BodyType; import top.octopusyan.manager.http.config.HttpConstant; -import top.octopusyan.manager.ProxyManager; /** * @author : octopus yan diff --git a/src/main/java/top/octopusyan/manager/FrpManager.java b/src/main/java/top/octopusyan/manager/FrpManager.java index 5a40094..77a3de2 100644 --- a/src/main/java/top/octopusyan/manager/FrpManager.java +++ b/src/main/java/top/octopusyan/manager/FrpManager.java @@ -3,14 +3,19 @@ package top.octopusyan.manager; import javafx.application.Platform; import javafx.scene.control.TextArea; import lombok.Data; +import lombok.EqualsAndHashCode; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; +import top.octopusyan.http.Api; +import top.octopusyan.manager.http.EasyHttp; +import top.octopusyan.manager.http.model.ResponseClass; import top.octopusyan.model.ApplicatonStore; import top.octopusyan.model.ProxySetupModel; +import top.octopusyan.utils.JsoupUtil; import java.io.*; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; +import java.util.*; /** * @author : octopus yan @@ -24,6 +29,7 @@ public class FrpManager { public static final String FRPC_CLIENT_FILE_NAME = "frpclienttmpfile.exe"; private final String FRPC_CONF_PREFIX_NAME = "proxy_"; private final String FRPC_CONF_SUFFIX_NAME = ".ini"; + private static final Map serverConfigraution = new HashMap<>(3); /** * 应用临时目录 地址 */ @@ -65,6 +71,7 @@ public class FrpManager { */ public void start() { thread = new ProxyThread(); + thread.setName(FRPC_CONF_PREFIX_NAME + model.getSort()); thread.start(); } @@ -98,14 +105,14 @@ public class FrpManager { */ public void clearLog() { if (console != null) - Platform.runLater(() -> console.clear()); + Platform.runLater(console::clear); } /** * 隧道配置文件地址 */ private String getConfigFilePath() { - return YAN_FRP_TEMP_DIR_PATH + File.separator + FRPC_CONF_PREFIX_NAME + model.getProxyName() + FRPC_CONF_SUFFIX_NAME; + return YAN_FRP_TEMP_DIR_PATH + File.separator + FRPC_CONF_PREFIX_NAME + model.getSort() + FRPC_CONF_SUFFIX_NAME; } /** @@ -121,11 +128,62 @@ public class FrpManager { frpcConfigFile.deleteOnExit(); // 写入服务配置 - FileUtils.write(frpcConfigFile, ProxyManager.getUserServerConfig(model.get().getNode()), StandardCharsets.UTF_8); + FileUtils.write(frpcConfigFile, getUserFrpServerConfig(model.get().getNode()), StandardCharsets.UTF_8); // 写入隧道配置 - FileUtils.write(frpcConfigFile, ProxyManager.getProxyConfig(model), StandardCharsets.UTF_8, true); + FileUtils.write(frpcConfigFile, getProxyFrpConfig(model), StandardCharsets.UTF_8, true); } + + public static String getUserFrpServerConfig(int node) { + String config = serverConfigraution.get(node); + if (StringUtils.isNotEmpty(config)) return config; + try { + String result = EasyHttp.builder() + .api(Api.getServerConfiguration(node)) + .pathParam(String.valueOf(node)) + .execute(new ResponseClass() { + }); + config = JsoupUtil.getServerConfiguration(result); + if (StringUtils.isNotEmpty(config)) serverConfigraution.put(node, config); + return config; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static String getProxyFrpConfig(ProxySetupModel setup) { + String n = "\n"; + // 基础配置 + StringBuilder stringBuilder = new StringBuilder("[" + ApplicatonStore.getAccount() + "_" + setup.getSort() + "]\n"); + stringBuilder.append("privilege_mode = true\n") + .append("type = ").append(setup.getProxyType().contains("http") ? "http" : setup.getProxyType()).append(n) + .append("local_ip = ").append(setup.getLocalIp()).append(n) + .append("local_port = ").append(setup.getLocalPort()).append(n).append(n); + + if ("http".equals(setup.getProxyType()) || "https".equals(setup.getProxyType())) { + // HTTP / HTTPS + stringBuilder.append("custom_domains = ").append(setup.getDomain()).append(setup.getDomainSuffix()).append(n); + if (!StringUtils.isEmpty(setup.getLocations())) + stringBuilder.append("locations = ").append(setup.getLocations()).append(n); + if (!StringUtils.isEmpty(setup.getLocations())) + stringBuilder.append("host_header_rewrite = ").append(setup.getHostHeaderRewrite()).append(n); + if (!StringUtils.isEmpty(setup.getLocations())) + stringBuilder.append("header_X-From-Where = ").append(setup.getHeader_X_From_Where()).append(n); + } else { + // TCP / UDP / XTCP / STCP + stringBuilder.append("remote_port = ").append(setup.getRemotePort()).append(n); + if (!StringUtils.isEmpty(setup.getSk())) + stringBuilder.append("sk = ").append(setup.getSk()).append(n); + } + // 压缩和加密 + stringBuilder.append("use_encryption = ").append(setup.isUseEncryption()).append(n) + .append("use_compression = ").append(setup.isUseCompression()).append(n).append(n); + + return stringBuilder.toString(); + } + + /** * 初始化frpc客户端文件 */ @@ -135,7 +193,7 @@ public class FrpManager { frpc.createNewFile(); // 复制 frpc - copyFileUsingFileStreams(FrpManager.class.getResourceAsStream("/static/frpc.exe"), frpc); + copyFileUsingFileStreams(Objects.requireNonNull(FrpManager.class.getResourceAsStream("/static/frpc.exe"))); } // frpc 临时配置文件 @@ -154,7 +212,7 @@ public class FrpManager { try { FileUtils.deleteDirectory(frpconfigDir); - for (File file : FileUtils.getTempDirectory().listFiles()) { + for (File file : Objects.requireNonNull(FileUtils.getTempDirectory().listFiles())) { if (file.getPath().contains(YAN_FRP_TEMP_DIR_NAME) && file.canWrite() && file.isDirectory()) { FileUtils.deleteDirectory(file); } @@ -171,11 +229,11 @@ public class FrpManager { } } - private static void copyFileUsingFileStreams(InputStream input, File dest) + private static void copyFileUsingFileStreams(InputStream input) throws IOException { OutputStream output = null; try { - output = new FileOutputStream(dest); + output = new FileOutputStream(FrpManager.frpc); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) > 0) { @@ -183,12 +241,13 @@ public class FrpManager { } } finally { input.close(); - output.close(); + if (output != null) output.close(); } } + @EqualsAndHashCode(callSuper = true) @Data - class ProxyThread extends Thread{ + class ProxyThread extends Thread { private volatile boolean exit = false; @Override @@ -216,13 +275,13 @@ public class FrpManager { String line; while ((line = readStdout.readLine()) != null && !exit) { String finalLine = line; - System.out.println("yan-frp-info:" + line); - Platform.runLater(() -> console.appendText("yan-frp-info:" + finalLine + "\n")); + System.out.println(line); + Platform.runLater(() -> console.appendText(finalLine + "\n")); } } catch (IOException e) { e.printStackTrace(); - // + // TODO 报错 Platform.runLater(() -> console.appendText("yan-frp-error:" + e.getMessage() + "\n")); Platform.runLater(() -> console.appendText("yan-frp-error:启动失败\n")); } diff --git a/src/main/java/top/octopusyan/manager/ProxyManager.java b/src/main/java/top/octopusyan/manager/ProxyManager.java index 8b2dcd9..140c382 100644 --- a/src/main/java/top/octopusyan/manager/ProxyManager.java +++ b/src/main/java/top/octopusyan/manager/ProxyManager.java @@ -13,14 +13,9 @@ import top.octopusyan.http.request.ProxySetup; import top.octopusyan.manager.http.EasyHttp; import top.octopusyan.manager.http.listener.OnHttpListener; import top.octopusyan.manager.http.model.ResponseClass; -import top.octopusyan.model.ApplicatonStore; -import top.octopusyan.model.ProxySetupModel; import top.octopusyan.utils.AlertUtil; -import top.octopusyan.utils.JsoupUtil; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; @@ -32,7 +27,6 @@ import java.util.stream.Collectors; */ public class ProxyManager { private static String csrf; - private static Map serverConfigraution = new HashMap<>(3); /** * 初始化隧道设置 @@ -170,6 +164,14 @@ public class ProxyManager { .request(new OnHttpListener() { @Override public void onSucceed(String result) { + + // 不成功 + if(!result.contains("成功")){ + Platform.runLater(() -> AlertUtil.error(result).header(null).show()); + listener.onSucceed(null); + return; + } + getList(new OnHttpListener>() { @Override public void onSucceed(List result) { @@ -181,7 +183,9 @@ public class ProxyManager { return; } } + listener.onSucceed(null); } + listener.onSucceed(null); } @Override @@ -207,53 +211,4 @@ public class ProxyManager { int i1 = htmlStr.indexOf("\"", i + 18); ProxyManager.csrf = htmlStr.substring(i + 18, i1); } - - public static String getUserServerConfig(int node) { - String config = serverConfigraution.get(node); - if (StringUtils.isNotEmpty(config)) return config; - try { - String result = EasyHttp.builder() - .api(Api.getServerConfiguration(node)) - .pathParam(String.valueOf(node)) - .execute(new ResponseClass() { - }); - config = JsoupUtil.getServerConfiguration(result); - if (StringUtils.isNotEmpty(config)) serverConfigraution.put(node, config); - return config; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public static String getProxyConfig(ProxySetupModel setup) { - String n = "\n"; - // 基础配置 - StringBuilder stringBuilder = new StringBuilder("[" + ApplicatonStore.getAccount() + "_" + setup.getProxyName() + "]\n"); - stringBuilder.append("privilege_mode = true\n") - .append("type = ").append(setup.getProxyType().contains("http") ? "http" : setup.getProxyType()).append(n) - .append("local_ip = ").append(setup.getLocalIp()).append(n) - .append("local_port = ").append(setup.getLocalPort()).append(n).append(n); - - if ("http".equals(setup.getProxyType()) || "https".equals(setup.getProxyType())) { - // HTTP / HTTPS - stringBuilder.append("custom_domains = ").append(setup.getDomain()).append(setup.getDomainSuffix()).append(n); - if (!StringUtils.isEmpty(setup.getLocations())) - stringBuilder.append("locations = ").append(setup.getLocations()).append(n); - if (!StringUtils.isEmpty(setup.getLocations())) - stringBuilder.append("host_header_rewrite = ").append(setup.getHostHeaderRewrite()).append(n); - if (!StringUtils.isEmpty(setup.getLocations())) - stringBuilder.append("header_X-From-Where = ").append(setup.getHeader_X_From_Where()).append(n); - } else { - // TCP / UDP / XTCP / STCP - stringBuilder.append("remote_port = ").append(setup.getRemotePort()).append(n); - if (!StringUtils.isEmpty(setup.getSk())) - stringBuilder.append("sk = ").append(setup.getSk()).append(n); - } - // 压缩和加密 - stringBuilder.append("use_encryption = ").append(setup.isUseEncryption()).append(n) - .append("use_compression = ").append(setup.isUseCompression()).append(n).append(n); - - return stringBuilder.toString(); - } } diff --git a/src/main/java/top/octopusyan/manager/http/EasyHttp.java b/src/main/java/top/octopusyan/manager/http/EasyHttp.java index 3d0b217..ecc5700 100644 --- a/src/main/java/top/octopusyan/manager/http/EasyHttp.java +++ b/src/main/java/top/octopusyan/manager/http/EasyHttp.java @@ -9,15 +9,11 @@ import top.octopusyan.manager.http.api.ParamApi; import top.octopusyan.manager.http.api.PathParamApi; import top.octopusyan.manager.http.api.RequestApi; import top.octopusyan.manager.http.callback.NormalCallback; -import top.octopusyan.manager.http.listener.OnHttpListener; -import top.octopusyan.manager.http.model.CallProxy; -import top.octopusyan.manager.http.model.HttpHeaders; -import top.octopusyan.manager.http.model.HttpParams; -import top.octopusyan.manager.http.model.JsonBody; import top.octopusyan.manager.http.config.BodyType; import top.octopusyan.manager.http.config.HttpConstant; +import top.octopusyan.manager.http.listener.OnHttpListener; +import top.octopusyan.manager.http.model.*; import top.octopusyan.manager.http.request.IRequestHandler; -import top.octopusyan.manager.http.model.ResponseClass; import java.io.File; import java.io.InputStream; @@ -195,7 +191,7 @@ public class EasyHttp { try { mCallProxy = new CallProxy(createCall()); Response response = mCallProxy.execute(); - return (Result) mHandler.requestSucceed(getRequestApi(), response, mHandler.getType(responseClass)); + return mHandler.requestSucceed(getRequestApi(), response, mHandler.getType(responseClass)); } catch (Exception e) { throw mHandler.requestFail(getRequestApi(), e); } diff --git a/src/main/java/top/octopusyan/manager/http/EasyUtils.java b/src/main/java/top/octopusyan/manager/http/EasyUtils.java index 493e1c6..402c54d 100644 --- a/src/main/java/top/octopusyan/manager/http/EasyUtils.java +++ b/src/main/java/top/octopusyan/manager/http/EasyUtils.java @@ -7,11 +7,11 @@ import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.RequestBody; import org.apache.commons.lang3.StringUtils; +import top.octopusyan.manager.ThreadPoolManager; import top.octopusyan.manager.http.annotation.HttpIgnore; import top.octopusyan.manager.http.annotation.HttpRename; import top.octopusyan.manager.http.config.ContentType; import top.octopusyan.manager.http.model.UpdateBody; -import top.octopusyan.manager.ThreadPoolManager; import java.io.*; import java.lang.reflect.Field; diff --git a/src/main/java/top/octopusyan/manager/http/HttpConfig.java b/src/main/java/top/octopusyan/manager/http/HttpConfig.java index d685743..a7b8da2 100644 --- a/src/main/java/top/octopusyan/manager/http/HttpConfig.java +++ b/src/main/java/top/octopusyan/manager/http/HttpConfig.java @@ -1,6 +1,6 @@ package top.octopusyan.manager.http; -import okhttp3.*; +import okhttp3.OkHttpClient; import top.octopusyan.manager.http.config.ILogStrategy; import top.octopusyan.manager.http.request.IRequestHandler; @@ -47,11 +47,11 @@ public class HttpConfig { private HttpConfig(OkHttpClient client) { mClient = client; - mParams = new HashMap(); + mParams = new HashMap<>(); mHeaders = new HashMap<>(); } private HttpConfig() { - mParams = new HashMap(); + mParams = new HashMap<>(); mHeaders = new HashMap<>(); initHttpClient(); } @@ -81,7 +81,7 @@ public class HttpConfig { public HttpConfig setParams(HashMap params) { if (params == null) { - params = new HashMap(); + params = new HashMap<>(); } mParams = params; return this; @@ -207,6 +207,6 @@ public class HttpConfig { } private void initHttpClient() { - ; + } } diff --git a/src/main/java/top/octopusyan/manager/http/callback/BaseCallback.java b/src/main/java/top/octopusyan/manager/http/callback/BaseCallback.java index e802dc5..5fd7e47 100644 --- a/src/main/java/top/octopusyan/manager/http/callback/BaseCallback.java +++ b/src/main/java/top/octopusyan/manager/http/callback/BaseCallback.java @@ -3,10 +3,11 @@ package top.octopusyan.manager.http.callback; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; +import org.jetbrains.annotations.NotNull; +import top.octopusyan.manager.http.EasyUtils; import top.octopusyan.manager.http.HttpConfig; import top.octopusyan.manager.http.HttpLog; import top.octopusyan.manager.http.model.CallProxy; -import top.octopusyan.manager.http.EasyUtils; import java.io.IOException; import java.net.SocketTimeoutException; @@ -34,7 +35,7 @@ public abstract class BaseCallback implements Callback { } @Override - public void onResponse(Call call, Response response) { + public void onResponse(@NotNull Call call, @NotNull Response response) { try { // 收到响应 onResponse(response); @@ -48,7 +49,7 @@ public abstract class BaseCallback implements Callback { } @Override - public void onFailure(Call call, IOException e) { + public void onFailure(@NotNull Call call, @NotNull IOException e) { // 服务器请求超时重试 if (e instanceof SocketTimeoutException && mRetryCount < HttpConfig.getInstance().getRetryCount()) { // 设置延迟 N 秒后重试该请求 diff --git a/src/main/java/top/octopusyan/manager/http/callback/NormalCallback.java b/src/main/java/top/octopusyan/manager/http/callback/NormalCallback.java index 5d15d8b..02c4f55 100644 --- a/src/main/java/top/octopusyan/manager/http/callback/NormalCallback.java +++ b/src/main/java/top/octopusyan/manager/http/callback/NormalCallback.java @@ -2,12 +2,12 @@ package top.octopusyan.manager.http.callback; import javafx.application.Platform; import okhttp3.Response; -import top.octopusyan.manager.http.listener.OnHttpListener; -import top.octopusyan.manager.http.HttpLog; -import top.octopusyan.manager.http.model.CallProxy; -import top.octopusyan.manager.http.api.RequestApi; -import top.octopusyan.manager.http.request.IRequestHandler; import top.octopusyan.manager.http.EasyUtils; +import top.octopusyan.manager.http.HttpLog; +import top.octopusyan.manager.http.api.RequestApi; +import top.octopusyan.manager.http.listener.OnHttpListener; +import top.octopusyan.manager.http.model.CallProxy; +import top.octopusyan.manager.http.request.IRequestHandler; /** * @author : octopus yan diff --git a/src/main/java/top/octopusyan/manager/http/config/LogStrategy.java b/src/main/java/top/octopusyan/manager/http/config/LogStrategy.java index 4281598..9cc61d4 100644 --- a/src/main/java/top/octopusyan/manager/http/config/LogStrategy.java +++ b/src/main/java/top/octopusyan/manager/http/config/LogStrategy.java @@ -1,8 +1,8 @@ package top.octopusyan.manager.http.config; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.apache.commons.lang3.StringUtils; import top.octopusyan.manager.http.EasyHttp; import top.octopusyan.manager.http.HttpConfig; diff --git a/src/main/java/top/octopusyan/manager/http/model/UpdateBody.java b/src/main/java/top/octopusyan/manager/http/model/UpdateBody.java index 967449c..ebd57aa 100644 --- a/src/main/java/top/octopusyan/manager/http/model/UpdateBody.java +++ b/src/main/java/top/octopusyan/manager/http/model/UpdateBody.java @@ -5,8 +5,8 @@ import okhttp3.RequestBody; import okio.BufferedSink; import okio.Okio; import okio.Source; -import top.octopusyan.manager.http.config.ContentType; import top.octopusyan.manager.http.EasyUtils; +import top.octopusyan.manager.http.config.ContentType; import java.io.File; import java.io.FileNotFoundException; diff --git a/src/main/java/top/octopusyan/manager/http/request/IRequestHandler.java b/src/main/java/top/octopusyan/manager/http/request/IRequestHandler.java index 06c65f7..011df00 100644 --- a/src/main/java/top/octopusyan/manager/http/request/IRequestHandler.java +++ b/src/main/java/top/octopusyan/manager/http/request/IRequestHandler.java @@ -1,12 +1,12 @@ package top.octopusyan.manager.http.request; -import java.lang.reflect.Type; - import okhttp3.Request; import okhttp3.Response; import top.octopusyan.manager.http.EasyUtils; import top.octopusyan.manager.http.api.RequestApi; +import java.lang.reflect.Type; + public interface IRequestHandler { /** @@ -28,7 +28,7 @@ public interface IRequestHandler { * * @throws Exception 如果抛出则回调失败 */ - Object requestSucceed(RequestApi api, Response response, Type type) throws Exception; + Result requestSucceed(RequestApi api, Response response, Type type) throws Exception; /** * 请求失败 diff --git a/src/main/java/top/octopusyan/manager/http/request/RequestHandler.java b/src/main/java/top/octopusyan/manager/http/request/RequestHandler.java index 488b22c..647f6c3 100644 --- a/src/main/java/top/octopusyan/manager/http/request/RequestHandler.java +++ b/src/main/java/top/octopusyan/manager/http/request/RequestHandler.java @@ -8,8 +8,8 @@ import okhttp3.Headers; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; -import top.octopusyan.manager.http.api.RequestApi; import top.octopusyan.manager.http.HttpLog; +import top.octopusyan.manager.http.api.RequestApi; import top.octopusyan.manager.http.exception.*; import top.octopusyan.manager.http.response.BaseRest; @@ -24,8 +24,8 @@ import java.net.UnknownHostException; /** * @author octopus yan * @email octopus_yan@foxmail.com - * @Description 请求处理类 - * @createTime 2021-07-23 11:55:26 + * @description 请求处理类 + * @create 2021-07-23 11:55:26 */ public final class RequestHandler implements IRequestHandler { @@ -125,7 +125,7 @@ public final class RequestHandler implements IRequestHandler { } @Override - public Exception requestFail(RequestApi api, Exception e) { + public Exception requestFail(RequestApi api, Exception e) { // 判断这个异常是不是自己抛的 if (e instanceof HttpException) { if (e instanceof TokenException) { @@ -159,9 +159,6 @@ public final class RequestHandler implements IRequestHandler { /** * 传入需要连接的IP,返回是否连接成功 - * - * @param remoteInetAddr - * @return */ public static boolean isReachable(String remoteInetAddr) { boolean reachable = false; diff --git a/src/main/java/top/octopusyan/model/ApplicatonStore.java b/src/main/java/top/octopusyan/model/ApplicatonStore.java index f6a03bb..e758176 100644 --- a/src/main/java/top/octopusyan/model/ApplicatonStore.java +++ b/src/main/java/top/octopusyan/model/ApplicatonStore.java @@ -41,6 +41,7 @@ public class ApplicatonStore { private static final SimpleBooleanProperty registerSuccess = new SimpleBooleanProperty(); private static final ObservableList proxyList = FXCollections.observableList(new ArrayList<>()); private static final SimpleIntegerProperty selectProxy = new SimpleIntegerProperty(0); + public static String selectProxyName = null; public static void setAccount(String account) { ApplicatonStore.account.set(account); @@ -153,11 +154,8 @@ public class ApplicatonStore { if (appDataJson.containsKey(AUTO_LOGIN_KEY)) autoLogin.set(appDataJson.getBoolean(AUTO_LOGIN_KEY)); // 记住 if (appDataJson.containsKey(REMEMBER_ME_KEY)) rememberMe.set(appDataJson.getBoolean(REMEMBER_ME_KEY)); - // TODO 已选择隧道 - if (appDataJson.containsKey(SELECT_PROXY_NAME_KEY)) { -// String proxtName = appDataJson.getString(SELECT_PROXY_NAME_KEY); -// proxyListProperty().addListener(new ); - } + // 已选择隧道 + if (appDataJson.containsKey(SELECT_PROXY_NAME_KEY)) selectProxyName = appDataJson.getString(SELECT_PROXY_NAME_KEY); } /** @@ -174,7 +172,7 @@ public class ApplicatonStore { if (rememberMe.get() && password.get() != null) appDataJson.put(PASSWORD_KEY, password.get()); // 选择隧道 if (selectProxy.getValue() != null && proxyList.size() > 0 && proxyList.size() > selectProxy.get()) - appDataJson.put(SELECT_PROXY_NAME_KEY, proxyList.get(selectProxy.getValue())); + appDataJson.put(SELECT_PROXY_NAME_KEY, proxyList.get(selectProxy.getValue()).getProxy_name()); // 自动登录 appDataJson.put(AUTO_LOGIN_KEY, autoLogin.get()); // 记住 diff --git a/src/main/java/top/octopusyan/utils/FxmlUtil.java b/src/main/java/top/octopusyan/utils/FxmlUtil.java index 8651f8e..fc48145 100644 --- a/src/main/java/top/octopusyan/utils/FxmlUtil.java +++ b/src/main/java/top/octopusyan/utils/FxmlUtil.java @@ -2,9 +2,7 @@ package top.octopusyan.utils; import javafx.fxml.FXMLLoader; import javafx.fxml.JavaFXBuilderFactory; -import sun.reflect.Reflection; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; /** diff --git a/src/main/java/top/octopusyan/utils/KaptchaUtil.java b/src/main/java/top/octopusyan/utils/KaptchaUtil.java index f5ef656..fca9212 100644 --- a/src/main/java/top/octopusyan/utils/KaptchaUtil.java +++ b/src/main/java/top/octopusyan/utils/KaptchaUtil.java @@ -1,6 +1,5 @@ package top.octopusyan.utils; -import com.google.code.kaptcha.Producer; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; @@ -15,7 +14,7 @@ import java.util.Properties; */ public class KaptchaUtil { public static volatile KaptchaUtil util; - private DefaultKaptcha defaultKaptcha; + private final DefaultKaptcha defaultKaptcha; private KaptchaUtil(DefaultKaptcha kaptcha){ this.defaultKaptcha = kaptcha;