Comments (2)
This is how I am constructing the TorClient (in OSGi). Note: I have copied the classes over into my own package.
import com.subgraph.orchid.*;
import org.apache.felix.dm.annotation.api.Component;
import org.apache.felix.dm.annotation.api.Inject;
import org.apache.felix.dm.annotation.api.Start;
import org.apache.felix.dm.annotation.api.Stop;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import java.io.File;
@Component
public class TorClientComponent {
@Inject
private BundleContext bundleContext;
private ServiceRegistration<TorClient> serviceRegistration = null;
private TorConfig torConfig;
private DirectoryStore directoryStore;
private TorClient torClient;
@Start
public void start() throws Exception {
torConfig = Tor.createConfig();
File torData = bundleContext.getDataFile("torData");
if(torData.exists()) {
if(!torData.isDirectory()) {
throw new Exception("the tor directory should not be a file");
}
}
else {
torData.mkdir();
}
torConfig.setDataDirectory(torData);
directoryStore = new DirectoryStoreImpl(torConfig);
torClient = new TorClient(directoryStore);
torClient.start();
torClient.waitUntilReady(10000);
serviceRegistration = bundleContext.registerService(TorClient.class, torClient, null);
}
@Stop
public void stop() {
torClient.stop();
if(serviceRegistration != null) {
serviceRegistration.unregister();
serviceRegistration = null;
}
}
}
from orchid.
Why not just mimic what is done in TorClient.main(null);
https://github.com/subgraph/Orchid/blob/develop/src/com/subgraph/orchid/TorClient.java#L183
Here is what I did to create a singleton class to wrap the TorClient:
package com.anonyread.util.http;
import com.subgraph.orchid.TorClient;
import com.subgraph.orchid.TorInitializationListener;
import java.net.InetSocketAddress;
import java.net.Proxy;
public class TorClientFactory {
private static final int PROXY_PORT = 9150;
private static final String PROXY_HOST = "localhost";
private static TorClient client;
private static boolean isStarting = false;
private static boolean isRunning = false;
public static TorClient getTorClient(){
return client;
}
public static boolean hasOpenTorTunnel(){
return isStarting || isRunning;
}
public static void openTunnel(){
if(!isRunning){
isStarting = true;
client = new TorClient();
client.enableSocksListener(PROXY_PORT);
client.addInitializationListener(createInitalizationListner());
client.start();
client.enableSocksListener();
}
while(!isRunning){
try{
Thread.sleep(1000l);
} catch(Exception e){
//swallow
}
}
}
public static Proxy getProxy(){
return new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(PROXY_HOST, PROXY_PORT));
}
public static void closeTunnel(){
while(isStarting){
try{
Thread.sleep(100l);
} catch(Exception e){
//swallow
}
}
client.stop();
client = null;
isRunning = false;
isStarting = false;
}
private static TorInitializationListener createInitalizationListner() {
return new TorInitializationListener() {
@Override
public void initializationProgress(String message, int percent) {
System.out.println(">>> [ " + percent + "% ]: " + message);
}
@Override
public void initializationCompleted() {
System.out.println("Tor is ready to go!");
isRunning = true;
isStarting = false;
}
};
}
}
from orchid.
Related Issues (20)
- Is this project dead ? HOT 10
- any documentation, examples? HOT 3
- Default Port and IP HOT 1
- Broken in Android 6? HOT 6
- Add readme HOT 1
- Please tag version 1.0.0 in GitHub
- New tor circuit? HOT 2
- Tor issues
- WARNING: Failed to download key certificates … 404 Not found HOT 5
- Gradle/mvn Build, OSGi bundle and MVN artefact.
- Workaround for JCE Unlimited Strength Jurisdiction policy files ? HOT 3
- Circuits
- Orchid library failed on Samsung S6 after updating phone...
- Cannot build circuits because we don't have enough directory information HOT 38
- Add project description
- How to build it from source?
- hidden service HOT 4
- can it works on windows-mobile pocket pc?
- TorOutputStream improvments
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from orchid.