wifi认证Portal开发系列:portal协议的java封装

Posted 睡猪遇上狼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wifi认证Portal开发系列:portal协议的java封装相关的知识,希望对你有一定的参考价值。

 一、报文封装类

AbstractPortalMsg.java
Portal协议数据报文封装类
  1 package org.yoki.edu.common.protocol.portal.msg;
  2 
  3 import lombok.Data;
  4 import lombok.ToString;
  5 import org.yoki.edu.common.protocol.portal.msg.attr.MsgAttr;
  6 
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 
 10 /**
 11  * Portal协议数据报文封装类
 12  *
 13  * @author Sky$
 14  * @Description: TODO
 15  * @date 2017/10/24$ 19:16$
 16  */
 17 @Data
 18 @ToString
 19 public abstract class AbstractPortalMsg {
 20 
 21     protected int ver;
 22     protected int type;
 23     protected int papChap = 1;
 24     protected int rsvd = 0;
 25     protected int serialNo;
 26     protected int reqId;
 27     protected String userIp;
 28     protected int userPort = 0;
 29     protected int errCode;
 30     protected int attrNum;
 31     /**
 32      * 属性列表
 33      */
 34     protected List<MsgAttr> attrList = new ArrayList<>();
 35 
 36     /**
 37      * 添加属性的方法
 38      *
 39      * @param attr
 40      * @return
 41      */
 42     public List<MsgAttr> addMsgAttr(MsgAttr attr) {
 43         if (null == attrList) {
 44             attrList = new ArrayList<>();
 45         }
 46         attrList.add(attr);
 47         return attrList;
 48     }
 49 
 50 
 51     /**
 52      * 将hander部分的字段转化为16个字节
 53      *
 54      * @return
 55      */
 56     protected byte[] getHander16Bytes() {
 57         byte[] b = new byte[16];
 58         b[0] = (byte) (ver & 0xff);
 59         b[1] = (byte) (type & 0xff);
 60         b[2] = (byte) (papChap & 0xff);
 61         b[3] = (byte) (rsvd & 0xff);
 62         b[4] = (byte) (serialNo >> 8 & 0xff);
 63         b[5] = (byte) (serialNo & 0xff);
 64         b[6] = (byte) (reqId >> 8 & 0xff);
 65         b[7] = (byte) (reqId & 0xff);
 66         byte[] ip = ipv4Address2BinaryArray(userIp);
 67         System.arraycopy(ip, 0, b, 8, 4);
 68         b[12] = (byte) (userPort >> 8 & 0xff);
 69         b[13] = (byte) (userPort & 0xff);
 70         b[14] = (byte) (errCode & 0xff);
 71         b[15] = (byte) (attrNum & 0xff);
 72         return b;
 73     }
 74 
 75     /**
 76      * 将属性列表转化为字节数组
 77      *
 78      * @return
 79      */
 80     protected byte[] getAttrBytes() {
 81         int attrByteNum = 0;
 82         if (attrList != null && !attrList.isEmpty()) {
 83             for (MsgAttr a : attrList) {
 84                 attrByteNum += a.getAttrLen();
 85             }
 86         }
 87         byte[] b = new byte[attrByteNum];
 88         int index = 0;
 89         if (attrList != null && !attrList.isEmpty()) {
 90             for (MsgAttr a : attrList) {
 91                 System.arraycopy(a.getByteValues(), 0, b, index, a.getByteValues().length);
 92                 index += a.getByteValues().length;
 93             }
 94         }
 95         return b;
 96     }
 97 
 98     /**
 99      * 抽象方法:获取该报文类的字节数组<br>
100      * 抽象出该方法是因为移动Portal和华为Portal协议有些区别<br>
101      * 华为Portal有一个MD5加密字段
102      *
103      * @return
104      */
105     public abstract byte[] toByteArray();
106 
107     /**
108      * 抽象方法:通过报文字节数组,解析出各个字段值,并赋值<br>
109      * 抽象出该方法是因为移动Portal和华为Portal协议有些区别<br>
110      * 华为Portal有一个MD5加密字段
111      *
112      * @param input Portal协议报文字节数组
113      */
114     public abstract void parse(byte[] input);
115 
116     /**
117      * 通过报文字节数组,解析出header部分的信息,并赋值
118      *
119      * @param input Portal协议报文字节数组
120      */
121     protected void parseHeader(byte[] input) {
122         this.setVer(input[0]);
123         this.setType(input[1]);
124         this.setPapChap(input[2]);
125         this.setRsvd(input[3]);
126 
127         this.setSerialNo(((0xff & input[4]) << 8) | (0xff & input[5]));
128         this.setReqId(((0xff & input[6]) << 8) | (0xff & input[7]));
129 
130         this.setUserIp((input[8] & 0xff) + "." + (input[9] & 0xff) + "." + (input[10] & 0xff) + "." + (input[11] & 0xff));
131         this.setUserPort(((0xff & input[12]) << 8) | (0xff & input[13]));
132 
133         this.setErrCode(0xff & input[14]);
134         this.setAttrNum(0xff & input[15]);
135     }
136 
137     /**
138      * 通过报文字节数组,解析出attr部分的信息,并赋值
139      *
140      * @param attrBytes attr数组的字节数组
141      * @param attrNum   attr的个数
142      */
143     protected void parseAttr(byte[] attrBytes, int attrNum) {
144         List<MsgAttr> attrList = new ArrayList<MsgAttr>();
145         int count = attrNum;
146         if (count > 0) {
147             int t = 0;
148             for (int i = 0; i < count; i++) {
149                 int attrType = attrBytes[t];
150                 int attrLen = attrBytes[t + 1];
151                 byte[] d = new byte[attrLen - 2];
152                 System.arraycopy(attrBytes, t + 2, d, 0, attrLen - 2);
153                 MsgAttr c = new MsgAttr(attrType, new String(d));
154                 attrList.add(c);
155                 t += c.getAttrLen();
156             }
157         }
158         this.setAttrList(attrList);
159     }
160 
161     /**
162      * IP地址转换工具方法,将IP字符串转换为字节数组
163      *
164      * @param ipAdd
165      * @return
166      */
167     protected byte[] ipv4Address2BinaryArray(String ipAdd) {
168         byte[] binIP = new byte[4];
169         String[] strs = ipAdd.split("\\\\.");
170         for (int i = 0; i < strs.length; i++) {
171             binIP[i] = (byte) Integer.parseInt(strs[i]);
172         }
173         return binIP;
174     }
175 
176 
177 }
View Code

