Skip to content

Commit 529fa02

Browse files
author
Jose Damico
committed
DebugLog now is an singleton
1 parent 363eb01 commit 529fa02

File tree

7 files changed

+108
-156
lines changed

7 files changed

+108
-156
lines changed
Binary file not shown.

java-socks-proxy-server/src/org/jdamico/socks/server/StartProxy.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33

44
import org.jdamico.socks.server.commons.Constants;
5-
import org.jdamico.socks.server.commons.DebugLog;
65
import org.jdamico.socks.server.impl.ProxyServerInitiator;
76

87

98
public class StartProxy {
109

11-
10+
public static boolean enableLog = false;
1211
public static void main(String[] args) {
1312

1413

15-
DebugLog.EnableLog = true;
14+
enableLog = true;
1615

1716
new ProxyServerInitiator(Constants.LISTEN_PORT, Constants.PROXY_HOST, Constants.PROXY_PORT ).start();
1817
}
Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,81 @@
1-
/*************************************************************************
2-
FILE : Log.java
31

4-
Author : Svetoslav Tchekanov ([email protected])
5-
6-
Description: Log class definition.
7-
8-
Log.class is the logging system of the SSH Proxy
9-
10-
11-
Copyright notice:
12-
Written by Svetoslav Tchekanov ([email protected])
13-
Copyright(c) 2000
14-
15-
This code may be used in compiled form in any way you desire. This
16-
file may be redistributed unmodified by any means PROVIDING it is
17-
not sold for profit without the authors written consent, and
18-
providing that this notice and the authors name is included. If
19-
the source code in this file is used in any commercial application
20-
then a simple email would be nice.
21-
22-
This file is provided "as is" with no expressed or implied warranty.
23-
The author accepts no liability if it causes any damage to your
24-
computer.
25-
26-
*************************************************************************/
272

283
package org.jdamico.socks.server.commons;
294

30-
///////////////////////////////////////////////
5+
316

327
import java.net.Socket;
338
import java.net.DatagramPacket;
349
import java.net.InetAddress;
3510

36-
///////////////////////////////////////////////
11+
import org.jdamico.socks.server.StartProxy;
12+
13+
3714

3815
public class DebugLog
3916
{
40-
41-
public static final String EOL = "\r\n";
4217

43-
public static boolean EnableLog = true;
44-
45-
/////////////////////////////////////////////////
18+
private static DebugLog INSTANCE = null;
19+
private DebugLog() {}
4620

47-
public static void Println( String txt ) {
48-
if( EnableLog ) Print( txt + EOL );
21+
public static DebugLog getInstance(){
22+
if(INSTANCE == null) INSTANCE = new DebugLog();
23+
return INSTANCE;
4924
}
25+
5026

51-
/////////////////////////////////////////////////
27+
public void println( String txt ) {
28+
if( StartProxy.enableLog ) print( txt + Constants.EOL );
29+
}
30+
5231

53-
public static void Print( String txt ) {
54-
if( !EnableLog ) return;
32+
public void print( String txt ) {
33+
if( !StartProxy.enableLog ) return;
5534
if( txt == null ) return;
5635
System.out.print( txt );
5736
}
5837

5938
/////////////////////////////////////////////////
6039

61-
public static void Error( String txt ) {
62-
if( EnableLog ) Println( "Error : " + txt );
40+
public void error( String txt ) {
41+
if( StartProxy.enableLog ) println( "Error : " + txt );
6342
}
6443

6544
/////////////////////////////////////////////////
6645

67-
public static void Error( Exception e ) {
68-
if( !EnableLog ) return;
69-
Println( "ERROR : " + e.toString() );
46+
public void error( Exception e ) {
47+
if( !StartProxy.enableLog ) return;
48+
println( "ERROR : " + e.toString() );
7049
e.printStackTrace();
7150
}
7251

7352
/////////////////////////////////////////////////
7453

75-
public static String IP2Str( InetAddress IP ) {
54+
public String iP2Str( InetAddress IP ) {
7655
if( IP == null ) return "NA/NA";
7756

7857
return IP.getHostName()+"/"+IP.getHostAddress();
7958
}
8059

8160
/////////////////////////////////////////////////
8261

83-
public static String getSocketInfo( Socket sock ) {
62+
public String getSocketInfo( Socket sock ) {
8463

8564
if( sock == null ) return "<NA/NA:0>";
8665

87-
return "<"+IP2Str( sock.getInetAddress() )+":"+
66+
return "<"+iP2Str( sock.getInetAddress() )+":"+
8867
sock.getPort() + ">";
8968
}
9069

91-
/////////////////////////////////////////////////
9270

93-
public static String getSocketInfo( DatagramPacket DGP ) {
71+
72+
public String getSocketInfo( DatagramPacket DGP ) {
9473

9574
if( DGP == null ) return "<NA/NA:0>";
9675

97-
return "<"+IP2Str( DGP.getAddress() )+":"+
76+
return "<"+iP2Str( DGP.getAddress() )+":"+
9877
DGP.getPort() + ">";
9978
}
10079

101-
/////////////////////////////////////////////////
80+
10281
}
103-
/////////////////////////////////////////////////////

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public ProxyHandler(Socket clientSocket) {
4242
m_ClientSocket.setSoTimeout( Constants.DEFAULT_PROXY_TIMEOUT );
4343
}
4444
catch( SocketException e ) {
45-
DebugLog.Error( "Socket Exception during seting Timeout." );
45+
DebugLog.getInstance().error( "Socket Exception during seting Timeout." );
4646
}
4747
}
4848

4949
m_Buffer = new byte[ Constants.DEFAULT_BUF_SIZE ];
5050

51-
DebugLog.Println( "Proxy Created." );
51+
DebugLog.getInstance().println( "Proxy Created." );
5252
}
5353

5454
public void setLock( Object lock ) {
@@ -59,7 +59,7 @@ public void start()
5959
{
6060
m_TheThread = new Thread( this );
6161
m_TheThread.start();
62-
DebugLog.Println( "Proxy Started." );
62+
DebugLog.getInstance().println( "Proxy Started." );
6363
}
6464

6565
public void stop() {
@@ -74,7 +74,7 @@ public void stop() {
7474
m_ClientSocket = null;
7575
m_ServerSocket = null;
7676

77-
DebugLog.Println( "Proxy Stopped." );
77+
DebugLog.getInstance().println( "Proxy Stopped." );
7878

7979
m_TheThread.interrupt();
8080
}
@@ -84,7 +84,7 @@ public void run()
8484
setLock( this );
8585

8686
if( !prepareClient() ) {
87-
DebugLog.Error( "Proxy - client socket is null !" );
87+
DebugLog.getInstance().error( "Proxy - client socket is null !" );
8888
return;
8989
}
9090

@@ -130,7 +130,7 @@ public void close() {
130130
m_ServerSocket = null;
131131
m_ClientSocket = null;
132132

133-
DebugLog.Println( "Proxy Closed." );
133+
DebugLog.getInstance().println( "Proxy Closed." );
134134
}
135135

136136
public void sendToClient( byte[] buffer ) {
@@ -146,7 +146,7 @@ public void sendToClient( byte[] buffer, int len ) {
146146
m_ClientOutput.flush();
147147
}
148148
catch( IOException e ) {
149-
DebugLog.Error( "Sending data to client" );
149+
DebugLog.getInstance().error( "Sending data to client" );
150150
}
151151
}
152152

@@ -163,7 +163,7 @@ public void sendToServer( byte[] buffer, int len ) {
163163
m_ServerOutput.flush();
164164
}
165165
catch( IOException e ) {
166-
DebugLog.Error( "Sending data to server" );
166+
DebugLog.getInstance().error( "Sending data to server" );
167167
}
168168
}
169169

@@ -177,14 +177,14 @@ public void connectToServer( String server, int port ) throws IOException, Unkno
177177

178178
if( server.equals("") ) {
179179
close();
180-
DebugLog.Error( "Invalid Remote Host Name - Empty String !!!" );
180+
DebugLog.getInstance().error( "Invalid Remote Host Name - Empty String !!!" );
181181
return;
182182
}
183183

184184
m_ServerSocket = new Socket( server, port );
185185
m_ServerSocket.setSoTimeout( Constants.DEFAULT_PROXY_TIMEOUT );
186186

187-
DebugLog.Println( "Connected to "+DebugLog.getSocketInfo( m_ServerSocket ) );
187+
DebugLog.getInstance().println( "Connected to "+DebugLog.getInstance().getSocketInfo( m_ServerSocket ) );
188188
prepareServer();
189189
}
190190

@@ -204,8 +204,8 @@ public boolean prepareClient() {
204204
m_ClientOutput= m_ClientSocket.getOutputStream();
205205
}
206206
catch( IOException e ) {
207-
DebugLog.Error( "Proxy - can't get I/O streams!" );
208-
DebugLog.Error( e );
207+
DebugLog.getInstance().error( "Proxy - can't get I/O streams!" );
208+
DebugLog.getInstance().error( e );
209209
return false;
210210
}
211211
return true;
@@ -225,10 +225,10 @@ public void processRelay() {
225225
break;
226226
case Constants.SOCKS5_Version: comm = new Socks5Impl( this );
227227
break;
228-
default: DebugLog.Error( "Invalid SOKCS version : "+SOCKS_Version );
228+
default: DebugLog.getInstance().error( "Invalid SOKCS version : "+SOCKS_Version );
229229
return;
230230
}
231-
DebugLog.Println( "Accepted SOCKS "+SOCKS_Version+" Request." );
231+
DebugLog.getInstance().println( "Accepted SOCKS "+SOCKS_Version+" Request." );
232232

233233
comm.Authenticate( SOCKS_Version );
234234
comm.GetClientCommand();
@@ -247,7 +247,7 @@ public void processRelay() {
247247
}
248248
}
249249
catch( Exception e ) {
250-
DebugLog.Error( e );
250+
DebugLog.getInstance().error( e );
251251
}
252252
}
253253

@@ -318,7 +318,7 @@ public int checkClientData() {
318318
return 0;
319319
}
320320
catch( IOException e ) {
321-
DebugLog.Println( "Client connection Closed!" );
321+
DebugLog.getInstance().println( "Client connection Closed!" );
322322
close(); // Close the server on this exception
323323
return -1;
324324
}
@@ -345,7 +345,7 @@ public int checkServerData() {
345345
return 0;
346346
}
347347
catch( IOException e ) {
348-
DebugLog.Println( "Server connection Closed!" );
348+
DebugLog.getInstance().println( "Server connection Closed!" );
349349
close(); // Close the server on this exception
350350
return -1;
351351
}
@@ -357,8 +357,8 @@ public int checkServerData() {
357357
}
358358

359359
public void logServerData( int traffic ) {
360-
DebugLog.Println("Srv data : "+
361-
DebugLog.getSocketInfo( m_ClientSocket ) +
360+
DebugLog.getInstance().println("Srv data : "+
361+
DebugLog.getInstance().getSocketInfo( m_ClientSocket ) +
362362
" << <"+
363363
comm.m_ServerIP.getHostName()+"/"+
364364
comm.m_ServerIP.getHostAddress()+":"+
@@ -368,8 +368,8 @@ public void logServerData( int traffic ) {
368368

369369

370370
public void logClientData( int traffic ) {
371-
DebugLog.Println("Cli data : "+
372-
DebugLog.getSocketInfo( m_ClientSocket ) +
371+
DebugLog.getInstance().println("Cli data : "+
372+
DebugLog.getInstance().getSocketInfo( m_ClientSocket ) +
373373
" >> <"+
374374
comm.m_ServerIP.getHostName()+"/"+
375375
comm.m_ServerIP.getHostAddress()+":"+

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public ProxyServerInitiator(int listenPort, String proxyHost, int proxyPort) {
3232

3333
m_lock = this;
3434
m_nPort = listenPort;
35-
DebugLog.Println( "SOCKS Server Created." );
35+
DebugLog.getInstance().println( "SOCKS Server Created." );
3636
}
3737

3838
public void setLock( Object lock ) {
@@ -43,12 +43,12 @@ public void setLock( Object lock ) {
4343
public void start() {
4444
m_TheThread = new Thread( this );
4545
m_TheThread.start();
46-
DebugLog.Println( "SOCKS Server Started." );
46+
DebugLog.getInstance().println( "SOCKS Server Started." );
4747
}
4848

4949
public void stop() {
5050

51-
DebugLog.Println( "SOCKS Server Stopped." );
51+
DebugLog.getInstance().println( "SOCKS Server Stopped." );
5252
m_TheThread.interrupt();
5353
}
5454

@@ -70,7 +70,7 @@ public void close() {
7070
}
7171
m_ListenSocket = null;
7272

73-
DebugLog.Println( "SOCKS Server Closed." );
73+
DebugLog.getInstance().println( "SOCKS Server Closed." );
7474
}
7575

7676
public boolean isActive() {
@@ -87,7 +87,7 @@ private void prepareToListen() throws java.net.BindException, IOException {
8787
if( m_nPort == 0 ) {
8888
m_nPort = m_ListenSocket.getLocalPort();
8989
}
90-
DebugLog.Println( "SOCKS Server Listen at Port : " + m_nPort );
90+
DebugLog.getInstance().println( "SOCKS Server Listen at Port : " + m_nPort );
9191
}
9292
}
9393

@@ -98,12 +98,12 @@ protected void listen() {
9898
prepareToListen();
9999
}
100100
catch( java.net.BindException e ) {
101-
DebugLog.Error( "The Port "+m_nPort+" is in use !" );
102-
DebugLog.Error( e );
101+
DebugLog.getInstance().error( "The Port "+m_nPort+" is in use !" );
102+
DebugLog.getInstance().error( e );
103103
return;
104104
}
105105
catch( IOException e ) {
106-
DebugLog.Error( "IO Error Binding at port : "+m_nPort );
106+
DebugLog.getInstance().error( "IO Error Binding at port : "+m_nPort );
107107
return;
108108
}
109109

@@ -123,15 +123,15 @@ public void checkClientConnection() {
123123
{
124124
Socket clientSocket = m_ListenSocket.accept();
125125
clientSocket.setSoTimeout( Constants.DEFAULT_SERVER_TIMEOUT );
126-
DebugLog.Println( "Connection from : " + DebugLog.getSocketInfo( clientSocket ) );
126+
DebugLog.getInstance().println( "Connection from : " + DebugLog.getInstance().getSocketInfo( clientSocket ) );
127127
ProxyHandler proxy = new ProxyHandler(clientSocket );
128128
proxy.start();
129129
}
130130
catch( InterruptedIOException e ) {
131131
// This exception is thrown when accept timeout is expired
132132
}
133133
catch( Exception e ) {
134-
DebugLog.Error( e );
134+
DebugLog.getInstance().error( e );
135135
}
136136
} // synchronized
137137
}

0 commit comments

Comments
 (0)