forked from hdiniz/rtpdump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
353 lines (283 loc) · 7.73 KB
/
main.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"fmt"
"net"
"os"
"sync"
"time"
"github.com/hdiniz/rtpdump/codecs"
"github.com/hdiniz/rtpdump/console"
"github.com/hdiniz/rtpdump/esp"
"github.com/hdiniz/rtpdump/log"
"github.com/hdiniz/rtpdump/rtp"
"github.com/urfave/cli"
)
func loadKeyFile(c *cli.Context) error {
return esp.LoadKeyFile(c.GlobalString("key-file"))
}
var streamsCmd = func(c *cli.Context) error {
loadKeyFile(c)
inputFile := c.Args().First()
if len(c.Args()) <= 0 {
cli.ShowCommandHelp(c, "streams")
return cli.NewExitError("wrong usage for streams", 1)
}
rtpReader, err := rtp.NewRtpReader(inputFile)
if err != nil {
return cli.NewMultiError(cli.NewExitError("failed to open file", 1), err)
}
defer rtpReader.Close()
rtpStreams := rtpReader.GetStreams()
if len(rtpStreams) <= 0 {
fmt.Println("No streams found")
return nil
}
for _, v := range rtpStreams {
fmt.Printf("%s\n", v)
}
return nil
}
var playCmd = func(c *cli.Context) error {
loadKeyFile(c)
inputFile := c.Args().First()
if inputFile == "" {
cli.ShowCommandHelp(c, "play")
return cli.NewExitError("wrong usage for play", 1)
}
host := c.String("host")
port := c.Int("port")
rtpReader, err := rtp.NewRtpReader(inputFile)
if err != nil {
return cli.NewMultiError(cli.NewExitError("failed to open file", 1), err)
}
defer rtpReader.Close()
rtpStreams := rtpReader.GetStreams()
if len(rtpStreams) <= 0 {
fmt.Println("No streams found")
return nil
}
var rtpStreamsOptions []string
for _, v := range rtpStreams {
rtpStreamsOptions = append(rtpStreamsOptions, v.String())
}
streamIndex, err := console.ExpectIntRange(
0,
len(rtpStreams),
console.ListPrompt("Choose RTP Stream", rtpStreamsOptions...))
if err != nil {
return cli.NewMultiError(cli.NewExitError("invalid input", 1), err)
}
if streamIndex == 0 {
fmt.Print("Playing all streams\n\n")
// Locate the start time of first stream
var baseTime time.Time = rtpStreams[0].StartTime
for _, v := range rtpStreams {
if v.StartTime.Before(baseTime) {
baseTime = v.StartTime
}
}
var waitGroup sync.WaitGroup
for i, stream := range rtpStreams {
// Compute delay start w/respect to start of initial stream
first := stream.RtpPackets[0]
delay := first.ReceivedAt.Sub(baseTime)
waitGroup.Add(1)
go playStream(&waitGroup, i+1, stream, host, port+(2*i), delay)
}
// wait for all streams players to complete
waitGroup.Wait()
fmt.Printf("All streams completed\n\n")
} else {
stream := rtpStreams[streamIndex-1]
return playStream(nil, streamIndex, stream, host, port, 0)
}
return nil
}
func playStream(wg *sync.WaitGroup, streamIndex int, stream *rtp.RtpStream, host string, port int, delay time.Duration) error {
// if run as part of a waitgroup, notify done at the completion
if wg != nil {
defer wg.Done()
}
fmt.Printf("(%-3d) %s -> Streaming to: %s:%d\n", streamIndex, stream, host, port)
RemoteAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", host, port))
conn, err := net.DialUDP("udp", nil, RemoteAddr)
defer conn.Close()
if err != nil {
fmt.Printf("Some error: %v\n", err)
return err
}
if delay > 0 {
fmt.Printf("(%-3d) Delaying of (%d) ns\n", streamIndex, delay.Nanoseconds())
time.Sleep(delay)
}
len := len(stream.RtpPackets)
for i, v := range stream.RtpPackets {
fmt.Printf("(%-3d) ", streamIndex)
fmt.Println(v)
conn.Write(v.Data)
if i < len-1 {
next := stream.RtpPackets[i+1]
wait := next.ReceivedAt.Sub(v.ReceivedAt)
time.Sleep(wait)
}
}
fmt.Printf("(%-3d) Completed\n", streamIndex)
return nil
}
var dumpCmd = func(c *cli.Context) error {
loadKeyFile(c)
inputFile := c.Args().First()
if inputFile == "" {
cli.ShowCommandHelp(c, "dump")
return cli.NewExitError("wrong usage for dump", 1)
}
rtpReader, err := rtp.NewRtpReader(inputFile)
if err != nil {
return cli.NewMultiError(cli.NewExitError("failed to open file", 1), err)
}
defer rtpReader.Close()
return doInteractiveDump(c, rtpReader)
}
func doInteractiveDump(c *cli.Context, rtpReader *rtp.RtpReader) error {
rtpStreams := rtpReader.GetStreams()
if len(rtpStreams) <= 0 {
fmt.Println("No streams found")
return nil
}
var rtpStreamsOptions []string
for _, v := range rtpStreams {
rtpStreamsOptions = append(rtpStreamsOptions, v.String())
}
streamIndex, err := console.ExpectIntRange(
1,
len(rtpStreams),
console.ListPrompt("Choose RTP Stream", rtpStreamsOptions...))
if err != nil {
return cli.NewMultiError(cli.NewExitError("invalid input", 1), err)
}
fmt.Printf("(%-3d) %s\n\n", streamIndex, rtpStreams[streamIndex-1])
var codecList []string
for _, v := range codecs.CodecList {
codecList = append(codecList, v.Name)
}
codecIndex, err := console.ExpectIntRange(
1,
len(codecs.CodecList),
console.ListPrompt("Choose codec:", codecList...))
if err != nil {
return cli.NewMultiError(cli.NewExitError("invalid input", 1), err)
}
fmt.Printf("(%-3d) %s\n\n", codecIndex, codecs.CodecList[codecIndex-1].Name)
codecMetadata := codecs.CodecList[codecIndex-1]
optionsMap := make(map[string]string)
for _, v := range codecMetadata.Options {
var optionValue string
if v.RestrictValues {
optionValue, err = console.ExpectRestrictedString(
v.ValidValues,
console.KeyValuePrompt(fmt.Sprintf("%s - %s", v.Name, v.Description),
v.ValidValues, v.ValueDescription))
} else {
optionValue, err = console.ExpectAnyString(
console.Prompt(fmt.Sprintf("%s - %s: ", v.Name, v.Description)))
}
if err != nil {
return cli.NewMultiError(cli.NewExitError("invalid input", 1), err)
}
optionsMap[v.Name] = optionValue
}
outputFile, err := console.ExpectAnyString(console.Prompt("Output file: "))
if err != nil {
return cli.NewMultiError(cli.NewExitError("invalid input", 1), err)
}
fmt.Printf("%s\n", outputFile)
codec := codecMetadata.Init()
err = codec.SetOptions(optionsMap)
if err != nil {
return err
}
codec.Init()
f, err := os.Create(outputFile)
defer f.Close()
f.Write(codec.GetFormatMagic())
for _, r := range rtpStreams[streamIndex-1].RtpPackets {
frames, err := codec.HandleRtpPacket(r)
if err == nil {
f.Write(frames)
}
}
f.Sync()
return nil
}
func codecsList(c *cli.Context) error {
codec := c.Args().First()
found := codec == ""
for _, v := range codecs.CodecList {
if found || codec == v.Name {
fmt.Printf("%s\n", v.Describe())
found = true
}
}
if !found {
fmt.Printf("Codec %s not available\n", codec)
}
return nil
}
func main() {
log.SetLevel(log.INFO)
app := cli.NewApp()
app.Name = "rtpdump"
app.Version = "0.9.0"
cli.AppHelpTemplate += `
/\_/\
( o.o )
> ^ <
`
app.Commands = []cli.Command{
{
Name: "streams",
Aliases: []string{"s"},
Usage: "display rtp streams in pcap file",
ArgsUsage: "[pcap-file]",
Action: streamsCmd,
},
{
Name: "dump",
Aliases: []string{"d"},
Usage: "dumps rtp payload to file",
ArgsUsage: "[pcap-file]",
Action: dumpCmd,
},
{
Name: "play",
Aliases: []string{"p"},
Usage: "replays the selected rtp stream ;)",
ArgsUsage: "[pcap-file]",
Action: playCmd,
Flags: []cli.Flag{
cli.StringFlag{Name: "host", Value: "localhost", Usage: "destination host for replayed RTP packets"},
cli.IntFlag{Name: "port", Value: 1234, Usage: "destination port for replayed RTP packets"},
},
},
{
Name: "codecs",
Aliases: []string{"c"},
Usage: "lists supported codecs information",
Subcommands: cli.Commands{
cli.Command{
Name: "list",
Action: codecsList,
ArgsUsage: "[codec name or empty for all]",
},
},
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "key-file, k",
Value: "esp-keys.txt",
Usage: "Load ipsec keys from `FILE`",
},
}
app.Run(os.Args)
}