1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-22 19:40:35 +08:00
This commit is contained in:
fumiama
2021-10-24 20:29:44 +08:00
parent 8b6e74f756
commit fa1608165b
9 changed files with 158 additions and 100 deletions

View File

@@ -20,10 +20,8 @@ type Tunnel struct {
func Create(peer string, srcport uint16, destport uint16) (s Tunnel, err error) {
logrus.Infoln("[tunnel] create from", srcport, "to", destport)
var l link.Link
l, err = link.Connect(peer)
s.l, err = link.Connect(peer)
if err == nil {
s.l = &l
s.in = make(chan []byte, 4)
s.out = make(chan []byte, 4)
s.src = srcport
@@ -68,7 +66,9 @@ func (s *Tunnel) Close() error {
func (s *Tunnel) handleWrite() {
for b := range s.in {
logrus.Debugln("[tunnel] write recv", b)
if b == nil {
logrus.Errorln("[tunnel] write recv nil")
break
}
logrus.Debugln("[tunnel] writing", len(b), "bytes...")
@@ -86,8 +86,10 @@ func (s *Tunnel) handleRead() {
for {
p := s.l.Read()
if p == nil {
logrus.Errorln("[tunnel] read recv nil")
break
}
logrus.Debugln("[tunnel] read recv", p.Data)
s.out <- p.Data
}
}

View File

@@ -4,20 +4,24 @@ import (
"testing"
"github.com/fumiama/WireGold/gold/link"
"github.com/sirupsen/logrus"
)
func TestTunnel(t *testing.T) {
link.SetMyself([32]byte{}, "127.0.0.1:1234")
link.AddPeer("192.168.1.1", [32]byte{}, "127.0.0.1:1235", 0)
link.Listen("127.0.0.1:1234")
link.Listen("127.0.0.1:1235")
tunn, err := Create("192.168.1.1", 1, 1)
logrus.SetLevel(logrus.DebugLevel)
link.SetMyself([32]byte{}, "192.168.1.2", "127.0.0.1:1236")
link.AddPeer("192.168.1.2", [32]byte{}, "127.0.0.1:1236", 0)
tunn, err := Create("192.168.1.2", 1, 1)
if err != nil {
t.Error(err)
} else {
tunn.Write(([]byte)("1234"))
sendb := ([]byte)("1234")
tunn.Write(sendb)
p := make([]byte, 4)
tunn.Read(p)
t.Log(p)
if string(sendb) != string(p) {
t.Log("error: recv", p)
t.Fail()
}
}
}