PortalV1Msg.java
移动Portal协议数据报文封装类
 1 package org.yoki.edu.common.protocol.portal.msg;
 2 
 3 import lombok.NoArgsConstructor;
 4 import org.yoki.edu.common.protocol.portal.msg.attr.MsgAttr;
 5 
 6 import java.util.List;
 7 
 8 /**
 9  * 移动Portal协议数据报文封装类
10  *
11  * @author Sky$
12  * @Description: TODO
13  * @date 2017/10/25$ 14:52$
14  */
15 @NoArgsConstructor
16 public class PortalV1Msg extends AbstractPortalMsg {
17 
18     public PortalV1Msg(int ver, int type, int serialNo, int reqId, String userIp, int errCode, int attrNum, List<MsgAttr> attr) {
19         super();
20         this.ver = ver;
21         this.type = type;
22         this.serialNo = serialNo;
23         this.reqId = reqId;
24         this.userIp = userIp;
25         this.errCode = errCode;
26         this.attrNum = attrNum;
27         this.attrList = attr;
28     }
29 
30     public PortalV1Msg(int ver, int type, int papChap, int rsvd, int serialNo, int reqId, String userIp, int userPort,
31                        int errCode, int attrNum, List<MsgAttr> attr) {
32         super();
33         this.ver = ver;
34         this.type = type;
35         this.papChap = papChap;
36         this.rsvd = rsvd;
37         this.serialNo = serialNo;
38         this.reqId = reqId;
39         this.userIp = userIp;
40         this.userPort = userPort;
41         this.errCode = errCode;
42         this.attrNum = attrNum;
43         this.attrList = attr;
44     }
45 
46 
47     @Override
48     public byte[] toByteArray() {
49         byte[] headerBytes = getHander16Bytes();
50         byte[] attrBytes = getAttrBytes();
51         byte[] b = new byte[headerBytes.length + attrBytes.length];
52         if (null != headerBytes && headerBytes.length > 0) {
53             System.arraycopy(headerBytes, 0, b, 0, headerBytes.length);
54         }
55         b[15] = (byte) this.attrList.size();
56         if (null != attrBytes && attrBytes.length > 0) {
57             System.arraycopy(attrBytes, 0, b, 16, attrBytes.length);
58         }
59         return b;
60     }
61 
62 
63     @Override
64     public void parse(byte[] input) {
65 
66         if (null != input && input.length >= 16) {
67             byte[] headerBytes = new byte[16];
68             System.arraycopy(input, 0, headerBytes, 0, headerBytes.length);
69             this.parseHeader(headerBytes);
70 
71             int attrNum = input[15];
72             byte[] attrBytes = new byte[input.length - 16];
73             System.arraycopy(input, 16, attrBytes, 0, attrBytes.length);
74             parseAttr(attrBytes, attrNum);
75         }
76 
77     }
78 
79 }
View Code

