52: def local(*args)
53: if args.length < 3 || args.length > 4
54: raise ArgumentError, "expected 3 or 4 parameters, got #{args.length}"
55: end
56:
57: local_port_type = :long
58:
59: socket = begin
60: if args.first.class == UNIXServer
61: local_port_type = :string
62: args.shift
63: else
64: bind_address = "127.0.0.1"
65: bind_address = args.shift if args.first.is_a?(String) && args.first =~ /\D/
66: local_port = args.shift.to_i
67: local_port_type = :long
68: TCPServer.new(bind_address, local_port)
69: end
70: end
71:
72: remote_host = args.shift
73: remote_port = args.shift.to_i
74:
75: @local_forwarded_ports[[local_port, bind_address]] = socket
76:
77: session.listen_to(socket) do |server|
78: client = server.accept
79: debug { "received connection on #{socket}" }
80:
81: channel = session.open_channel("direct-tcpip", :string, remote_host, :long, remote_port, :string, bind_address, local_port_type, local_port) do |achannel|
82: achannel.info { "direct channel established" }
83: end
84:
85: prepare_client(client, channel, :local)
86:
87: channel.on_open_failed do |ch, code, description|
88: channel.error { "could not establish direct channel: #{description} (#{code})" }
89: channel[:socket].close
90: end
91: end
92: end