43 lines
702 B
Go
43 lines
702 B
Go
package tun
|
|
|
|
import (
|
|
"github.com/pkg/taptun"
|
|
"io"
|
|
"mbpl3p/proxy"
|
|
)
|
|
|
|
type SourceSink struct {
|
|
tun *taptun.Tun
|
|
bufferSize int
|
|
}
|
|
|
|
func NewTun(namingScheme string, bufferSize int) (ss SourceSink, err error) {
|
|
ss.tun, err = taptun.NewTun(namingScheme)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
ss.bufferSize = bufferSize
|
|
return
|
|
}
|
|
|
|
func (t *SourceSink) Source() (proxy.Packet, error) {
|
|
buf := make([]byte, t.bufferSize)
|
|
|
|
read, err := t.tun.Read(buf)
|
|
if err != nil {
|
|
return proxy.Packet{}, err
|
|
}
|
|
|
|
if read == 0 {
|
|
return proxy.Packet{}, io.EOF
|
|
}
|
|
|
|
return proxy.NewPacket(buf[:read]), nil
|
|
}
|
|
|
|
func (t *SourceSink) Sink(packet proxy.Packet) error {
|
|
_, err := t.tun.Write(packet.Raw())
|
|
return err
|
|
}
|