1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
;;; tunnel.scm -- SSH tunnels
;; Copyright (C) 2015-2021 Artyom V. Poptsov <poptsov.artyom@gmail.com>
;;
;; This file is a part of Guile-SSH.
;;
;; Guile-SSH is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; Guile-SSH is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Guile-SSH. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; High-level API built upon the basic port forwarding facilities for managing
;; port forwards.
;;; Code:
(define-module (ssh tunnel)
#:use-module (rnrs io ports)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-9 gnu)
#:use-module (srfi srfi-11)
#:use-module (ice-9 receive)
#:use-module (ice-9 threads)
#:use-module (rnrs bytevectors)
#:use-module (ssh session)
#:use-module (ssh channel)
#:export (make-tunnel
tunnel?
tunnel-reverse?
tunnel-session
tunnel-bind-address
tunnel-port
tunnel-host
tunnel-host-port
start-forward
call-with-ssh-forward
;; Helper procedures
make-tunnel-channel
tunnel-open-forward-channel))
;;; Tunnel type
(define-record-type <tunnel>
(%make-tunnel session
timeout
bind-address
port
host
host-port
reverse?)
tunnel?
(session tunnel-session) ; <session>
(timeout tunnel-timeout) ; <number>
(bind-address tunnel-bind-address) ; <string>
(port tunnel-port) ; <number>
(host tunnel-host) ; <string>
(host-port tunnel-host-port) ; <number>
(reverse? tunnel-reverse?)) ; <boolean>
(set-record-type-printer!
<tunnel>
(lambda (tunnel port)
"Print information about a TUNNEL to a PORT."
(let ((tunnel-address (number->string (object-address tunnel) 16)))
(if (tunnel-reverse? tunnel)
(format port "#<tunnel ~a:~a <- ~a:~a ~a>"
(tunnel-host tunnel)
(tunnel-host-port tunnel)
(if (tunnel-bind-address tunnel)
(tunnel-bind-address tunnel)
"*")
(tunnel-port tunnel)
tunnel-address)
(format port "#<tunnel ~a:~a -> ~a:~a ~a>"
(tunnel-bind-address tunnel)
(tunnel-port tunnel)
(tunnel-host tunnel)
(tunnel-host-port tunnel)
tunnel-address)))))
(define (make-tunnel-channel tunnel)
(let ((channel (make-channel (tunnel-session tunnel))))
(unless channel
(error "Could not make a channel" tunnel))
channel))
(define (tunnel-open-forward-channel tunnel)
"Open a new forward channel for a TUNNEL. Return the newly created open
channel, or throw an error if a channel could not be opened."
(let ((channel (make-tunnel-channel tunnel)))
(case (channel-open-forward channel
#:source-host (tunnel-bind-address tunnel)
#:local-port (tunnel-port tunnel)
#:remote-host (tunnel-host tunnel)
#:remote-port (tunnel-host-port tunnel))
((ok)
channel)
(else =>
(lambda (res) (error "Could not open forward channel" tunnel res))))))
(define (tunnel-listen-forward tunnel)
"Return value is undefined."
(receive (result port)
(channel-listen-forward (tunnel-session tunnel)
#:address (tunnel-bind-address tunnel)
#:port (tunnel-port tunnel))
;; TODO: Handle port
(or (eq? result 'ok)
(error "Could not open forward channel" tunnel result))))
;;; Procedures
(define* (make-tunnel session
#:key
(bind-address "127.0.0.1")
port
host
(host-port port)
(timeout 1000)
(reverse? #f))
"Make a new SSH tunnel using SESSION. The procedure returns a new <tunnel>
object.
In case of direct port forwarding (when REVERSE? is set to #f), a BIND-ADDRESS
is a host from which the connections are originated, and a PORT is a port on
which the tunnel will be listening to the incoming connections. A HOST and a
HOST-PORT is a host and port to which the connections are forwarded.
Setting REVERSE? to #t changes the direction of the tunnel and a reverse port
forwarding tunnel will be created. In this case a server allocates a socket
to listen to PORT on the remote side, and whenever a connection is made to
this port, the connection is forwarded over the secure channel, and a
connection is made to HOST and HOST-PORT from the local machine. HOST can be
set to #f to tell the server to listen on all addresses and known protocol
families. Setting a PORT to 0 tells the server to bind the first unprivileged
port.
The procedure does not binds ports nor transfers data to the port (in case of
reverse port forwarding), you should start port forwarding by means of the
procedures that operate on a <tunnel> object -- e.g. 'start-forward' or
'call-with-ssh-forward'."
(let ((timeout (if (and timeout (> timeout 0))
timeout
1)))
(%make-tunnel session
timeout
bind-address
port
host
host-port
reverse?)))
(define-syntax-rule (p1->p2? p1 p2)
"Return #t if P1 and P2 are open ports and P1 has data that can be read, #f
otherwise."
(and (not (port-closed? p1))
(not (port-closed? p2))
(char-ready? p1)))
(define-syntax cond-io
(syntax-rules (else <- -> =>)
((_ (p1 -> p2 => proc) ...)
(cond ((p1->p2? p1 p2) (proc p1 p2)) ...))
((_ (p1 <- p2 => proc) ...)
(cond ((p1->p2? p2 p1) (proc p1 p2)) ...))
((_ (p1 -> p2 => proc) ... (else exp ...))
(cond ((p1->p2? p1 p2) (proc p1 p2)) ... (else exp ...)))
((_ (p1 <- p2 => proc) ... (else exp ...))
(cond ((p1->p2? p2 p1) (proc p1 p2)) ... (else exp ...)))))
(define (transfer port-1 port-2)
"Transfer data from a PORT-1 to a PORT-2. Close both ports if reading from
the PORT-1 returns EOF."
(let ((data (get-bytevector-some port-1)))
(if (not (eof-object? data))
(put-bytevector port-2 data)
(begin
(close port-1)
(close port-2)))))
(define (tunnel-timeout/s+us tunnel)
"Get a TUNNEL timeout as two values: timeout in seconds and microseconds."
(let ((timeout (tunnel-timeout tunnel)))
(values (and timeout (quotient timeout 1000000))
(and timeout (remainder timeout 1000000)))))
(define (main-loop tunnel sock idle-proc)
"Start the main loop of a TUNNEL. Accept connections on SOCK, transfer data
between SOCK and the remote side. Call IDLE-PROC as
(idle-proc client-socket channel)
when no data is available."
(let-values (((timeout-s timeout-us) (tunnel-timeout/s+us tunnel)))
(define (select-client client)
(select (list client) '() '() timeout-s timeout-us))
(while (connected? (tunnel-session tunnel))
(catch #t
(lambda ()
(let* ((channel (tunnel-open-forward-channel tunnel))
(client-connection (accept sock))
(client (car client-connection)))
(while (channel-open? channel)
(cond-io
(client -> channel => transfer)
(channel -> client => transfer)
(else
(let ((selected (select-client client)))
(when (null? (car selected))
(idle-proc client channel))))))))
(const #t)))))
(define (main-loop/reverse tunnel idle-proc)
(define (tunnel-connect tunnel sock)
"Make a connection for a reverse TUNNEL. The return value is
unspecified."
(connect sock AF_INET
(inet-pton AF_INET (tunnel-host tunnel))
(tunnel-host-port tunnel)))
(let ((timeout (tunnel-timeout tunnel)))
(while (connected? (tunnel-session tunnel))
(receive (channel port)
(channel-accept-forward (tunnel-session tunnel) 1000)
(when channel
(let ((sock (socket PF_INET SOCK_STREAM 0)))
(tunnel-connect tunnel sock)
(while (channel-open? channel)
(cond-io
(channel -> sock => transfer)
(sock -> channel => transfer)
(else
;; XXX: Very hacky. We should use something like 'select'
;; here.
(when (channel-open? channel)
(usleep timeout)
(idle-proc sock channel)))))))))))
(define* (start-forward tunnel #:optional (idle-proc (const #f)))
"Start port forwarding for a TUNNEL. Call IDLE-PROC as
(idle-proc client-socket channel)
when no data is available to forward. If no IDLE-PROC is specified then a
procedure that always returns #f is used instead."
(if (tunnel-reverse? tunnel)
(begin
(tunnel-listen-forward tunnel)
(main-loop/reverse tunnel idle-proc))
(let ((sock (socket PF_INET SOCK_STREAM 0)))
(bind sock AF_INET (inet-pton AF_INET (tunnel-bind-address tunnel))
(tunnel-port tunnel))
(listen sock 10)
(main-loop tunnel sock idle-proc)
(close sock))))
(define (call-with-ssh-forward tunnel proc)
"Call a procedure PROC as (proc channel) where CHANNEL is a channel that
forwards all the received data to a remote side through a TUNNEL, and vice
versa. Return the result the PROC call."
(let ((channel (tunnel-open-forward-channel tunnel)))
(dynamic-wind
(const #f)
(lambda ()
(proc channel))
(lambda ()
(channel-cancel-forward (channel-get-session channel)
(tunnel-host tunnel)
(tunnel-host-port tunnel))))))
;;; tunnel.scm ends here.
|