Skip to content

Commit

Permalink
Refine code, switch to log4j2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bestoa committed Apr 17, 2016
1 parent fe73c6b commit 0ed4a4e
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 29 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version '0.5'
version '0.5.1'

apply plugin: 'java'

Expand All @@ -11,6 +11,8 @@ repositories {
dependencies {
compile 'gnu.getopt:java-getopt:1.0.13'
compile 'org.bouncycastle:bcprov-jdk15on:1.54'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.5'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.5'
}

jar() {
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/shadowsocks/SSLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,29 @@
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import shadowsocks.util.Config;
import shadowsocks.nio.tcp.SSNioTcpRelayLocalUnit;
import shadowsocks.nio.tcp.SSTcpRelayLocalUnit;
import shadowsocks.crypto.CryptoFactory;

public class SSLocal {

public static Logger log = LogManager.getLogger(SSLocal.class.getName());

public void start()
{
int lport = Config.get().getLocalPort();
Executor service = Executors.newCachedThreadPool();
try(ServerSocketChannel server = ServerSocketChannel.open()) {
server.bind(new InetSocketAddress(lport));
server.setOption(StandardSocketOptions.SO_REUSEADDR, true);
System.out.println("starting client at " + server.socket().getLocalSocketAddress());
log.info("Starting local at " + server.socket().getLocalSocketAddress());
while (true) {
SocketChannel local = server.accept();
local.setOption(StandardSocketOptions.TCP_NODELAY, true);
service.execute(new SSNioTcpRelayLocalUnit(local));
service.execute(new SSTcpRelayLocalUnit(local));
}
}catch(IOException e){
e.printStackTrace();
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/shadowsocks/SSMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@
*/
package shadowsocks;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import shadowsocks.util.Config;
import shadowsocks.crypto.CryptoFactory;

public class SSMain{

public static Logger log = LogManager.getLogger(SSMain.class.getName());

public static final String VERSION = "0.5.1";

public static void main(String argv[])
{
System.out.println("Shadowsocks v0.5");
log.info("Shadowsocks " + VERSION);
Config.getConfigFromArgv(argv);
//make sure this method could work.
try{
CryptoFactory.create(Config.get().getMethod(), Config.get().getPassword());
}catch(Exception e){
e.printStackTrace();
log.error("Error crypto method", e);
return;
}
if(Config.get().isServerMode()){
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/shadowsocks/SSServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,29 @@
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import shadowsocks.util.Config;
import shadowsocks.nio.tcp.SSNioTcpRelayServerUnit;
import shadowsocks.nio.tcp.SSTcpRelayServerUnit;
import shadowsocks.crypto.CryptoFactory;

public class SSServer {

public static Logger log = LogManager.getLogger(SSServer.class.getName());

public void start()
{
int port = Config.get().getPort();
Executor service = Executors.newCachedThreadPool();
try(ServerSocketChannel server = ServerSocketChannel.open()) {
server.bind(new InetSocketAddress(port));
server.setOption(StandardSocketOptions.SO_REUSEADDR, true);
System.out.println("starting server at " + server.socket().getLocalSocketAddress());
log.info("Starting server at " + server.socket().getLocalSocketAddress());
while (true) {
SocketChannel local = server.accept();
local.setOption(StandardSocketOptions.TCP_NODELAY, true);
service.execute(new SSNioTcpRelayServerUnit(local));
service.execute(new SSTcpRelayServerUnit(local));
}
}catch(IOException e){
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.net.StandardSocketOptions;
import java.util.Iterator;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import shadowsocks.crypto.SSCrypto;
import shadowsocks.crypto.CryptoFactory;
import shadowsocks.crypto.CryptoException;
Expand All @@ -37,7 +40,9 @@
import shadowsocks.auth.HmacSHA1;
import shadowsocks.auth.AuthException;

public class SSNioTcpRelayLocalUnit implements Runnable {
public class SSTcpRelayLocalUnit implements Runnable {

public static Logger log = LogManager.getLogger(SSTcpRelayLocalUnit.class.getName());

final private int BUFF_LEN = 16384; /* 16K */

Expand All @@ -51,6 +56,8 @@ public class SSNioTcpRelayLocalUnit implements Runnable {

private SocketChannel mClient;

InetSocketAddress mTargetAddress;

public SSCrypto mCryptor;

// Read buffer
Expand Down Expand Up @@ -175,7 +182,8 @@ private void parseHead(SocketChannel local, SocketChannel remote) throws IOExcep
while(mBuffer.hasRemaining())
local.write(mBuffer);

System.out.println("Target = " + addr + " port = " + port);
mTargetAddress = new InetSocketAddress(addr, port);
log.info("Target address is " + mTargetAddress);

// Create auth head
if (mOneTimeAuth){
Expand Down Expand Up @@ -284,7 +292,7 @@ private void TcpRelay(SocketChannel local) throws IOException, CryptoException,
}catch(InterruptedException e){
//ignore
}catch(IOException | CryptoException e){
e.printStackTrace();
log.error("Target address is " + mTargetAddress, e);
}

}
Expand All @@ -301,11 +309,11 @@ public void run()
mOneTimeAuth = Config.get().isOTAEnabled();
TcpRelay(client);
}catch(Exception e){
e.printStackTrace();
log.error("Target address is " + mTargetAddress, e);
}
}

public SSNioTcpRelayLocalUnit (SocketChannel c)
public SSTcpRelayLocalUnit (SocketChannel c)
{
mClient = c;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.net.StandardSocketOptions;
import java.util.Iterator;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import shadowsocks.crypto.SSCrypto;
import shadowsocks.crypto.CryptoFactory;
import shadowsocks.crypto.CryptoException;
Expand All @@ -37,7 +40,9 @@
import shadowsocks.auth.HmacSHA1;
import shadowsocks.auth.AuthException;

public class SSNioTcpRelayServerUnit implements Runnable {
public class SSTcpRelayServerUnit implements Runnable {

public static Logger log = LogManager.getLogger(SSTcpRelayServerUnit.class.getName());

final private int BUFF_LEN = 16384; /* 16K */

Expand Down Expand Up @@ -322,7 +327,7 @@ private void TcpRelay(SocketChannel local) throws IOException, CryptoException,
Selector selector = Selector.open();)
{
remote.setOption(StandardSocketOptions.TCP_NODELAY, true);
System.out.println("Connecting " + mRemoteAddress + " from " + local.socket().getRemoteSocketAddress());
log.info("Connecting " + mRemoteAddress + " from " + local.socket().getRemoteSocketAddress());
//Still use socket with timeout since some time, remote is unreachable, then client closed
//but this thread is still hold. This will decrease CLOSE_wait state
remote.socket().connect(mRemoteAddress, CONNECT_TIMEOUT);
Expand All @@ -334,8 +339,7 @@ private void TcpRelay(SocketChannel local) throws IOException, CryptoException,
}catch(InterruptedException e){
//ignore
}catch(IOException | CryptoException e){
System.err.println("Target address: " + mRemoteAddress);
e.printStackTrace();
log.error("Target address is " + mRemoteAddress, e);
}

}
Expand All @@ -353,11 +357,11 @@ public void run()
mExpectAuthResult = new byte[HmacSHA1.AUTH_LEN];
TcpRelay(client);
}catch(Exception e){
e.printStackTrace();
log.error("Target address is " + mRemoteAddress, e);
}
}

public SSNioTcpRelayServerUnit (SocketChannel c)
public SSTcpRelayServerUnit (SocketChannel c)
{
mClient = c;
}
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/shadowsocks/util/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
*/
package shadowsocks.util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import gnu.getopt.Getopt;

public class Config{

public static Logger log = LogManager.getLogger(Config.class.getName());

private static Config mConfig;

private String mPassword;
Expand Down Expand Up @@ -127,41 +132,41 @@ public static void getConfigFromArgv(String argv[])
{
case 'm':
arg = g.getOptarg();
System.out.println("Get method: " + arg);
log.info("Crypto method: " + arg);
Config.get().setMethod(arg);
break;
case 'k':
arg = g.getOptarg();
System.out.println("Get key: " + arg);
log.info("Password: " + arg);
Config.get().setPassowrd(arg);
break;
case 'p':
arg = g.getOptarg();
int port = Integer.parseInt(arg);
System.out.println("Get port: " + port);
log.info("Port: " + port);
Config.get().setPort(port);
break;
case 'a':
System.out.println("OTA enforcing mode.");
log.info("OTA enforcing mode.");
Config.get().setOTAEnabled(true);
break;
case 'S':
System.out.println("Server mode.");
log.info("Server mode.");
Config.get().setServerMode(true);
break;
case 'L':
System.out.println("Local mode.");
log.info("Local mode.");
Config.get().setServerMode(false);
break;
case 's':
arg = g.getOptarg();
System.out.println("Get server: " + arg);
log.info("Server address: " + arg);
Config.get().setServer(arg);
break;
case 'l':
arg = g.getOptarg();
int lport = Integer.parseInt(arg);
System.out.println("Get local port: " + lport);
log.info("Local port: " + lport);
Config.get().setLocalPort(lport);
break;
case '?':
Expand All @@ -175,6 +180,5 @@ public static void getConfigFromArgv(String argv[])
private static void help()
{
//TODO
System.out.println("HELP");
}
}
13 changes: 13 additions & 0 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

0 comments on commit 0ed4a4e

Please sign in to comment.