### Resolve Hostname via SOCKS Source: https://github.com/astro/socksify-ruby/blob/master/doc/index.html Use Socksify::resolve to get the IPv4 address of a hostname through the SOCKS proxy. This is useful for DNS resolution over SOCKS. ```ruby Socksify::resolve("spaceboyz.net") # => "87.106.131.203" ``` -------------------------------- ### Enable SOCKS proxy for Net::HTTP Source: https://github.com/astro/socksify-ruby/blob/master/README.md Load the HTTP extension to enable the socks_proxy method for Net::HTTP. ```ruby require 'socksify/http' ``` -------------------------------- ### Command Line Proxy Wrapper Source: https://context7.com/astro/socksify-ruby/llms.txt Run Ruby scripts or interactive sessions through a SOCKS proxy using the command-line interface. ```bash # Basic usage - run script through local Tor proxy $ socksify_ruby localhost 9050 myscript.rb # Run script through custom SOCKS proxy with arguments $ socksify_ruby proxy.example.com 1080 script.rb arg1 arg2 # Start interactive Ruby session through SOCKS $ socksify_ruby localhost 9050 irb> require 'net/http' irb> Net::HTTP.get(URI('http://ipecho.net/plain')) # => Returns IP address of Tor exit node # Usage in shebang for scripts #!/usr/bin/env socksify_ruby localhost 9050 require 'net/http' puts Net::HTTP.get(URI('http://check.torproject.org')) ``` -------------------------------- ### Command Line Usage - socksify_ruby Source: https://context7.com/astro/socksify-ruby/llms.txt A command-line wrapper that runs any Ruby script with all TCP connections routed through a SOCKS proxy. Useful for running existing scripts through Tor or other SOCKS proxies without code modification. ```APIDOC ## Command Line Usage - socksify_ruby ### Description A command-line wrapper that runs any Ruby script with all TCP connections routed through a SOCKS proxy. Useful for running existing scripts through Tor or other SOCKS proxies without code modification. ### Usage `socksify_ruby [script_args...]` ### Examples ```bash # Basic usage - run script through local Tor proxy $ socksify_ruby localhost 9050 myscript.rb # Run script through custom SOCKS proxy with arguments $ socksify_ruby proxy.example.com 1080 script.rb arg1 arg2 # Start interactive Ruby session through SOCKS $ socksify_ruby localhost 9050 irb> require 'net/http' irb> Net::HTTP.get(URI('http://ipecho.net/plain')) # => Returns IP address of Tor exit node # Usage in shebang for scripts #!/usr/bin/env socksify_ruby localhost 9050 require 'net/http' puts Net::HTTP.get(URI('http://check.torproject.org')) ``` ``` -------------------------------- ### Net::HTTP with SOCKS Authentication Source: https://github.com/astro/socksify-ruby/blob/master/doc/index.html Configure Net::HTTP to use a SOCKS proxy with authentication credentials. Provide username and password alongside proxy details. ```ruby Net::HTTP.SOCKSProxy('127.0.0.1', 9050, 'username', 'p4ssw0rd') ``` -------------------------------- ### Enable Debug Logging Source: https://context7.com/astro/socksify-ruby/llms.txt Toggle diagnostic output to monitor SOCKS connection attempts and authentication steps. ```ruby require 'socksify' # Enable debug output Socksify::debug = true # Now all SOCKS operations will print colored debug messages TCPSocket.socks_server = "127.0.0.1" TCPSocket.socks_port = 9050 socket = TCPSocket.new("example.com", 80) # Output: # 14:32:01 Connecting to SOCKS server 127.0.0.1:9050 # 14:32:01 Sending no authentication # 14:32:01 Waiting for authentication reply # 14:32:01 Sending destination address # 14:32:01 example.com # 14:32:01 Waiting for SOCKS reply # 14:32:01 Waiting for bind_addr # 14:32:01 Connected to example.com:80 over SOCKS # Disable debug output Socksify::debug = false ``` -------------------------------- ### Configure SOCKS Bypass List Source: https://context7.com/astro/socksify-ruby/llms.txt Manage hostnames that should connect directly instead of through the SOCKS proxy. ```ruby require 'socksify' # Configure SOCKS proxy TCPSocket.socks_server = "127.0.0.1" TCPSocket.socks_port = 9050 # View default ignores puts TCPSocket.socks_ignores # => ["localhost"] # Add hosts to bypass list TCPSocket.socks_ignores << "internal.example.com" TCPSocket.socks_ignores << "192.168.1.1" # Connections to ignored hosts go direct direct_socket = TCPSocket.new("localhost", 3000) # Direct connection proxy_socket = TCPSocket.new("external.com", 80) # Through SOCKS # Replace entire ignore list TCPSocket.socks_ignores = ["localhost", "*.local", "10.0.0.0/8"] ``` -------------------------------- ### Resolve hostnames via SOCKS Source: https://github.com/astro/socksify-ruby/blob/master/README.md Use the library to resolve hostnames to IPv4 addresses through the SOCKS proxy. ```ruby Socksify.resolve("spaceboyz.net") # => "87.106.131.203" ``` -------------------------------- ### Authenticate Net::HTTP SOCKS proxy Source: https://github.com/astro/socksify-ruby/blob/master/README.md Provide username and password credentials when initializing a SOCKS proxy for Net::HTTP. ```ruby require 'socksify/http' uri = URI.parse('http://ipecho.net/plain') Net::HTTP.socks_proxy('127.0.0.1', 1080, username: 'my_username', password: 'my_pwd').start(uri.host, uri.port) do |http| req = Net::HTTP::Get.new uri resp = http.request(req) puts resp.inspect puts resp.body end ``` -------------------------------- ### Enable diagnostic debugging Source: https://github.com/astro/socksify-ruby/blob/master/README.md Toggle verbose diagnostic messages for the library. ```ruby Socksify::debug = true ``` -------------------------------- ### Net::HTTP SOCKS Proxy Basic Usage Source: https://context7.com/astro/socksify-ruby/llms.txt Routes HTTP requests through a local SOCKS5 proxy like Tor. Ensure the proxy is running before execution. ```ruby require 'socksify/http' # Basic usage - route HTTP request through local Tor proxy uri = URI.parse('http://ipecho.net/plain') Net::HTTP.socks_proxy('127.0.0.1', 9050).start(uri.host, uri.port) do |http| req = Net::HTTP::Get.new(uri) resp = http.request(req) puts resp.code # => "200" puts resp.body # => end ``` ```ruby # HTTPS request through SOCKS proxy uri = URI.parse('https://api.example.com/data') proxy = Net::HTTP.socks_proxy('127.0.0.1', 9050) proxy.start(uri.host, uri.port, use_ssl: true) do |http| request = Net::HTTP::Get.new(uri) request['Authorization'] = 'Bearer token123' response = http.request(request) puts JSON.parse(response.body) end ``` -------------------------------- ### Run Ruby Script via SOCKS Proxy Source: https://github.com/astro/socksify-ruby/blob/master/doc/index.html Execute a Ruby script with all TCP connections redirected through a specified SOCKS5 proxy. Requires the proxy address and port. ```bash socksify_ruby localhost 9050 script.rb ``` -------------------------------- ### Net::HTTP via SOCKS Proxy Source: https://github.com/astro/socksify-ruby/blob/master/doc/index.html Integrate Net::HTTP with SOCKS proxying by requiring 'socksify/http' and using Net::HTTP.SOCKSProxy. This method does not rely on TCPSocket class variables. ```ruby require 'socksify/http' uri = URI.parse('http://rubyforge.org/') Net::HTTP.SOCKSProxy('127.0.0.1', 9050).start(uri.host, uri.port) do |http| http.get(uri.path) end # => # ``` -------------------------------- ### Socksify.proxy - Temporary Proxy Usage Source: https://context7.com/astro/socksify-ruby/llms.txt Temporarily use a different SOCKS proxy for a block of code. Settings are automatically restored afterwards, even if errors occur. ```APIDOC ## Socksify.proxy - Temporary Proxy Usage ### Description Temporarily use a different SOCKS proxy for a block of code. Settings are automatically restored afterwards, even if errors occur. ### Method `Socksify.proxy(server, port)` ### Parameters #### Path Parameters - **server** (string) - Required - The hostname or IP address of the SOCKS proxy. - **port** (integer) - Required - The port number of the SOCKS proxy. ### Request Example ```ruby require 'socksify' Socksify.proxy('other-proxy.example.com', 1080) do # All connections in this block use the temporary proxy puts TCPSocket.socks_server # => "other-proxy.example.com" puts TCPSocket.socks_port # => 1080 socket = TCPSocket.new("target.com", 80) # ... use socket through temporary proxy socket.close end # Original settings restored puts TCPSocket.socks_server # => "127.0.0.1" puts TCPSocket.socks_port # => 9050 # Failback on error - settings are restored even if an exception occurs begin Socksify.proxy('temp-proxy.com', 8080) do raise StandardError, "Something went wrong" end rescue StandardError # Original settings still restored puts TCPSocket.socks_server # => "127.0.0.1" end ``` ``` -------------------------------- ### Socksify.resolve - DNS Resolution via SOCKS Source: https://context7.com/astro/socksify-ruby/llms.txt Resolves hostnames to IP addresses through the SOCKS proxy, preventing DNS leaks. Ensure SOCKS proxy is configured before calling. ```ruby require 'socksify' # Configure SOCKS proxy first TCPSocket.socks_server = "127.0.0.1" TCPSocket.socks_port = 9050 # Resolve hostname through SOCKS (prevents DNS leaks) ip_address = Socksify.resolve("spaceboyz.net") puts ip_address # => "87.106.131.203" # Resolve Google DNS ip = Socksify.resolve("dns.google.com") puts ip # => "8.8.8.8" or "8.8.4.4" # Reverse DNS lookup hostname = Socksify.resolve("8.8.8.8") puts hostname # => "dns.google" # Handle resolution errors begin Socksify.resolve("nonexistent.example.com") rescue SOCKSError::HostUnreachable => e puts "Host not found: #{e.message}" end ``` -------------------------------- ### Use Net::HTTP with SOCKS proxy Source: https://github.com/astro/socksify-ruby/blob/master/README.md Route HTTP requests through a SOCKS proxy using the socks_proxy method. ```ruby require 'socksify/http' uri = URI.parse('http://ipecho.net/plain') Net::HTTP.socks_proxy('127.0.0.1', 9050).start(uri.host, uri.port) do |http| req = Net::HTTP::Get.new uri resp = http.request(req) puts resp.inspect puts resp.body end # => # # => ``` -------------------------------- ### Configure global TCPSocket proxy Source: https://github.com/astro/socksify-ruby/blob/master/README.md Set global SOCKS server and port for TCPSocket instances. Note that this approach is deprecated in Ruby 3.1 and later. ```ruby require 'socksify' TCPSocket.socks_server = "127.0.0.1" TCPSocket.socks_port = 9050 rubyforge_www = TCPSocket.new("rubyforge.org", 80) # => # ``` -------------------------------- ### Socksify::debug - Debug Logging Source: https://context7.com/astro/socksify-ruby/llms.txt Enables colorful diagnostic output for debugging SOCKS connections, showing connection attempts, authentication steps, and resolved addresses. ```APIDOC ## Socksify::debug - Debug Logging ### Description Enables colorful diagnostic output for debugging SOCKS connections, showing connection attempts, authentication steps, and resolved addresses. ### Method `Socksify::debug = true` (to enable) `Socksify::debug = false` (to disable) ### Request Example ```ruby require 'socksify' # Enable debug output Socksify::debug = true # Now all SOCKS operations will print colored debug messages TCPSocket.socks_server = "127.0.0.1" TCPSocket.socks_port = 9050 socket = TCPSocket.new("example.com", 80) # Output: # 14:32:01 Connecting to SOCKS server 127.0.0.1:9050 # 14:32:01 Sending no authentication # 14:32:01 Waiting for authentication reply # 14:32:01 Sending destination address # 14:32:01 example.com # 14:32:01 Waiting for SOCKS reply # 14:32:01 Waiting for bind_addr # 14:32:01 Connected to example.com:80 over SOCKS # Disable debug output Socksify::debug = false ``` ``` -------------------------------- ### Socksify.proxy - Temporary Proxy Block Source: https://context7.com/astro/socksify-ruby/llms.txt Temporarily switches to a different SOCKS proxy for the duration of a block, then restores the original settings. Useful for routing specific operations through different proxies. ```ruby require 'socksify' # Set default proxy TCPSocket.socks_server = "127.0.0.1" TCPSocket.socks_port = 9050 ``` -------------------------------- ### TCPSocket.socks_ignores - Bypass List Configuration Source: https://context7.com/astro/socksify-ruby/llms.txt Configures a list of hostnames that should bypass the SOCKS proxy and connect directly. By default, "localhost" is in the ignore list. ```APIDOC ## TCPSocket.socks_ignores - Bypass List ### Description Configures a list of hostnames that should bypass the SOCKS proxy and connect directly. By default, "localhost" is in the ignore list. ### Method `TCPSocket.socks_ignores` (getter/setter) ### Parameters No direct parameters for the getter. The setter accepts an array of strings. ### Request Example ```ruby require 'socksify' # Configure SOCKS proxy TCPSocket.socks_server = "127.0.0.1" TCPSocket.socks_port = 9050 # View default ignores puts TCPSocket.socks_ignores # => ["localhost"] # Add hosts to bypass list TCPSocket.socks_ignores << "internal.example.com" TCPSocket.socks_ignores << "192.168.1.1" # Connections to ignored hosts go direct direct_socket = TCPSocket.new("localhost", 3000) # Direct connection proxy_socket = TCPSocket.new("external.com", 80) # Through SOCKS # Replace entire ignore list TCPSocket.socks_ignores = ["localhost", "*.local", "10.0.0.0/8"] ``` ``` -------------------------------- ### SOCKS Proxy with Block Usage in Ruby Source: https://github.com/astro/socksify-ruby/blob/master/doc/index.html Use SOCKS proxying for a block of code, ensuring connections within the block are proxied. This is useful for temporary proxying needs. ```ruby require 'socksify' require 'open-uri' Socksify::proxy("127.0.0.1", 9050) { open('http://rubyforge.org').read # => # } ``` -------------------------------- ### SOCKSError Exception Handling Source: https://context7.com/astro/socksify-ruby/llms.txt Provides specific exception classes for different SOCKS protocol errors, enabling granular error handling for connection failures. ```APIDOC ## SOCKSError Exception Handling ### Description Provides specific exception classes for different SOCKS protocol errors, enabling granular error handling for connection failures. ### Exception Classes - `SOCKSError::ServerFailure` - `SOCKSError::NotAllowed` - `SOCKSError::NetworkUnreachable` - `SOCKSError::HostUnreachable` - `SOCKSError::ConnectionRefused` - `SOCKSError::TTLExpired` - `SOCKSError::CommandNotSupported` - `SOCKSError::AddressTypeNotSupported` - `SOCKSError` (General SOCKS error) ### Request Example ```ruby require 'socksify' require 'socksify/http' # Handle specific SOCKS errors begintc TCPSocket.socks_server = "127.0.0.1" TCPSocket.socks_port = 9050 socket = TCPSocket.new("blocked-site.onion", 80) rescue SOCKSError::ServerFailure => e puts "SOCKS server error: #{e.message}" rescue SOCKSError::NotAllowed => e puts "Connection blocked by proxy rules: #{e.message}" rescue SOCKSError::NetworkUnreachable => e puts "Network unreachable: #{e.message}" rescue SOCKSError::HostUnreachable => e puts "Host unreachable: #{e.message}" rescue SOCKSError::ConnectionRefused => e puts "Connection refused: #{e.message}" rescue SOCKSError::TTLExpired => e puts "TTL expired: #{e.message}" rescue SOCKSError::CommandNotSupported => e puts "Command not supported by proxy: #{e.message}" rescue SOCKSError::AddressTypeNotSupported => e puts "Address type not supported: #{e.message}" rescue SOCKSError => e puts "General SOCKS error: #{e.message}" end # Authentication error handling begintc proxy = Net::HTTP.socks_proxy('127.0.0.1', 1080, username: 'user', password: 'wrong_password') proxy.start('example.com', 80) { |http| http.get('/') } rescue SOCKSError => e puts "Authentication failed: #{e.message}" # => "SOCKS authentication failed" end ``` ``` -------------------------------- ### TCPSocket SOCKS Configuration (Legacy) Source: https://context7.com/astro/socksify-ruby/llms.txt Configures the global TCPSocket class to route all TCP connections through a SOCKS proxy. This method is deprecated for Ruby 3.1+ and should be replaced with Net::HTTP.socks_proxy. ```ruby require 'socksify' # Configure global SOCKS proxy (deprecated in Ruby 3.1+) TCPSocket.socks_server = "127.0.0.1" TCPSocket.socks_port = 9050 # All TCPSocket connections now go through SOCKS socket = TCPSocket.new("example.com", 80) socket.write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") response = socket.read socket.close puts response ``` ```ruby # With authentication TCPSocket.socks_server = "127.0.0.1" TCPSocket.socks_port = 1080 TCPSocket.socks_username = "user" TCPSocket.socks_password = "password" socket = TCPSocket.new("target.example.com", 443) # ... use socket socket.close ``` -------------------------------- ### Handle SOCKS Exceptions Source: https://context7.com/astro/socksify-ruby/llms.txt Catch specific SOCKS protocol errors to implement granular error handling logic. ```ruby require 'socksify' require 'socksify/http' # Handle specific SOCKS errors begin TCPSocket.socks_server = "127.0.0.1" TCPSocket.socks_port = 9050 socket = TCPSocket.new("blocked-site.onion", 80) rescue SOCKSError::ServerFailure => e puts "SOCKS server error: #{e.message}" rescue SOCKSError::NotAllowed => e puts "Connection blocked by proxy rules: #{e.message}" rescue SOCKSError::NetworkUnreachable => e puts "Network unreachable: #{e.message}" rescue SOCKSError::HostUnreachable => e puts "Host unreachable: #{e.message}" rescue SOCKSError::ConnectionRefused => e puts "Connection refused: #{e.message}" rescue SOCKSError::TTLExpired => e puts "TTL expired: #{e.message}" rescue SOCKSError::CommandNotSupported => e puts "Command not supported by proxy: #{e.message}" rescue SOCKSError::AddressTypeNotSupported => e puts "Address type not supported: #{e.message}" rescue SOCKSError => e puts "General SOCKS error: #{e.message}" end # Authentication error handling begin proxy = Net::HTTP.socks_proxy('127.0.0.1', 1080, username: 'user', password: 'wrong_password') proxy.start('example.com', 80) { |http| http.get('/') } rescue SOCKSError => e puts "Authentication failed: #{e.message}" # => "SOCKS authentication failed" end ``` -------------------------------- ### Explicit SOCKS Usage in Ruby Source: https://github.com/astro/socksify-ruby/blob/master/doc/index.html Configure TCPSocket to use a SOCKS proxy for all subsequent connections. Note: This method is not thread-safe as it uses class variables. ```ruby require 'socksify' TCPSocket::socks_server = "127.0.0.1" TCPSocket::socks_port = 9050 rubyforge_www = TCPSocket.new("rubyforge.org", 80) # => # ``` -------------------------------- ### Temporarily Override SOCKS Proxy Source: https://context7.com/astro/socksify-ruby/llms.txt Use a block to route connections through a specific proxy temporarily, ensuring settings are restored even if an error occurs. ```ruby Socksify.proxy('other-proxy.example.com', 1080) do # All connections in this block use the temporary proxy puts TCPSocket.socks_server # => "other-proxy.example.com" puts TCPSocket.socks_port # => 1080 socket = TCPSocket.new("target.com", 80) # ... use socket through temporary proxy socket.close end # Original settings restored puts TCPSocket.socks_server # => "127.0.0.1" puts TCPSocket.socks_port # => 9050 # Failback on error - settings are restored even if an exception occurs begin Socksify.proxy('temp-proxy.com', 8080) do raise StandardError, "Something went wrong" end rescue StandardError # Original settings still restored puts TCPSocket.socks_server # => "127.0.0.1" end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.