-
how to redirect tcp?
Hi all,
Here is my problem:
I have one computer which is connected directly to the internet through a network card and modem. using a second network card i connect a hub and to that hub i connect several computers.
Now, when the computers behind the hub use some network program the info goes out but when data to the port comes back in it cannot find the original computer since it is behind the computer connected to the internet (All computers share the same IP, of course).
Now what i want to do is rather simple. i want to write a program that "catches" incoming traffic through a specific port and redirects it to the same port on a different ip. This program should be put on the computer connected to the internet and theoretically should solve my problem.
I am not sure exactly, however, of how to go by it. If someone could explain to me the correct usage of sockets for this problem or at least link me to some useful source code that does something alike (and i'm pretty sure i'm not the first person with this problem, so there should be plenty out there) i'd be very thankful
tx in advance
Me
-
I think it would be safer and easier to buy a little router.
eschew obfuscation
-
I agree wholeheartedly, but my interest in this is academic
-
Then you want to simulate a router, embedded in "master" pc ? Sounds like a pretty good idea though. Good luck !
eschew obfuscation
-
it's not that difficult to build a tcp-redirection within java.
what you have to do is a simple serversocket that accepts incoming connections.
the connection is represented by an inputstream.
on the other hand, the serversocket has to open a client connection to the target, represented by an outputstream. now the serversocket just has to write everything received from the inputstream to the outputstream.
that's all. i once write that, but don't have the code at hand.
-
That's exactly what i tried to do but at some point i just keep getting null strings from the input and of course it doesn't help the output anything.
What i miss is the semantics, such as exactly how to build the input and output streams from the sockets. If any1 could tell me which streams are needed for the i/o i'll be very grateful.
-
i posted the code at http://narcanti.keyboardsamurais.de/tcpproxy.html
it should run and can be modified as needed.
-
I have an error connecting to the page, could u repost please?
-
well, my site is temporarily down. so here the code:
Code:
public class TcpProxy {
static class Listener extends Thread {
final ServerSocket server;
final String remoteHost;
final int remotePort;
Listener(ServerSocket server, String remoteHost, int remotePort) {
this.server = server;
this.remoteHost = remoteHost;
this.remotePort = remotePort;
}
public void run() {
try {
while (true) {
try {
Socket client = server.accept();
System.out.println("accepted connection from "
+ client.getInetAddress() + ":"
+ client.getPort());
new Connection(client, remoteHost, remotePort).start();
} catch (Exception e) {
e.printStackTrace();
}
}
} finally {
System.exit(0);
}
}
}
static class Connection extends Thread {
final Socket client;
final Socket target;
final InputStream clientIn;
final InputStream targetIn;
final OutputStream clientOut;
final OutputStream targetOut;
Connection(Socket client, String remoteHost, int remotePort)
throws Exception {
this.client = client;
try {
clientIn = client.getInputStream();
clientOut = client.getOutputStream();
target = new Socket(remoteHost, remotePort);
try {
targetIn = target.getInputStream();
targetOut = target.getOutputStream();
} catch (Exception e) {
try {
target.close();
} catch (Throwable t) {
//
}
throw e;
}
} catch (Exception e) {
try {
client.close();
} catch (Throwable t) {
//
}
throw e;
}
}
public void run() {
byte buf[] = new byte[4096];
int av, r;
while (true) {
try {
av = clientIn.available();
if (av > 0) {
r = clientIn.read(buf);
targetOut.write(buf, 0, r);
}
av = targetIn.available();
if (av > 0) {
r = targetIn.read(buf);
clientOut.write(buf, 0, r);
}
Thread.sleep(1);
if (target.isClosed() || !target.isConnected()
|| client.isClosed() || !client.isConnected()) {
System.out.println("closed connection from "
+ client.getInetAddress() + ":"
+ client.getPort());
try {
target.close();
} catch (Throwable t) {
//
}
try {
client.close();
} catch (Throwable t) {
//
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
*
* @param args
* @throws IOException
*/
public static void main(String args[]) throws IOException {
int localPort = Integer.parseInt(args[0]);
String remoteHost = args[1];
int remotePort = Integer.parseInt(args[2]);
System.out.println("TCP proxy");
System.out.println("local port: " + localPort);
System.out.println("remote host: " + remoteHost);
System.out.println("remote port: " + remotePort);
ServerSocket server = new ServerSocket(localPort, 50);
new Listener(server, remoteHost, remotePort).start();
}
}
-
Similar Threads
-
Replies: 0
Last Post: 06-10-2006, 05:20 AM
-
Replies: 0
Last Post: 01-03-2006, 11:30 AM
-
By Larry Rebich in forum Web
Replies: 3
Last Post: 05-23-2002, 07:44 AM
-
By Mikael Nyberg in forum Mobile
Replies: 0
Last Post: 06-26-2001, 04:30 AM
-
By Lisa Tang in forum Java
Replies: 0
Last Post: 06-27-2000, 09:27 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|