Proxy server with java

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A Simple Java Multi-Threaded Tcp/IP Proxy Server

oksuz/tcp-proxy

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Simple tcp/ip proxy server written in java.

It uses shadowJar jar plugin for making fat-jat. You can build fat-jar with following command:

This command creates fat-jar under build/libs/tcp-proxy.jar .

Application takes three arguments for creating tcp server and connecting to remote host

  • -DremoteHost=remoteServerOrIpAddress
  • -DremotePort=remoteServerPort
  • -Dport=tcpServerPort
java -DremoteHost=server.tld -DremotePort=10000 -Dport=5000 -jar tcp-proxy.jar 

In the above example application accepts connection on 5000 port and forwards request to server.tld:10000 and also send server.tld:10000’s responses to client

This application also has docker image. You can pull image from https://hub.docker.com/r/oksuz/tcp-proxy/

You can run this image like this:

docker run --name my-tcp-proxy -p5000:5000 -eREMOTE_HOST=server.tld -eREMOTE_PORT=10000 -d oksuz/tcp-proxy 

Default exposed port is 5000 . It means if you don’t provide PORT env. variable default tcp server port is 5000. Also you can provide PORT env variable like this:

docker run --name my-tcp-proxy -p5001:5001 -ePORT=5001 -eREMOTE_HOST=server.tld -eREMOTE_PORT=10000 -d oksuz/tcp-proxy 

Источник

Proxy server with java

In these examples you can find different ways of creating simple proxy socket servers. This can be useful to you for a number of reasons:

  • To capture traffic between a client and server.
  • To limit the uploading/downloading the bandwidth to find out how your site is loaded with slow connections.
  • To see how your system reacts when there is trouble in your network.
  • To modify the content (sent of received) of a client/server «on the fly».
  • To produce statistics about traffic.

* You can use the examples here as base for your development. None of the features have been implemented. The following examples are mere proxies. You cannot always use them as HTTP proxies.

Example 1

import java.io.*; import java.net.*; /** * * @author jcgonzalez.com * */ public class ProxyMultiThread < public static void main(String[] args) < try < if (args.length != 3) throw new IllegalArgumentException("insuficient arguments"); // and the local port that we listen for connections on String host = args[0]; int remoteport = Integer.parseInt(args[1]); int localport = Integer.parseInt(args[2]); // Print a start-up message System.out.println("Starting proxy for " + host + ":" + remoteport + " on port " + localport); ServerSocket server = new ServerSocket(localport); while (true) < new ThreadProxy(server.accept(), host, remoteport); >> catch (Exception e) < System.err.println(e); System.err.println("Usage: java ProxyMultiThread " + " "); > > > /** * Handles a socket connection to the proxy server from the client and uses 2 * threads to proxy between server and client * * @author jcgonzalez.com * */ class ThreadProxy extends Thread < private Socket sClient; private final String SERVER_URL; private final int SERVER_PORT; ThreadProxy(Socket sClient, String ServerUrl, int ServerPort) < this.SERVER_URL = ServerUrl; this.SERVER_PORT = ServerPort; this.sClient = sClient; this.start(); >@Override public void run() < try < final byte[] request = new byte[1024]; byte[] reply = new byte[4096]; final InputStream inFromClient = sClient.getInputStream(); final OutputStream outToClient = sClient.getOutputStream(); Socket client = null, server = null; // connects a socket to the server try < server = new Socket(SERVER_URL, SERVER_PORT); >catch (IOException e) < PrintWriter out = new PrintWriter(new OutputStreamWriter( outToClient)); out.flush(); throw new RuntimeException(e); >// a new thread to manage streams from server to client (DOWNLOAD) final InputStream inFromServer = server.getInputStream(); final OutputStream outToServer = server.getOutputStream(); // a new thread for uploading to the server new Thread() < public void run() < int bytes_read; try < while ((bytes_read = inFromClient.read(request)) != -1) < outToServer.write(request, 0, bytes_read); outToServer.flush(); //TODO CREATE YOUR LOGIC HERE >> catch (IOException e) < >try < outToServer.close(); >catch (IOException e) < e.printStackTrace(); >> >.start(); // current thread manages streams from server to client (DOWNLOAD) int bytes_read; try < while ((bytes_read = inFromServer.read(reply)) != -1) < outToClient.write(reply, 0, bytes_read); outToClient.flush(); //TODO CREATE YOUR LOGIC HERE >> catch (IOException e) < e.printStackTrace(); >finally < try < if (server != null) server.close(); if (client != null) client.close(); >catch (IOException e) < e.printStackTrace(); >> outToClient.close(); sClient.close(); > catch (IOException e) < e.printStackTrace(); >> >