MsgAttr.java
Portal数据报文Attr字段封装父类
 1 package org.yoki.edu.common.protocol.portal.msg.attr;
 2 
 3 import lombok.Data;
 4 import lombok.Getter;
 5 import lombok.NoArgsConstructor;
 6 
 7 import java.util.Arrays;
 8 
 9 /**
10  * Portal数据报文Attr字段封装父类
11  *
12  * @author Sky$
13  * @Description: TODO
14  * @date 2017/10/24$ 20:44$
15  */
16 @Data
17 public class MsgAttr {
18 
19     private int attrType;
20     private int attrLen;
21     private String attrValue;
22     private byte[] byteValues;
23 
24     protected MsgAttr() {
25 
26     }
27 
28     public MsgAttr(int attrType, String attrValue) {
29         this.attrType = attrType;
30         this.attrLen = 2 + attrValue.getBytes().length;
31         this.attrValue = attrValue;
32 
33         byteValues = new byte[attrLen];
34         byteValues[0] = (byte) attrType;
35         byteValues[1] = (byte) attrLen;
36         System.arraycopy(attrValue.getBytes(), 0, byteValues, 2, attrValue.getBytes().length);
37     }
38 
39 }
View Code

UserNameMsgAttr.java
 1 package org.yoki.edu.common.protocol.portal.msg.attr;
 2 
 3 /**
 4  * @author Sky$
 5  * @Description: TODO
 6  * @date 2017/10/29$ 15:05$
 7  */
 8 public class UserNameMsgAttr extends MsgAttr {
 9 
10    public  UserNameMsgAttr(String userName){
11         super(0x01 , userName) ;
12    }
13 
14 }
View Code

 

PasswordMsgAttr.java
 1 package org.yoki.edu.common.protocol.portal.msg.attr;
 2 
 3 /**
 4  * @author Sky$
 5  * @Description: TODO
 6  * @date 2017/10/29$ 15:08$
 7  */
 8 public class PasswordMsgAttr extends MsgAttr {
 9 
10     public  PasswordMsgAttr(String password){
11         super(0x02 , password) ;
12     }
13 
14 }
View Code

ChallengeMsgAttr.java
 1 package org.yoki.edu.common.protocol.portal.msg.attr;
 2 
 3 /**
 4  * @author Sky$
 5  * @Description: TODO
 6  * @date 2017/10/29$ 15:09$
 7  */
 8 public class ChallengeMsgAttr extends MsgAttr {
 9 
10     public  ChallengeMsgAttr(String challenge){
11         super(0x03 , challenge) ;
12     }
13 
14 }
View Code

 

ChapPasswordMsgAttr.java
 1 package org.yoki.edu.common.protocol.portal.msg.attr;
 2 
 3 /**
 4  * @author Sky$
 5  * @Description: TODO
 6  * @date 2017/10/29$ 15:09$
 7  */
 8 public class ChapPasswordMsgAttr extends MsgAttr {
 9 
10     public  ChapPasswordMsgAttr(String chapPassword){
11         super(0x04 , chapPassword) ;
12     }
13 
14 }
View Code

 

二、工具类
ChapEncryptUtils.java
Chap加密工具类
 1 package org.yoki.edu.common.protocol.portal.utils;
 2 
 3 import org.yoki.edu.common.utils.encrypt.EncryptUtils;
 4 
 5 /**
 6  * Chap加密
 7  *
 8  * @author Sky$
 9  * @Description: TODO
10  * @date 2017/10/27$ 17:20$
11  */
12 public class ChapEncryptUtils {
13 
14     public static byte[] encryptChap(int reqId, String challenge, String pwd) {
15         byte[] chapPwd = encryptChap(reqId, challenge.getBytes(), pwd.getBytes());
16         return chapPwd;
17     }
18 
19     public static byte[] encryptChap(int reqId, byte[] challenge, byte[] pwd) {
20         /**
21          * Chap_Password(Chap密码)的生成:
22          * Chap_Password的生成遵循标准的Radious协议中的Chap_Password 生成方法(参见RFC2865)。
23          * 密码加密使用MD5算法,MD5函数的输入为ChapID + Password +Challenge  (reqId有AC生成, ChapID是ReqID的低8位)
24          * 其中,ChapID取reqId的低 8 位,Password的长度不够协议规定的最大长度,其后不需要补零。
25          * Chap_Password = MD5 (ChapID+ Password + Challenge )
26          */
27         byte[] buf = new byte[1 + pwd.length + challenge.length];
28         buf[0] = (byte) (reqId & 0xff);
29         System.arraycopy(pwd, 0, buf, 1, pwd.length);
30         System.arraycopy(challenge, 0, buf, 1 + pwd.length, challenge.length);
31         byte[] chapPwd = EncryptUtils.encryptMD5Bytes(buf);
32         return chapPwd;
33     }
34 

以上是关于wifi认证Portal开发系列:portal协议的java封装的主要内容,如果未能解决你的问题,请参考以下文章

wifi认证Portal开发系列:Radius与FreeRadius简介

CMCC portal 协议wireshark 抓包分析

CMCC portal 协议wireshark 抓包分析

CMCC portal 协议wireshark 抓包分析

portal认证系统怎么放没网

无线热点登陆认证原理探究---captive portal 什么是Captive Portal