2011-10-24 22:36:08 +00:00
|
|
|
#lang racket/base
|
|
|
|
;; (Temporary) example client and server
|
|
|
|
|
|
|
|
(require racket/tcp)
|
|
|
|
(require racket/pretty)
|
|
|
|
(require racket/match)
|
2011-10-25 20:45:15 +00:00
|
|
|
(require racket/class)
|
|
|
|
(require racket/port)
|
2011-10-26 23:16:16 +00:00
|
|
|
(require racket/sandbox)
|
2011-10-24 22:36:08 +00:00
|
|
|
|
2011-10-27 18:15:44 +00:00
|
|
|
(require "ssh-service.rkt")
|
2011-10-24 22:36:08 +00:00
|
|
|
(require "standard-thread.rkt")
|
|
|
|
|
2011-10-27 19:25:06 +00:00
|
|
|
(require "conversation.rkt")
|
|
|
|
|
2011-10-27 18:15:44 +00:00
|
|
|
#;(define (t-client)
|
2011-10-24 22:36:08 +00:00
|
|
|
(let-values (((i o) (tcp-connect "localhost"
|
|
|
|
2323
|
|
|
|
;;22
|
|
|
|
)))
|
|
|
|
(let ((api (ssh-session 'client i o)))
|
|
|
|
(printf "Got API ~v\n" api)
|
|
|
|
(semaphore-wait (make-semaphore 0)))))
|
|
|
|
|
2011-10-27 22:13:49 +00:00
|
|
|
(define *environments* (make-hash))
|
2011-10-27 19:25:06 +00:00
|
|
|
|
|
|
|
(define *interaction* (make-room 'interaction))
|
2011-10-31 12:54:51 +00:00
|
|
|
(spy-on *interaction*)
|
2011-10-27 19:25:06 +00:00
|
|
|
|
2011-10-27 22:13:49 +00:00
|
|
|
(define *interaction-handle* (make-parameter #f))
|
|
|
|
|
2011-10-27 19:25:06 +00:00
|
|
|
(define *prompt* "RacketSSH> ")
|
|
|
|
|
|
|
|
(define (->string/safe bs)
|
|
|
|
(cond
|
|
|
|
((string? bs) bs)
|
|
|
|
((bytes? bs) (with-handlers ((exn? (lambda (e) (bytes->string/latin-1 bs))))
|
|
|
|
(bytes->string/utf-8 bs)))
|
|
|
|
(else (call-with-output-string (lambda (p) (write bs p))))))
|
|
|
|
|
|
|
|
(define (dump-interactions handle)
|
|
|
|
(display *prompt*)
|
|
|
|
(let retry-without-prompt ()
|
|
|
|
(sync (handle-evt (send handle listen-evt)
|
|
|
|
(lambda (message)
|
|
|
|
(display "\033[1G\033[2K") ;; clear current line
|
|
|
|
(let loop ((message message))
|
|
|
|
(when message
|
|
|
|
(match message
|
|
|
|
((arrived who) (printf "*** ~a arrived\n" (->string/safe who)))
|
|
|
|
((departed who why) (printf "*** ~a departed (~a)\n"
|
|
|
|
(->string/safe who)
|
|
|
|
(->string/safe why)))
|
|
|
|
((says who what #f)
|
|
|
|
(printf " ~a: ~a\n" (->string/safe who) (->string/safe what)))
|
|
|
|
((says who what topic)
|
|
|
|
(printf " ~a (~a): ~a\n"
|
|
|
|
(->string/safe who)
|
|
|
|
(->string/safe topic)
|
|
|
|
(->string/safe what))))
|
|
|
|
(loop (send handle try-listen))))
|
|
|
|
(dump-interactions handle)))
|
|
|
|
(handle-evt (peek-string-evt 1 0 #f (current-input-port))
|
|
|
|
(lambda (s)
|
|
|
|
(cond
|
|
|
|
((eof-object? s))
|
|
|
|
((char-whitespace? (string-ref s 0))
|
|
|
|
(read-string 1 (current-input-port))
|
|
|
|
(retry-without-prompt))
|
|
|
|
(else 'ready-to-read-something-real)))))))
|
|
|
|
|
|
|
|
(define (call-with-interaction-prompt-read handle thunk)
|
|
|
|
(parameterize ((current-prompt-read (lambda ()
|
|
|
|
(dump-interactions handle)
|
|
|
|
(read-syntax "<ssh-session>" (current-input-port)))))
|
|
|
|
(thunk)))
|
|
|
|
|
2011-10-27 22:13:49 +00:00
|
|
|
(define (get-user-environment username)
|
|
|
|
(when (not (hash-has-key? *environments* username))
|
|
|
|
(hash-set! *environments* username (make-base-namespace)))
|
|
|
|
(hash-ref *environments* username))
|
2011-10-27 19:25:06 +00:00
|
|
|
|
2011-10-27 18:15:44 +00:00
|
|
|
(define (repl-shell username in out)
|
2011-10-27 19:25:06 +00:00
|
|
|
(define handle (join-room *interaction* username))
|
2011-10-27 22:13:49 +00:00
|
|
|
(define env (get-user-environment username))
|
2011-10-27 18:15:44 +00:00
|
|
|
(parameterize ((current-input-port in)
|
|
|
|
(current-output-port out)
|
|
|
|
(current-error-port out)
|
|
|
|
(sandbox-input in)
|
|
|
|
(sandbox-output out)
|
|
|
|
(sandbox-error-output out)
|
2011-10-27 22:13:49 +00:00
|
|
|
(current-namespace env)
|
|
|
|
(sandbox-namespace-specs (list (lambda () env))))
|
|
|
|
(parameterize ((current-eval (make-evaluator '(begin))))
|
|
|
|
(printf "Hello, ~a.\n" username)
|
|
|
|
(eval `(,*interaction-handle* ,handle))
|
2011-10-27 19:50:18 +00:00
|
|
|
(eval `(define say ,(lambda (utterance)
|
2011-10-27 22:13:49 +00:00
|
|
|
(printf " You: ~a\n" (->string/safe utterance))
|
|
|
|
(send (*interaction-handle*) say utterance)
|
2011-10-27 19:50:18 +00:00
|
|
|
(void))))
|
2011-10-27 19:25:06 +00:00
|
|
|
(call-with-interaction-prompt-read handle read-eval-print-loop))
|
2011-10-27 18:15:44 +00:00
|
|
|
(fprintf out "\nGoodbye!\n")
|
|
|
|
(close-input-port in)
|
|
|
|
(close-output-port out)))
|
2011-10-24 22:36:08 +00:00
|
|
|
|
|
|
|
(define (t-server)
|
2011-10-27 22:14:04 +00:00
|
|
|
(define s (tcp-listen 2322 4 #t))
|
2011-10-24 22:36:08 +00:00
|
|
|
(printf "Accepting...\n")
|
2011-10-27 19:25:06 +00:00
|
|
|
(tcp-pty-ssh-server s repl-shell #:prompt *prompt*))
|
2011-10-24 22:36:08 +00:00
|
|
|
|
|
|
|
(if (getenv "clientmode")
|
2011-10-27 18:15:44 +00:00
|
|
|
(void) #;(t-client)
|
2011-10-24 22:36:08 +00:00
|
|
|
(t-server))
|