api-tunnels.texi 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. @c -*-texinfo-*-
  2. @c This file is part of Guile-SSH Reference Manual.
  3. @c Copyright (C) 2015 Artyom V. Poptsov
  4. @c See the file guile-ssh.texi for copying conditions.
  5. @node Tunnels
  6. @section Tunnels
  7. @cindex Tunnels
  8. The following procedures from @code{(ssh tunnel)} module are a high-level API
  9. built upon the basic port forwarding facilities for managing port forwards.
  10. @deffn {Scheme Procedure} make-tunnel session [#:bind-address=''127.0.0.1''] #:port #:host [#:host-port=port] [#:reverse?=#f]
  11. Make a new SSH tunnel using @var{session} from @var{bind-address} and
  12. @var{port} to a @var{host} and @var{host-port}.
  13. The procedure is capable of creating both direct and reverse port forwarding
  14. tunnels; the type of a tunnel is determined by @var{reverse?} argument. If
  15. @var{reverse?} is set to @code{#f} then a reverse port forwarding tunnel will
  16. be created.
  17. Setting @var{reverse?} to @code{#t} changes the direction of the tunnel and a
  18. reverse port forwarding tunnel will be created. In this case a server
  19. allocates a socket to listen to @var{port} on the remote side, and whenever a
  20. connection is made to this port, the connection is forwarded over the secure
  21. channel, and a connection is made to @var{host} and @var{host-port} from the
  22. local machine. @var{host} can be set to @code{#f} to tell the server to
  23. listen on all addresses and known protocol families. Setting a @var{port} to
  24. 0 tells the server to bind the first unprivileged port.
  25. The procedure does not binds ports nor transfers data to the port (in case of
  26. reverse port forwarding), you should start port forwarding by means of the
  27. procedures that operate on a <tunnel> object -- e.g. @code{start-forward} or
  28. @code{call-with-ssh-forward}.
  29. Return a new tunnel object.
  30. @end deffn
  31. @deffn {Scheme Procedure} tunnel? x
  32. Return @code{#t} if @var{x} is an Guile-SSH tunnel, @code{#f} otherwise.
  33. @end deffn
  34. @deffn {Scheme procedure} tunnel-reverse? x
  35. Check if @var{x} is a reverse port forwarding tunnel.
  36. @end deffn
  37. @deffn {Scheme procedure} tunnel-session tunnel
  38. Get a session associated with a @var{tunnel}.
  39. @end deffn
  40. @deffn {Scheme Procedure} tunnel-bind-address tunnel
  41. Get a source host of a @var{tunnel}.
  42. @end deffn
  43. @deffn {Scheme Procedure} tunnel-port tunnel
  44. Get a local port of a @var{tunnel}.
  45. @end deffn
  46. @deffn {Scheme Procedure} tunnel-host tunnel
  47. Get a remote host of a @var{tunnel}.
  48. @end deffn
  49. @deffn {Scheme Procedure} tunnel-host-port tunnel
  50. Get a remote port of a @var{tunnel}.
  51. @end deffn
  52. @deffn {Scheme Procedure} start-forward tunnel
  53. Start port forwarding on @var{tunnel}. The procedure actually binds tunnel
  54. ports and forwards data.
  55. @end deffn
  56. @deffn {Scheme Procedure} call-with-ssh-forward tunnel proc
  57. Open a new @var{tunnel} and start port forwarding. @var{proc} is called with
  58. a socket as an argument. All I/O on the socket will be forwarded to the
  59. remote host and port of a @var{tunnel}. Return the result the @var{proc}
  60. call.
  61. As a practical example, let's say you want to use
  62. @url{https://www.gnu.org/software/guile-rpc/, Guile-RPC} over SSH. Here's how
  63. you can implement an RPC call using @code{call-with-ssh-forward}:
  64. @lisp
  65. (let ((t (make-tunnel session
  66. #:port 12345
  67. ;; Suppose a Guile-RPC server listens on
  68. ;; 127.0.0.1:23456 on the remote host:
  69. #:host "127.0.0.1"
  70. #:host-port 23456)))
  71. (call-with-ssh-forward t
  72. (lambda (socket)
  73. (RPC-PROGRAM-proc some-data #x123 socket))))
  74. @end lisp
  75. The full example of an RPC client that uses a SSH tunnel is in
  76. @file{$prefix/share/guile-ssh/examples/rpc} directory.
  77. @end deffn
  78. @subsection Example
  79. Here is a simple Guile program that connects to ``www.example.org'' and starts
  80. port forwading from the local port 8080 to the port 80 on the remote host:
  81. @lisp
  82. #!/usr/bin/guile \
  83. -e main
  84. !#
  85. (use-modules (ssh session)
  86. (ssh auth)
  87. (ssh key)
  88. (ssh tunnel))
  89. (define (main args)
  90. (let ((s (make-session #:user "alice"
  91. #:host "localhost"
  92. #:port 22
  93. #:log-verbosity 'nolog))
  94. (k (private-key-from-file "/home/alice/.ssh/id_rsa")))
  95. (connect! s)
  96. (userauth-public-key! s k)
  97. (let ((t (make-tunnel s
  98. #:port 8080
  99. #:host "www.example.org"
  100. #:host-port 80)))
  101. (start-forward t))))
  102. @end lisp
  103. @c Local Variables:
  104. @c TeX-master: "guile-ssh.texi"
  105. @c End: