Sha256: 60d49be5145764b2b36994a9eafe4b402d6b80dcbc526b072012bf279ed2cc71

Contents?: true

Size: 1.45 KB

Versions: 213

Compression:

Stored size: 1.45 KB

Contents

module EventMachine
  module Protocols
    # Basic SOCKS v4 client implementation
    #
    # Use as you would any regular connection:
    #
    # class MyConn < EM::P::Socks4
    #   def post_init
    #     send_data("sup")
    #   end
    #
    #   def receive_data(data)
    #     send_data("you said: #{data}")
    #   end
    # end
    #
    # EM.connect socks_host, socks_port, MyConn, host, port
    #
    class Socks4 < Connection
      def initialize(host, port)
        @host = Socket.gethostbyname(host).last
        @port = port
        @socks_error_code = nil
        @buffer = ''
        setup_methods
      end

      def setup_methods
        class << self
          def post_init; socks_post_init; end
          def receive_data(*a); socks_receive_data(*a); end
        end
      end

      def restore_methods
        class << self
          remove_method :post_init
          remove_method :receive_data
        end
      end

      def socks_post_init
        header = [4, 1, @port, @host, 0].flatten.pack("CCnA4C")
        send_data(header)
      end

      def socks_receive_data(data)
        @buffer << data
        return  if @buffer.size < 8

        header_resp = @buffer.slice! 0, 8
        _, r = header_resp.unpack("cc")
        if r != 90
          @socks_error_code = r
          close_connection
          return
        end

        restore_methods

        post_init
        receive_data(@buffer)  unless @buffer.empty?
      end
    end
  end
end

Version data entries

213 entries across 210 versions & 17 rubygems

Version Path
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.0.0/gems/eventmachine-1.2.7/lib/em/protocols/socks4.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/eventmachine-1.2.7/lib/em/protocols/socks4.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/eventmachine-1.2.7/lib/em/protocols/socks4.rb
eventmachine-mkroman-1.3.0.dev.1 lib/em/protocols/socks4.rb
wj_eventmachine-1.3.2 lib/em/protocols/socks4.rb
wj_eventmachine-1.3.1 lib/em/protocols/socks4.rb
wj_eventmachine-1.3.0.dev.1 lib/em/protocols/socks4.rb
sensu-em-2.7.0-java lib/em/protocols/socks4.rb
eventmachine-1.2.7-java lib/em/protocols/socks4.rb
eventmachine-1.2.7-x64-mingw32 lib/em/protocols/socks4.rb
eventmachine-1.2.7-x86-mingw32 lib/em/protocols/socks4.rb
eventmachine-1.2.7 lib/em/protocols/socks4.rb
eventmachine-1.2.6-x64-mingw32 lib/em/protocols/socks4.rb
eventmachine-1.2.6-x86-mingw32 lib/em/protocols/socks4.rb
eventmachine-1.2.6-java lib/em/protocols/socks4.rb
eventmachine-1.2.6 lib/em/protocols/socks4.rb
eventmachine-1.2.5-x86-mingw32 lib/em/protocols/socks4.rb
eventmachine-1.2.5-x64-mingw32 lib/em/protocols/socks4.rb
eventmachine-1.2.5-java lib/em/protocols/socks4.rb
eventmachine-1.2.5 lib/em/protocols/socks4.rb