Tor Ruby Socks Proxy


SUBMITTED BY: crazyren

DATE: May 24, 2016, 1:06 p.m.

FORMAT: Ruby

SIZE: 3.3 kB

HITS: 85158

  1. #!/usr/bin/env ruby
  2. require 'socket'
  3. require 'thread'
  4. localport = 9166
  5. server = TCPServer.open(localport)
  6. puts "Listening on port tcp/#{localport}..."
  7. MY_IP = "127.0.0.1"
  8. @reply_650OK = "650 NOTICE New control connection opened.\r\n"
  9. @reply_250OK = "250 OK\r\n"
  10. @reply_bootstrap_phase = "250-status/bootstrap-phase=NOTICE BOOTSTRAP PROGRESS=100 TAG=done SUMMARY=\"Done\"\r\n"
  11. @reply_net_listeners_socks = "250-net/listeners/socks=\"#{MY_IP}:9150\" \"127.0.0.1:9150\"\r\n"
  12. @reply_Socks4Proxy = "250 Socks4Proxy\r\n"
  13. @reply_Socks5Proxy = "250 Socks5Proxy\r\n"
  14. @reply_HTTPSProxy = "250 HTTPSProxy\r\n"
  15. @reply_ReachableAddresses = "250 ReachableAddresses\r\n"
  16. @reply_UseBridges = "250 UseBridges=0\r\n"
  17. @reply_Bridge = "250 Bridge\r\n"
  18. def reply(client, string)
  19. puts " -> #{string}"
  20. client.puts string
  21. end
  22. def process_getinfo(client, req)
  23. if req['status/bootstrap-phase']
  24. reply client, @reply_bootstrap_phase
  25. reply client, @reply_250OK
  26. elsif req['net/listeners/socks']
  27. reply client, @reply_net_listeners_socks
  28. reply client, @reply_250OK
  29. elsif req['Socks4Proxy']
  30. reply client, @reply_Socks4Proxy
  31. elsif req['Socks5Proxy']
  32. reply client, @reply_Socks5Proxy
  33. elsif req['HTTPSProxy']
  34. reply client, @reply_HTTPSProxy
  35. elsif req['ReachableAddresses']
  36. reply client, @reply_ReachableAddresses
  37. elsif req['UseBridges']
  38. reply client, @reply_UseBridges
  39. elsif req['Bridge']
  40. reply client, @reply_Bridge
  41. else
  42. puts "UNKNOWN GETINFO request!!!"
  43. end
  44. end
  45. def process_getconf(client, req)
  46. if req['Socks4Proxy']
  47. reply client, @reply_Socks4Proxy
  48. elsif req['Socks5Proxy']
  49. reply client, @reply_Socks5Proxy
  50. elsif req['HTTPSProxy']
  51. reply client, @reply_HTTPSProxy
  52. elsif req['ReachableAddresses']
  53. reply client, @reply_ReachableAddresses
  54. elsif req['UseBridges']
  55. reply client, @reply_UseBridges
  56. elsif req['Bridge']
  57. reply client, @reply_Bridge
  58. else
  59. puts "UNKNOWN GETCONF request!!!"
  60. end
  61. end
  62. def process_request(client)
  63. loop do
  64. begin
  65. req = client.gets
  66. next unless req
  67. puts "(#{Thread.current}) #{req}"
  68. if req.start_with? 'GETINFO'
  69. process_getinfo(client, req)
  70. elsif req.start_with? 'SETEVENTS'
  71. reply client, @reply_250OK
  72. puts "SETEVENTS !!!"
  73. #reply client, @reply_650OK
  74. return
  75. elsif req.start_with? 'SIGNAL NEWNYM'
  76. reply client, @reply_250OK
  77. puts "NEWNYM !!!"
  78. return
  79. elsif req.start_with? 'TAKEOWNERSHIP'
  80. reply client, @reply_250OK
  81. elsif req.start_with? 'AUTHENTICATE'
  82. reply client, @reply_250OK
  83. elsif req.start_with? 'RESETCONF'
  84. reply client, @reply_250OK
  85. elsif req.start_with? 'GETCONF'
  86. process_getconf(client, req)
  87. elsif req.start_with? 'SETCONF' or req.start_with? 'SAVECONF'
  88. reply client, @reply_250OK
  89. end
  90. rescue Exception => e
  91. puts e.message
  92. end
  93. end
  94. end
  95. loop do
  96. Thread.start(server.accept) do |client|
  97. port, ip = Socket.unpack_sockaddr_in(client.getpeername)
  98. puts "(#{Thread.current}) New connection from: #{ip}:#{port}"
  99. process_request(client)
  100. end
  101. end

comments powered by Disqus