Skip to content

Commit ccb473e

Browse files
author
Jose Damico
committed
Code normalized.
Created a interface for sock impl.
1 parent 529fa02 commit ccb473e

File tree

7 files changed

+228
-275
lines changed

7 files changed

+228
-275
lines changed

java-socks-proxy-server/src/org/jdamico/socks/server/commons/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ public interface Constants {
2525
public static final byte SC_CONNECT = 0x01;
2626
public static final byte SC_BIND = 0x02;
2727
public static final byte SC_UDP = 0x03;
28+
29+
public static final int MAX_ADDR_LEN = 255;
2830
}

java-socks-proxy-server/src/org/jdamico/socks/server/commons/DebugLog.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55

66

7-
import java.net.Socket;
8-
import java.net.DatagramPacket;
9-
import java.net.InetAddress;
7+
import java.net.DatagramPacket;
8+
import java.net.Socket;
109

1110
import org.jdamico.socks.server.StartProxy;
1211

@@ -49,21 +48,13 @@ public void error( Exception e ) {
4948
e.printStackTrace();
5049
}
5150

52-
/////////////////////////////////////////////////
53-
54-
public String iP2Str( InetAddress IP ) {
55-
if( IP == null ) return "NA/NA";
56-
57-
return IP.getHostName()+"/"+IP.getHostAddress();
58-
}
59-
60-
/////////////////////////////////////////////////
51+
6152

6253
public String getSocketInfo( Socket sock ) {
6354

6455
if( sock == null ) return "<NA/NA:0>";
6556

66-
return "<"+iP2Str( sock.getInetAddress() )+":"+
57+
return "<"+Utils.getInstance().iP2Str( sock.getInetAddress() )+":"+
6758
sock.getPort() + ">";
6859
}
6960

@@ -73,7 +64,7 @@ public String getSocketInfo( DatagramPacket DGP ) {
7364

7465
if( DGP == null ) return "<NA/NA:0>";
7566

76-
return "<"+iP2Str( DGP.getAddress() )+":"+
67+
return "<"+Utils.getInstance().iP2Str( DGP.getAddress() )+":"+
7768
DGP.getPort() + ">";
7869
}
7970

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.jdamico.socks.server.commons;
2+
3+
import java.net.InetAddress;
4+
import java.net.UnknownHostException;
5+
6+
public class Utils {
7+
8+
private static Utils INSTANCE = null;
9+
private Utils() {}
10+
11+
public static Utils getInstance(){
12+
if(INSTANCE == null) INSTANCE = new Utils();
13+
return INSTANCE;
14+
}
15+
16+
17+
public InetAddress calcInetAddress( byte[] addr ) {
18+
InetAddress IA = null;
19+
String sIA = "";
20+
21+
if( addr.length < 4 ) {
22+
DebugLog.getInstance().error( "calcInetAddress() - Invalid length of IP v4 - "+addr.length+" bytes" );
23+
return null;
24+
}
25+
26+
// IP v4 Address Type
27+
for( int i=0; i<4; i++ ) {
28+
sIA += byte2int( addr[i] );
29+
if( i<3 ) sIA += ".";
30+
}
31+
32+
try {
33+
IA = InetAddress.getByName( sIA );
34+
}
35+
catch( UnknownHostException e ) {
36+
return null;
37+
}
38+
39+
return IA; // IP Address
40+
}
41+
42+
public int byte2int( byte b ) {
43+
int res = b;
44+
if( res < 0 ) res = (int)( 0x100 + res );
45+
return res;
46+
}
47+
48+
49+
public int calcPort( byte Hi, byte Lo ) {
50+
51+
return ( (byte2int( Hi ) << 8) | byte2int( Lo ) );
52+
}
53+
54+
55+
public String iP2Str( InetAddress IP ) {
56+
if( IP == null ) return "NA/NA";
57+
58+
return IP.getHostName()+"/"+IP.getHostAddress();
59+
}
60+
61+
}

java-socks-proxy-server/src/org/jdamico/socks/server/impl/ProxyHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,19 +230,19 @@ public void processRelay() {
230230
}
231231
DebugLog.getInstance().println( "Accepted SOCKS "+SOCKS_Version+" Request." );
232232

233-
comm.Authenticate( SOCKS_Version );
234-
comm.GetClientCommand();
233+
comm.authenticate( SOCKS_Version );
234+
comm.getClientCommand();
235235

236-
switch ( comm.Command ) {
237-
case Constants.SC_CONNECT : comm.Connect();
236+
switch ( comm.socksCommand ) {
237+
case Constants.SC_CONNECT : comm.connect();
238238
relay();
239239
break;
240240

241-
case Constants.SC_BIND : comm.Bind();
241+
case Constants.SC_BIND : comm.bind();
242242
relay();
243243
break;
244244

245-
case Constants.SC_UDP : comm.UDP();
245+
case Constants.SC_UDP : comm.udp();
246246
break;
247247
}
248248
}

0 commit comments

Comments
 (0)