|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "compress/zlib" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + |
| 10 | + "github.com/pkg/errors" |
| 11 | + "github.com/xhebox/bstruct/byteorder" |
| 12 | + "github.com/xhebox/sbutils/lib/packet" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + ErrUnknownType = errors.New("packet of unknown type") |
| 17 | +) |
| 18 | + |
| 19 | +type FilterCallback func(*Filter, packet.Packet) |
| 20 | + |
| 21 | +type Filter struct { |
| 22 | + in io.Reader |
| 23 | + out io.Writer |
| 24 | + // filter function |
| 25 | + cb FilterCallback |
| 26 | + End byteorder.ByteOrder |
| 27 | +} |
| 28 | + |
| 29 | +func NewFilter(in io.Reader, out io.Writer, cb FilterCallback) *Filter { |
| 30 | + r := &Filter{} |
| 31 | + r.in = in |
| 32 | + r.out = out |
| 33 | + r.cb = cb |
| 34 | + r.End = byteorder.BigEndian |
| 35 | + return r |
| 36 | +} |
| 37 | + |
| 38 | +func (r *Filter) ReadPacket() (*packet.BasePacket, error) { |
| 39 | + pktType, e := byteorder.Uint8(r.in) |
| 40 | + if e != nil { |
| 41 | + return nil, errors.WithStack(e) |
| 42 | + } |
| 43 | + |
| 44 | + length, e := byteorder.Varint(r.in, r.End) |
| 45 | + if e != nil { |
| 46 | + return nil, errors.WithStack(e) |
| 47 | + } |
| 48 | + abslength := int64(length) |
| 49 | + if abslength < 0 { |
| 50 | + abslength = -abslength |
| 51 | + } |
| 52 | + |
| 53 | + buf := &bytes.Buffer{} |
| 54 | + |
| 55 | + in := io.LimitReader(r.in, abslength) |
| 56 | + |
| 57 | + if length < 0 { |
| 58 | + zin, e := zlib.NewReader(in) |
| 59 | + if e != nil { |
| 60 | + return nil, errors.WithStack(e) |
| 61 | + } |
| 62 | + |
| 63 | + defer zin.Close() |
| 64 | + in = zin |
| 65 | + } |
| 66 | + |
| 67 | + _, e = io.Copy(buf, in) |
| 68 | + if e != nil { |
| 69 | + return nil, errors.WithStack(e) |
| 70 | + } |
| 71 | + |
| 72 | + return &packet.BasePacket{Type: pktType, Buf: buf.Bytes()}, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (r *Filter) WritePacket(pkt *packet.BasePacket) error { |
| 76 | + e := byteorder.PutUint8(r.out, pkt.Type) |
| 77 | + if e != nil { |
| 78 | + return errors.WithStack(e) |
| 79 | + } |
| 80 | + |
| 81 | + e = byteorder.PutVarint(r.out, r.End, int64(len(pkt.Buf))) |
| 82 | + if e != nil { |
| 83 | + return errors.WithStack(e) |
| 84 | + } |
| 85 | + |
| 86 | + _, e = r.out.Write(pkt.Buf) |
| 87 | + if e != nil { |
| 88 | + return errors.WithStack(e) |
| 89 | + } |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +// loop function |
| 95 | +func (r *Filter) Loop() error { |
| 96 | + for { |
| 97 | + pkt, e := r.ReadPacket() |
| 98 | + if e != nil { |
| 99 | + return errors.WithStack(e) |
| 100 | + } |
| 101 | + |
| 102 | + p, e := pkt.Parse(r.End) |
| 103 | + if e != nil { |
| 104 | + fmt.Println(e) |
| 105 | + } |
| 106 | + |
| 107 | + if p != nil { |
| 108 | + if r.cb != nil { |
| 109 | + r.cb(r, p) |
| 110 | + } |
| 111 | + |
| 112 | + pkt, e = p.Pack(r.End) |
| 113 | + if e != nil { |
| 114 | + return errors.WithStack(e) |
| 115 | + } |
| 116 | + } else if pkt.Type == 13 { |
| 117 | + ioutil.WriteFile("../out", pkt.Buf, 0644) |
| 118 | + } |
| 119 | + |
| 120 | + if e := r.WritePacket(pkt); e != nil { |
| 121 | + return errors.WithStack(e) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments