Вы находитесь на странице: 1из 9

import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.security.MessageDigest; import java.util.Collection; import java.util.HashMap; import java.util.

Iterator; import java.util.Vector;

/* * To change this template, choose Tools | Templates * and open the template in the editor. */

/** * * @author bala */ public class ServerComm extends Thread {

DatagramSocket welcomeSocket;

int listenport = 7000;

String keyatthisperiod = "-1";

HashMap ipaddrtoallow = new HashMap();

static ServerComm instance;

PrivilagedAccessVerfier priaccessverfier;

private ServerComm() {

} public void setVerfier(PrivilagedAccessVerfier v) { priaccessverfier = v;

} public static ServerComm getInstance() { if (instance == null) { instance = new ServerComm();

instance.start();

} return instance;

void printAllowedIPTable() { System.out.println("**********Allowed IP ******************");

Collection c = ipaddrtoallow.values();

Iterator it = c.iterator();

while(it.hasNext()) { System.out.println((String)it.next());

System.out.println("****************************************");

} boolean verifyIfCanAllow(String ip) {

String ipadd = (String)ipaddrtoallow.get(ip);

if (ipadd == null) { System.out.println(" Ip address not in the allowed list so rejecting");

printAllowedIPTable();

return false;

//String cmt = computeMac(ip);

//if(text.equals(cmt)) //{ return true;

//}

//return true;

public String computeMac(String ipaddress) { try {

MessageDigest md = MessageDigest.getInstance("SHA1");

String fullmsg = keyatthisperiod;

fullmsg = fullmsg + ipaddress;

md.update(fullmsg.getBytes());

byte[] output = md.digest();

System.out.println(" "+bytesToHex(output));

InetAddress address = InetAddress.getByName(ipaddress);

byte[] bytes = address.getAddress();

bytes[3] = output[0];

InetAddress outadd = InetAddress.getByAddress(bytes);

String result = outadd.getHostAddress();

System.out.println("The output result is " + result);

return result;

} catch(Exception e) { e.printStackTrace(); }

return null; }

public static String bytesToHex(byte[] b) { char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; StringBuffer buf = new StringBuffer(); for (int j=0; j<b.length; j++) { buf.append(hexDigit[(b[j] >> 4) & 0x0f]); buf.append(hexDigit[b[j] & 0x0f]); } return buf.toString(); }

public void run() { try { welcomeSocket = new DatagramSocket(listenport);

byte[] receiveData = new byte[2048];

while (true) {

DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

welcomeSocket.receive(receivePacket); String sentence = new String( receivePacket.getData()); System.out.println("!!!!recieved packet");

if (sentence.startsWith("REFIP#")) { String [] t = sentence.split("#");

ipaddrtoallow.put(t[1],t[1]);

printAllowedIPTable();

GUI.getInstance().writetologinred("Recieved request for ip to add in allow list " + t[1]);

} else if(sentence.startsWith("KEY#")) {

String [] t = sentence.split("#");

keyatthisperiod = t[1];

priaccessverfier.setKey(Integer.parseInt(keyatthisperiod));

GUI.getInstance().writetologinred(" Recived the Key at this time period " + keyatthisperiod);

} catch(Exception e) { e.printStackTrace(); }

Вам также может понравиться