Skip to content

Commit

Permalink
fix(telnet): Properly handle ordering of term and window size
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Apr 18, 2024
1 parent 7c98832 commit b424eb5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
13 changes: 8 additions & 5 deletions internal/server/telnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,20 @@ func (s *TelnetServer) Handler(ctx context.Context, conn net.Conn, m *movie.Movi
ctx, cancel := context.WithCancel(ctx)
defer cancel()

termCh := make(chan telnet.TermInfo)
termCh := make(chan string)
defer close(termCh)
sizeCh := make(chan telnet.WindowSize, 1)
defer close(sizeCh)
go func() {
// Proxy input to program
_ = telnet.Proxy(conn, inW, termCh)
_ = telnet.Proxy(conn, inW, termCh, sizeCh)
cancel()
}()

var profile termenv.Profile
select {
case info := <-termCh:
profile = util.Profile(info.Term)
case term := <-termCh:
profile = util.Profile(term)
case <-time.After(250 * time.Millisecond):
profile = termenv.ANSI256
}
Expand All @@ -139,7 +142,7 @@ func (s *TelnetServer) Handler(ctx context.Context, conn net.Conn, m *movie.Movi
go func() {
for {
select {
case info := <-termCh:
case info := <-sizeCh:
if info.Width != 0 && info.Height != 0 {
program.Send(tea.WindowSizeMsg{
Width: int(info.Width),
Expand Down
20 changes: 7 additions & 13 deletions internal/server/telnet/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ import (
log "github.com/sirupsen/logrus"
)

type TermInfo struct {
Term string
WindowSize
}

type WindowSize struct {
Width, Height uint16
}

func Proxy(conn net.Conn, proxy io.Writer, termCh chan TermInfo) error {
func Proxy(conn net.Conn, proxy io.Writer, termCh chan string, sizeCh chan WindowSize) error {
reader := bufio.NewReaderSize(conn, 64)
var info TermInfo
var wroteTelnetCommands bool
var wroteTermType bool

Expand Down Expand Up @@ -71,19 +65,19 @@ func Proxy(conn net.Conn, proxy io.Writer, termCh chan TermInfo) error {
switch Operator(command[0]) {
case TerminalType:
if len(command) > 5 && !wroteTermType {
wroteTermType = true
info.Term = string(command[2 : len(command)-2])
log.Trace("Got terminal type")
termCh <- info
termCh <- string(command[2 : len(command)-2])
wroteTermType = true
}
case NegotiateAboutWindowSize:
if len(command) > 5 {
if len(command) >= 5 {
log.Trace("Got window size")
r := bytes.NewReader(command[1 : len(command)-2])
if err := binary.Read(r, binary.BigEndian, &info.WindowSize); err != nil {
var size WindowSize
if err := binary.Read(r, binary.BigEndian, &size); err != nil {
return err
}
termCh <- info
sizeCh <- size
}
}
}
Expand Down

0 comments on commit b424eb5

Please sign in to comment.