removed up check and header
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Jake Hillion 2021-03-28 19:49:25 +01:00
parent 2b1922ba29
commit 710f89446b

View File

@ -5,19 +5,12 @@ import (
"io" "io"
"log" "log"
"mpbl3p/proxy" "mpbl3p/proxy"
"net"
"os" "os"
"strings"
"sync"
"time"
) )
type SourceSink struct { type SourceSink struct {
tun wgtun.Device tun wgtun.Device
mtu int mtu int
up bool
upMu sync.Mutex
} }
func NewTun(name string, mtu int) (t wgtun.Device, err error) { func NewTun(name string, mtu int) (t wgtun.Device, err error) {
@ -34,46 +27,14 @@ func NewFromFile(fd uintptr, mtu int) (ss *SourceSink, err error) {
} }
ss.mtu = mtu ss.mtu = mtu
ss.upMu.Lock()
go func() {
defer ss.upMu.Unlock()
for {
tunName, err := ss.tun.Name()
if err != nil {
panic(err)
}
iface, err := net.InterfaceByName(tunName)
if err != nil {
panic(err)
}
if strings.Contains(iface.Flags.String(), "up") {
log.Println("tun is up")
ss.up = true
return
}
time.Sleep(100 * time.Millisecond)
}
}()
return return
} }
func (t *SourceSink) Close() error { func (t *SourceSink) Close() error {
t.upMu.Lock()
t.up = false
return t.tun.Close() return t.tun.Close()
} }
func (t *SourceSink) Source() (proxy.Packet, error) { func (t *SourceSink) Source() (proxy.Packet, error) {
if !t.up {
t.upMu.Lock()
t.upMu.Unlock()
}
buf := make([]byte, t.mtu) buf := make([]byte, t.mtu)
read, err := t.tun.Read(buf, 4) read, err := t.tun.Read(buf, 4)
@ -85,17 +46,12 @@ func (t *SourceSink) Source() (proxy.Packet, error) {
return nil, io.EOF return nil, io.EOF
} }
return proxy.SimplePacket(buf[:read]), nil return proxy.SimplePacket(buf[4:read]), nil
} }
var good, bad float64 var good, bad float64
func (t *SourceSink) Sink(packet proxy.Packet) error { func (t *SourceSink) Sink(packet proxy.Packet) error {
if !t.up {
t.upMu.Lock()
t.upMu.Unlock()
}
_, err := t.tun.Write(packet.Contents(), 4) _, err := t.tun.Write(packet.Contents(), 4)
if err != nil { if err != nil {
switch err.(type) { switch err.(type) {