Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.Scanner;
- class Repeat extends Thread{
- public Repeat otherRepeat;
- public Scanner rx;
- public PrintWriter tx;
- public Repeat(Socket src,Socket tar) throws IOException{
- rx = new Scanner(src.getInputStream());
- tx = new PrintWriter(tar.getOutputStream());
- }
- public void setOtherRepeat(Repeat others){
- otherRepeat = others;
- }
- public void run(){
- try {
- while(rx.hasNext()){
- tx.print(rx.next());
- tx.flush();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- try {
- otherRepeat.stop();
- otherRepeat.tx.flush();
- otherRepeat.tx.close();
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- }
- }
- class TunnelThread extends Thread{
- Socket src;
- Socket tar;
- Repeat srcR;
- Repeat tarR;
- public TunnelThread(Socket source,Socket target){
- src = source;
- tar = target;
- }
- public void run(){
- try {
- srcR = new Repeat(src, tar);
- tarR = new Repeat(tar, src);
- srcR.setOtherRepeat(tarR);
- tarR.setOtherRepeat(srcR);
- srcR.start();
- tarR.start();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- class Listener extends Thread{
- //TreeMap
- ServerSocket listen;
- String targetIP;
- int targetPort;
- public Listener(int localPort,String targetIP,int targetPort) throws IOException{
- listen = new ServerSocket(localPort);
- this.targetIP = targetIP;
- this.targetPort = targetPort;
- }
- public void run(){
- while(true){
- try {
- (new TunnelThread(listen.accept(),new Socket(targetIP,targetPort))).start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public class tunnel {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- try {
- Listener li = new Listener(991,"127.0.0.1",17);
- li.start();
- // li.stop();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement