5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-23 13:25:39 +00:00
yggdrasil-go/misc/tests/tuntest.go

46 lines
893 B
Go
Raw Normal View History

2017-12-29 04:16:20 +00:00
package main
import (
"log"
"net"
"sync"
"github.com/FlexibleBroadband/tun-go"
)
// first start server tun server.
func main() {
wg := sync.WaitGroup{}
// local tun interface read and write channel.
rCh := make(chan []byte, 1024)
// read from local tun interface channel, and write into remote udp channel.
wg.Add(1)
go func() {
wg.Done()
for {
data := <-rCh
// if data[0]&0xf0 == 0x40
// write into udp conn.
log.Println("tun->conn:", len(data))
log.Println("read!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
log.Println("src:", net.IP(data[8:24]), "dst:", net.IP(data[24:40]))
}
}()
2018-01-04 22:37:51 +00:00
address := net.ParseIP("fc00::1")
2017-12-29 04:16:20 +00:00
tuntap, err := tun.OpenTun(address)
2018-01-04 22:37:51 +00:00
if err != nil {
panic(err)
}
2017-12-29 04:16:20 +00:00
defer tuntap.Close()
// read data from tun into rCh channel.
wg.Add(1)
go func() {
2018-01-04 22:37:51 +00:00
if err := tuntap.Read(rCh); err != nil {
panic(err)
}
2017-12-29 04:16:20 +00:00
wg.Done()
}()
wg.Wait()
}