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
package com.walker.security;
 
import com.walker.security.util.ClassPathResource;
 
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
 
public class DefaultSpiderLoader implements SpiderLoader {
 
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // update code 
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private GenerateCoder generateCoder = new GenerateCoder();
    
    // 未用到的变量,可以删除
    String[] lisence = null;
        
//    @Override
    public Object load() {
        return doLoad();
    }
    
    private Object[] doLoad(){
        Object[] result = new Object[2];
        InputStream inputStream = getFile();
        if(inputStream == null){
            result[0] = STATE_FAILED;
            return result;
        }
        BufferedInputStream bis = new BufferedInputStream(inputStream);
        try{
            int size = bis.available();
            byte[] contentBytes = new byte[size];
            bis.read(contentBytes);
            
//            String content = new String(contentBytes);
//            System.out.println(content);
            result[0] = STATE_OK;
            result[1] = contentBytes;
            
            // 真正的处理逻辑
            return generateCoder.readLisenceFile();
            
        } catch(IOException ex){
//            throw new SecurityRuntimeException("read file error: ", ex);
            ex.printStackTrace();
            result[0] = STATE_FAILED;
            return result;
        } finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {}
            }
        }
    }
 
    private InputStream getFile(){
//        InputStream input = this.getClass().getResourceAsStream("/resource/walker_sn_walkersoft.bin");
//        /resources/wk_sn_lic.bin
//        InputStream input = this.getClass().getResourceAsStream("wk_sn_lic.bin");
//        System.out.println("get file = " + input);
//        return input;
 
//        ClassPathResource file = new ClassPathResource("com.walker.security.my_walker_log.bin");
        ClassPathResource file = new ClassPathResource("wk_sn_lic.bin");
        if(file.exists()){
            try {
                return file.getInputStream();
            } catch (IOException e) {
                throw new SecurityRuntimeException("wk_sn_lic.bin not found!");
            }
        }
        return null;
    }
    
    private static final int STATE_OK = 1;
    private static final int STATE_FAILED = 0;
    
    private class GenerateCoder{
        private String pk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCL4fwI6s3zdK0RIDQ0o9xQ7Lq4CoETtOKbDRSCwdCeQ5PrWys4tlenCGOds5SAkOrXFt7wOt0Z33PlAMXpLxAq1gDp/vlOLPPxTfm7S6mUhGNrYOv/0FXQ3rWa7G/UZaXlybn7WLcr2yFaJbwSxxKWa/WuNc1wyCii1S854ZvGBQIDAQAB";
        /**
         * 返回读取的协议文件内容。
         * @return 数组:[0]=状态码,[1]=属性集合string[]
         */
        public Object[] readLisenceFile(){
            Object[] result = new Object[2];
            InputStream inputStream = getFile();
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            try{
                int size = bis.available();
                byte[] contentBytes = new byte[size];
                bis.read(contentBytes);
                
                // 解密文件字节内容
                byte[] fileContent = RSAUtils.decryptByPublicKey(contentBytes, pk);
                String content = new String(fileContent);
//                System.out.println(content);
                if(content == null || content.equals("")){
                    throw new IllegalArgumentException("not found lisence content!");
                }
                
                String[] props = content.split("\r\n");
                result[0] = STATE_OK;
                result[1] = props;
                lisence = props;
                return result;
                
            } catch(Exception ex){
//                throw new SecurityRuntimeException("read file error: ", ex);
                ex.printStackTrace();
                result[0] = STATE_FAILED;
                return result;
            } finally {
                if(inputStream != null){
                    try {
                        inputStream.close();
                    } catch (IOException e) {}
                }
            }
        }
    }
    
//    public static void main(String[] args){
//        SpiderLoader loader = new DefaultSpiderLoader();
//        Object result = loader.load();
//        Object[] rs = (Object[])result;
//        if(rs != null){
//            System.out.println("status: " + rs[0]);
//            String[] props = (String[])rs[1];
//            if(props != null){
//                for(String s : props){
//                    System.out.println(s);
//                }
//            }
//        }
//    }
}