mirror of
https://github.com/buger/goreplay.git
synced 2024-04-21 12:32:02 +00:00
61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
raw "github.com/buger/gor/raw_socket_listener"
|
|
"log"
|
|
"net"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// RAWInput used for intercepting traffic for given address
|
|
type RAWInput struct {
|
|
data chan []byte
|
|
address string
|
|
expire time.Duration
|
|
}
|
|
|
|
// NewRAWInput constructor for RAWInput. Accepts address with port as argument.
|
|
func NewRAWInput(address string, expire time.Duration) (i *RAWInput) {
|
|
i = new(RAWInput)
|
|
i.data = make(chan []byte)
|
|
i.address = address
|
|
i.expire = expire
|
|
|
|
go i.listen(address)
|
|
|
|
return
|
|
}
|
|
|
|
func (i *RAWInput) Read(data []byte) (int, error) {
|
|
buf := <-i.data
|
|
copy(data, buf)
|
|
|
|
return len(buf), nil
|
|
}
|
|
|
|
func (i *RAWInput) listen(address string) {
|
|
address = strings.Replace(address, "[::]", "127.0.0.1", -1)
|
|
|
|
Debug("Listening for traffic on: " + address)
|
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
log.Fatal("input-raw: error while parsing address", err)
|
|
}
|
|
|
|
listener := raw.NewListener(host, port, i.expire)
|
|
|
|
for {
|
|
// Receiving TCPMessage object
|
|
m := listener.Receive()
|
|
|
|
i.data <- m.Bytes()
|
|
}
|
|
}
|
|
|
|
func (i *RAWInput) String() string {
|
|
return "RAW Socket input: " + i.address
|
|
}
|