How does a "proxy server" work - java

I'm trying to implement a simple ProxyServer to analyze traffix between a connected device and the outside world.
The iPhone (for example) is configured to use a proxy to 192.168.1.10:8080.
My proxy server (on 192.168.1.10) will be listening on the port 8080.
The iPhone tries to connect to http://google.com:80, or ftp://somehost.com:21/ it sends a request to the proxy server (port 8080), which is supposed to contact google.com at port 80 and somehost at post 21.
So I figured it would be something like this:
//Accept incoming
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Log incoming requests
//Send to remote server on behalf of my device
Socket remote = new Socket(remoteHost, remotePort);
//forward the same data to remote client..
//wait for the reply
//send to my device
How to know the remote port/host to forward the request to?
What did I miss?
Thank you

Related

Android UDP hole punching

I am trying to make an streaming app and I want to use UDP. I would like the connection to be P2P. So I have a java tcp server running on google cloud, an android phone and my pc. When android connects to tcp server it will create a DatagramSocket on the server that will receive the packet android sends to get the port and ip. Same for the pc. Each client will get the other's client ip and port and each client will get the port that the server received (EG: android sends from port 18000, it will get other's client ip and port and port 18000). Then I close the initial DatagramSocket and open a new one that is bound to the port it gets from server (in our eg 18000). But the connection is never created between the pc and android (android is on 4G and pc on router , so different networks)
socket = new DatagramSocket(Integer.parseInt(po));
Here is how I create the P2P datgram socket, where po is 18000 (each client has different po - the android has 18000 and the pc has let's say 45000), I get those port from tcp server and is ok.
public void send() throws NumberFormatException, IOException
{
//only for testing
byte[] buffer = new byte[1024];
buffer = "test".getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length,InetAddress.getByName(ip),Integer.parseInt(port));
socket.send(packet);
}
Here is how i send data from the P2P datagram. The ip is the other's client public ip and the port is the other's client port that is listening.
#Override
public void run() {
// TODO Auto-generated method stub
while (true) {
byte[] buffer = new byte[64000];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try {
socket.receive(packet);
//acum ca am primit
String ana = new String(packet.getData());
System.err.println("Am primit de la celalalt "+ana);
}
}
Here is the thread that listens for incoming packets.
The main problem is that this works but only when the client are on the same network as the server!Even if I both client are on the same netowk , but the server is on a different one, they won't receive anything. Data sent from TCP is 100% correct.
DatagramPacket packet= new DatagramPacket(buffer,buffer.length, InetAddress.getByName("server_ip"),port_main);
socket_udp.send(packet);
Here is how I send the UDP packet to the server in order to get the port back.
I tried using the same socket_udp ,without creating socket var again, it failed too.

TCP socket connection over internet

I am doing a project for which connection between server and client is required.
I did it by adding TCP sockets.
Here is the code fraction :
Server:
ServerSocket welcomeSocket = new ServerSocket(80);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
WorkerThread wt = new WorkerThread(connectionSocket, id);
Thread t = new Thread(wt);
t.start();
workerThreadCount++;
}
Client :
Socket skt = new Socket("192.168.0.108", 80); // The IP address is from cmd->ipconfig/all-> IPv4 Address
outToServer = new PrintWriter(skt.getOutputStream(), true);
inFromServer = new BufferedReader(new InputStreamReader(skt.getInputStream()));
It all works when both ends are in same device/under same WiFi.But I don't understand what to do for creating connection over internet.
Please help with clear steps.
Here:
Socket skt = new Socket("192.168.0.108", 80);
That is local address. If you want to have a server that is reachable on the internet, then that server needs to have its global public IP address!
In other words: you have to make sure that the server can be reached from the internet somehow. For example by turning to some service provider that hosts servers that you can then equip with your code!
The whole purpose of 192.168 addresses is to be defined only in a local subnet.
Alternatively, you have to check if your ISP has a service where the ISP assigns an IP address to your connection, and that allows calls from the internet to go to your "place".
Meaning: when you want to receive phone calls, you need a phone that is connected to the phone net!
In order to connect to a socket over WAN, you must port forward that port to your local device. This can be done in your routers' settings.
192.168.0.108 --> That's your local IP-address.
This can be used on your local network without any requirements for port forwarding whatsoever. However, to use it over WAN, execute the following steps:
Step 1: Search for your routers' model number and port forwarding on Google on how-to forward port 80 to your local IP-address. Warning: use a static IP-address on your local device to prevent your IP from changing after a reboot.
Step 2: Go to a website like IP Chicken and find your external IP-address.
You can then connect to your socket using:
Socket skt = new Socket("[EXTERNALIP]", 80);
Please be noticed: unless you have a business network, your external IP-address will probably change from time to time.

Java - Program works only for localhost

I use the following code for the client
InetAddress host = InetAddress.getByName("XXX.XXX.XXX.XXX");
Socket socket = new Socket(host,serverPort);
and for the server
ServerSocket serverSocket = new ServerSocket(portAdd);
Socket server = serverSocket.accept();
It works if I am using localhost but not when I use an IP address.
Firewall is not blocking the connection.

Chat using TCP in Java

I am making a chat in Java which uses a TCP protocol.
I have a client and a server side.
To send a message to another user, I have to send the message to the server through my client, and the server has to send it to another client.
The server holds the addresses of both online users. When I send a private message, the server finds the ip and a port and creates a socket from them.
The problem is that it doesn’t work correctly.
Here’s the code:
int portNumber = 4444;
String host = "192.168.0.100”;
Socket link;
try {
link = new Socket(host, portNumber);
// Then I set to already created PrintWriter the outputstream
out = new PrintWriter(link.getOutputStream(), true);
} catch (Exception e) {}
// Unfortunately the server freezes here (it doesn't show anything).
How to solve this problem? Where dod I make a mistake?
Thank you in advance.
You shouldn't create a new Socket to send a message. Instead, use a socket of an existing connection.
The sequence should be the following:
Client A connects to the server (server stores the connection as SocketA).
Client B connects to the server (server stores the connection as SocketB).
Server reads a private message from SocketA. The message is addressed to client B.
Server finds the existing socket for client B. It's SocketB.
Server sends the message into SocketB.

Read data from a particular port and specified IP using java

I would like to read the messages from a particular port.For example the IP is 1.2.3.4 and the port is 1000. Already the IP is used for receiving some messages. What I would like to do is to listen to that particular IP and receive all the messages using a java program. Will
SocketServer do the purpose??
ServerSocket ss = new ServerSocket(1000);
Socket socket = new Socket("1.2.3.4",1000);
socket = ss.accept();
Is it possible to read every contents that are being received by the particular IP and port?
To listen to a specific address you have to create a ServerSocket like this
ServerSocket ss = new ServerSocket(); // Unbound socket
ss.bind(new InetSocketAddress("1.2.3.4", 1000)); // Bind the socket to a specific interface
Socket client = ss.accept();
This way the server socket is bound to a specific network interface and will only receive incoming connections from it.

Categories

Resources