⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 peerfuncimpl.java

📁 Java下模拟实现p2p平台下的洋葱路由协议
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.JOptionPane;
import java.io.*;
import java.net.*;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
import java.security.interfaces.*;
import java.math.*;
import java.lang.String;
import javax.net.ssl.*;

import com.pangpang.sheji.PeerInterFace;

public class PeerFuncImpl extends JFrame{
	private static int chunk_size= 80;
    private static String cipher_delimiter = "&";
    private static PeerInterFace peerUI;
    private static String name="";
    private JButton bb[];
    public static int num;
    public static String fName="";
    public static String sFName="";
    public static String onlinePort="";
    public static String senderPo="";
    public static int portGroup[];
    public static int portN;
    
    public void certWithProxy()throws Exception{
    	ObjectOutputStream out;
        ObjectInputStream  in;
    	System.out.println("cert pressed");
    	System.setProperty("javax.net.ssl.trustStore","clientgood");
		SSLSocketFactory ssf=(SSLSocketFactory)SSLSocketFactory.getDefault();
		Socket s=ssf.createSocket("210.43.107.148",5432);
		
		out=new ObjectOutputStream(s.getOutputStream());
	    out.flush();
        in=new ObjectInputStream(s.getInputStream());
		
	    out.writeObject("1;"+num);
	    System.out.println("send out a cert request!");
		fName=(String)in.readObject();
		fName=fName.trim();
	    System.out.println(fName);
		in.close();
		out.close();
		s.close();
    }
    public void resetWithProxy()throws Exception{
    	System.out.println("reset pressed");
    	ObjectOutputStream out;
    	ObjectInputStream in;
    	System.setProperty("javax.net.ssl.trustStore","clientgood");
		SSLSocketFactory ssf=(SSLSocketFactory)SSLSocketFactory.getDefault();
		Socket s=ssf.createSocket("210.43.107.148",5432);
		
		out=new ObjectOutputStream(s.getOutputStream());
	    out.flush();
	    out.writeObject("2;"+num);
	    System.out.println("send out a reset request!");
	    	    in=new ObjectInputStream(s.getInputStream());
	    String temp=(String)in.readObject();
	    System.out.println(temp);
	    in.close();
		out.close();
		s.close();
    }
    public void quitWithProxy()throws Exception{
    	System.out.println("quit pressed");
    	ObjectOutputStream out;
    	ObjectInputStream in;
    	System.setProperty("javax.net.ssl.trustStore","clientgood");
		SSLSocketFactory ssf=(SSLSocketFactory)SSLSocketFactory.getDefault();
		Socket s=ssf.createSocket("210.43.107.148",5432);
		
		out=new ObjectOutputStream(s.getOutputStream());
			    out.flush();
	    out.writeObject("3;"+num);
	    System.out.println("send out a quit request!");
	    in=new ObjectInputStream(s.getInputStream());
	    String temp=(String)in.readObject();
	    System.out.println(temp);
	    in.close();
		out.close();
		s.close();
    }
    public void acrWithProxy()throws Exception{
    	System.out.println("acr pressed");
    	ObjectOutputStream out;
        ObjectInputStream  in;
    	System.out.println("cert pressed");
    	System.setProperty("javax.net.ssl.trustStore","clientgood");
		SSLSocketFactory ssf=(SSLSocketFactory)SSLSocketFactory.getDefault();
		Socket s=ssf.createSocket("210.43.107.148",5432);
		
		out=new ObjectOutputStream(s.getOutputStream());
			    out.flush();
		out.writeObject("4");	    
        in=new ObjectInputStream(s.getInputStream());
		
	    System.out.println("send out a acr request!");
		onlinePort=(String)in.readObject();
	    System.out.println(onlinePort);
	    onlinePort=onlinePort.trim();
	    String []pp=onlinePort.split(";");
	    portGroup=new int[100];
	    portN=pp.length;
	    for(int i=0;i<pp.length;i++){
	         portGroup[i]=Integer.parseInt(pp[i]);
	         System.out.println("portGroup"+i+":"+portGroup[i]);
	    }
		in.close();
		out.close();
		s.close();
    }
    public void rfnWithProxy()throws Exception{
    	System.out.println("rfn pressed");
    	ObjectOutputStream out;
        ObjectInputStream  in;
    	System.setProperty("javax.net.ssl.trustStore","clientgood");
		SSLSocketFactory ssf=(SSLSocketFactory)SSLSocketFactory.getDefault();
		Socket s=ssf.createSocket("210.43.107.148",5432);
		
		out=new ObjectOutputStream(s.getOutputStream());
			    out.flush();
                in=new ObjectInputStream(s.getInputStream());
		
	    out.writeObject("5;"+sFName);
	    System.out.println("send out a rfn request!");
		senderPo=(String)in.readObject();
	    System.out.println(senderPo);
	    peerUI.setSenderP(senderPo);
		in.close();
		out.close();
		s.close();
    }
    public void sendWithProxy()throws Exception{
    	System.out.println("send pressed");
    	int firstRouter,secondRouter,thirdRouter;
    	String ss=peerUI.getMessage();
    	for(int i=0;i<portN;i++)
    	    System.out.println(portGroup[i]);
    	firstRouter=Integer.parseInt(peerUI.getReceiverPort());
    	System.out.println("firstRouter="+firstRouter);
        //firstRouter=(int)(Math.random*6);
    	//while(firstRouter==num)
    	//    firstRouter=(int)(Math.random*6);
    	secondRouter=portGroup[(int)(Math.random()*portN)];
    	while(secondRouter==firstRouter||secondRouter==num)
    	    secondRouter=portGroup[(int)(Math.random()*portN)];
        System.out.println("secondRouter="+secondRouter);
    	thirdRouter=portGroup[(int)(Math.random()*portN)];
    	while(thirdRouter==firstRouter||thirdRouter==secondRouter||thirdRouter==num)
    	    thirdRouter=portGroup[(int)(Math.random()*portN)];
    	System.out.println("thirdRouter="+thirdRouter);
    	String s5678="2;"+name+":"+fName+":"+ss;
		int choice=firstRouter%1536+512;
		String c5678=encode(s5678,choice);
		String s4567="1;"+firstRouter+";4;"+c5678;
		choice=secondRouter%1536+512;
		String c4567=encode(s4567,choice);
		String s3456="1;"+secondRouter+";4;"+c4567;
		choice=thirdRouter%1536+512;
		String c3456=encode(s3456,choice);
		String str="4;"+c3456;
		System.out.println("str="+str);
		//System.out.println(decode(c3456,choice));
		String host="localhost";
		DatagramSocket s=null;
		try{
			s=new DatagramSocket();
			byte[]buffer;
			buffer=str.getBytes();
			//System.out.println(str);
			//for(int i=0;i<buffer.length;i++)
			//     System.out.println(buffer[i]);
			InetAddress ia=InetAddress.getByName(host);
			DatagramPacket dgp=new DatagramPacket(buffer,buffer.length,ia,thirdRouter);
			s.send(dgp);
			//byte[]buffer2=new byte[100];
			//char[] buffer2=new char[100];
			/*dgp=new DatagramPacket(buffer2,buffer2.length,ia,3456);
			s.receive(dgp);
			System.out.println(new String(dgp.getData()));*/
		}
		catch(IOException e)
		{
			System.out.println(e.toString());
		
		}
		finally
		{
			if(s!=null)
			s.close();
		}
    }
    public PeerFuncImpl(){
    	super("PEER");
    	bb=new JButton[7];
    	getContentPane().setLayout(new BorderLayout());
    	peerUI=new PeerInterFace();
    	bb[1]=peerUI.getCertButton();
    	bb[1].addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e)
    		{
    			try{
    				certWithProxy();
    			   }catch(Exception ec){
    			   	System.out.println(ec.toString());
    			   }
    		}
    	});
    	bb[2]=peerUI.getResetButton();
    	bb[2].addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e)
    		{
    			try{
    				resetWithProxy();
    			   }catch(Exception err){
    			   	System.out.println(err.toString());
    			   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -