mirror of
https://github.com/buger/goreplay.git
synced 2024-04-21 12:32:02 +00:00
54 lines
877 B
Go
54 lines
877 B
Go
package main
|
|
|
|
import (
|
|
raw "github.com/buger/gor/raw_socket_listener"
|
|
"log"
|
|
"net"
|
|
"strings"
|
|
)
|
|
|
|
type RAWInput struct {
|
|
data chan []byte
|
|
address string
|
|
}
|
|
|
|
func NewRAWInput(address string) (i *RAWInput) {
|
|
i = new(RAWInput)
|
|
i.data = make(chan []byte)
|
|
i.address = address
|
|
|
|
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)
|
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
log.Fatal("input-raw: error while parsing address", err)
|
|
}
|
|
|
|
listener := raw.NewListener(host, port)
|
|
|
|
for {
|
|
// Receiving TCPMessage object
|
|
m := listener.Receive()
|
|
|
|
i.data <- m.Bytes()
|
|
}
|
|
}
|
|
|
|
func (i *RAWInput) String() string {
|
|
return "RAW Socket input: " + i.address
|
|
}
|