Sha256: ce130065aaf944b5aa202417ad4a1ce0b2035646df3b71cd8deb1b247ee1155a

Contents?: true

Size: 921 Bytes

Versions: 30

Compression:

Stored size: 921 Bytes

Contents

# frozen_string_literal: true

module Nonnative
  class SocketPair
    def initialize(proxy)
      @proxy = proxy
    end

    def connect(local_socket)
      remote_socket = create_remote_socket

      loop do
        ready = select([local_socket, remote_socket], nil, nil)

        break if pipe(ready, local_socket, remote_socket)
        break if pipe(ready, remote_socket, local_socket)
      end
    ensure
      local_socket.close
      remote_socket&.close
    end

    protected

    attr_reader :proxy

    def create_remote_socket
      ::TCPSocket.new(proxy.host, proxy.port)
    end

    def pipe(ready, socket1, socket2)
      if ready[0].include?(socket1)
        data = read(socket1)
        return true if data.empty?

        write socket2, data
      end

      false
    end

    def read(socket)
      socket.recv(1024)
    end

    def write(socket, data)
      socket.write(data)
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
nonnative-1.83.0 lib/nonnative/socket_pair.rb
nonnative-1.82.0 lib/nonnative/socket_pair.rb
nonnative-1.81.0 lib/nonnative/socket_pair.rb
nonnative-1.80.0 lib/nonnative/socket_pair.rb
nonnative-1.79.0 lib/nonnative/socket_pair.rb
nonnative-1.78.0 lib/nonnative/socket_pair.rb
nonnative-1.77.0 lib/nonnative/socket_pair.rb
nonnative-1.76.0 lib/nonnative/socket_pair.rb
nonnative-1.75.0 lib/nonnative/socket_pair.rb
nonnative-1.74.1 lib/nonnative/socket_pair.rb
nonnative-1.74.0 lib/nonnative/socket_pair.rb
nonnative-1.73.0 lib/nonnative/socket_pair.rb
nonnative-1.72.0 lib/nonnative/socket_pair.rb
nonnative-1.71.0 lib/nonnative/socket_pair.rb
nonnative-1.70.0 lib/nonnative/socket_pair.rb
nonnative-1.69.0 lib/nonnative/socket_pair.rb
nonnative-1.68.0 lib/nonnative/socket_pair.rb
nonnative-1.67.0 lib/nonnative/socket_pair.rb
nonnative-1.66.0 lib/nonnative/socket_pair.rb
nonnative-1.65.0 lib/nonnative/socket_pair.rb