diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..55113ba
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml
index 26d3b24..d984b2c 100644
--- a/.idea/kotlinc.xml
+++ b/.idea/kotlinc.xml
@@ -4,6 +4,6 @@
-
+
\ No newline at end of file
diff --git a/src/main/java/cn/reddragon/eportal/Authenticator.java b/src/main/java/cn/reddragon/eportal/Authenticator.java
index 6608773..fcac619 100644
--- a/src/main/java/cn/reddragon/eportal/Authenticator.java
+++ b/src/main/java/cn/reddragon/eportal/Authenticator.java
@@ -21,6 +21,7 @@ public class Authenticator {
public static String userIndex = null;
public static boolean online = false;
public static LoginType type;
+ public static boolean error = false;
public static HttpURLConnection login(String username, String password, String service, String queryString) {
String content = "userId=" + username + "&password=" + password + "&service=" + service + "&queryString=" + queryString + "&operatorPwd=" + "&operatorUserId=" + "&validcode=" + "&passwordEncrypt=false";
@@ -39,7 +40,7 @@ public static HttpURLConnection login(String username, String password, String s
}
public static void updateUserIndex() {
- if (!Authenticator.getOnline()) {
+ if (!online) {
userIndex = null;
}
try {
@@ -88,10 +89,6 @@ public static HttpURLConnection logout() {
}
}
- public static boolean getOnline() {
- return online;
- }
-
public static String userIndex() {
if (userIndex == null) {
updateUserIndex();
@@ -106,20 +103,30 @@ public static void checkOnline() {
connection.connect();
Map> result = connection.getHeaderFields();
String redirectLocation = result.get("Location").get(0);
+ // 判断是否在线
if (Objects.equals(redirectLocation, "Http://123.123.123.123")) {
online = false;
} else online = redirectLocation.contains("http://10.96.0.155/eportal/./success.jsp?");
+
if (HelloApplication.controller != null)
Platform.runLater(() -> {
- if (HelloApplication.controller.button.isDisabled())
+ if (error)
HelloApplication.controller.resultText.setText("");
- HelloApplication.controller.button.setDisable(false);
+ //HelloApplication.controller.button.setDisable(false);
});
+ error = false;
+ if (connection.getResponseCode() != HttpURLConnection.HTTP_MOVED_TEMP) {
+ if (HelloApplication.controller != null) {
+ Platform.runLater(() -> HelloApplication.controller.resultText.setText("Auth server error!"));
+ error = true;
+ }
+ }
} catch (SocketTimeoutException e) {
if (HelloApplication.controller != null) {
Platform.runLater(() -> {
HelloApplication.controller.resultText.setText("Auth server connection timeout!");
- HelloApplication.controller.button.setDisable(true);
+ error = true;
+ //HelloApplication.controller.button.setDisable(true);
});
}
online = false;
@@ -127,7 +134,8 @@ public static void checkOnline() {
if (HelloApplication.controller != null) {
Platform.runLater(() -> {
HelloApplication.controller.resultText.setText("No Internet connection!");
- HelloApplication.controller.button.setDisable(true);
+ error = true;
+ //HelloApplication.controller.button.setDisable(true);
});
}
online = false;
@@ -137,6 +145,14 @@ public static void checkOnline() {
}
}
+ public static void updateStatus() {
+ checkOnline();
+ if (online) {
+ updateUserIndex();
+ updateSession();
+ }
+ }
+
public static void updateSession() {
new Thread(() -> {
try {
@@ -167,11 +183,9 @@ private static String updateSessionInternal() {
//System.out.println(resultJson.toString());
String r = resultJson.get("result").getAsString();
if (Objects.equals(r, "success")) {
- //更新userIndex
- Authenticator.userIndex = resultJson.get("userIndex").getAsString();
- // 获取当前用户
+ // 设置当前用户
Platform.runLater(() -> HelloApplication.controller.user.setText("User: " + resultJson.get("userName").getAsString() + " (" + resultJson.get("userId").getAsString() + ")"));
- // 获取运营商、剩余时间
+ // 设置运营商、剩余时间
JsonArray ballArray = JsonParser.parseString(resultJson.get("ballInfo").getAsString()).getAsJsonArray();
if (ballArray.get(1).getAsJsonObject().get("displayName").getAsString().equals("我的运营商")) {
for (LoginType it : LoginType.values()) {
diff --git a/src/main/java/cn/reddragon/eportal/HelloApplication.java b/src/main/java/cn/reddragon/eportal/HelloApplication.java
index 620f74f..cf9dbba 100644
--- a/src/main/java/cn/reddragon/eportal/HelloApplication.java
+++ b/src/main/java/cn/reddragon/eportal/HelloApplication.java
@@ -18,11 +18,9 @@ public class HelloApplication extends Application {
while (true) {
try {
//System.out.println("Heartbeat.");
- Thread.sleep(2500L);
- Authenticator.checkOnline();
- if (Authenticator.getOnline())
- Authenticator.updateSession();
+ Authenticator.updateStatus();
Platform.runLater(controller::updateUI);
+ Thread.sleep(5000L);
} catch (Exception e) {
//throw new RuntimeException(e);
e.printStackTrace();
@@ -33,7 +31,7 @@ public class HelloApplication extends Application {
public static void main(String[] args) {
Authenticator.checkOnline();
try {
- if (Authenticator.getOnline()) {
+ if (Authenticator.online) {
System.out.println("Already connected!");
Authenticator.updateUserIndex();
//System.exit(0);
@@ -76,7 +74,7 @@ public void start(Stage stage) throws IOException {
//updateUI();
askThread.start();
Authenticator.checkOnline();
- if (!Authenticator.getOnline()) {
+ if (!Authenticator.online) {
controller.onLoginButtonClick();
}
}
diff --git a/src/main/java/cn/reddragon/eportal/HelloController.java b/src/main/java/cn/reddragon/eportal/HelloController.java
index 220afb1..6eb4563 100644
--- a/src/main/java/cn/reddragon/eportal/HelloController.java
+++ b/src/main/java/cn/reddragon/eportal/HelloController.java
@@ -5,6 +5,7 @@
import cn.reddragon.eportal.utils.URIEncoder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
+import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
@@ -16,11 +17,12 @@
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.nio.charset.Charset;
+import java.util.Arrays;
import java.util.Objects;
public class HelloController {
@FXML
- public ChoiceBox selector;
+ public ChoiceBox selector;
@FXML
public Label resultText;
@FXML
@@ -39,7 +41,7 @@ public class HelloController {
public void updateUI() {
StringBuilder sb = new StringBuilder();
sb.append("Status: ");
- if (Authenticator.getOnline()) {
+ if (Authenticator.online) {
button.setText("Logout");
sb.append("Online ");
for (LoginType it : LoginType.values()) {
@@ -47,45 +49,50 @@ public void updateUI() {
sb.append('(');
sb.append(it.displayName);
sb.append(')');
+ break;
}
}
- /*
- if (Authenticator.type == LoginType.CHINAMOBILE)
- sb.append("(ChinaMobile)");
- else
- sb.append("(WAN)");
- */
} else {
button.setText("Login");
sb.append("Offline");
user.setText("User: null");
+ remainLabel.setText("Time remaining:");
}
statusLabel.setText(sb.toString());
}
@FXML
protected void onLoginButtonClick() {
- if (Authenticator.getOnline()) {
- button.setDisable(true);
+ button.setDisable(true);
+ if (Authenticator.online) {
if (button.getText().equals("Login")) {
resultText.setText("Already logged in!");
button.setText("Logout");
} else if (button.getText().equals("Logout")) {
- try {
- HttpURLConnection connection = Authenticator.logout();
- if (connection == null) {
- resultText.setText("Error!");
- button.setDisable(false);
- return;
+ new Thread(() -> {
+ try {
+ HttpURLConnection connection = Authenticator.logout();
+ if (connection == null) {
+ Platform.runLater(() -> {
+ resultText.setText("Error!");
+ button.setDisable(false);
+ });
+ return;
+ }
+ JsonObject resultMessage = JsonParser.parseString(IOUtils.readText(connection.getInputStream())).getAsJsonObject();
+ System.out.println(resultMessage.toString());
+ Platform.runLater(() -> resultText.setText(resultMessage.get("result").getAsString() + ":" + resultMessage.get("message").getAsString()));
+ Authenticator.updateStatus();
+ Platform.runLater(this::updateUI);
+ } catch (Exception e) {
+ e.printStackTrace();
+ Platform.runLater(() -> resultText.setText(e.getMessage()));
}
- JsonObject resultMessage = JsonParser.parseString(IOUtils.readText(connection.getInputStream())).getAsJsonObject();
- System.out.println(resultMessage.toString());
- resultText.setText(resultMessage.get("result").getAsString() + ":" + resultMessage.get("message").getAsString());
- } catch (Exception e) {
- e.printStackTrace();
- }
+ Platform.runLater(() -> button.setDisable(false));
+ }).start();
+
}
- button.setDisable(false);
+ //button.setDisable(false);
return;
}
String username = userNameField.getText();
@@ -96,67 +103,58 @@ protected void onLoginButtonClick() {
String password = passwordField.getText();
if (password.isBlank()) {
resultText.setText("Please enter your password!");
+ return;
}
- button.setDisable(true);
String userId = URIEncoder.encodeURI(URIEncoder.encodeURI(username));
String pw = URIEncoder.encodeURI(URIEncoder.encodeURI(password));
- String mode = (String) selector.getValue();
- String serviceString = "";
- /*
- if (mode.equals("LAN")) {
- serviceString = URIEncoder.encodeURI(URIEncoder.encodeURI("校园内网服务(in-campus NET)"));
- } else if (mode.equals("WAN")) {
- serviceString = URIEncoder.encodeURI(URIEncoder.encodeURI("校园外网服务(out-campus NET)"));
- //serviceString = URIEncoder.encodeURI(URIEncoder.encodeURI("校园网(Campus NET)"));
- } else if (mode.equals("ChinaMobile")) {
- serviceString = URIEncoder.encodeURI(URIEncoder.encodeURI("中国移动(CMCC NET)"));
- }
- */
- for (LoginType it : LoginType.values()) {
- if (Objects.equals(it.displayName, mode)) {
- serviceString = URIEncoder.encodeURI(URIEncoder.encodeURI(it.authName));
- break;
- }
- }
- try {
- //获取queryString
- HttpURLConnection connection = HttpUtils.make("http://123.123.123.123", "GET");
- connection.setInstanceFollowRedirects(false);
- connection.connect();
- InputStreamReader reader = new InputStreamReader(connection.getInputStream());
- StringBuilder sb = new StringBuilder();
- int c = reader.read();
- boolean b = false;
- while (c != -1) {
- if (b) {
- sb.append((char) c);
+ String mode = selector.getValue();
+ String serviceString = Arrays.stream(LoginType.values()).filter(it -> Objects.equals(it.displayName, mode)).findFirst().map(it -> URIEncoder.encodeURI(URIEncoder.encodeURI(it.authName))).orElse("");
+ new Thread(() -> {
+ try {
+ //获取queryString
+ HttpURLConnection connection = HttpUtils.make("http://123.123.123.123", "GET");
+ connection.setInstanceFollowRedirects(false);
+ connection.connect();
+ InputStreamReader reader = new InputStreamReader(connection.getInputStream());
+ StringBuilder sb = new StringBuilder();
+ int c = reader.read();
+ boolean b = false;
+ while (c != -1) {
+ if (b) {
+ sb.append((char) c);
+ }
+ if (c == '\'') {
+ b = !b;
+ }
+ c = reader.read();
}
- if (c == '\'') {
- b = !b;
+ sb.deleteCharAt(sb.length() - 1);
+ sb.delete(0, sb.indexOf("?") + 1);
+ //登录
+ HttpURLConnection loginConnection = Authenticator.login(userId, pw, serviceString, URLEncoder.encode(URLEncoder.encode(sb.toString(), Charset.defaultCharset()), Charset.defaultCharset()));
+ if (loginConnection == null) {
+ Platform.runLater(() -> {
+ resultText.setText("Error!");
+ button.setDisable(false);
+ });
+ return;
}
- c = reader.read();
- }
- sb.deleteCharAt(sb.length() - 1);
- sb.delete(0, sb.indexOf("?") + 1);
- //登录
- HttpURLConnection loginConnection = Authenticator.login(userId, pw, serviceString, URLEncoder.encode(URLEncoder.encode(sb.toString(), Charset.defaultCharset()), Charset.defaultCharset()));
- if (loginConnection == null) {
- resultText.setText("Error!");
- button.setDisable(false);
- return;
- }
- //获取结果
- JsonObject resultMessage = JsonParser.parseString(IOUtils.readText(loginConnection.getInputStream())).getAsJsonObject();
- System.out.println(resultMessage.toString());
- String result = resultMessage.get("result").getAsString();
- resultText.setText(resultMessage.get("result").getAsString() + ":" + resultMessage.get("message").getAsString());
- if (result.equals("success")) {
- Authenticator.userIndex = resultMessage.get("userIndex").getAsString();
- Config.save(username, password, (byte) selector.getItems().indexOf(mode));
+ //获取结果
+ JsonObject resultMessage = JsonParser.parseString(IOUtils.readText(loginConnection.getInputStream())).getAsJsonObject();
+ System.out.println(resultMessage.toString());
+ String result = resultMessage.get("result").getAsString();
+ Platform.runLater(() -> resultText.setText(resultMessage.get("result").getAsString() + ":" + resultMessage.get("message").getAsString()));
+ if (result.equals("success")) {
+ Authenticator.userIndex = resultMessage.get("userIndex").getAsString();
+ Config.save(username, password, (byte) selector.getItems().indexOf(mode));
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ Platform.runLater(() -> resultText.setText(e.getMessage()));
}
- } catch (IOException e) {
- e.printStackTrace();
- }
- button.setDisable(false);
+ Platform.runLater(() -> button.setDisable(false));
+ Authenticator.updateStatus();
+ Platform.runLater(this::updateUI);
+ }).start();
}
}
\ No newline at end of file