Java tutorial
package com.dc.tes; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.security.SecureRandom; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Timer; import java.util.TimerTask; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.SystemUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; import sun.misc.BASE64Decoder; import com.dc.tes.channel.IAdapterChannel; import com.dc.tes.channel.IChannel; import com.dc.tes.channel.IListenerChannel; import com.dc.tes.exception.LicenseException; import com.dc.tes.util.RuntimeUtils; import com.dc.tes.util.XmlUtils; import com.dc.tes.net.*; /** * License? * * @author lijic * */ class License { /** * ADAPTER ??? */ private static final int C_ADAPTER_LIST_SIZE = 128; private static String Sys_Name; private static final Log log = LogFactory.getLog(License.class); private static String LICENSE_SERVER_IP; private static int LICENSE_SERVER_PORT; private static int CORE_PORT; /** * License * * @param core * * @return License?? */ static String CheckLicense(Core core) { String license = RuntimeUtils.ReadResource("license.dat", RuntimeUtils.utf8); return Lisence(core, license); } static String CheckLicense_withServer(Core core) { Document doc = XmlUtils.LoadXml(RuntimeUtils.OpenResource("base.xml")); LICENSE_SERVER_IP = XmlUtils.SelectNodeText(doc, "//config/LICENSE_SERVER_IP"); LICENSE_SERVER_PORT = Integer.parseInt(XmlUtils.SelectNodeText(doc, "//config/LICENSE_SERVER_PORT")); CORE_PORT = Integer.parseInt(XmlUtils.SelectNodeText(doc, "//config/port")); Sys_Name = core.instanceName; Message request = new Message(MessageType.LICENSE); request.put(MessageItem.LICENSE.SIGN, 0); request.put(MessageItem.LICENSE.PORT, CORE_PORT); request.put(MessageItem.LICENSE.SYSNAME, core.instanceName); System.out.println("?" + request); String license = LicenseServerConnect(request); // license = LicenseServerConnect(request);/ HeadBeat(); return Lisence(core, license); } private static String LicenseServerConnect(Message request) { String license = ""; Socket client = null; try { client = new Socket(LICENSE_SERVER_IP, LICENSE_SERVER_PORT); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); license = "Lisence?"; log.error(license); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); license = "Lisence?"; log.error(license); } try { client.getOutputStream().write(request.Export()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); license = "?Lisence??"; log.error(license); } Message response = null; try { response = new Message(client.getInputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); license = "?Lisence??"; log.error(license); } if (response.getInteger(MessageItem.LICENSE.AVAILABLE) == 0) { license = "License???" + response.getString(MessageItem.LICENSE.MSG); log.error(license); } if (request.getInteger(MessageItem.LICENSE.SIGN) == 0) { license = response.getString(MessageItem.LICENSE.LICENSEFILE); } return license; } private static void HeadBeat() { new Timer(true).schedule(new TimerTask() { @Override public void run() { Message request = new Message(MessageType.LICENSE); request.put(MessageItem.LICENSE.SIGN, 1); request.put(MessageItem.LICENSE.PORT, CORE_PORT); request.put(MessageItem.LICENSE.SYSNAME, Sys_Name); System.out.println("" + request); String license = LicenseServerConnect(request); if (license.length() > 0) { log.error("lisence?,"); System.exit(-1); } } }, 500, 60 * 1000); } private static String Lisence(Core core, String license) { // ?? boolean[] adapterFlag = new boolean[C_ADAPTER_LIST_SIZE]; // ? Date[] adapterDate = new Date[C_ADAPTER_LIST_SIZE]; // Date tesDate; // ?? int adapterNum; try { // ?License //String license = RuntimeUtils.ReadResource("license.dat", RuntimeUtils.utf8); byte[] _enData = new BASE64Decoder().decodeBuffer(license); byte[] _buffer = new byte[_enData.length - 8]; System.arraycopy(_enData, 0, _buffer, 0, _enData.length - 8); license = new String(decrypt(_buffer, "nuclearg".getBytes())); String[] segments = StringUtils.split(license, "\r"); for (int i = 0; i < C_ADAPTER_LIST_SIZE; i++) adapterFlag[i] = segments[0].charAt(i) == '1'; String[] _adapterDate = segments[1].split("\\|"); for (int i = 0; i < C_ADAPTER_LIST_SIZE; i++) if (adapterFlag[i]) adapterDate[i] = _adapterDate[i + 1].equals("00000000") ? null : new SimpleDateFormat("yyyyMMdd").parse(_adapterDate[i + 1]); tesDate = segments[2].equals("00000000") ? null : new SimpleDateFormat("yyyyMMdd").parse(segments[2]); adapterNum = Integer.parseInt(segments[3]); } catch (Exception ex) { throw new LicenseException("?License", ex); } if (tesDate != null && new Date().after(tesDate)) throw new LicenseException(""); int count = 0; StringBuffer buffer = new StringBuffer(); List<String> disabledChannelNames = new ArrayList<String>(); for (String name : core.channels.getChannelNames()) if (core.channels.getChannel(name) instanceof IAdapterChannel) { count++; Class<? extends IChannel> cls = core.channels.getChannel(name).getClass(); String pName = core.channels.getChannel(name).getClass().getPackage().getName(); pName = pName.substring(pName.lastIndexOf('.') + 1); int id = pName.hashCode() & 0x7fffffff % 64; if (ArrayUtils.contains(cls.getInterfaces(), IListenerChannel.class)) id += 64; if (!adapterFlag[id] || (adapterDate[id] != null && new Date().after(adapterDate[id]))) disabledChannelNames.add(name); } if (adapterNum > 0 && count > adapterNum) throw new LicenseException("??license?"); for (String disabledChannel : disabledChannelNames) { buffer.append("License?" + disabledChannel + "??") .append(SystemUtils.LINE_SEPARATOR); core.channels.getChannelNames().remove(disabledChannel); } return buffer.toString(); } public static byte[] decrypt(byte[] src, byte[] key) throws Exception { // DES???? SecureRandom sr = new SecureRandom(); // ?DESKeySpec DESKeySpec dks = new DESKeySpec(key); // ?DESKeySpec?? // SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher?? Cipher cipher = Cipher.getInstance("DES"); // Cipher cipher.init(Cipher.DECRYPT_MODE, securekey, sr); // ?? // ?? return cipher.doFinal(src); } }