shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.walker.security;
 
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
 
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
public class SystemLogMan {
    
//    private final transient Logger logger = LoggerFactory.getLogger(getClass());
    
    private static final SpiderLoader loader = new DefaultSpiderLoader();
    
    private static final Man man = new Man();
    
    private static final SystemLogMan slm = new SystemLogMan();
    
    private static final String LOCALHOST_1 = "127.0.0.1";
    private static final String LOCALHOST_2 = "localhost";
    
    
    private SystemLogMan(){}
    
    public static final SystemLogMan getInstance(){
        return slm;
    }
    
    public final void checkMan(String host){
        // 如果是开发环境,本地测试,不再检查注册
        if(host != null && (host.indexOf(LOCALHOST_1) >= 0 || host.indexOf(LOCALHOST_2) >= 0)){
            return;
        }
        doManCheck();
    }
    
    /**
     * 真正判断授权的方法
     */
    public final void checkMan(){
        doCheckMan2();
    }
    
    private final void doCheckMan2(){
        man.checkMan();
    }
    
    private final void doManCheck(){
        if(man.manStatus){
            // do nothing...
        }
        Object[] result = (Object[])loader.load();
        if(result == null || result.length != 2){
            man.manStatus = false;
            man.getEating();
        }
        // 从用户配置文件中返回协议结果
        int status = (Integer)result[0];
        if(status == 1){
            man.setMan((byte[])result[1]);
            if(!man.manStatus){
                man.getEating();
            }
        } else {
            System.out.println("-------- load sysLogMan failed!");
            man.getEating();
        }
    }
    
    private static class Man{
        
        private boolean manStatus = false;
        
        private LocalSpider spider = new DefaultLocalSpider();
        
        /**
         * 设置协议内容。</p>
         * Mac地址\r\n公匙
         * @param content
         */
        public void setMan(byte[] content){
            doCheckMan(content);
        }
        
        private void doCheckMan(byte[] content){
            if(content == null || content.length <= 0){
                return;
            }
            int size = content.length;
//            System.out.println("读取的文件长度 = " + size);
            
            // 消息内容
            ByteBuffer messageBuffer = ByteBuffer.allocate(1024);
            
            ByteBuffer buffer = ByteBuffer.wrap(content);
            byte temp;
            for(int i=0; i<size; i++){
                temp = buffer.get();
                if(((char)temp) != '\n'){
                    messageBuffer.put(temp);
                } else {
                    break;
                }
            }
            messageBuffer.flip();
            byte[] message = new byte[messageBuffer.remaining()];
            messageBuffer.get(message);
            display("消息体", message);
            
            // 公匙内容
            byte[] publicKeys = new byte[buffer.remaining()];
//            System.out.println("剩余公匙长度,publicKeys: " + publicKeys.length);
            buffer.get(publicKeys);
            display("pk", publicKeys);
            
            try {
                byte[] addressBytes = RSAUtils.decryptByPublicKey(message, new String(publicKeys));
                String source = spider.getMyIdentifier();
                String now = new String(addressBytes);
//                System.out.println("source = " + source + ", now = " + now);
                if(source != null && now != null && source.equals(now)){
//                    System.out.println("=========== ok!");
                    manStatus = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }
        
        private void getEating(){
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println("------> my walker ea logging...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
        private void display(String msg, byte[] bytes){
//            System.out.println(msg + "长度: " + bytes.length);
//            System.out.println(msg + "内容: " + new String(bytes));
        }
        
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // 第二次修改的方法,只有下面的方法是真正的方法。
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        
        private int type = 0;
        private String address;
        private long st = 0;
        private long et = 0;
        
        public void setMan(String[] array){
            doSetMan(array);
        }
        
        private void doSetMan(String[] array){
            if(array == null || array.length != 4){
                throw new IllegalArgumentException();
            }
            type = Integer.parseInt(array[0].toString());
            address = array[1];
            st = Long.parseLong(array[2]);
            et = Long.parseLong(array[3]);
            manStatus = true;
        }
        
        public void checkMan(){
            doCheckLisence();
        }
        
        private void doCheckLisence(){
            if(manStatus){
                // 已经加载过协议就直接读取,判断
            } else {
                // 还没有加载
                Object[] result = (Object[])loader.load();
 
//                for (Object o : result){
//                    System.out.println(o.toString());
//                }
 
                if(result == null || result.length != 2)
                    throw new SecurityRuntimeException();
                if(!result[0].toString().equals("1")){
                    throw new SecurityRuntimeException();
                }
                this.setMan((String[])result[1]);
            }
            if(type == 0){
                // 仅通过ip授权
                doValidateAddress();
                
            } else if(type == 1){
                // 仅通过时间段授权
                doValidateDate();
                
            } else if(type == 2){
                // 两种同时使用
                doValidateAddress();
                doValidateDate();
                
            } else {
                throw new SecurityRuntimeException("unknown authen type: " + type, null);
            }
        }
        
        private boolean doValidateAddress(){
            List<String> addresses = spider.getMyIdentifiers();
//            System.out.println("........... addresses: " + addresses);
            if(addresses != null && addresses.contains(address)){
                // ok
                return true;
            } else {
                throw new SecurityRuntimeException("address error:", null);
            }
        }
        
        private boolean doValidateDate(){
            long _cuTime = System.currentTimeMillis();
            if(_cuTime > st && _cuTime < et){
                return true;
            } else {
                throw new SecurityRuntimeException("date expired:", null);
            }
        }
    }
    
//    public static void main(String[] args){
//        SystemLogMan.getInstance().checkMan();
//    }
}