Java proxy gateway available!

Aaron Williams (
Fri, 9 May 1997 12:07:02 PST

I have a preliminary Java proxy client available for getting out through
a firewall. Right now it basically just emulates the Perl script, but
it will work on systems that don't support Perl. It may have some bugs
as it's just something I quickly hacked together (and my Java knowlege
is not that great yet).

I cannot write a Java gateway for the other side due to the fact that
Java will block forever when receiving on a UDP port. At least in the
OS/2 1.0.2 version interrupt() does not work to break this.

I have included the code below:

// DES Challenge gateway for going through a firewall
// Written by Aaron Williams
// I take no responsibility for any damage this may cause (if any)
// or any bugs in this code.
import java.lang.*;
import java.lang.Integer;
import java.lang.Object;
import java.lang.Thread;

public class UDPGateway
public static void main(String args[])

static void start(String args[])
try {
if (args.length < 4) {
System.out.println("Too few arguments!");
System.out.println("java UDPGateway proxy port gateway port");

String proxyaddr = args[0];
Integer proxyport = new Integer(args[1]);
String gateway = args[2];
Integer gatewayport = new Integer(args[3]);

System.out.println("Proxy: "+proxyaddr);
System.out.println("Proxy Port: "+proxyport.intValue());
System.out.println("Gateway: "+gateway);
System.out.println("Gateway Port: "+gatewayport.intValue());

// bind to datagram socket
DatagramSocket socket = new DatagramSocket(8669);

byte buffer[] = new byte[1480];

DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
Proxy proxy = new Proxy(proxyaddr, proxyport.intValue(), gateway,
gatewayport.intValue(), packet);
} catch (Exception ex) {

class Proxy implements Runnable
String gateway, proxy;
Thread thread = null;
DatagramPacket packet = null;
int proxyport, gatewayport;

public Proxy(String px, int pp, String gw, int gp, DatagramPacket dp)
proxy = px;
proxyport = pp;
gatewayport = gp;
packet = dp;
gateway = gw;
this.thread = new Thread(this);
this.thread.setName("Proxy thread");
public void run()
try {
byte b[] = packet.getData();
System.out.println("Packet length:"+packet.getLength());
String s = new String(b,0);
s = s.substring(0,packet.getLength());

InetAddress destaddr = packet.getAddress();
System.out.println("Source Address = "+packet.getAddress().getHostName());
//System.out.println("Source Address = "+destaddr.getHostAddress());
//System.out.println("Source Address = "+destaddr.getHostName());

Socket socket = new Socket(proxy, proxyport);
DataInputStream instream = new DataInputStream(socket.getInputStream());
PrintStream outstream = new PrintStream(socket.getOutputStream(), true);

String out = "GET http://"+gateway+":"+gatewayport+"/";

// convert spaces
// The default URL encoding scheme does not work with the gateway!
int strlen = s.length();
for (int i=0; i<strlen; i++)
char c = s.charAt(i);
if (c == ' ')
out += "%20";
out += c;

//String out = "GET http://"+gateway+":"+gatewayport+"/"+URLEncoder.encode(s)+" HTTP/1.0";
System.out.println(out+" HTTP/1.0");
outstream.println(out+" HTTP/1.0");
outstream.println("User-Agent: UDP2TCPGateway/1.1");
String line = null;
// skip over HTTP headers
do {
line = instream.readLine();
} while (line.length() > 0);
// read the response
line = instream.readLine();
if (line == null)

int bytesread=0;
int bufferindex=0;
int strindex=0;
strlen = line.length();

// This is a pain! I also have to convert the data from the
// gateway back. Looks like I only need to worry about spaces,
// though. My own gateways should be *much* better, however.

byte outbuffer[] = new byte[2048];
byte tempbuf[] = new byte[1];

do {
if (line.charAt(strindex) != '%')
line.getBytes(strindex, strindex+1, outbuffer, bufferindex);
System.out.print(line.substring(strindex, strindex+1));
if (line.substring(strindex, strindex+3).equals("%20"))
System.out.print(' ');
outbuffer[bufferindex] = 0x20;
strindex += 2;
System.out.println("Unknown % Value! Line="+line);
System.out.println("Substring = "+line.substring(strindex, strindex+3)+"###");
} while (strindex < strlen);


DatagramPacket outpacket = new DatagramPacket(outbuffer, bytesread,
destaddr, packet.getPort());

DatagramSocket udpsocket = new DatagramSocket();
try {
} catch (Exception ex) {
System.out.println("Exception sending datagram "+ex);
System.out.println("data = "+new String(outpacket.getData(),0));

System.out.println("addr = "+outpacket.getAddress().getHostName());
System.out.println("addr = "+destaddr.getHostName());

System.out.println("port = "+outpacket.getPort());


} catch (Exception ex) {


Aaron Williams
Adaptec, Inc.
(408) 945-8600 x3425
Inter-Networking Technology
Check out our web site at HTTP://
Personal Java-based home page at HTTP:// (9am-midnight PST only)