Advertisement
Guest User

TorControl

a guest
Jan 3rd, 2017
1,318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. package tor;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStreamWriter;
  5. import java.net.Socket;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9.  
  10. public class TorControl {
  11. public static final String COOKIE_FILE="D:\\Tor Browser\\Browser\\TorBrowser\\Data\\Tor\\control_auth_cookie";
  12. public static final String TOR_IP="127.0.0.1", TOR_PORT="9150";
  13. public static final int CONTROLPORT=9151;
  14.  
  15. private OutputStreamWriter out;
  16. private TorReader in;
  17. private String magic;
  18.  
  19. public TorControl() throws IOException{
  20. this(new Socket(TOR_IP, CONTROLPORT), COOKIE_FILE);
  21. }
  22.  
  23. public TorControl(Socket s, String controlFile) throws IOException{
  24. //Get Magic Cookie
  25. Path path = Paths.get(controlFile);
  26. magic = hex(Files.readAllBytes(path));
  27.  
  28. in = new TorReader(s.getInputStream());
  29. out = new OutputStreamWriter(s.getOutputStream());
  30. }
  31.  
  32. public String write(String data) throws IOException{
  33. out.write(data+"\r\n");out.flush();
  34. return in.read();
  35. }
  36.  
  37. public String newIdentity() throws IOException{
  38. return write("SIGNAL NEWNYM");
  39. }
  40.  
  41. public String authenticate() throws IOException{
  42. return write("AUTHENTICATE "+magic+"\r\n");
  43. }
  44.  
  45. public void useProxy(){
  46. System.setProperty("java.net.preferIPv4Stack" , "true");
  47. System.setProperty("socksProxyHost", TOR_IP);
  48. System.setProperty("socksProxyPort", TOR_PORT);
  49. }
  50.  
  51. //Used for magic number
  52. private static final char[] NYBBLES = {
  53. '0', '1', '2', '3', '4', '5', '6', '7',
  54. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  55. };
  56.  
  57. public static String hex(byte[] ba) {
  58. StringBuffer buf = new StringBuffer();
  59. for (int i = 0; i < ba.length; ++i) {
  60. int b = (ba[i]) & 0xff;
  61. buf.append(NYBBLES[b >> 4]);
  62. buf.append(NYBBLES[b&0x0f]);
  63. }
  64. return buf.toString();
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement