I'm testing Silvertunnel-NG lib. I want to have a server that accepts connections and a client in the same application.
At this moment, I'm trying to connect to my own hidden service created by Silvertunnel-NG lib. I can do it, but my hostname change in every execution. I can connect but first I have to create a new hidden service, I can't use an onion address created in other execution.
This is my test code:
public static void setHiddenService(File dir, TorNetLayerUtil tNLU) throws IOException {
// Create new private+public hidden service key
TorHiddenServicePrivateNetAddress tHSPNA = tNLU.createNewTorHiddenServicePrivateNetAddress();
// write new private+public hidden service key to directory
dir.mkdir();
tNLU.writeTorHiddenServicePrivateNetAddressToFiles(dir, tHSPNA);
}
public static TorHiddenServicePortPrivateNetAddress readHiddenService(File dir, TorNetLayerUtil tNLU, int port) throws IOException {
TorHiddenServicePrivateNetAddress netAddress = tNLU.readTorHiddenServicePrivateNetAddressFromFiles(dir, true);
return new TorHiddenServicePortPrivateNetAddress(netAddress, port);
}
public static void setUp() {
System.setProperty(TorConfig.SYSTEMPROPERTY_TOR_MINIMUM_ROUTE_LENGTH, "2");
System.setProperty(TorConfig.SYSTEMPROPERTY_TOR_MAXIMUM_ROUTE_LENGTH, "2");
System.setProperty(TorConfig.SYSTEMPROPERTY_TOR_MINIMUM_IDLE_CIRCUITS, "2");
TorConfig.reloadConfigFromProperties();
}
public static void main(String[] args) throws IOException {
setUp(); // setup TOR for tests
TorNetLayerUtil tNLU = TorNetLayerUtil.getInstance();
File dir = new File("/key/"); // directory of hostname file
setHiddenService(dir, tNLU); // create a new hidden service
TorHiddenServicePortPrivateNetAddress netAddress = readHiddenService(dir, tNLU, 80);
// get TorNetLayer instance and wait until it is ready
NetLayer netLayer = NetFactory.getInstance().getNetLayerById(NetLayerIDs.TOR);
netLayer.waitUntilReady();
TorNetServerSocket netServerSocket = (TorNetServerSocket) netLayer.createNetServerSocket(null, netAddress);
...
If i comment "setHiddenService(dir, tNLU);" there are a connection problem, because my hostname have changed.
Can someone tell me what is going on?
Lib: Silvertunnel-NG 0.0.4.
Update: I have created a new method to get hidden service from a private_key file.
public static TorHiddenServicePrivateNetAddress createHiddenServiceFromPrivKey(File pkfile, TorNetLayerUtil tNLU) throws IOException{
final String privateKeyPEMStr = FileUtil.readFile(pkfile);
final TorHiddenServicePrivateNetAddress netAddressWithoutPort = tNLU.parseTorHiddenServicePrivateNetAddressFromStrings(privateKeyPEMStr, null, false);
return netAddressWithoutPort;
}
Update: I recommend to use Stem (Python based) in order to control TOR. Works better.