Advertisement
SpyMomiji

JAVA Tunnel

Dec 13th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStream;
  7. import java.io.OutputStreamWriter;
  8. import java.io.PrintWriter;
  9. import java.net.ServerSocket;
  10. import java.net.Socket;
  11. import java.util.Scanner;
  12.  
  13.  
  14.  
  15. class Repeat extends Thread{
  16.    
  17.     public Repeat otherRepeat;
  18.     public Scanner rx;
  19.     public PrintWriter tx;
  20.    
  21.     public Repeat(Socket src,Socket tar) throws IOException{
  22.             rx = new Scanner(src.getInputStream());
  23.             tx = new PrintWriter(tar.getOutputStream());
  24.     }
  25.    
  26.     public void setOtherRepeat(Repeat others){
  27.         otherRepeat = others;
  28.     }
  29.    
  30.     public void run(){
  31.        
  32.         try {
  33.            
  34.             while(rx.hasNext()){
  35.                 tx.print(rx.next());
  36.                 tx.flush();
  37.             }
  38.            
  39.         } catch (Exception e) {
  40.             e.printStackTrace();
  41.         }finally{
  42.             try {
  43.                 otherRepeat.stop();
  44.                 otherRepeat.tx.flush();
  45.                 otherRepeat.tx.close();
  46.             } catch (Exception e2) {
  47.                 e2.printStackTrace();
  48.             }
  49.         }
  50.     }
  51. }
  52.  
  53. class TunnelThread extends Thread{
  54.     Socket src;
  55.     Socket tar;
  56.     Repeat srcR;
  57.     Repeat tarR;
  58.    
  59.     public TunnelThread(Socket source,Socket target){
  60.         src = source;
  61.         tar = target;
  62.     }
  63.    
  64.     public void run(){
  65.         try {
  66.             srcR = new Repeat(src, tar);
  67.             tarR = new Repeat(tar, src);
  68.             srcR.setOtherRepeat(tarR);
  69.             tarR.setOtherRepeat(srcR);
  70.            
  71.             srcR.start();
  72.             tarR.start();
  73.            
  74.         } catch (IOException e) {
  75.             // TODO Auto-generated catch block
  76.             e.printStackTrace();
  77.         }
  78.        
  79.     }
  80.    
  81. }
  82.  
  83. class Listener extends Thread{
  84.     //TreeMap
  85.    
  86.     ServerSocket listen;
  87.     String targetIP;
  88.     int targetPort;
  89.    
  90.     public Listener(int localPort,String targetIP,int targetPort) throws IOException{
  91.         listen = new ServerSocket(localPort);
  92.         this.targetIP = targetIP;
  93.         this.targetPort = targetPort;
  94.     }
  95.    
  96.     public void run(){
  97.         while(true){
  98.             try {
  99.                 (new TunnelThread(listen.accept(),new Socket(targetIP,targetPort))).start();
  100.             } catch (IOException e) {
  101.                 e.printStackTrace();
  102.             }
  103.         }
  104.     }
  105.    
  106. }
  107.  
  108. public class tunnel {
  109.  
  110.     /**
  111.      * @param args
  112.      */
  113.     public static void main(String[] args) {
  114.         // TODO Auto-generated method stub
  115.         try {
  116.             Listener li = new Listener(991,"127.0.0.1",17);
  117.             li.start();
  118.            
  119.            
  120. //          li.stop();
  121.         } catch (IOException e) {
  122.             // TODO Auto-generated catch block
  123.             e.printStackTrace();
  124.         }
  125.     }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement