jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.ld.igds.protocol.gps.analysis;
 
import com.ld.igds.io.notify.NotifyGpsMacService;
import com.ld.igds.oa.dto.GpsData;
import com.ld.igds.protocol.gps.Gps808ServerEngine;
import com.ld.igds.protocol.utl.GpsUtils;
import com.ld.igds.util.BytesUtil;
import com.ld.io.api.InvokeResult;
import com.ld.io.api.IoSession;
 
import lombok.extern.slf4j.Slf4j;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
/**
 * GPS-808协议鉴权
 */
@Slf4j
@Component(AnalysisAuthInfo.BEAN_ID)
public class AnalysisAuthInfo {
 
    public static final String BEAN_ID = "gps.analysisAuthInfo";
 
    @Autowired
    private NotifyGpsMacService notifyGpsMacService;
 
    /**
     * 7E 01 02 00 06 01 38 20 03 63 16 01 82 01 38 20 03 63 16 86 7E   (21)
     * 7E
     * 0102  消息id
     * 0006  消息体属性
     * 01 38 20 03 63 16  手机卡号
     * 01 82 消息流水号
     * 01 38 20 03 63 16  鉴权码
     * 86 检验码
     * 7E
     * 鉴权通过则回复通用应答
     * @param ip
     * @param port
     * @param message
     */
    public void analysisAuthInfo(IoSession ioSession, String ip, Integer port, String message) {
        //01 02 00 06 01 38 20 03 63 16 01 82 01 38 20 03 63 16 86
        String sn = message.substring(24, 36);
        String tempPhone = message.substring(8, 20);
        GpsData data = new GpsData();
        data.setPhone(tempPhone);
        data.setIp(ip);
        data.setPort(port);
        data.setSn(sn);
        boolean flag = notifyGpsMacService.authSn(data);
        if(flag){
            //鉴权通过,将此次鉴权的ip和port信息更新到缓存和数据库
            notifyGpsMacService.registerAndAuthNotify(data);
            //鉴权应答
            authResponse(ip, port, message);
        }else {
            //不通过则直接断开连接
            log.debug("鉴权不通过");
            ioSession.destroy();
        }
    }
    /**
     * 鉴权应答(通用应答)
     *
     * @param ip
     * @param port
     * @param message
     */
    private static void authResponse(String ip, Integer port, String message) {
        //01 02 00 06 01 38 20 03 63 16 01 82 01 38 20 03 63 16 86
        //应答:7E 80 01 00 05 01 38 20 03 63 16 01 AE 01 AE 01 02 00 E4 7E
        String msg = "";
        //消息头信息(消息ID+消息体属性+手机卡号+鉴权消息流水号)
        msg += GpsUtils.MSG_22;
        msg += "0005";
        msg += message.substring(8, 20);
        msg += message.substring(20, 24);
 
        //消息体信息(注册消息流水号+鉴权消息ID+消息成功应答)
        msg += message.substring(20, 24);
        msg += message.substring(0, 4);
        msg += "00";
 
        //获取校验码
        String checkCode = GpsUtils.checkCode(msg);
        msg += checkCode;
 
        //对消息整体转义:7D-->7D01,7E-->7D02
        msg = GpsUtils.escapeCode1(msg);
 
        //首尾添加标识符
        msg = GpsUtils.addIdentifier(msg);
 
        //鉴权应答
        byte[] mess = BytesUtil.hexStrToBytes(msg);
        InvokeResult result = Gps808ServerEngine.push(ip, port, mess);
        log.debug("鉴权通过,发送应答结果:{}", result.toString());
    }
}