校园网自动登录模块

This commit is contained in:
筱锋xiao_lfeng 2023-10-31 09:21:26 +08:00
parent 3c63b88563
commit c6c72f6e4d
9 changed files with 291 additions and 15 deletions

10
pom.xml
View File

@ -30,6 +30,16 @@
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version> <!-- 使用最新版本 -->
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -2,12 +2,14 @@ package com.xiaolfeng.dormstar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author 筱锋xiao_lfeng
* @version v1.0.0
*/
@SpringBootApplication
@EnableScheduling
public class DormStarApplication {
public static void main(String[] args) {
SpringApplication.run(DormStarApplication.class, args);

View File

@ -1,6 +1,7 @@
package com.xiaolfeng.dormstar.controllers.view;
import com.xiaolfeng.dormstar.cache.RamDataCache;
import com.xiaolfeng.dormstar.services.GetWxxyNetworkInfo;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Controller;
@ -16,6 +17,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
@RequiredArgsConstructor
public class IndexController {
private final RamDataCache ramDataCache;
private final GetWxxyNetworkInfo getWxxyNetworkInfo;
@GetMapping("/")
public String getIndex(@NotNull Model model) {
@ -27,6 +29,8 @@ public class IndexController {
public String getCenter(@NotNull Model model) {
model.addAttribute("title", ramDataCache.name);
model.addAttribute("version", ramDataCache.version);
getWxxyNetworkInfo.getWxxyNetWork(model);
model.addAttribute("build");
return "center";
}

View File

@ -0,0 +1,23 @@
package com.xiaolfeng.dormstar.entities;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
/**
* 绑定信息
*
* @author 筱锋xiao_lfeng
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class DrcomEntity {
@JsonProperty("time")
private int time;
@JsonProperty("v46ip")
private String ipv4;
@JsonProperty("ss6")
private String loginIp;
@JsonProperty("uid")
private String uid;
}

View File

@ -0,0 +1,19 @@
package com.xiaolfeng.dormstar.entities;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
/**
* 登录信息
*
* @author 筱锋xiao_lfeng
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class DrcomLoginEntity {
@JsonProperty("result")
private int result;
@JsonProperty("msg")
private String message;
}

View File

@ -0,0 +1,157 @@
package com.xiaolfeng.dormstar.services;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaolfeng.dormstar.entities.DrcomEntity;
import com.xiaolfeng.dormstar.entities.DrcomLoginEntity;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.time.LocalTime;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 筱锋xiao_lfeng
* @version v1.0.0
*/
@Component
public class DayScheduleService {
private final Request login;
private final Request loginBody;
public DayScheduleService() {
login = new Request.Builder()
.url("http://10.1.99.100:801/eportal/portal/login?callback=dr1003&login_method=1&user_account=,0,22344233@cmcc&user_password=061823zcw&jsVersion=4.1.3&terminal_type=1&lang=zh-cn&v=8101&lang=zh")
.build();
loginBody = new Request.Builder()
.url("http://10.1.99.100/drcom/chkstatus?callback=dr1002&jsVersion=4.X&v=1117&lang=zh")
.build();
}
@Scheduled(cron = "0 0 7 * * ?")
public void morningAutoLogin() {
OkHttpClient client = new OkHttpClient();
// 检查是否可以访问内网
try (Response ignored = client.newCall(loginBody).execute()) {
// 处理登录
try (Response responseLogin = client.newCall(login).execute()) {
if (responseLogin.body() != null) {
String getData = responseLogin.body().string();
Matcher matcher = Pattern.compile("dr1003\\(([^)]+)\\)").matcher(getData);
String data = null;
if (matcher.find()) {
data = matcher.group(1);
}
ObjectMapper objectMapper = new ObjectMapper();
DrcomLoginEntity drocm = objectMapper.readValue(data, DrcomLoginEntity.class);
System.out.println("[INFO] " + drocm.getMessage());
}
} catch (IOException ignoredSecond) {
System.out.println("[WARNING] 无法登录");
}
} catch (IOException ignoredFirst) {
System.out.println("[WARNING] 不处于校园网内,请手动管理网络");
}
}
@Scheduled(fixedDelay = 60000)
public void executePeriodicTask() {
LocalTime localTime = LocalTime.now();
if (localTime.isAfter(LocalTime.of(7, 0)) && localTime.isBefore(LocalTime.of(23, 0))) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://suggestion.baidu.com/su?wd=201%E4%BA%AC%E6%B5%B7%E5%B8%82%E5%AE%BF%E8%88%8D")
.build();
try (Response response = client.newCall(request).execute()) {
// 检查是否可以获取外网内容
if (response.body() != null) {
System.out.println("[INFO] 已登录");
}
} catch (IOException ignoreFirst) {
// 检查是否可以访问内网
try (Response ignored = client.newCall(loginBody).execute()) {
// 处理登录
try (Response responseLogin = client.newCall(login).execute()) {
if (responseLogin.body() != null) {
String getData = responseLogin.body().string();
Matcher matcher = Pattern.compile("dr1003\\(([^)]+)\\)").matcher(getData);
String data = null;
if (matcher.find()) {
data = matcher.group(1);
}
ObjectMapper objectMapper = new ObjectMapper();
DrcomLoginEntity drocm = objectMapper.readValue(data, DrcomLoginEntity.class);
System.out.println("[INFO] " + drocm.getMessage());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (IOException ignoreSecond) {
System.out.println("[WARNING] 不处于校园网内,请手动管理网络");
}
}
}
}
@Scheduled(cron = "0 0 23 * * 0-4")
@Scheduled(cron = "0 30 23 * * 5-6")
public void theDayChangeNetwork() {
OkHttpClient client = new OkHttpClient();
// 获取当前登录信息
try (Response response = client.newCall(loginBody).execute()) {
// 处理信息
if (response.body() != null) {
// 获取数据
String getData = response.body().string();
Matcher matcher = Pattern.compile("dr1002\\(([^)]+)\\)").matcher(getData);
String data = null;
if (matcher.find()) {
data = matcher.group(1);
}
DrcomEntity drcom = new ObjectMapper().readValue(data, DrcomEntity.class);
Matcher getUser = Pattern.compile("^[0-9]+").matcher(drcom.getUid());
String user = getUser.group(1);
// 解除当前登录
Request removeLogin = new Request.Builder()
.url("http://10.1.99.100:801/eportal/portal/mac/unbind?callback=dr1002&user_account=" + user + "&jsVersion=4.1.3&v=1721&lang=zh")
.build();
try (Response responseRemoveLogin = client.newCall(removeLogin).execute()) {
if (responseRemoveLogin.body() != null) {
// 处理无锡学院登录
Request loginDefault = new Request.Builder()
.url("http://10.1.99.100:801/eportal/portal/login?callback=dr1003&login_method=1&user_account=,0,22344233&user_password=061823zcw&jsVersion=4.1.3&terminal_type=1&lang=zh-cn&v=6795&lang=zh")
.build();
try (Response responseDefault = client.newCall(loginDefault).execute()) {
if (responseDefault.body() != null) {
String getDataDefault = responseDefault.body().string();
Matcher matcherDefault = Pattern.compile("dr1003\\(([^)]+)\\)").matcher(getDataDefault);
String dataDefault = null;
if (matcher.find()) {
dataDefault = matcherDefault.group(1);
}
ObjectMapper objectMapper = new ObjectMapper();
DrcomLoginEntity drocm = objectMapper.readValue(dataDefault, DrcomLoginEntity.class);
System.out.println("[INFO] " + drocm.getMessage());
}
} catch (IOException ignored) {
System.out.println("[WARNING] 不处于校园网内,请手动管理网络");
}
} else {
System.out.println("[ERROR] 对象服务器异常");
}
} catch (IOException ignored) {
System.out.println("[WARNING] 不处于校园网内,请手动管理网络");
}
} else {
System.out.println("[ERROR] 对象服务器异常");
}
} catch (IOException ignored) {
System.out.println("[WARNING] 不处于校园网内,请手动管理网络");
}
}
}

View File

@ -1,6 +1,17 @@
package com.xiaolfeng.dormstar.services;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaolfeng.dormstar.entities.DrcomEntity;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 筱锋xiao_lfeng
@ -8,7 +19,52 @@ import org.springframework.stereotype.Service;
@Service
public class GetWxxyNetworkInfo {
public String getWxxyNetWork() {
return null;
public void getWxxyNetWork(Model model) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://10.1.99.100/drcom/chkstatus?callback=dr1002&jsVersion=4.X&v=1117&lang=zh")
.build();
try (Response response = client.newCall(request).execute()) {
if (response.body() != null) {
String getData = response.body().string();
Matcher matcher = Pattern.compile("dr1002\\(([^)]+)\\)").matcher(getData);
String dr1002 = null;
if (matcher.find()) {
dr1002 = matcher.group(1);
}
ObjectMapper objectMapper = new ObjectMapper();
DrcomEntity drocm = objectMapper.readValue(dr1002, DrcomEntity.class);
model.addAttribute("getIpv4", drocm.getIpv4());
model.addAttribute("getLoginIp", drocm.getLoginIp());
model.addAttribute("getUid", drocm.getUid());
if (drocm.getUid() != null) {
Matcher getStudentNumber = Pattern.compile("^[0-9]+").matcher(drocm.getUid());
Matcher getServiceProvider = Pattern.compile("[A-Za-z]+$").matcher(drocm.getUid());
if (getStudentNumber.find()) {
model.addAttribute("getStudentNumber", getStudentNumber.group(0));
}
if (getServiceProvider.find()) {
if ("cmcc".equals(getServiceProvider.group(0))) {
model.addAttribute("getServiceProvider", "移动节点");
} else if ("telecom".equals(getServiceProvider.group(0))) {
model.addAttribute("getServiceProvider", "电信节点");
} else if ("uniom".equals(getServiceProvider.group(0))) {
model.addAttribute("getServiceProvider", "联通节点");
} else {
model.addAttribute("getServiceProvider", "无锡学院");
}
} else {
model.addAttribute("getServiceProvider", "未知");
}
model.addAttribute("getLogin", "已登录");
} else {
model.addAttribute("getLogin", "未登录");
System.out.println("未登录");
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -1,5 +1,5 @@
server:
port: 8080
port: 8000
mybatis:
configuration:
map-underscore-to-camel-case: true

View File

@ -25,7 +25,7 @@
<div class="flex items-center">
<div class="flex-shrink-0">
<img alt="Your Company" class="h-8 w-8"
src="https://tailwindui.com/img/logos/mark.svg?color=indigo&shade=500">
src="">
</div>
<div class="ml-10 flex space-x-4 w-full">
<a aria-current="page" class="bg-gray-900 text-white rounded-md px-3 py-2 text-sm font-medium"
@ -40,7 +40,7 @@
<span class="sr-only">Open user menu</span>
<img alt=""
class="h-8 w-8 rounded-full"
src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80">
src="">
</div>
</div>
</div>
@ -52,16 +52,21 @@
</div>
</header>
<main>
<div class="mx-auto max-w-7xl py-6 px-6 lg:px-8 grid grid-cols-5">
<div class="grid-cols-1">
<a class="block p-6 bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700"
href="#">
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">Noteworthy
technology
acquisitions 2021</h5>
<p class="font-normal text-gray-700 dark:text-gray-400">Here are the biggest enterprise technology
acquisitions of 2021 so far, in reverse chronological order.</p>
</a>
<div class="mx-auto max-w-7xl py-6 px-6 lg:px-8 grid grid-cols-5 gap-4">
<div class="col-span-2">
<div class="block p-6 bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700">
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">宿舍网络状态</h5>
<div>获取IP地址<span class="font-bold" th:text="${getIpv4}"></span></div>
<div>是否登录:<span class="font-bold" th:text="${getLogin}"></span></div>
<div>登陆账号:<span class="font-bold" th:text="${getStudentNumber}"></span></div>
<div>节点信息:<span class="font-bold" th:text="${getServiceProvider}"></span></div>
</div>
</div>
<div class="col-span-3">
<div class="block p-6 bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700">
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">登录日志</h5>
</div>
</div>
</div>
</main>