Skip to content

Commit

Permalink
Merge pull request #21 from bestoa/dev
Browse files Browse the repository at this point in the history
Merge dev to master.
  • Loading branch information
Bestoa committed May 15, 2016
2 parents c595c10 + f459fd8 commit 2fa6d56
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 124 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ It will remove complex funtions.
This need JDK 8.

#Current state:
Simple server + client. Version 0.6.1
Simple server + client. Version 0.6.2

Support these args:

Expand All @@ -20,8 +20,8 @@ Support these args:
4. -a OTA enforcing mode
5. -l local port
6. -s server
7. -S server mode(default)
8. -L Local mode(client)
7. -S server mode
8. -L Local mode(client, default)
9. -c config file

Crypto method:
Expand Down Expand Up @@ -63,12 +63,12 @@ $ bin/shadowsocks -L ...
$ gradle fatJar
```

Then you will get shadowsocks-java-xx.jar in build/libs.
Then you will get shadowsocks-fat-xx.jar in build/libs.

#### How to run
```
//Server
$ java -jar shadowsocks-java-xx.jar -S ...
$ java -jar shadowsocks-fat-xx.jar -S ...
//Local
$ java -jar shadowsocks-java-xx.jar -L ...
$ java -jar shadowsocks-fat-xx.jar -L ...
```
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version '0.6.1'
version '0.6.2'

apply plugin: 'java'
apply plugin: 'application'
Expand Down Expand Up @@ -28,6 +28,8 @@ task ('fatJar', type: Jar, dependsOn: classes){
'Implementation-Version': version,
'Main-Class': 'shadowsocks.Main'
}
baseName = 'shadowsocks'
appendix = 'fat'
from {configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }} //pack dependent jar
from 'build/classes/main'
from 'build/resources/main'
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/shadowsocks/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Main{

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

public static final String VERSION = "0.6.1";
public static final String VERSION = "0.6.2";

public static void main(String argv[])
{
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/shadowsocks/nio/tcp/BufferHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2016 Author:NU11 bestoapache@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package shadowsocks.nio.tcp;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.io.IOException;

public class BufferHelper {
/* return true means send success */
public static boolean writeToRemote(SocketChannel remote, ByteBuffer buffer) throws IOException
{
//Add timeout to avoid 100% cpu when write failed for a long time.
long timeout = System.currentTimeMillis() + 15*1000L;
while(buffer.hasRemaining()) {
remote.write(buffer);
if (System.currentTimeMillis() > timeout) {
return false;
}
}
return true;
}

public static int readFormRemote(SocketChannel remote, ByteBuffer buffer) throws IOException
{
//Add timeout to avoid 100% cpu when write failed for a long time.
long timeout = System.currentTimeMillis() + 15*1000L;
int size = 0;
int total_size = 0;
while(buffer.hasRemaining()) {
size = remote.read(buffer);
if (size < 0)
break;
else
total_size += size;
if (System.currentTimeMillis() > timeout) {
break;
}
}
return total_size;
}
}
29 changes: 16 additions & 13 deletions src/main/java/shadowsocks/nio/tcp/LocalTcpWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class LocalTcpWorker extends TcpWorker {
* OTA will add 10 bytes HMAC-SHA1 in the end of the head.
*
*/
private void parseHead(SocketChannel local, SocketChannel remote) throws IOException, CryptoException, AuthException
private void parseHeader(SocketChannel local, SocketChannel remote) throws IOException, CryptoException, AuthException
{
//skip method list (max 1+1+255)
mBufferWrap.prepare(257);
Expand Down Expand Up @@ -101,15 +101,15 @@ private void parseHead(SocketChannel local, SocketChannel remote) throws IOExcep
mStreamUpData.write(data[3]);

//get addr
InetAddress addr;
StringBuffer addr = new StringBuffer();
if (addrtype == Session.ADDR_TYPE_IPV4) {
//get IPV4 address
mBufferWrap.prepare(4);
mBufferWrap.readWithCheck(local, 4);
data = mBuffer.array();
byte [] ipv4 = new byte[4];
System.arraycopy(data, 0, ipv4, 0, 4);
addr = InetAddress.getByAddress(ipv4);
addr.append(InetAddress.getByAddress(ipv4).toString());
mStreamUpData.write(data, 0, 4);
}else if (addrtype == Session.ADDR_TYPE_HOST) {
//get address len
Expand All @@ -122,13 +122,15 @@ private void parseHead(SocketChannel local, SocketChannel remote) throws IOExcep
mBufferWrap.prepare(len);
mBufferWrap.readWithCheck(local, len);
data = mBuffer.array();
addr = InetAddress.getByName(new String(data, 0, len));
addr.append(new String(data, 0, len));
mStreamUpData.write(data, 0, len);
} else {
//do not support other addrtype now.
throw new IOException("Unsupport addr type: " + addrtype + "!");
}

addr.append(':');

//get port
mBufferWrap.prepare(2);
mBufferWrap.readWithCheck(local, 2);
Expand All @@ -137,6 +139,10 @@ private void parseHead(SocketChannel local, SocketChannel remote) throws IOExcep

mStreamUpData.write(mBuffer.array(), 0, 2);

addr.append(port);
mSession.set(addr.toString(), false);
log.info("Target address: " + addr);

//reply
mBufferWrap.prepare(10);
// 05 00 00 01 + 0.0.0.0:8888
Expand All @@ -147,10 +153,6 @@ private void parseHead(SocketChannel local, SocketChannel remote) throws IOExcep
while(mBuffer.hasRemaining())
local.write(mBuffer);

InetSocketAddress target = new InetSocketAddress(addr, port);
mSession.set(target, false);
log.info("Target address: " + target);

// Create auth head
if (mOneTimeAuth){
byte [] authKey = SSAuth.prepareKey(mCryptor.getIV(true), mCryptor.getKey());
Expand All @@ -163,8 +165,7 @@ private void parseHead(SocketChannel local, SocketChannel remote) throws IOExcep
data = mStreamUpData.toByteArray();
byte [] result = mCryptor.encrypt(data, data.length);
ByteBuffer out = ByteBuffer.wrap(result);
while(out.hasRemaining())
remote.write(out);
BufferHelper.writeToRemote(remote, out);
}

@Override
Expand Down Expand Up @@ -207,8 +208,10 @@ protected boolean send(SocketChannel source, SocketChannel target, int direct) t
result = mCryptor.decrypt(mBuffer.array(), size);
}
ByteBuffer out = ByteBuffer.wrap(result);
while(out.hasRemaining())
target.write(out);
if (!BufferHelper.writeToRemote(target, out)) {
mSession.dump(log, new IOException("Some data send failed."));
return true;
}
return false;
}

Expand All @@ -222,7 +225,7 @@ protected InetSocketAddress getRemoteAddress(SocketChannel local/*unused*/)
protected void preTcpRelay(SocketChannel local, SocketChannel remote)
throws IOException, CryptoException, AuthException
{
parseHead(local, remote);
parseHeader(local, remote);
}
@Override
protected void postTcpTelay(SocketChannel local, SocketChannel remote)
Expand Down
Loading

0 comments on commit 2fa6d56

Please sign in to comment.