Explorar o código

feature:修改自动连接校园网的逻辑;

通过调用登陆接口的方式进行校园网登陆,代替模拟点击
yang yi hai 3 meses
pai
achega
75b2479bda

+ 19 - 0
pom.xml

@@ -46,6 +46,25 @@
             <artifactId>slf4j-nop</artifactId>
             <version>1.6.1</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <version>1.18.20</version>
+        </dependency>
+        <!-- https://mvnrepository.com/artifact/cn.afterturn/easypoi-base -->
+        <dependency>
+            <groupId>cn.afterturn</groupId>
+            <artifactId>easypoi-base</artifactId>
+            <version>4.4.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+            <version>2.15.4</version>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 212 - 9
src/main/java/space/anyi/autoConnectNetwork/Main.java

@@ -1,25 +1,48 @@
 package space.anyi.autoConnectNetwork;
 
+import cn.afterturn.easypoi.excel.ExcelImportUtil;
+import cn.afterturn.easypoi.excel.entity.ImportParams;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.firefox.FirefoxDriver;
 import org.openqa.selenium.firefox.FirefoxOptions;
+import space.anyi.autoConnectNetwork.entry.Student;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
 
 /**
  * @ProjectName: autoConnectNetwork
  * @FileName: Main
  * @Author: 杨逸
  * @Data:2024/5/28 8:38
- * @Description: TODO
+ * @Description:
  */
 public class Main {
+    private static final ObjectMapper objectMapper = new ObjectMapper();
     public static void main(String[] args) throws IOException, InterruptedException {
+        f2();
+    }
+
+    /**
+     * 模拟点击的方式连接校园网
+     * @param args
+     * @throws IOException
+     * @throws InterruptedException
+     * @description:
+     * @author: 杨逸
+     * @data:2024/10/16 12:56:21
+     * @since 1.0.0
+     */
+    public static void f1(String[] args) throws IOException, InterruptedException {
         if (args.length!=4) {
             System.out.println("参数错误");
             System.out.println("用户名,密码,firefoxDriver,firefoxPath未设置正确");
@@ -43,6 +66,167 @@ public class Main {
         }
     }
 
+    /**
+     * 调接口连接校园网
+     * @throws IOException
+     * @throws InterruptedException
+     * @description:
+     * @author: 杨逸
+     * @data:2024/10/16 13:18:22
+     * @since 1.0.0
+     */
+    public static void f2() throws IOException, InterruptedException {
+        //初始化校园网信息
+        List<Student> list = new ArrayList<>();
+        ImportParams importParams = new ImportParams();
+        importParams.setTitleRows(1);
+        importParams.setHeadRows(1);
+        list = ExcelImportUtil.importExcel(new File("账号密码.xlsx"), Student.class, importParams);
+        int mod = list.size(),index  = 0;
+
+        while (true){
+            //判断网络是否连接
+            if (!connected()){
+                System.out.println("=======================");
+                System.out.println("无网络连接");
+                System.out.println("index = " + index);
+                //连接网络
+                Student student = list.get(index);
+                String userId = student.getSno().trim();
+                if(userId.charAt(0) == '\'')userId = userId.substring(1);
+                String password = student.getCardId().trim().substring(0,10);
+                if(password.charAt(0) == '\'')password = student.getCardId().trim().substring(1,11);
+                System.out.println("用户名:"+userId);
+                System.out.println("密码:"+password);
+                try {
+                    //连接校园网
+                    connection(userId,password);
+                }catch (IOException e){
+                    e.printStackTrace();
+                    System.err.println("检查网络连接是否已经连接到校园网的WiFi");
+                    break;
+                }
+
+                index++;
+                index%=mod;
+            }
+            Thread.sleep(1000);
+        }
+
+    }
+
+    private static void connection(String userId, String password) throws IOException {
+        String queryString= "nasip%3Dcc5b64e516a1fa61d915e184b913e171";
+        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://10.30.252.71/eportal/index.jsp?nasip=cc5b64e516a1fa61d915e184b913e171").openConnection();
+        //打开首页获取cookie和session
+        String cookie;
+        {
+            httpURLConnection.setRequestMethod("GET");
+            httpURLConnection.connect();
+            //print(httpURLConnection.getInputStream());
+            System.out.println(httpURLConnection.getResponseCode());
+            Map<String, List<String>> headerFields = httpURLConnection.getHeaderFields();
+            cookie = headerFields.get("Set-Cookie").get(0);
+            System.out.println("cookie = " + cookie);
+            httpURLConnection.disconnect();
+        }
+
+        //登陆
+        String userIndex = "";
+        {
+            //电信
+            String service = "",passwordEncrypt = "false";
+            service = "DianXin";
+            httpURLConnection = (HttpURLConnection) new URL("http://10.30.252.71/eportal/InterFace.do?method=login").openConnection();
+            httpURLConnection.setRequestMethod("POST");
+            httpURLConnection.setRequestProperty("Cookie",cookie);
+            httpURLConnection.setDoOutput(true);
+
+            StringBuilder stringBuilder = new StringBuilder("").append("userId=").append(userId).append("&password=").append(password).append("&service=").append(service).append("&queryString=").append(queryString).append("&passwordEncrypt=").append(passwordEncrypt);
+            OutputStream outputStream = httpURLConnection.getOutputStream();
+            outputStream.write(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
+            outputStream.flush();
+            outputStream.close();
+
+            httpURLConnection.connect();
+            System.out.println(httpURLConnection.getResponseCode());
+            String response = print(httpURLConnection.getInputStream());
+
+            String s = response.split(":")[1].split(",")[0];
+            userIndex = s.substring(1,s.length()-1);
+            System.out.println(userIndex);
+            httpURLConnection.disconnect();
+
+            Map map = objectMapper.readValue(response, Map.class);
+            String message = (String) map.get("message");
+            //if ("false::PWD_REGULAR_MODIFY_WEB".equals(message)){
+            //    System.out.println("重置密码");
+            //    return true;
+            //}
+            //联通
+            service = "LianTong";
+            if (userIndex.length()<4){
+                httpURLConnection = (HttpURLConnection) new URL("http://10.30.252.71/eportal/InterFace.do?method=login").openConnection();
+                httpURLConnection.setRequestMethod("POST");
+                httpURLConnection.setRequestProperty("Cookie",cookie);
+                httpURLConnection.setDoOutput(true);
+
+                stringBuilder = new StringBuilder("").append("userId=").append(userId).append("&password=").append(password).append("&service=").append(service).append("&queryString=").append(queryString).append("&passwordEncrypt=").append(passwordEncrypt);
+                outputStream = httpURLConnection.getOutputStream();
+                outputStream.write(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
+                outputStream.flush();
+                outputStream.close();
+
+                httpURLConnection.connect();
+                System.out.println(httpURLConnection.getResponseCode());
+                response = print(httpURLConnection.getInputStream());
+                s = response.split(":")[1].split(",")[0];
+                userIndex = s.substring(1,s.length()-1);
+                System.out.println(userIndex);
+                httpURLConnection.disconnect();
+                map = objectMapper.readValue(response, Map.class);
+                message = (String) map.get("message");
+                //if ("false::PWD_REGULAR_MODIFY_WEB".equals(message)){
+                //    System.out.println("重置密码");
+                //    return true;
+                //}
+            }
+        }
+
+
+        if (userIndex.length()<4)return ;
+        //获取用户信息
+        byte[] userIndexBytes = new String("userIndex=" + userIndex).getBytes();;
+        {
+            httpURLConnection = (HttpURLConnection) new URL("http://10.30.252.71/eportal/InterFace.do?method=getOnlineUserInfo").openConnection();
+            httpURLConnection.setRequestMethod("POST");
+            httpURLConnection.setRequestProperty("Cookie",cookie);
+            httpURLConnection.setDoOutput(true);
+
+            OutputStream outputStream = httpURLConnection.getOutputStream();
+            outputStream.write(userIndexBytes);
+            outputStream.flush();
+            outputStream.close();
+
+            httpURLConnection.connect();
+            System.out.println(httpURLConnection.getResponseCode());
+            print(httpURLConnection.getInputStream());
+            httpURLConnection.disconnect();
+        }
+
+    }
+
+    /**
+     * 连接网络
+     * @param username
+     * @param password
+     * @param firefoxPath
+     * @throws InterruptedException
+     * @description:
+     * @author: 杨逸
+     * @data:2024/10/16 12:56:45
+     * @since 1.0.0
+     */
     private static void connection(String username, String password,String firefoxPath) throws InterruptedException {
         FirefoxDriver firefoxDriver = null;
         try {
@@ -100,7 +284,7 @@ public class Main {
      * @since 1.0.0
      */
     private static boolean connected() throws IOException {
-        return ping("baidu.com",5);
+        return ping("bing.com",5) || ping("baidu.com",5);
     }
 
     /**
@@ -114,14 +298,13 @@ public class Main {
      * @data:2024/05/28 08:49:12
      * @since 1.0.0
      */
-    private static boolean ping(String target_name, int out_time)
-            throws IOException {
+    private static boolean ping(String target_name, int out_time) throws IOException {
 
         Runtime runtime = Runtime.getRuntime();
 
         String ping_command = "ping " + target_name + " -w " + out_time;
 
-        System.out.println("命令格式:" + ping_command);
+        //System.out.println("命令格式:" + ping_command);
 
         Process process = runtime.exec(ping_command);
 
@@ -135,7 +318,7 @@ public class Main {
 
         while (null != (line = bufferedReader.readLine())) {
 
-            System.out.println(line);
+            //System.out.println(line);
 
             if (line.startsWith("bytes from",3))
                 return true;
@@ -148,4 +331,24 @@ public class Main {
         return false;
     }
 
+    /**
+     * 打印输入流
+     * @param inputStream
+     * @return {@code String }
+     * @throws IOException
+     * @description:
+     * @author: 杨逸
+     * @data:2024/10/16 13:13:17
+     * @since 1.0.0
+     */
+    private static String print(InputStream inputStream) throws IOException {
+        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
+        StringBuilder stringBuilder = new StringBuilder();
+        while (bufferedReader.ready()) {
+            stringBuilder.append(bufferedReader.readLine());
+        }
+        System.out.println(stringBuilder.toString());
+        bufferedReader.close();
+        return stringBuilder.toString();
+    }
 }

+ 45 - 0
src/main/java/space/anyi/autoConnectNetwork/entry/Student.java

@@ -0,0 +1,45 @@
+package space.anyi.autoConnectNetwork.entry;
+
+import cn.afterturn.easypoi.excel.annotation.Excel;
+import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @Projectname: webMgaicDemo
+ * @Filename: Student
+ * @Author: 杨逸
+ * @Data:2023/12/19 20:38
+ * @Description:
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+@ExcelTarget("student")
+public class Student {
+    @Excel(name = "姓名",orderNum = "0")
+    private String name;
+    @Excel(name = "性别",orderNum = "1")
+    private String sex;
+    @Excel(name = "联系方式",orderNum = "2")
+    private String phone;
+    @Excel(name = "国籍",orderNum = "3")
+    private String gj;
+    @Excel(name = "身份证号",orderNum = "4")
+    private String cardId;
+    @Excel(name = "学号",orderNum = "5")
+    private String sno;
+    @Excel(name = "学院",orderNum = "6")
+    private String collage;
+    @Excel(name = "紧急联系人",orderNum = "7")
+    private String jjlxr;
+    @Excel(name = "紧急联系人电话",orderNum = "8")
+    private String jjlxrPhone;
+    @Excel(name = "衣服尺码",orderNum = "9")
+    private String clothesSize;
+    @Excel(name = "血型",orderNum = "10")
+    private String bloodType;
+    @Excel(name = "有无医保",orderNum = "11")
+    private String hasYB;
+}