Posts Tagged - proxy

Java Damn Simple HTTP Proxy

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HttpProxy {

  private static byte[] CONNECTION_ESTABLISHED =
      "HTTP/1.0 200 Connection established\r\nProxy-Agent: HTTP Proxy/1.0\r\n\r\n".getBytes();

  public static void main(String[] args) throws IOException {
    ExecutorService threadPool = Executors.newCachedThreadPool();
    ServerSocket serverSocket = new ServerSocket(8080);
    while (true) {
      threadPool.submit(new UpstramHandler(serverSocket.accept()));
    }
  }

  static class UpstramHandler implements Runnable {

    private final Socket socket;
    private final ExecutorService executorService;

    UpstramHandler(Socket socket) {
      this.socket = socket;
      this.executorService = Executors.newSingleThreadExecutor();
    }

    @Override
    public void run() {
      byte[] buffer = new byte[2048];
      while (true) {
        try {
          Socket remoteSocket = null;
          int recv = 1;
          while (recv > 0) {
            recv = socket.getInputStream().read(buffer);
            if (recv > 0) {
              if (recv > 8) {
                String s = new String(buffer, 0, 8);
                if (s.contains("CONNECT")) {
                  int endlIndx = 0;
                  for (int i = 8; i < recv - 1; i++) {
                    if (buffer[i] == '\r' && buffer[i + 1] == '\n') {
                      endlIndx = i + 1;
                      break;
                    }
                  }
                  if (endlIndx > 8) {
                    String request = new String(buffer, 0, endlIndx + 1);
                    int connectIndex = request.indexOf("CONNECT");
                    int startIndex = request.indexOf(" ", connectIndex) + 1;
                    int endIndex = request.indexOf(" ", startIndex);
                    String[] hostnameAndPort;
                    if (endIndex < startIndex) {
                      hostnameAndPort = request.substring(startIndex).split(":");
                    } else {
                      hostnameAndPort = request.substring(startIndex, endIndex).split(":");
                    }
                    String hostname = hostnameAndPort[0];
                    int port = Integer.parseInt(hostnameAndPort[1]);
                    remoteSocket = new Socket(hostname, port);
                    socket.getOutputStream().write(CONNECTION_ESTABLISHED);
                    socket.getOutputStream().flush();
                    executorService.submit(new DownstreamHandler(socket, remoteSocket));
                    continue;
                  }
                }
              }
              if (remoteSocket != null) {
                remoteSocket.getOutputStream().write(buffer, 0, recv);
                remoteSocket.getOutputStream().flush();
              }
            }
          }
        } catch (IOException e) {
          System.err.println(e);
        }
      }
    }
  }

  static class DownstreamHandler implements Runnable {

    private final Socket socket;
    private final Socket remoteSocket;

    DownstreamHandler(Socket socket, Socket remoteSocket) {
      this.socket = socket;
      this.remoteSocket = remoteSocket;
    }

    @Override
    public void run() {
      byte[] buffer = new byte[2048];
      int recv = 1;
      while (recv > 0) {
        try {
          recv = remoteSocket.getInputStream().read(buffer);
          if (recv > 0 && remoteSocket != null) {
            socket.getOutputStream().write(buffer, 0, recv);
            socket.getOutputStream().flush();
          }
        } catch (IOException e) {
          System.err.println(e);
        }
      }
    }
  }
}

Read More

Nginx HTTP Load Balancer

$ sudo vim /etc/nginx/nginx.conf

http {

  # round robin
  upstream rumahpemuridan {
      server 127.0.0.1:8081 weight=3;
      server 127.0.0.1:8082 max_conns=2;
      queue 100 timeout=70;
  }

  server {
    listen 80;
    server_name rumahpemuridan.com;
    location / {
      proxy_pass http://rumahpemuridan;
    }
  }
}

Read More

Install Nginx and Let's Encrypt on Ubuntu 20.04

Prerequisites

  • You have domain name pointing to your server public IP.

Installation process

  • sudo apt update

  • sudo apt install nginx

  • sudo systemctl enable nginx

  • sudo apt install certbot

  • sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

  • sudo mkdir -p /var/lib/letsencrypt/.well-known

  • sudo chgrp www-data /var/lib/letsencrypt

  • sudo chmod g+s /var/lib/letsencrypt

  • sudo vim /etc/nginx/snippets/letsencrypt.conf

location ^~ /.well-known/acme-challenge/ {
  allow all;
  root /var/lib/letsencrypt/;
  default_type "text/plain";
  try_files $uri =404;
}
  • sudo vim /etc/nginx/snippets/ssl.conf
ssl_dhparam /etc/ssl/certs/dhparam.pem;

ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
ssl_prefer_server_ciphers on;

ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 30s;

add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload";
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
  • sudo vim /etc/nginx/sites-available/dev.cmsnesia.com.conf
server {
  listen 80;
  listen [::]:80;
  server_name dev.cmsnesia.com;
  
  root /var/www/html
  
  include snippets/letsencrypt.conf;
}
  • sudo ln -s /etc/nginx/sites-available/dev.cmsnesia.com.conf /etc/nginx/sites-enabled/

  • sudo systemctl restart nginx

  • sudo certbot certonly --agree-tos --email [email protected] --webroot -w /var/lib/letsencrypt/ -d dev.cmsnesia.com

  • sudo vim /etc/nginx/sites-available/dev.cmsnesia.com.conf

server {
    listen 80;
    listen [::]:80;
    server_name dev.cmsnesia.com;
  
    root /var/www/html
    include snippets/letsencrypt.conf;
    return 301 https://dev.cmsnesia.com$request_uri; # redirect http to https
}

server {
    listen 443 ssl http2;
    server_name dev.cmsnesia.com;

    ssl_certificate /etc/letsencrypt/live/dev.cmsnesia.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dev.cmsnesia.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/dev.cmsnesia.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;
}

  • sudo vim /etc/cron.d/certbot
    0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --renew-hook "systemctl reload nginx"
    
  • sudo certbot renew --dry-run

Read More

Tor On Android

  1. Install Termux from Google Playstore.

  2. Open Termux, then install tor (“pkg update && pkg install”).

  3. Run tor for tunneling:

    • Tunneling over HTTP “tor –HTTPTunnelPort 8118”.

    • For more details pls read tor docs (“pkg install man && man tor”).

  4. Install Firefox browser from Google Playstore.

  5. Open Firefox, type “about:config” in the URL bar.

  6. Set “network.proxy.type” to “1” (Configure proxy manually).

  7. Set “network.proxy.http” to “127.0.0.1”, and “network.proxy.http.port” to “8118”.

Note*)

  • DNS is not encrypted.

  • By default Tor opening socks(x) listener on 127.0.0.1:9050.

  • You can use Tor config file ($PREFIX/etc/tor/torrc) instead of passing Tor command line args.

Firefox => Tor local proxy (socks(x)/http/etc) => Tor network (proxy => proxy => proxy ….)

Read More