Implement LAN proxy in Java

Source: Internet
Author: User

1. Set the Java environment

2. Compile the following classes

3. Run the. Class file in DOS

4. Set Internet Explorer for other computers on the LAN

 

Internet Explorer ---> properties --> connection ---> LAN Settings --> RUN proxy (computer with Internet access) IP address and port number;

 

 

 

Implementation class

 

Import java.net .*;
Import java. Io .*;

Public class httpproxy extends thread {
Static public int connect_retries = 5;
Static public int connect_pause = 5;
Static public int timeout = 50;
Static public int bufsiz= 1024;
Static public Boolean logging = false;
Static public outputstream log = NULL;
// Socket used for data input
Protected Socket socket;
// Superior proxy server, optional
Static private string parent = NULL;
Static private int parentport =-1;
Static public void setparentproxy (string name, int pport ){
Parent = Name;
Parentport = pport;
}

// Create a proxy thread on the given socket.
Public httpproxy (socket s) {socket = s; Start ();}

Public void writelog (int c, Boolean browser) throws ioexception {
// Log. Write (C );
}

Public void writelog (byte [] bytes, int offset, int Len, Boolean browser) throws ioexception {
// For (INT I = 0; I <Len; I ++) writelog (INT) bytes [Offset + I], browser );
}

// By default, log information is output
// Standard output device
// A derived class can overwrite it
Public String processhostname (string URL, string host, int port, socket sock ){
Java. Text. dateformat Cal = java. Text. dateformat. getdatetimeinstance ();
// System. Out. println (Cal. Format (New java. util. Date () + "-" + URL + ""
// + Sock. getinetaddress () + "/N ");
Return host;
}

 

// The thread that executes the operation
Public void run (){
String line;
String host;
Int Port = 80;
Socket outbound = NULL;
Try {
Socket. setsotimeout (timeout );
Inputstream is = socket. getinputstream ();
Outputstream OS = NULL;
Try {
// Obtain the content of the request line
Line = "";
Host = "";
Int state = 0;
Boolean Space;
While (true ){
Int c = is. Read ();
If (C =-1) break;
If (logging) writelog (C, true );
Space = character. iswhitespace (char) C );
Switch (state ){
Case 0:
If (Space) continue;
State = 1;
Case 1:
If (Space ){
State = 2;
Continue;
}
Line = line + (char) C;
Break;
Case 2:
If (Space) continue; // skip multiple blank characters
State = 3;
Case 3:
If (Space ){
State = 4;
// Retrieve only the host name
String host0 = host;
Int N;
N = host. indexof ("//");
If (n! =-1) host = host. substring (n + 2 );
N = host. indexof ('/');
If (n! =-1) host = host. substring (0, N );
// Analyze possible port numbers
N = host. indexof (":");
If (n! =-1 ){
Port = integer. parseint (host. substring (n + 1 ));
Host = host. substring (0, N );
}
Host = processhostname (host0, host, port, socket );
If (parent! = NULL ){
Host = parent;
Port = parentport;
}
Int retry = connect_retries;
While (retry --! = 0 ){
Try {
Outbound = new socket (host, Port );
Break;
} Catch (exception e ){}
// Wait
Thread. Sleep (connect_pause );
}
If (outbound = NULL) break;
Outbound. setsotimeout (timeout );
OS = outbound. getoutputstream ();
OS. Write (line. getbytes ());
OS. Write ('');
OS. Write (host0.getbytes ());
OS. Write ('');
Pipe (is, outbound. getinputstream (), OS, socket. getoutputstream ());
Break;
}
Host = Host + (char) C;
Break;
}
}
}
Catch (ioexception e ){}

} Catch (exception e ){}
Finally {
Try {socket. Close ();} catch (exception E1 ){}
Try {outbound. Close ();} catch (exception E2 ){}
}
}

Void pipe (inputstream is0, inputstream is1,
Outputstream os0, outputstream os1) throws ioexception {
Try {
Int IR;
Byte bytes [] = new byte [bufsiz];
While (true ){
Try {
If (IR = is0.read (bytes)> 0 ){
Os0.write (bytes, 0, IR );
If (logging) writelog (bytes, 0, IR, true );
}
Else if (IR <0)
Break;
} Catch (interruptedioexception e ){}
Try {
If (IR = is1.read (bytes)> 0 ){
Os1.write (bytes, 0, IR );
If (logging) writelog (bytes, 0, IR, false );
}
Else if (IR <0)
Break;
} Catch (interruptedioexception e ){}
}
} Catch (exception E0 ){
// System. Out. println ("pipe exception:" + E0 );
}
}

Static public void startproxy (INT port, class clobj ){
Serversocket ssock;
Socket sock;
Try {
Ssock = new serversocket (port );
While (true ){
Class [] Sarg = new class [1];
Object [] Arg = new object [1];
Sarg [0] = socket. Class;
Try {
Java. Lang. Reflect. constructor cons = clobj. getdeclaredconstructor (Sarg );
Arg [0] = ssock. Accept ();
Cons. newinstance (ARG); // create an instance of httpproxy or its derived class
} Catch (exception e ){
Socket esock = (socket) Arg [0];
Try {esock. Close ();} catch (exception EC ){}
}
}
} Catch (ioexception e ){
}
}

// Simple main method used for testing
Static public void main (string ARGs []) {
System. Out. println ("START Proxy Server/n on port 808 ");
Httpproxy. log = system. out;
Httpproxy. Logging = false;
Httpproxy. startproxy (808, httpproxy. Class );
}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.