DUP代理,解决nignx在win7没有udp转发功能.txt import java.io.FileInputStream;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.sql.Connection;import java.sql.DriverManager;import
import java.io.FileInputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Properties; import java.util.Random; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import redis.clients.jedis.Jedis; public class ForwardWork { private int listenPort; public ListObjLists; public List NotList; public ForwardWork(){} public ForwardWork(List o,List n,int listenPort ){ this.ObjLists=o; this.NotList=n; this.listenPort=listenPort; } DatagramSocket server=null; DatagramPacket recvPacket =null; int count=0; public void startProxy(){//启动UDP转发 try { System.out.println("location port :"+listenPort); server = new DatagramSocket(listenPort); if (ObjLists.size()==1) { while(true){ byte[] Buf = new byte[256]; recvPacket = new DatagramPacket(Buf,Buf.length); server.receive(recvPacket); UdpForwar(ObjLists.get(0),Buf); count +=1; } }else if(ObjLists.size()==0){ byte[] Buf = new byte[256]; recvPacket = new DatagramPacket(Buf,Buf.length); server.receive(recvPacket); Random rand = new Random(); int randNum = rand.nextInt(NotList.size()); UdpForwar(NotList.get(randNum),Buf); count +=1; }else { while (true) { byte[] Buf = new byte[256]; recvPacket = new DatagramPacket(Buf,Buf.length); server.receive(recvPacket); Random rand = new Random(); int randNum = rand.nextInt(ObjLists.size()); ForwardObj o= ObjLists.get(randNum); UdpForwar(o, Buf); count +=1; //map.put(o.getPort()+"",map.get(o.getPort())+1); } } } catch (Exception e) { server.close(); e.printStackTrace(); } } /** * 发送 * @param obj * @param buf */ public void UdpForwar(ForwardObj obj,byte[] buf){ try { DatagramPacket sendPacket = new DatagramPacket( buf,buf.length, InetAddress.getByName(obj.getAddr()) ,obj.getPort()); server.send(sendPacket); if (count >100) { System.out.println("随机挑选出转发地址为:"+obj.getAddr()); count=0; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 删除redis里考场hash * @param url * @param port */ public void clearRedis(String url,int port,List lists ){ long num=0; Jedis jedis=null; try { jedis = new Jedis(url ,port); for (String key : lists) { if(jedis.exists(key)) num += jedis.del(key); } System.out.println("In zhe Redis,Examination Site was successfully deleted "+num+" ."); } catch (Exception e) { System.out.println("clearRedis() Error! redis出错"); e.printStackTrace(); }finally { jedis.close(); } } /** * 查询考场 kcdddh字段 考场地点代码 * @param url * @param user * @param password * @return */ public List select(String url,String user,String password){ Connection con = null;// 创建一个数据库连接 PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement ResultSet result = null;// 创建一个结果集对象 try { Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序 con = DriverManager.getConnection(url, user, password);// 获取连接 String sql = "select t.kcdddh from DRV_EXAMINATION_SITE t";// 预编译语句,“?”代表参数 pre = con.prepareStatement(sql);// 实例化预编译语句 result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数 List kcdddhs=new ArrayList<>(); while (result.next()) kcdddhs.add(result.getString("KCDDDH")); return kcdddhs; } catch (Exception e) { System.out.println( "oracle Connection problem!(oracle出错)"); e.printStackTrace(); }finally { try { // 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源 // 注意关闭的顺序,最后使用的最先关闭 if (result != null) result.close(); if (pre != null) pre.close(); if (con != null) con.close(); } catch (Exception e) { e.printStackTrace(); } } return null; } public static void main(String[] args) { String filePath = System.getProperty("user.dir"); System.out.println("project path:"+filePath); //参数表 List ObjLists=new ArrayList<>(); List NotList=new ArrayList<>(); int listenPort = 0; try { //读取工程下面配置文件 //InputStream in= ForwardWork.class.getResourceAsStream("UDP.properties"); //读取项目同路径下的文件 D:\workspace\UDPProxy3\.properties FileInputStream fileInputStream=new FileInputStream("UDP.properties"); Properties prop = new Properties(); prop.load(fileInputStream); Iterator it=prop.stringPropertyNames().iterator(); while(it.hasNext()){ String key=it.next(); if ("listen".equals(key)) { listenPort=Integer.valueOf(prop.getProperty(key)); continue; }else if ("redis".equals(key)) { String[] ar=prop.getProperty(key).split(":", 2); ForwardObj.redisUrl=ar[0]; ForwardObj.redisport=Integer.valueOf(ar[1]); continue; }else if ("ORACLE".equals(key)) { String[] ar=prop.getProperty(key).split(",", 3); ForwardObj.url=ar[0]; ForwardObj.username=ar[1]; ForwardObj.password=ar[2]; continue; } String pro2=prop.getProperty(key); String[] array=pro2.split(","); if (array.length<3) { ForwardObj forwardObj1=new ForwardObj(); forwardObj1.setAddr(array[0].trim()); forwardObj1.setPort(Integer.valueOf(array[1].trim())); NotList.add(forwardObj1); continue; } ForwardObj forwardObj=new ForwardObj(); for (String str2 : array) { if (1 future =scheduledExecutorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { ForwardWork forwardWork=new ForwardWork(); List Examinations=forwardWork.select(ForwardObj.url, ForwardObj.username,ForwardObj.password); forwardWork.clearRedis(ForwardObj.redisUrl,ForwardObj.redisport, Examinations); System.out.println("Reidis(kcdddh) cleanout time .销毁redis里对象时间到了"); } },10,10,TimeUnit.SECONDS); //处理完配置文件,启动UDP代理 ForwardWork forwardWork=new ForwardWork(ObjLists,NotList,listenPort); System.out.println("start UDP proxy... UDP正常工作..."); //注意:堵塞主线程 forwardWork.startProxy(); }//main结束 } class ForwardObj { //redis参数 public static String redisUrl; public static int redisport; //orcale参数 public static String url = null; public static String username = null; public static String password = null; private String addr; private int port; private int weight=0; public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } } //外部配置文件格式 #\u672c\u673a\u76d1\u542c\u7aef\u53e3 listen=8040 #\u8f6c\u53d1UDP\u914d\u7f6e,IP,\u7aef\u53e3\u53f7,\u6743\u91cd1-10 upd1=192.168.5.96,8041,1 upd2=192.168.5.96,8042,10 upd3=192.168.5.96,8043,1