feat: add ascii diagram to tunnel creation message

This commit is contained in:
wpetit 2023-09-18 07:59:03 -06:00
parent 1bdad36787
commit 56d7174b96
3 changed files with 60 additions and 7 deletions

7
diagram.txt Normal file
View File

@ -0,0 +1,7 @@
NAT NAT
My machine | {{ .Hostname }} | Remote Machine
+----------+ | +----------+ | +----------+
| |<local_service_port> | {{.Pp}}| |{{.Pp}} | {{.Rp}}| |
| +<-----------------------+----------->+ +<-------------+------->+ |
+----------+ | +----------+ | +----------+
| |

View File

@ -1,4 +1,6 @@
**/*.go
diagram.txt
.env
Makefile {
prep: make build
daemon: make run

View File

@ -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: "<port>",
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 <local-port>:0.0.0.0:1 %s@%s -p %d
ssh -L <port>: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)