diff --git a/diagram.txt b/diagram.txt new file mode 100644 index 0000000..aa7dbad --- /dev/null +++ b/diagram.txt @@ -0,0 +1,7 @@ + NAT NAT + My machine | {{ .Hostname }} | Remote Machine + +----------+ | +----------+ | +----------+ + | | | {{.Pp}}| |{{.Pp}} | {{.Rp}}| | + | +<-----------------------+----------->+ +<-------------+------->+ | + +----------+ | +----------+ | +----------+ + | | \ No newline at end of file diff --git a/modd.conf b/modd.conf index 649a319..b522da4 100644 --- a/modd.conf +++ b/modd.conf @@ -1,4 +1,6 @@ **/*.go +diagram.txt +.env Makefile { prep: make build daemon: make run diff --git a/session_handler.go b/session_handler.go index c9a31ab..6f5f1e4 100644 --- a/session_handler.go +++ b/session_handler.go @@ -1,13 +1,23 @@ package rebound import ( + "bytes" "fmt" "io" + "log" + "strings" + "text/template" + + _ "embed" "github.com/gliderlabs/ssh" "github.com/pkg/errors" ) +//go:embed diagram.txt +var asciiDiagram string +var asciiDiagramTmpl = template.Must(template.New("").Parse(asciiDiagram)) + func (s *Server) handleSession(sess ssh.Session) { ctx := sess.Context() sessionID := SessionID(ctx.SessionID()) @@ -15,9 +25,7 @@ func (s *Server) handleSession(sess ssh.Session) { s.log("(%s): session opened", sessionID) message := ` -Welcome on Rebound ! - -Type Ctrl+C or Ctrl+D to exit. + Welcome on Rebound ! ` if _, err := sess.Write([]byte(message)); err != nil { @@ -71,7 +79,9 @@ func (s *Server) handleSessionData(sess ssh.Session, data SessionData) error { } func (s *Server) writeConsumerMessage(sess ssh.Session, data SessionData) error { - message := `` + message := ` + Type Ctrl+C or Ctrl+D to exit. + ` if _, err := sess.Write([]byte(message)); err != nil { return errors.WithStack(err) @@ -81,12 +91,46 @@ func (s *Server) writeConsumerMessage(sess ssh.Session, data SessionData) error } func (s *Server) writeProviderMessage(sess ssh.Session, data SessionData) error { + var diagramBuff bytes.Buffer + + hostname := s.opts.PublicHost + if len(hostname) < 24 { + halfPadding := (24 - len(hostname)) / 2 + leftPadding := strings.Repeat(" ", halfPadding) + rightPadding := strings.Repeat(" ", halfPadding) + hostname = fmt.Sprintf("%s%s%s", leftPadding, hostname, rightPadding) + if len(hostname) > 24 { + hostname = hostname[0:23] + } + } else if len(hostname) >= 24 { + hostname = hostname[0:20] + "..." + } + + log.Printf("'%s'", hostname) + + tmplData := struct { + Pp string + Rp string + Hostname string + }{ + Pp: fmt.Sprintf("%04d", s.opts.PublicPort), + Rp: "", + Hostname: hostname, + } + + if err := asciiDiagramTmpl.Execute(&diagramBuff, tmplData); err != nil { + return errors.WithStack(err) + } + message := fmt.Sprintf(` -You can connect to your tunnel by running in an other terminal: + You can connect to your tunnel by running in an other terminal: -ssh -L :0.0.0.0:1 %s@%s -p %d + ssh -L :127.0.0.1:1 %s@%s -p %d -`, data.Token, s.opts.PublicHost, s.opts.PublicPort) +%s + + Type Ctrl+C or Ctrl+D to exit. +`, data.Token, s.opts.PublicHost, s.opts.PublicPort, diagramBuff.String()) if _, err := sess.Write([]byte(message)); err != nil { return errors.WithStack(err)