add tor sub projects as modules

This commit is contained in:
Manfred Karrer 2015-11-06 00:43:21 +01:00
parent d33ebbed27
commit 8347b9f572
100 changed files with 121050 additions and 55 deletions

View file

@ -0,0 +1,119 @@
package io.nucleo.net;
import com.msopentech.thali.java.toronionproxy.JavaOnionProxyContext;
import com.msopentech.thali.java.toronionproxy.JavaOnionProxyManager;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
public class TorNodeTest {
private static final int hsPort = 55555;
private static CountDownLatch serverLatch = new CountDownLatch(1);
private static TorNode<JavaOnionProxyManager, JavaOnionProxyContext> node;
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException, InstantiationException {
File dir = new File("tor-test");
dir.mkdirs();
for (String str : args)
System.out.print(str + " ");
node = new TorNode<JavaOnionProxyManager, JavaOnionProxyContext>(dir) {
};
final ServiceDescriptor hiddenService = node.createHiddenService(hsPort);
new Thread(new Server(hiddenService.getServerSocket())).start();
serverLatch.await();
if (args.length != 2)
new Client(node.connectToHiddenService(hiddenService.getHostname(), hiddenService.getServicePort())).run();
else {
System.out.println("\nHs Running, pres return to connect to " + args[0] + ":" + args[1]);
final Scanner scanner = new Scanner(System.in);
scanner.nextLine();
new Client(node.connectToHiddenService(args[0], Integer.parseInt(args[1])), scanner).run();
}
// node.shutdown();
}
private static class Client implements Runnable {
private Socket sock;
private final Scanner scanner;
private Client(Socket sock, Scanner scanner) {
this.sock = sock;
this.scanner = scanner;
}
private Client(Socket sock) {
this(sock, new Scanner(System.in));
}
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
OutputStreamWriter out = new OutputStreamWriter(sock.getOutputStream());
System.out.print("\n> ");
String input = scanner.nextLine();
out.write(input + "\n");
out.flush();
String aLine = null;
while ((aLine = in.readLine()) != null) {
System.out.println(aLine);
System.out.print("\n> ");
input = scanner.nextLine();
out.write(input + "\n");
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static class Server implements Runnable {
private final ServerSocket socket;
private Server(ServerSocket socket) {
this.socket = socket;
}
@Override
public void run() {
System.out.println("Wating for incoming connections...");
serverLatch.countDown();
try {
while (true) {
Socket sock = socket.accept();
System.out.println("Accepting Client " + sock.getRemoteSocketAddress() + " on port " + sock.getLocalPort());
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
OutputStreamWriter out = new OutputStreamWriter(sock.getOutputStream());
String aLine = null;
while ((aLine = in.readLine()) != null) {
System.out.println("ECHOING " + aLine);
out.write("ECHO " + aLine + "\n");
out.flush();
if (aLine.equals("END"))
break;
}
sock.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

View file

@ -0,0 +1,146 @@
package io.nucleo.net.node;
import com.msopentech.thali.java.toronionproxy.JavaOnionProxyContext;
import com.msopentech.thali.java.toronionproxy.JavaOnionProxyManager;
import io.nucleo.net.*;
import io.nucleo.net.Node.Server;
import io.nucleo.net.proto.ContainerMessage;
import io.nucleo.net.proto.exceptions.ConnectionException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.regex.Pattern;
public class NodeTest {
private static boolean running;
static Connection currentCon = null;
static class Listener implements ConnectionListener {
@Override
public void onMessage(Connection con, ContainerMessage msg) {
System.err.println("RXD: " + msg.getPayload().toString() + " < " + con.getPeer());
}
@Override
public void onDisconnect(Connection con, DisconnectReason reason) {
if (con.equals(currentCon))
currentCon = null;
System.err.println(con.getPeer() + " has disconnected: " + reason.toString());
}
@Override
public void onError(Connection con, ConnectionException e) {
System.err.println("Connection " + con.getPeer() + ": " + e.getMessage());
e.printStackTrace();
}
@Override
public void onReady(Connection con) {
System.err.println(con.getPeer() + " is ready");
currentCon = con;
}
}
public static void main(String[] args) throws InstantiationException, IOException {
if ((args.length != 2) && (args.length != 1)) {
System.err.println("1 or 2 params required: service port, or hidden service dir + port");
return;
}
final Node node;
final ArrayList<ConnectionListener> listener = new ArrayList<>(1);
listener.add(new Listener());
if (args.length == 2) {
File dir = new File(args[0]);
dir.mkdirs();
TorNode<JavaOnionProxyManager, JavaOnionProxyContext> tor = new TorNode<JavaOnionProxyManager, JavaOnionProxyContext>(
dir) {
};
node = new Node(tor.createHiddenService(Integer.parseInt(args[1])), tor);
} else {
node = new Node(new TCPServiceDescriptor("localhost", Integer.parseInt(args[0])));
}
final Server server = node.startListening(new ServerConnectListener() {
@Override
public void onConnect(Connection con) {
con.addMessageListener(listener.get(0));
try {
con.listen();
} catch (ConnectionException e) {
// never happens
}
System.out.println("Connection to " + con.getPeer() + " established :-)");
}
});
running = true;
Scanner scan = new Scanner(System.in);
System.out.println("READY!");
String line = null;
System.out.print("\n" + node.getLocalName() + " >");
while (running && ((line = scan.nextLine()) != null)) {
String[] cmd = {line};
if (line.contains(" "))
cmd = line.split(Pattern.quote(" "));
switch (cmd[0]) {
case "con":
if (cmd.length == 2) {
String host = cmd[1];
try {
node.connect(host, listener);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
break;
case "list":
int i = 0;
for (Connection con : new LinkedList<>(node.getConnections())) {
System.out.println("\t" + (i++) + " " + con.getPeer());
}
break;
case "sel":
try {
if (cmd.length == 2) {
int index = Integer.parseInt(cmd[1]);
currentCon = new LinkedList<>(node.getConnections()).get(index);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "send":
try {
if (cmd.length >= 2) {
if (currentCon != null) {
currentCon.sendMessage(new ContainerMessage(line.substring(4)));
} else
System.err.println("NO node active!");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "end":
server.shutdown();
break;
default:
break;
}
System.out.print("\n" + node.getLocalName() + ":" + (currentCon == null ? "" : currentCon.getPeer()) + " >");
}
}
}