|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "runtime" |
| 9 | + |
| 10 | + "github.com/gopacket/gopacket" |
| 11 | + "github.com/gopacket/gopacket/layers" |
| 12 | + "github.com/gopacket/gopacket/pcapgo" |
| 13 | + "github.com/kor44/extcap" |
| 14 | + |
| 15 | + "github.com/buglloc/Mr.Duppl/software/pkg/dupplcap" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + usePacketFoldingOpt = "packet-folding" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + usePacketFolding = extcap.NewConfigBoolOpt(usePacketFoldingOpt, "Use packet folding"). |
| 24 | + Default(true) |
| 25 | +) |
| 26 | + |
| 27 | +func main() { |
| 28 | + app := extcap.App{ |
| 29 | + Usage: "dupplcap", |
| 30 | + Version: extcap.VersionInfo{ |
| 31 | + Info: "1.0.0", |
| 32 | + Help: "https://github.com/buglloc/Mr.Duppl", |
| 33 | + }, |
| 34 | + HelpPage: "DupplCAP - extcap application to integrate Mr.Duppl with Wireshark or something", |
| 35 | + GetInterfaces: getAllInterfaces, |
| 36 | + GetDLT: getDLT, |
| 37 | + GetAllConfigOptions: getAllConfigOptions, |
| 38 | + GetConfigOptions: getConfigOptions, |
| 39 | + StartCapture: startCapture, |
| 40 | + } |
| 41 | + |
| 42 | + app.Run(os.Args) |
| 43 | +} |
| 44 | + |
| 45 | +func getAllInterfaces() ([]extcap.CaptureInterface, error) { |
| 46 | + ifaces, err := dupplcap.Ifaces() |
| 47 | + if err != nil { |
| 48 | + return nil, fmt.Errorf("unable to get information about interfaces: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + extIfaces := make([]extcap.CaptureInterface, len(ifaces)) |
| 52 | + for i, iface := range ifaces { |
| 53 | + // we use Name as Value to deal with Mr.Duppl replugs |
| 54 | + extIfaces[i] = extcap.CaptureInterface{ |
| 55 | + Value: iface.Name, |
| 56 | + Display: iface.Path, |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return extIfaces, nil |
| 61 | +} |
| 62 | + |
| 63 | +func getDLT(_ string) (extcap.DLT, error) { |
| 64 | + return extcap.DLT{ |
| 65 | + Number: dupplcap.LinkTypeUSBFullSpeed.Int(), |
| 66 | + Name: dupplcap.LinkTypeUSBFullSpeed.String(), |
| 67 | + }, nil |
| 68 | +} |
| 69 | + |
| 70 | +func getConfigOptions(_ string) ([]extcap.ConfigOption, error) { |
| 71 | + opts := []extcap.ConfigOption{ |
| 72 | + usePacketFolding, |
| 73 | + } |
| 74 | + |
| 75 | + return opts, nil |
| 76 | +} |
| 77 | + |
| 78 | +func getAllConfigOptions() []extcap.ConfigOption { |
| 79 | + opts := []extcap.ConfigOption{ |
| 80 | + usePacketFolding, |
| 81 | + } |
| 82 | + return opts |
| 83 | +} |
| 84 | + |
| 85 | +func startCapture(iface string, pipe io.WriteCloser, _ string, opts map[string]any) error { |
| 86 | + defer func() { _ = pipe.Close() }() |
| 87 | + |
| 88 | + dev, err := dupplcap.NewDeviceByName(iface) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("open device: %w", err) |
| 91 | + } |
| 92 | + defer func() { _ = dev.Close() }() |
| 93 | + |
| 94 | + packetFoldingEnabled := true |
| 95 | + if val, ok := opts[usePacketFoldingOpt]; ok { |
| 96 | + if en, ok := val.(bool); ok { |
| 97 | + packetFoldingEnabled = en |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if err := dev.StartCapture(packetFoldingEnabled); err != nil { |
| 102 | + return fmt.Errorf("start capture: %w", err) |
| 103 | + } |
| 104 | + defer func() { _ = dev.StopCapture() }() |
| 105 | + |
| 106 | + w, err := pcapgo.NewNgWriterInterface( |
| 107 | + pipe, |
| 108 | + pcapgo.NgInterface{ |
| 109 | + Name: filepath.Base(iface), |
| 110 | + OS: runtime.GOOS, |
| 111 | + LinkType: layers.LinkType(dupplcap.LinkTypeUSBFullSpeed.Int()), |
| 112 | + SnapLength: 0, //unlimited |
| 113 | + // TimestampResolution: 9, |
| 114 | + }, |
| 115 | + pcapgo.NgWriterOptions{ |
| 116 | + SectionInfo: pcapgo.NgSectionInfo{ |
| 117 | + Hardware: runtime.GOARCH, |
| 118 | + OS: runtime.GOOS, |
| 119 | + Application: "Mr.Duppl", //spread the word |
| 120 | + }, |
| 121 | + }, |
| 122 | + ) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("open pcapng writer: %w", err) |
| 125 | + } |
| 126 | + |
| 127 | + for { |
| 128 | + packet, err := dev.Packet() |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("read packet: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + ci := gopacket.CaptureInfo{ |
| 134 | + Length: len(packet), |
| 135 | + CaptureLength: len(packet), |
| 136 | + InterfaceIndex: 0, |
| 137 | + } |
| 138 | + err = w.WritePacket(ci, packet) |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf("write packet: %w", err) |
| 141 | + } |
| 142 | + |
| 143 | + err = w.Flush() |
| 144 | + if err != nil { |
| 145 | + return fmt.Errorf("flush packet: %w", err) |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments