1 /**
2  * The utillib library.
3  * More information is available at http://www.jinchess.com/.
4  * Copyright (C) 2002 Alexander Maryanovsky.
5  * All rights reserved.
6  *
7  * The utillib library is free software; you can redistribute
8  * it and/or modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * The utillib library is distributed in the hope that it will
13  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with utillib library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 package free.util;
23 
24 import java.io.*;
25 import java.net.Socket;
26 import java.net.ServerSocket;
27 
28 
29 /**
30  * Implements a TCP/IP bounce utility (proxy). You run bounce specifying which
31  * port to listen on, which host and on which port to connect to and it will
32  * act as a proxy relaying information between anyone who connects to it and the
33  * specified server.
34  */
35 
36 public class Bounce{
37 
38 
39 
40   /**
41    * The main method.
42    */
43 
main(String [] args)44   public static void main(String [] args){
45     if (args.length < 3){
46       printUsage();
47       System.exit(1);
48     }
49 
50     int localPort;
51     try{
52       localPort = Integer.parseInt(args[0]);
53     } catch (NumberFormatException e){
54         System.err.println("Bad local port value: "+args[0]);
55         printUsage();
56         System.exit(2);
57         return;
58       }
59 
60     String hostname = args[1];
61 
62     int remotePort;
63     try{
64       remotePort = Integer.parseInt(args[2]);
65     } catch (NumberFormatException e){
66         System.err.println("Bad remote port value: "+args[2]);
67         printUsage();
68         System.exit(3);
69         return;
70       }
71 
72     boolean shouldLog = args.length > 3 ? Boolean.valueOf(args[3]).booleanValue() : false;
73     int numConnections = 0;
74 
75     try{
76       ServerSocket ssock = new ServerSocket(localPort);
77       while (true){
78         Socket incomingSock = ssock.accept();
79         Socket outgoingSock = new Socket(hostname, remotePort);
80         numConnections++;
81 
82         InputStream incomingIn = incomingSock.getInputStream();
83         InputStream outgoingIn = outgoingSock.getInputStream();
84         OutputStream incomingOut = incomingSock.getOutputStream();
85         OutputStream outgoingOut = outgoingSock.getOutputStream();
86 
87         if (shouldLog){
88           String incomingLogName = "in-log-"+incomingSock.getInetAddress().getHostName()+ "("+localPort+")-" + numConnections + ".dat";
89           String outgoingLogName = "out-log-" + hostname + "("+remotePort + ")-" + numConnections+".dat";
90           OutputStream incomingLog = new FileOutputStream(incomingLogName);
91           incomingOut = new MultiOutputStream(incomingOut, incomingLog);
92           OutputStream outgoingLog = new FileOutputStream(outgoingLogName);
93           outgoingOut = new MultiOutputStream(outgoingOut, outgoingLog);
94         }
95 
96         PumpThread t1 = new PumpThread(incomingIn, outgoingOut);
97         PumpThread t2 = new PumpThread(outgoingIn, incomingOut);
98         t1.start();
99         t2.start();
100       }
101     } catch (IOException e){
102         e.printStackTrace();
103         System.exit(3);
104       }
105   }
106 
107 
108 
109   /**
110    * Dumps usage information to the standard error stream.
111    */
112 
printUsage()113   private static void printUsage(){
114     System.err.println("Bounce Utility");
115     System.err.println("Copyright (C) 2002 Alexander Maryanovsky");
116     System.err.println();
117     System.err.println("Usage: java free.util.Bounce localPort hostname remotePort [shouldLog]");
118     System.out.println();
119     System.out.println("Version 1.01 - 31 Nov. 2002");
120   }
121 
122 
123 
124 }
125