Permalink
Browse files

Move to AsyncHttpClient

  • Loading branch information...
1 parent 16916e7 commit c2ec2eee915fec4cb9df1ebdb459ddef661a7fb0 @untoldwind committed Jun 9, 2012
Showing with 242 additions and 434 deletions.
  1. +5 −0 client/src/main/java/com/objectcode/lostsocks/client/Main.java
  2. +4 −6 client/src/main/java/com/objectcode/lostsocks/client/config/IConfiguration.java
  3. +5 −5 client/src/main/java/com/objectcode/lostsocks/client/config/PasswordEncoder.java
  4. +4 −5 client/src/main/java/com/objectcode/lostsocks/client/config/PersistenceServiceConfiguration.java
  5. +34 −65 client/src/main/java/com/objectcode/lostsocks/client/config/PropertyFileConfiguration.java
  6. +10 −17 client/src/main/java/com/objectcode/lostsocks/client/engine/CompressedPacket.java
  7. +0 −136 client/src/main/java/com/objectcode/lostsocks/client/engine/EasySSLProtocolSocketFactory.java
  8. +0 −121 client/src/main/java/com/objectcode/lostsocks/client/engine/EasyX509TrustManager.java
  9. +3 −3 client/src/main/java/com/objectcode/lostsocks/client/engine/NIOGenericServer.java
  10. +20 −24 client/src/main/java/com/objectcode/lostsocks/client/engine/NIOServerBase.java
  11. +3 −3 client/src/main/java/com/objectcode/lostsocks/client/engine/NIOSocksServer.java
  12. +25 −21 client/src/main/java/com/objectcode/lostsocks/client/engine/RequestType.java
  13. +3 −3 client/src/main/java/com/objectcode/lostsocks/client/swing/MainFrame.java
  14. +14 −0 client/src/main/resources/logback.xml
  15. +4 −2 project/Build.scala
  16. +29 −19 server/app/controllers/Api.scala
  17. +3 −3 server/app/engine/ConnectionActor.scala
  18. +74 −0 server/app/engine/DownStreamReceiver.scala
  19. +1 −1 server/app/engine/ExtendedConnection.scala
  20. +1 −0 server/conf/routes
