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
package com.walker.security;
 
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
 
public class DefaultLocalSpider implements LocalSpider {
 
    private static final String host = "*.*.*.*";
    private List<String> identifiers = null;
    
//    @Override
    public String getMyIdentifier() {
        List<String> values = getMacAddresses();
        if(values == null || values.size() == 0){
            throw new Error("not found spider my identifier!");
        }
        return values.get(0);
    }
 
//    @Override
    public List<String> getMyIdentifiers() {
        return doGetMyIdentifiers();
    }
    
    private List<String> doGetMyIdentifiers(){
        if(this.identifiers == null){
            identifiers = getMacAddresses();
        }
        if(identifiers == null || identifiers.equals(""))
            throw new IllegalArgumentException();
        return identifiers;
    }
 
//    private List<String> getIpAddresses(){
//        Enumeration<NetworkInterface> el;
//        List<String> addresses = new ArrayList<String>();
//        StringBuilder sb = new StringBuilder();
//        try {
//            el = NetworkInterface.getNetworkInterfaces();
//            NetworkInterface ni = null;
//            while (el.hasMoreElements()) {
//                ni = el.nextElement();
////                if(ni.isLoopback()) continue;
//                if(ni.isVirtual()) continue;
//                if(!ni.isUp()) continue;
////                System.out.println("---- 网卡: " + ni.getDisplayName() + ", isLive: " + ni.isUp() + ", isVirtual = " + ni.isVirtual() + ", isLoop = " + ni.isLoopback());
//                byte[] mac = ni.getHardwareAddress();
//                if(mac != null && mac.length != 0){
//                    sb.delete(0, sb.length());
//                    String hexString = null;
//                    for(byte b : mac){
//                        hexString = Integer.toHexString(b & 0xFF);
//                        sb.append((hexString.length() == 1) ? "0" + hexString : hexString);
//                    }
//                    addresses.add(sb.toString());
//                }
//            }
//            System.out.println(addresses);
//
//        } catch (SocketException e1) {
//            e1.printStackTrace();
//            throw new Error();
//        }
//        return addresses;
//    }
    
    /**
     * 返回可用的网卡Mac地址集合
     * @return
     */
    private List<String> getMacAddresses(){
        Enumeration<NetworkInterface> el;
        List<String> addresses = new ArrayList<String>();
        StringBuffer sb = new StringBuffer();
        try {
            el = NetworkInterface.getNetworkInterfaces();
            NetworkInterface ni = null;
            while (el.hasMoreElements()) {
                ni = el.nextElement();
                if(ni.isLoopback()) continue;
                if(ni.isVirtual()) continue;
                if(!ni.isUp()) continue;
//                System.out.println("---- 网卡: " + ni.getDisplayName() + ", isLive: " + ni.isUp() + ", isVirtual = " + ni.isVirtual() + ", isLoop = " + ni.isLoopback());
                byte[] mac = ni.getHardwareAddress();
                if(mac != null && mac.length != 0){
                    sb.delete(0, sb.length());
                    String hexString = null;
                    for(byte b : mac){
                        hexString = Integer.toHexString(b & 0xFF);
                        sb.append((hexString.length() == 1) ? "0" + hexString : hexString);
                    }
                    addresses.add(sb.toString());
                }
            }
            System.out.println(addresses);
 
        } catch (SocketException e1) {
            e1.printStackTrace();
            throw new Error();
        }
        return addresses;
    }
    
    String getMacAddress(){
        String mac = "";  
        StringBuffer sb = new StringBuffer();
        try {
            NetworkInterface el = NetworkInterface.getByInetAddress(InetAddress.getByName(host));
            byte[] macs = el.getHardwareAddress();
            for(int i=0; i<macs.length; i++)  {  
                mac = Integer.toHexString(macs[i] & 0xFF);   
                if(mac.length() == 1){   
                    mac = '0' + mac;   
                }   
                sb.append(mac + "-");
            } 
        } catch (SocketException e1) {
            e1.printStackTrace();
            throw new Error();
        } catch(UnknownHostException ex){
            ex.printStackTrace();
            throw new Error();
        }
        mac = sb.toString();  
        mac = mac.substring(0, mac.length()-1);  
        return mac;
    }
    
//    private String hexByte(byte b) {
//        String s = "000000" + Integer.toHexString(b);
//        return s.substring(s.length() - 2);
//    }
    
//    public static void main(String[] args){
//        LocalSpider spider = new DefaultLocalSpider();
//        spider.getMyIdentifier();
//    }
}