1 package ZClient; 2 import java.io.IOException; 3 import java.net.InetAddress; 4 import java.net.UnknownHostException; 5 import java.util.Date; 6 import java.util.EventListener; 7 import java.util.EventObject; 8 import javax.swing.event.EventListenerList; 9 import jpcap.JpcapCaptor; 10 import jpcap.NetworkInterface; 11 import jpcap.PacketReceiver; 12 import ZClient.EAPSender.EAPNameExcetion; 13 import ZClient.PacketProc.EAPListener; 14 15 /** 16 * Dot1X is the main class of dot1x application<p> 17 * @author Lingle<p> 18 * Email: mea08@126.com<p> 19 */ 20 public class Dot1X{ 21 private static final Dot1X dot1x=new Dot1X(); 22 private static NetworkInterface[] devices = JpcapCaptor.getDeviceList(); 23 public static long LENOVO_KEEP_GAP=60000; 24 private NetworkInterface selectedDevice =null; 25 private final EAPSender eapSender=new EAPSender(); 26 private JpcapCaptor eapWatcher =null; 27 private byte[] ip =null; 28 private byte[] mac =null; 29 private EventListenerList listeners =null; 30 public class OpenDevException extends Exception{ 31 private static final long serialVersionUID = -3485654124088246345L; 32 public OpenDevException() { 33 super("Open Selected Device Error!"); 34 } 35 } 36 public class NoIPException extends Exception{ 37 private static final long serialVersionUID = -1398907348086842693L; 38 public NoIPException() { 39 super("Selected device has no IP!"); 40 } 41 } 42 public class NoDeviceException extends Exception{ 43 private static final long serialVersionUID = -6577467488384162978L; 44 public NoDeviceException() { 45 super("Have no device?"); 46 } 47 } 48 private boolean hasListerner() { 49 if(listeners==null){ 50 return false; 51 }else return true; 52 } 53 54 /** 55 * event define 56 */ 57 public class Dot1XMsgSourceObject { 58 public int msgType; 59 public String message; 60 public Dot1XMsgSourceObject (int type, String msg) { 61 msgType = type; 62 message = new Date().toLocaleString().substring(9, 17)+" "+msg; 63 } 64 } 65 public class Dot1XMsgEvent extends EventObject{ 66 67 private static final long serialVersionUID = 7003505759602936517L; 68 public Dot1XMsgEvent(int msgType, String messsage) { 69 super(new Dot1XMsgSourceObject(msgType,messsage)); 70 } 71 72 /** 73 * @param msgType 0:EAPMessage,1:KeepOnline,2:device,3:debug stderr 74 * @param messsage 75 */ 76 public Dot1XMsgSourceObject getSource() { 77 return (Dot1XMsgSourceObject) super.getSource(); 78 } 79 } 80 public interface Dot1XMsgListener extends EventListener { 81 public void msgUpdateAction(Dot1XMsgEvent dMsgEvent); 82 } 83 public void addDot1XMsgListener(Dot1XMsgListener listener) { 84 if (!hasListerner()) { 85 listeners=new EventListenerList(); 86 } 87 listeners.add(Dot1XMsgListener.class, listener); 88 } 89 /** 90 * @param msgType 0:EAPMessage,1:KeepOnline,2:device,3:debug stderr 91 * @param messsage 92 */ 93 private void postDot1XMessage(int msgType, String messsage) { 94 for (Dot1XMsgListener listener : listeners.getListeners(Dot1XMsgListener.class)) { 95 listener.msgUpdateAction(new Dot1XMsgEvent(msgType, messsage)); 96 } 97 } 98 interface ThreadStopListener extends EventListener { 99 public abstract void threadStopAction(Class<? extends Thread> t); 100 } 101 private void addThreadListener(ThreadStopListener listener){ 102 if (!hasListerner()) { 103 listeners=new EventListenerList(); 104 } 105 listeners.add(ThreadStopListener.class, listener); 106 } 107 private void postThreadStop(Class<? extends Thread> t) { 108 for (ThreadStopListener listener : listeners.getListeners(ThreadStopListener.class)) { 109 listener.threadStopAction(t); 110 } 111 } 112 private void removeThreadListener(ThreadStopListener listener) { 113 if (hasListerner()) listeners.remove(ThreadStopListener.class,listener); 114 } 115 116 private abstract class EAPSuccAdapter implements EAPListener{ 117 @Override 118 public void failureEventAction() {} 119 @Override 120 public void pktRecivedAction(String messsage) {} 121 } 122 private abstract class EAPFailAdapter implements EAPListener{ 123 @Override 124 public void successEventAction() {} 125 @Override 126 public void pktRecivedAction(String messsage) {} 127 } 128 private abstract class EAPMessAdapter implements EAPListener{ 129 @Override 130 public void successEventAction() {} 131 @Override 132 public void failureEventAction() {} 133 } 134 135 /** 136 * thread define 137 */ 138 private class KeepOnlineThread extends Thread implements ThreadStopListener{ 139 public KeepOnlineThread() { 140 setName("KeepOnline.."); 141 setPriority(7); 142 start(); 143 } 144 @Override 145 public synchronized void run() { 146 while (true) { 147 try { 148 Thread.sleep(LENOVO_KEEP_GAP); 149 eapSender.sendEapOnline(); 150 if (hasListerner()) 151 postDot1XMessage(1, "Send EAP_KEEP_ONLINE"); 152 } catch (InterruptedException e) { 153 if (hasListerner()) 154 postDot1XMessage(3, "Error:" + e.getMessage()); 155 } 156 } 157 } 158 @Override 159 public void threadStopAction(Class<? extends Thread> t) { 160 if (t.isInstance(this)) { 161 removeThreadListener(this); 162 stop(); 163 } 164 } 165 } 166 private class EapWatchThread extends Thread implements ThreadStopListener{ 167 public EapWatchThread() { 168 setName("EapWatching.."); 169 setPriority(7); 170 start(); 171 } 172 @Override 173 public synchronized void run() { 174 if (hasEapWatcher()) { 175 try { 176 eapWatcher.setFilter("ether dst " + getMACString() 177 + " and ether proto 0x888e", true); 178 } catch (IOException e) { 179 if (hasListerner()) 180 postDot1XMessage(3, e.getMessage()); 181 } 182 PacketReceiver pktReceiver = new PacketProc(eapSender); 183 ((PacketProc) pktReceiver).addEapListener(new EAPMessAdapter() { 184 public void pktRecivedAction(String messsage) { 185 if (hasListerner()) 186 postDot1XMessage(0, messsage); 187 } 188 }); 189 ((PacketProc) pktReceiver).addEapListener(new EAPSuccAdapter() { 190 @Override 191 public void successEventAction() { 192 postThreadStop(KeepOnlineThread.class); 193 addThreadListener(new KeepOnlineThread()); 194 } 195 }); 196 ((PacketProc) pktReceiver).addEapListener(new EAPFailAdapter() { 197 @Override 198 public void failureEventAction() { 199 postThreadStop(KeepOnlineThread.class); 200 eapWatcher.breakLoop(); 201 } 202 }); 203 eapWatcher.loopPacket(-1, pktReceiver); 204 removeThreadListener(this); 205 } 206 } 207 @Override 208 public void threadStopAction(Class<? extends Thread> t) { 209 if (t.isInstance(this)) { 210 if (hasEapWatcher()) eapWatcher.breakLoop(); 211 } 212 } 213 214 } 215 216 private Dot1X(){} 217 private boolean hasEapWatcher(){ 218 if(eapWatcher==null){ 219 return false; 220 }else return true; 221 } 222 public void setDevice(int devindex) throws NoIPException, NoDeviceException { 223 try {selectedDevice=devices[devindex];} catch (ArrayIndexOutOfBoundsException e1) { 224 throw new NoDeviceException(); 225 } 226 mac=selectedDevice.mac_address; 227 try {ip=selectedDevice.addresses[0].address.getAddress();} catch (ArrayIndexOutOfBoundsException e1) { 228 throw new NoIPException(); 229 } 230 try { 231 eapWatcher = JpcapCaptor.openDevice(selectedDevice, 65535, true, 20); 232 eapSender.setSender(eapWatcher.getJpcapSenderInstance(), ip, mac); 233 } catch (IOException e) {if (hasListerner()) postDot1XMessage(3, "Error:"+e.getMessage()); } 234 } 235 public void setName(String username,String password) throws OpenDevException{ 236 if(hasEapWatcher()){eapSender.setName(username, password);} 237 else throw new OpenDevException(); 238 } 239 public void Start() throws OpenDevException{ 240 if (hasEapWatcher()) { 241 postThreadStop(EapWatchThread.class); 242 postThreadStop(KeepOnlineThread.class); 243 addThreadListener(new EapWatchThread()); 244 try { 245 eapSender.sendEapStart(); 246 if (hasListerner()) 247 postDot1XMessage(0, "Send EAP_START"); 248 } catch (EAPNameExcetion e) { 249 if (hasListerner()) postDot1XMessage(0, e.getMessage()); 250 postThreadStop(EapWatchThread.class); 251 postThreadStop(KeepOnlineThread.class); 252 if (hasEapWatcher()) eapWatcher.breakLoop(); 253 } 254 } else throw new OpenDevException(); 255 } 256 public void Stop() 257 { 258 if (hasEapWatcher()) eapSender.sendEapLogOff(); 259 postThreadStop(EapWatchThread.class); 260 postThreadStop(KeepOnlineThread.class); 261 if (hasEapWatcher()) eapWatcher.breakLoop(); 262 postDot1XMessage(0, "EAP_STOP"); 263 } 264 265 public static String[] getDeviceList() { 266 String[] devs=new String[devices.length]; 267 for (int i=0; i<devices.length; i++) { 268 devs[i]=devices[i].description; 269 } 270 return devs; 271 } 272 public String getMACString() { 273 try { 274 return String.format("%02X:%02X:%02X:%02X:%02X:%02X", mac[0], 275 mac[1], mac[2], mac[3], mac[4], mac[5]); 276 } catch (java.lang.NullPointerException e) { 277 return "00:00:00:00"; 278 } 279 } 280 public String getIPString() { 281 try { 282 return InetAddress.getByAddress(ip).getHostAddress(); 283 } catch (UnknownHostException e) { 284 return "0.0.0.0"; 285 } 286 } 287 public static Dot1X getDot1XInstance() { 288 return dot1x; 289 } 290 291 }
浙公网安备 33010602011771号