Skip to content

Commit

Permalink
Bug fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
RedDragon0293 committed Apr 14, 2024
1 parent 0502fa3 commit b736153
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 99 deletions.
8 changes: 8 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 27 additions & 13 deletions src/main/java/cn/reddragon/eportal/Authenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 {
Expand Down Expand Up @@ -88,10 +89,6 @@ public static HttpURLConnection logout() {
}
}

public static boolean getOnline() {
return online;
}

public static String userIndex() {
if (userIndex == null) {
updateUserIndex();
Expand All @@ -106,28 +103,39 @@ public static void checkOnline() {
connection.connect();
Map<String, List<String>> 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;
} catch (NoRouteToHostException e) {
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;
Expand All @@ -137,6 +145,14 @@ public static void checkOnline() {
}
}

public static void updateStatus() {
checkOnline();
if (online) {
updateUserIndex();
updateSession();
}
}

public static void updateSession() {
new Thread(() -> {
try {
Expand Down Expand Up @@ -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()) {
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/cn/reddragon/eportal/HelloApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -76,7 +74,7 @@ public void start(Stage stage) throws IOException {
//updateUI();
askThread.start();
Authenticator.checkOnline();
if (!Authenticator.getOnline()) {
if (!Authenticator.online) {
controller.onLoginButtonClick();
}
}
Expand Down
156 changes: 77 additions & 79 deletions src/main/java/cn/reddragon/eportal/HelloController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> selector;
@FXML
public Label resultText;
@FXML
Expand All @@ -39,53 +41,58 @@ 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()) {
if (Objects.equals(it, Authenticator.type)) {
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();
Expand All @@ -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();
}
}

0 comments on commit b736153

Please sign in to comment.