|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "runtime" |
| 9 | + |
| 10 | + "github.com/gopacket/gopacket" |
| 11 | + "github.com/gopacket/gopacket/layers" |
| 12 | + "github.com/gopacket/gopacket/pcapgo" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + |
| 15 | + "github.com/buglloc/mr.duppl/software/pkg/dupplcap" |
| 16 | + "github.com/buglloc/mr.duppl/software/pkg/usbp" |
| 17 | +) |
| 18 | + |
| 19 | +var dumpArgs struct { |
| 20 | + Interface string |
| 21 | + NoFolding bool |
| 22 | + Output string |
| 23 | +} |
| 24 | + |
| 25 | +var dumpCmd = &cobra.Command{ |
| 26 | + Use: "dump", |
| 27 | + SilenceUsage: true, |
| 28 | + SilenceErrors: true, |
| 29 | + Short: "Dump Mr.Duppl capture", |
| 30 | + RunE: func(_ *cobra.Command, _ []string) error { |
| 31 | + dev, err := dupplcap.NewDevice(dumpArgs.Interface) |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("opening device: %w", err) |
| 34 | + } |
| 35 | + defer func() { _ = dev.Close() }() |
| 36 | + |
| 37 | + if err := dev.StartCapture(!dumpArgs.NoFolding); err != nil { |
| 38 | + return fmt.Errorf("start capture: %w", err) |
| 39 | + } |
| 40 | + defer func() { _ = dev.StopCapture() }() |
| 41 | + |
| 42 | + if len(dumpArgs.Output) != 0 { |
| 43 | + out, err := os.Create(dumpArgs.Output) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("creating output file: %w", err) |
| 46 | + } |
| 47 | + defer func() { _ = out.Close() }() |
| 48 | + |
| 49 | + return dumpCapture(dev, out) |
| 50 | + } |
| 51 | + |
| 52 | + return printCapture(dev) |
| 53 | + }, |
| 54 | +} |
| 55 | + |
| 56 | +func dumpCapture(dev *dupplcap.Device, out io.Writer) error { |
| 57 | + w, err := pcapgo.NewNgWriterInterface( |
| 58 | + out, |
| 59 | + pcapgo.NgInterface{ |
| 60 | + Name: dev.Iface(), |
| 61 | + OS: runtime.GOOS, |
| 62 | + LinkType: layers.LinkType(dupplcap.LinkTypeUSBFullSpeed.Int()), |
| 63 | + SnapLength: 0, //unlimited |
| 64 | + // TimestampResolution: 9, |
| 65 | + }, |
| 66 | + pcapgo.NgWriterOptions{ |
| 67 | + SectionInfo: pcapgo.NgSectionInfo{ |
| 68 | + Hardware: runtime.GOARCH, |
| 69 | + OS: runtime.GOOS, |
| 70 | + Application: "Mr.Duppl", //spread the word |
| 71 | + }, |
| 72 | + }, |
| 73 | + ) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("open pcapng writer: %w", err) |
| 76 | + } |
| 77 | + defer func() { _ = w.Flush() }() |
| 78 | + |
| 79 | + for { |
| 80 | + packet, err := dev.Packet() |
| 81 | + if err != nil { |
| 82 | + if errors.Is(err, io.EOF) { |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + return fmt.Errorf("read packet: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + ci := gopacket.CaptureInfo{ |
| 90 | + Length: len(packet), |
| 91 | + CaptureLength: len(packet), |
| 92 | + InterfaceIndex: 0, |
| 93 | + } |
| 94 | + err = w.WritePacket(ci, packet) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("write packet: %w", err) |
| 97 | + } |
| 98 | + |
| 99 | + usbp.Print(packet, os.Stdout) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func printCapture(dev *dupplcap.Device) error { |
| 104 | + for { |
| 105 | + packet, err := dev.Packet() |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("read packet: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + usbp.Print(packet, os.Stdout) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func init() { |
| 115 | + flags := dumpCmd.PersistentFlags() |
| 116 | + flags.StringVarP(&dumpArgs.Interface, "iface", "i", "", "interface to use") |
| 117 | + flags.StringVarP(&dumpArgs.Output, "out", "o", "", "write PcapNG file") |
| 118 | + flags.BoolVar(&dumpArgs.NoFolding, "no-packet-folding", false, "disable packet folding") |
| 119 | +} |
0 commit comments