-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
320 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mr.duppl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package commands | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"runtime" | ||
|
||
"github.com/gopacket/gopacket" | ||
"github.com/gopacket/gopacket/layers" | ||
"github.com/gopacket/gopacket/pcapgo" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/buglloc/mr.duppl/software/pkg/dupplcap" | ||
"github.com/buglloc/mr.duppl/software/pkg/usbp" | ||
) | ||
|
||
var dumpArgs struct { | ||
Interface string | ||
NoFolding bool | ||
Output string | ||
} | ||
|
||
var dumpCmd = &cobra.Command{ | ||
Use: "dump", | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
Short: "Dump Mr.Duppl capture", | ||
RunE: func(_ *cobra.Command, _ []string) error { | ||
dev, err := dupplcap.NewDevice(dumpArgs.Interface) | ||
if err != nil { | ||
return fmt.Errorf("opening device: %w", err) | ||
} | ||
defer func() { _ = dev.Close() }() | ||
|
||
if err := dev.StartCapture(!dumpArgs.NoFolding); err != nil { | ||
return fmt.Errorf("start capture: %w", err) | ||
} | ||
defer func() { _ = dev.StopCapture() }() | ||
|
||
if len(dumpArgs.Output) != 0 { | ||
out, err := os.Create(dumpArgs.Output) | ||
if err != nil { | ||
return fmt.Errorf("creating output file: %w", err) | ||
} | ||
defer func() { _ = out.Close() }() | ||
|
||
return dumpCapture(dev, out) | ||
} | ||
|
||
return printCapture(dev) | ||
}, | ||
} | ||
|
||
func dumpCapture(dev *dupplcap.Device, out io.Writer) error { | ||
w, err := pcapgo.NewNgWriterInterface( | ||
out, | ||
pcapgo.NgInterface{ | ||
Name: dev.Iface(), | ||
OS: runtime.GOOS, | ||
LinkType: layers.LinkType(dupplcap.LinkTypeUSBFullSpeed.Int()), | ||
SnapLength: 0, //unlimited | ||
// TimestampResolution: 9, | ||
}, | ||
pcapgo.NgWriterOptions{ | ||
SectionInfo: pcapgo.NgSectionInfo{ | ||
Hardware: runtime.GOARCH, | ||
OS: runtime.GOOS, | ||
Application: "Mr.Duppl", //spread the word | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("open pcapng writer: %w", err) | ||
} | ||
defer func() { _ = w.Flush() }() | ||
|
||
for { | ||
packet, err := dev.Packet() | ||
if err != nil { | ||
if errors.Is(err, io.EOF) { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("read packet: %w", err) | ||
} | ||
|
||
ci := gopacket.CaptureInfo{ | ||
Length: len(packet), | ||
CaptureLength: len(packet), | ||
InterfaceIndex: 0, | ||
} | ||
err = w.WritePacket(ci, packet) | ||
if err != nil { | ||
return fmt.Errorf("write packet: %w", err) | ||
} | ||
|
||
usbp.Print(packet, os.Stdout) | ||
} | ||
} | ||
|
||
func printCapture(dev *dupplcap.Device) error { | ||
for { | ||
packet, err := dev.Packet() | ||
if err != nil { | ||
return fmt.Errorf("read packet: %w", err) | ||
} | ||
|
||
usbp.Print(packet, os.Stdout) | ||
} | ||
} | ||
|
||
func init() { | ||
flags := dumpCmd.PersistentFlags() | ||
flags.StringVarP(&dumpArgs.Interface, "iface", "i", "", "interface to use") | ||
flags.StringVarP(&dumpArgs.Output, "out", "o", "", "write PcapNG file") | ||
flags.BoolVar(&dumpArgs.NoFolding, "no-packet-folding", false, "disable packet folding") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/buglloc/mr.duppl/software/pkg/dupplcap" | ||
) | ||
|
||
var lsArgs struct { | ||
Name bool | ||
Path bool | ||
} | ||
|
||
var lsCmd = &cobra.Command{ | ||
Use: "ls", | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
Short: "List devices", | ||
RunE: func(_ *cobra.Command, _ []string) error { | ||
if !lsArgs.Name && !lsArgs.Path { | ||
lsArgs.Name = true | ||
lsArgs.Path = true | ||
} | ||
|
||
ifaces, err := dupplcap.Ifaces() | ||
if err != nil { | ||
return fmt.Errorf("unable to get information about interfaces: %w", err) | ||
} | ||
|
||
for _, iface := range ifaces { | ||
switch { | ||
case lsArgs.Name && lsArgs.Path: | ||
fmt.Println(iface.Name, iface.Path) | ||
case lsArgs.Name: | ||
fmt.Println(iface.Name) | ||
case lsArgs.Path: | ||
fmt.Println(iface.Path) | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
flags := lsCmd.PersistentFlags() | ||
flags.BoolVarP(&lsArgs.Name, "name", "n", false, "display device name") | ||
flags.BoolVarP(&lsArgs.Path, "path", "p", false, "display device path") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "mr.duppl", | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
} | ||
|
||
func Execute() error { | ||
return rootCmd.Execute() | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand( | ||
lsCmd, | ||
dumpCmd, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/buglloc/mr.duppl/software/cmd/mr.duppl/commands" | ||
) | ||
|
||
func main() { | ||
if err := commands.Execute(); err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.