@@ -1,8 +1,11 @@
package com.objectcode.lostsocks.client;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.core.util.StatusPrinter;
import com.objectcode.lostsocks.client.config.IConfiguration;
import com.objectcode.lostsocks.client.config.PropertyFileConfiguration;
import com.objectcode.lostsocks.client.swing.MainFrame;
+import org.slf4j.LoggerFactory;
import java.io.File;
@@ -11,6 +14,8 @@
*/
public class Main {
public static void main(String[] args) {
+ LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+ StatusPrinter.print(lc);
File configurationDir = new File(System.getProperty("user.home") + File.separator + ".sockstohttp");
@@ -1,7 +1,7 @@
package com.objectcode.lostsocks.client.config;
-import org.apache.http.HttpHost;
-import org.apache.http.client.HttpClient;
+import com.ning.http.client.AsyncHttpClient;
+import com.ning.http.client.Realm;
import java.net.URL;
@@ -73,9 +73,7 @@
public void save();
- public HttpClient createHttpClient();
+ public AsyncHttpClient createHttpClient();
- public HttpHost getTargetHost();
-
- public String getTargetPath();
+ public Realm getRealm();
}
@@ -1,7 +1,7 @@
package com.objectcode.lostsocks.client.config;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.security.SecureRandom;
@@ -11,7 +11,7 @@
*/
public class PasswordEncoder
{
- private final static Log log = LogFactory.getLog( PasswordEncoder.class );
+ private static final Logger log = LoggerFactory.getLogger(PasswordEncoder.class);
private final static byte[] JIDDER = new byte[]{
-122, -103, -49, 116, 69, 23, -101, 15, 28, -97, 33, 3, -108, 119, 38, 25,
@@ -120,7 +120,7 @@ public static String encodePassword( String password )
return hex.toString();
}
catch ( Exception e ) {
- log.fatal( "Exception", e );
+ log.error( "Exception", e );
}
return "";
}
@@ -162,7 +162,7 @@ public static String decodePassword( String encoded )
return new String( uftpassword, "UTF-8" );
}
catch ( Exception e ) {
- log.fatal( "Exception", e );
+ log.error( "Exception", e );
}
return "";
}
@@ -1,11 +1,10 @@
package com.objectcode.lostsocks.client.config;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import javax.jnlp.FileContents;
import javax.jnlp.PersistenceService;
-
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Properties;
@@ -14,7 +13,7 @@
* @author junglas
*/
public class PersistenceServiceConfiguration extends PropertyFileConfiguration {
- private final static Log log = LogFactory.getLog(PersistenceServiceConfiguration.class);
+ private static final Logger log = LoggerFactory.getLogger(PersistenceServiceConfiguration.class);
PersistenceService m_persistenceService;
@@ -33,7 +32,7 @@ public PersistenceServiceConfiguration(PersistenceService persistenceService, UR
m_url = new URL(m_codebase, "socks");
m_urlString = m_url.toString();
} catch (Exception e) {
- log.fatal("Exception", e);
+ log.error("Exception", e);
}
}
@@ -1,32 +1,26 @@
package com.objectcode.lostsocks.client.config;
+import com.ning.http.client.AsyncHttpClient;
+import com.ning.http.client.AsyncHttpClientConfig;
+import com.ning.http.client.ProxyServer;
+import com.ning.http.client.Realm;
+import com.ning.http.client.providers.netty.NettyAsyncHttpProvider;
import com.objectcode.lostsocks.client.utils.PropertiesHelper;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.http.HttpHost;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.auth.params.AuthPNames;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.params.AuthPolicy;
-import org.apache.http.conn.params.ConnRoutePNames;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.conn.PoolingClientConnectionManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;
-import java.util.ArrayList;
-import java.util.List;
import java.util.Properties;
/**
* @author junglas
* @created 28. Mai 2004
*/
public class PropertyFileConfiguration implements IConfiguration {
- private final static Log log = LogFactory.getLog(PropertyFileConfiguration.class);
+ private static final Logger log = LoggerFactory.getLogger(PropertyFileConfiguration.class);
// Socks server settings
private int m_port = 1080;
@@ -82,9 +76,7 @@
private boolean m_configurationChanged = false;
- private HttpClient m_httpClient;
-
- private HttpHost m_targetHost;
+ private AsyncHttpClient m_httpClient;
private String m_targetPath;
@@ -122,14 +114,15 @@ public void setUrlString(String url) {
m_urlString = url;
try {
if (m_urlString != null) {
+ if (m_urlString.endsWith("/"))
+ m_urlString = m_urlString.substring(0, m_urlString.length() - 1);
m_url = new URL(m_urlString);
} else {
m_url = null;
}
- m_targetHost = null;
m_targetPath = null;
} catch (Exception e) {
- log.fatal("Exception", e);
+ log.error("Exception", e);
}
}
@@ -348,7 +341,7 @@ public void setProxyPassword(String proxyPassword) {
m_configurationChanged = true;
m_httpClient = null;
}
- m_proxyPassword = proxyPassword;
+ m_proxyPassword = proxyPassword != null && proxyPassword.length() > 0 ? proxyPassword : null;
}
/**
@@ -372,7 +365,7 @@ public void setProxyUser(String proxyUser) {
m_configurationChanged = true;
m_httpClient = null;
}
- m_proxyUser = proxyUser;
+ m_proxyUser = proxyUser != null && proxyUser.length() > 0 ? proxyUser : null;
}
/**
@@ -387,59 +380,35 @@ public void setUseProxy(boolean useProxy) {
m_useProxy = useProxy;
}
- public HttpHost getTargetHost() {
- if (m_targetHost == null) {
- m_targetHost = new HttpHost(m_url.getHost(), m_url.getPort(), m_url.getProtocol());
- m_targetPath = m_url.getPath();
- if (m_targetPath.endsWith("/"))
- m_targetPath = m_targetPath.substring(0, m_targetPath.length() - 1);
- }
- return m_targetHost;
- }
-
- public String getTargetPath() {
- if (m_targetPath == null) {
- m_targetHost = new HttpHost(m_url.getHost(), m_url.getPort(), m_url.getProtocol());
- m_targetPath = m_url.getPath();
- if (m_targetPath.endsWith("/"))
- m_targetPath = m_targetPath.substring(0, m_targetPath.length() - 1);
- }
- return m_targetPath;
- }
-
- public HttpClient createHttpClient() {
+ public AsyncHttpClient createHttpClient() {
if (m_httpClient == null) {
synchronized (this) {
- PoolingClientConnectionManager clientConnectionManager = new PoolingClientConnectionManager();
- DefaultHttpClient httpClient = new DefaultHttpClient(clientConnectionManager);
-
- httpClient.getCredentialsProvider().setCredentials(
- new AuthScope(getTargetHost().getHostName(), getTargetHost().getPort()),
- new UsernamePasswordCredentials(m_user, m_password));
+ AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
if (m_useProxy) {
- HttpHost proxy = new HttpHost(m_proxyHost, Integer.parseInt(m_proxyPort));
- httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
- if (m_proxyNeedsAuthentication) {
- List<String> authpref = new ArrayList<String>();
- authpref.add(AuthPolicy.BASIC);
- authpref.add(AuthPolicy.DIGEST);
- httpClient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF,
- authpref);
- httpClient.getCredentialsProvider().setCredentials(
- new AuthScope(proxy.getHostName(), proxy.getPort()),
- new UsernamePasswordCredentials(m_proxyUser, m_proxyPassword));
-
- }
+ ProxyServer proxyServer = new ProxyServer(m_proxyHost, Integer.parseInt(m_proxyPort), m_proxyUser, m_proxyPassword);
+
+ configBuilder = configBuilder.setProxyServer(proxyServer);
}
- m_httpClient = httpClient;
+ configBuilder.setMaxRequestRetry(3);
+ NettyAsyncHttpProvider provider = new NettyAsyncHttpProvider(configBuilder.build());
+ m_httpClient = new AsyncHttpClient(provider, configBuilder.build());
}
}
return m_httpClient;
}
+ public Realm getRealm() {
+ Realm.RealmBuilder builder = new Realm.RealmBuilder();
+ builder.setScheme(Realm.AuthScheme.BASIC);
+ builder.setPrincipal(m_user);
+ builder.setPassword(m_password);
+
+ return builder.build();
+ }
+
/**
* Description of the Method
*/
@@ -454,7 +423,7 @@ public void load() {
m_configurationChanged = false;
} catch (Exception e) {
- log.fatal("Exception", e);
+ log.error("Exception", e);
}
}
@@ -469,7 +438,7 @@ protected void readProperties(Properties properties) {
try {
m_url = new URL(m_urlString);
} catch (Exception e) {
- log.fatal("Exception", e);
+ log.error("Exception", e);
}
m_port = PropertiesHelper.getInt(properties, "socks.server.port", m_port);
@@ -527,7 +496,7 @@ public void save() {
try {
properties.store(new FileOutputStream(m_configurationFile), "Sock to HTTP");
} catch (Exception e) {
- log.fatal("Exception", e);
+ log.error("Exception", e);
}
m_configurationChanged = false;
}
@@ -1,27 +1,18 @@
package com.objectcode.lostsocks.client.engine;
-import com.objectcode.lostsocks.client.config.PropertyFileConfiguration;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.http.HttpEntity;
-import org.apache.http.entity.ByteArrayEntity;
-import org.apache.http.entity.ContentType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-import java.io.ByteArrayOutputStream;
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class CompressedPacket {
- private final static Log log = LogFactory.getLog(PropertyFileConfiguration.class);
+ private static final Logger log = LoggerFactory.getLogger(CompressedPacket.class);
private final byte[] data;
private final boolean endOfCommunication;
- private final ContentType CONTENT_TYPE = ContentType.create("application/x-compressed-bytes");
-
public CompressedPacket(byte[] data, boolean endOfCommunication) {
this.data = data;
this.endOfCommunication = endOfCommunication;
@@ -55,7 +46,7 @@ public boolean isEndOfCommunication() {
return endOfCommunication;
}
- public HttpEntity toEntity() {
+ public byte[] toBody() {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream zos = new GZIPOutputStream(bos);
@@ -65,16 +56,17 @@ public HttpEntity toEntity() {
dos.writeBoolean(endOfCommunication);
dos.flush();
dos.close();
- return new ByteArrayEntity(bos.toByteArray(), CONTENT_TYPE);
+ return bos.toByteArray();
} catch (Exception e) {
log.error("Encoding problem " + e, e);
throw new RuntimeException(e);
}
}
- public static CompressedPacket fromEntity(HttpEntity entity) {
+
+ public static CompressedPacket fromStream(InputStream in) {
try {
- GZIPInputStream gis = new GZIPInputStream(entity.getContent());
+ GZIPInputStream gis = new GZIPInputStream(in);
DataInputStream dis = new DataInputStream(gis);
int length = dis.readInt();
byte[] data = new byte[length];
@@ -88,4 +80,5 @@ public static CompressedPacket fromEntity(HttpEntity entity) {
throw new RuntimeException(e);
}
}
+
}
Oops, something went wrong.

0 comments on commit c2ec2ee

Please sign in to comment.