-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add packet collector implementation with an error
- Loading branch information
1 parent
260f844
commit 3451015
Showing
4 changed files
with
127 additions
and
0 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
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 |
---|---|---|
@@ -1,5 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"golang.org/x/sys/windows" | ||
"log" | ||
|
||
"waffle/internal/worker" | ||
"waffle/pkg/permission" | ||
) | ||
|
||
func main() { | ||
if !windows.GetCurrentProcessToken().IsElevated() { | ||
if err := permission.RunMeElevated(); err != nil { | ||
panic(err.Error()) | ||
} | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
log.Println("starting collector") | ||
|
||
collector := worker.NewCollector(worker.CollectorConfig{ | ||
Protocol: "tcp", | ||
Port: "8080", | ||
}) | ||
|
||
if err := collector.Run(ctx); err != nil { | ||
panic(err.Error()) | ||
} | ||
} |
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,70 @@ | ||
package worker | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/google/gopacket" | ||
"github.com/google/gopacket/pcap" | ||
) | ||
|
||
type PacketSerializer interface { | ||
SerializePackets(ctx context.Context, packetsChan chan<- gopacket.Packet) error | ||
} | ||
|
||
type CollectorConfig struct { | ||
Protocol string | ||
Port string | ||
} | ||
|
||
type Collector struct { | ||
cfg *CollectorConfig | ||
serializer PacketSerializer | ||
} | ||
|
||
func NewCollector(cfg CollectorConfig) *Collector { | ||
return &Collector{ | ||
cfg: &cfg, | ||
} | ||
} | ||
|
||
func (c *Collector) Run(ctx context.Context) error { | ||
handle, err := pcap.OpenLive("eth0", 1600, true, pcap.BlockForever) | ||
if err != nil { | ||
return fmt.Errorf("pcap open live: %w", err) | ||
} | ||
|
||
if err := handle.SetBPFFilter(fmt.Sprintf("%s port %s", c.cfg.Protocol, c.cfg.Port)); err != nil { | ||
return fmt.Errorf("set BPFF filter: %w", err) | ||
} | ||
|
||
packetSource := gopacket.NewPacketSource(handle, handle.LinkType()) | ||
|
||
packetsChan := make(chan gopacket.Packet) | ||
defer func() { | ||
close(packetsChan) | ||
|
||
log.Println("collector closed") | ||
}() | ||
|
||
go func() { | ||
if err := c.serializer.SerializePackets(ctx, packetsChan); err != nil { | ||
log.Println("error in serialize packets") | ||
} | ||
}() | ||
|
||
for { | ||
select { | ||
case packet, ok := <-packetSource.Packets(): | ||
if !ok { | ||
log.Println("error reading packet") | ||
} | ||
|
||
packetsChan <- packet | ||
|
||
case <-ctx.Done(): | ||
return nil | ||
} | ||
} | ||
} |
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,30 @@ | ||
package permission | ||
|
||
import ( | ||
"fmt" | ||
"golang.org/x/sys/windows" | ||
"os" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
func RunMeElevated() error { | ||
verb := "runas" | ||
exe, _ := os.Executable() | ||
cwd, _ := os.Getwd() | ||
args := strings.Join(os.Args[1:], " ") | ||
|
||
verbPtr, _ := syscall.UTF16PtrFromString(verb) | ||
exePtr, _ := syscall.UTF16PtrFromString(exe) | ||
cwdPtr, _ := syscall.UTF16PtrFromString(cwd) | ||
argPtr, _ := syscall.UTF16PtrFromString(args) | ||
|
||
var showCmd int32 = 1 //SW_NORMAL | ||
|
||
err := windows.ShellExecute(0, verbPtr, exePtr, argPtr, cwdPtr, showCmd) | ||
if err != nil { | ||
return fmt.Errorf("windows shell execute: %w", err) | ||
} | ||
|
||
return nil | ||
} |