File: pktline.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (87 lines) | stat: -rw-r--r-- 1,877 bytes parent folder | download | duplicates (2)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package ssh

import (
	"io"

	"github.com/git-lfs/pktline"
	"github.com/rubyist/tracerx"
)

func pktlineReader(p Pktline) io.Reader {
	if pl, ok := p.(*pktline.Pktline); ok {
		return pktline.NewPktlineReaderFromPktline(pl, 65536)
	}
	tp := p.(*TraceablePktline)
	return pktline.NewPktlineReaderFromPktline(tp.pl, 65536)
}

type Pktline interface {
	ReadPacketList() ([]string, error)
	ReadPacketTextWithLength() (string, int, error)
	WritePacket([]byte) error
	WritePacketText(string) error
	WriteDelim() error
	WriteFlush() error
}

type TraceablePktline struct {
	id int
	pl *pktline.Pktline
}

func (tp *TraceablePktline) ReadPacketList() ([]string, error) {
	var list []string
	for {
		data, pktLen, err := tp.pl.ReadPacketTextWithLength()
		if err != nil {
			return nil, err
		}
		if pktLen <= 1 {
			tracerx.Printf("packet %02x < %04x", tp.id, pktLen)
		} else {
			tracerx.Printf("packet %02x < %s", tp.id, data)
		}

		if pktLen == 0 {
			break
		}

		list = append(list, data)
	}

	return list, nil
}

func (tp *TraceablePktline) ReadPacketTextWithLength() (string, int, error) {
	s, pktLen, err := tp.pl.ReadPacketTextWithLength()
	if err != nil {
		return "", 0, err
	}

	if pktLen <= 1 {
		tracerx.Printf("packet %02x < %04x", tp.id, pktLen)
	} else {
		tracerx.Printf("packet %02x < %s", tp.id, s)
	}
	return s, pktLen, nil
}

func (tp *TraceablePktline) WritePacket(b []byte) error {
	// Don't trace because this is probably binary data.
	return tp.pl.WritePacket(b)
}

func (tp *TraceablePktline) WritePacketText(s string) error {
	tracerx.Printf("packet %02x > %s", tp.id, s)
	return tp.pl.WritePacketText(s)
}

func (tp *TraceablePktline) WriteDelim() error {
	tracerx.Printf("packet %02x > 0001", tp.id)
	return tp.pl.WriteDelim()
}

func (tp *TraceablePktline) WriteFlush() error {
	tracerx.Printf("packet %02x > 0000", tp.id)
	return tp.pl.WriteFlush()
}