Compile and Use

java ProxyMultiThread 192.168.1.10 8080 9999

Starting proxy for 192.168.1.10:8180 on port 9999

(Now the traffic is redirected from localhost 9999 to 192.168.1.10 through the proxy. Keep in mind that a web browser may not always work because of the HTTP host tag, referer, javascript redirections etc.).

Use Example 1 as HTTP Proxy for Another HTTP Proxy

If your HTTP proxy is myproxy.test.net on port 380 and you want use java as proxy before it gets to your proxy, you should run it like this.

java ProxyMultiThread myproxy.test.net 80 9999

Now adjust your browser configuration to use the proxy at localhost 9999

img/0/87/settings.png

* Now all your traffic should go to the example 1 before it goes to your proxy.

Example 2

// This example is from _Java Examples in a Nutshell_. (http://www.oreilly.com) // Copyright (c) 1997 by David Flanagan // This example is provided WITHOUT ANY WARRANTY either expressed or implied. // You may study, use, modify, and distribute it for non-commercial purposes. // For any commercial use, see http://www.davidflanagan.com/javaexamples import java.io.*; import java.net.*; /** * This class implements a simple single-threaded proxy server. **/ public class SimpleProxyServer < /** The main method parses arguments and passes them to runServer */ public static void main(String[] args) throws IOException < try < // Check the number of arguments if (args.length != 3) throw new IllegalArgumentException("Wrong number of arguments."); // Get the command-line arguments: the host and port we are proxy for // and the local port that we listen for connections on String host = args[0]; int remoteport = Integer.parseInt(args[1]); int localport = Integer.parseInt(args[2]); // Print a start-up message System.out.println("Starting proxy for " + host + ":" + remoteport + " on port " + localport); // And start running the server runServer(host, remoteport, localport); // never returns >catch (Exception e) < System.err.println(e); System.err.println("Usage: java SimpleProxyServer " + " "); > > /** * This method runs a single-threaded proxy server for * host:remoteport on the specified local port. It never returns. **/ public static void runServer(String host, int remoteport, int localport) throws IOException < // Create a ServerSocket to listen for connections with ServerSocket ss = new ServerSocket(localport); // Create buffers for client-to-server and server-to-client communication. // We make one final so it can be used in an anonymous class below. // Note the assumptions about the volume of traffic in each direction. final byte[] request = new byte[1024]; byte[] reply = new byte[4096]; // This is a server that never returns, so enter an infinite loop. while(true) < // Variables to hold the sockets to the client and to the server. Socket client = null, server = null; try < // Wait for a connection on the local port client = ss.accept(); // Get client streams. Make them final so they can // be used in the anonymous thread below. final InputStream from_client = client.getInputStream(); final OutputStream to_client= client.getOutputStream(); // Make a connection to the real server // If we cannot connect to the server, send an error to the // client, disconnect, then continue waiting for another connection. try < server = new Socket(host, remoteport); >catch (IOException e) < PrintWriter out = new PrintWriter(new OutputStreamWriter(to_client)); out.println("Proxy server cannot connect to " + host + ":" + remoteport + ":\n" + e); out.flush(); client.close(); continue; >// Get server streams. final InputStream from_server = server.getInputStream(); final OutputStream to_server = server.getOutputStream(); // Make a thread to read the client's requests and pass them to the // server. We have to use a separate thread because requests and // responses may be asynchronous. new Thread() < public void run() < int bytes_read; try < while((bytes_read = from_client.read(request)) != -1) < to_server.write(request, 0, bytes_read); System.out.println(bytes_read+"to_server--->"+ new String(request, "UTF-8")+" <---"); to_server.flush(); >> catch (IOException e) <> // the client closed the connection to us, so close our // connection to the server. This will also cause the // server-to-client loop in the main thread exit. try catch (IOException e) <> > >.start(); // Meanwhile, in the main thread, read the server's responses // and pass them back to the client. This will be done in // parallel with the client-to-server request thread above. int bytes_read; try < while((bytes_read = from_server.read(reply)) != -1) < try < Thread.sleep(1); System.out.println(bytes_read+"to_client--->"+ new String(request, "UTF-8")+" <---"); >catch (InterruptedException e) < e.printStackTrace(); >to_client.write(reply, 0, bytes_read); to_client.flush(); > > catch(IOException e) <> // The server closed its connection to us, so close our // connection to our client. This will make the other thread exit. to_client.close(); > catch (IOException e) < System.err.println(e); >// Close the sockets no matter what happens each time through the loop. finally < try < if (server != null) server.close(); if (client != null) client.close(); >catch(IOException e) <> > >//while(true) > >

