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
|
diff --git a/pkg/pssh/server.go b/pkg/pssh/server.go
index 31446f2..31a4301 100644
--- a/pkg/pssh/server.go
+++ b/pkg/pssh/server.go
@@ -1,6 +1,7 @@
package pssh
import (
+ "bytes"
"context"
"crypto/ed25519"
"crypto/rand"
@@ -185,6 +186,32 @@ func (s *SSHServerConnSession) Pty() (*Pty, <-chan Window, bool) {
return s.pty, s.winch, true
}
+// Write overrides the embedded Channel's Write to normalize line endings when PTY is allocated.
+func (s *SSHServerConnSession) Write(p []byte) (n int, err error) {
+ s.mu.Lock()
+ hasPty := s.pty != nil
+ s.mu.Unlock()
+
+ if !hasPty {
+ // No PTY, write as-is
+ return s.Channel.Write(p)
+ }
+
+ // When PTY is active, normalize line endings like a real terminal would.
+ // Replace \n with \r\n, but avoid double \r\n.
+ normalized := bytes.ReplaceAll(p, []byte{'\n'}, []byte{'\r', '\n'})
+ normalized = bytes.ReplaceAll(normalized, []byte{'\r', '\r', '\n'}, []byte{'\r', '\n'})
+
+ // Write the normalized data
+ written, err := s.Channel.Write(normalized)
+
+ // Return the count based on original data length, not normalized
+ if written > len(p) {
+ written = len(p)
+ }
+ return written, err
+}
+
var _ context.Context = &SSHServerConnSession{}
func (sc *SSHServerConn) Handle(chans <-chan ssh.NewChannel, reqs <-chan *ssh.Request) error {
|