wgtun changes #16

Merged
JakeHillion merged 1 commits from wgtun into develop 2021-03-30 14:03:31 +01:00

View File

@ -5,19 +5,12 @@ import (
"io"
"log"
"mpbl3p/proxy"
"net"
"os"
"strings"
"sync"
"time"
)
type SourceSink struct {
tun wgtun.Device
mtu int
up bool
upMu sync.Mutex
}
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.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
}
func (t *SourceSink) Close() error {
t.upMu.Lock()
t.up = false
return t.tun.Close()
}
func (t *SourceSink) Source() (proxy.Packet, error) {
if !t.up {
t.upMu.Lock()
t.upMu.Unlock()
}
buf := make([]byte, t.mtu)
read, err := t.tun.Read(buf, 4)
@ -85,17 +46,12 @@ func (t *SourceSink) Source() (proxy.Packet, error) {
return nil, io.EOF
}
return proxy.SimplePacket(buf[:read]), nil
return proxy.SimplePacket(buf[4:read]), nil
}
var good, bad float64
func (t *SourceSink) Sink(packet proxy.Packet) error {
if !t.up {
t.upMu.Lock()
t.upMu.Unlock()
}
_, err := t.tun.Write(packet.Contents(), 4)
if err != nil {
switch err.(type) {