Example 3 HTTP proxy

I use this HTTP Java Proxy by Benjamin Kohl & Artem Melnik download here or go to the original site here.

Notes

  • You can use Thread.sleep to limit bandwidth, throw random exceptions to mess with the networking, modify the content etc.

Источник

Как создать и использовать прокси-сервер на Java

Узнайте, как создать и использовать прокси-сервер на Java для обеспечения безопасности и управления доступом в нашей пошаговой статье!

Прокси-сервер — это сервер, который служит посредником между клиентом и другим сервером. Он может быть использован для различных целей, таких как обеспечение безопасности, управление доступом и кеширование данных. В этой статье мы рассмотрим, как создать и использовать прокси-сервер на Java.

Шаг 1: Создание прокси-сервера

Для создания прокси-сервера на Java, воспользуйтесь следующими шагами:

  1. Создайте новый Java-проект и добавьте в него класс MyProxyServer .
  2. В этом классе создайте два объекта Socket : один для соединения с клиентом, другой для соединения с целевым сервером.
Socket clientSocket = null; Socket targetServerSocket = null;
  1. Создайте конструктор для класса MyProxyServer , который будет принимать порт прокси-сервера и адрес целевого сервера.

public MyProxyServer(int proxyPort, String targetServerAddress)

ServerSocket serverSocket = new ServerSocket(proxyPort);
  1. В бесконечном цикле принимайте соединения от клиентов и создавайте потоки для обработки их запросов.

Шаг 2: Обработка запросов клиентов

  1. Создайте класс ClientRequestHandler , который будет обрабатывать запросы клиентов.
  2. В этом классе создайте конструктор, который будет принимать Socket клиента и адрес целевого сервера.

public ClientRequestHandler(Socket clientSocket, String targetServerAddress)

  1. В методе run() класса ClientRequestHandler считывайте запрос клиента, обрабатывайте его и отправляйте на целевой сервер. Затем получите ответ от целевого сервера и перенаправьте его обратно клиенту.
InputStream clientInputStream = clientSocket.getInputStream(); OutputStream clientOutputStream = clientSocket.getOutputStream(); // Чтение запроса клиента и обработка данных // . // Отправка запроса на целевой сервер // . // Получение ответа от целевого сервера и отправка его клиенту // .
clientSocket.close(); targetServerSocket.close();

Шаг 3: Запуск прокси-сервера

Чтобы запустить ваш прокси-сервер, просто создайте и запустите объект MyProxyServer с необходимыми параметрами.

public static void main(String[] args)

Теперь ваш прокси-сервер готов к использованию! 😃

Заключение

В этой статье мы рассмотрели, как создать и использовать прокси-сервер на Java. Это может быть полезно для обеспечения безопасности, управления доступом или кеширования данных. Учтите, что приведенный код является базовым примером и может быть дополнен и оптимизирован в соответствии с вашими требованиями.

Не забудьте изучить дополнительные материалы и практиковаться для улучшения своих навыков в Java-разработке. Удачи вам в изучении Java! 🚀

Источник

Читайте также:  Php отправить put запрос
Оцените статью