mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-04 23:40:26 +08:00
feat: impl. new protol design & new head
This commit is contained in:
@@ -12,8 +12,7 @@ import (
|
||||
_ "github.com/fumiama/WireGold/gold/p2p/tcp" // support tcp connection
|
||||
_ "github.com/fumiama/WireGold/gold/p2p/udp" // support udp connection
|
||||
_ "github.com/fumiama/WireGold/gold/p2p/udplite" // support udplite connection
|
||||
"github.com/fumiama/orbyte"
|
||||
"github.com/fumiama/orbyte/pbuf"
|
||||
_ "github.com/fumiama/WireGold/gold/proto" // support basic protos
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
@@ -23,7 +22,7 @@ import (
|
||||
type Tunnel struct {
|
||||
l *link.Link
|
||||
in chan []byte
|
||||
out chan *orbyte.Item[head.Packet]
|
||||
out chan link.LinkData
|
||||
outcache []byte
|
||||
peerip net.IP
|
||||
src uint16
|
||||
@@ -35,7 +34,7 @@ func Create(me *link.Me, peer string) (s Tunnel, err error) {
|
||||
s.l, err = me.Connect(peer)
|
||||
if err == nil {
|
||||
s.in = make(chan []byte, 4)
|
||||
s.out = make(chan *orbyte.Item[head.Packet], 4)
|
||||
s.out = make(chan link.LinkData, 4)
|
||||
s.peerip = net.ParseIP(peer)
|
||||
} else {
|
||||
logrus.Errorln("[tunnel] create err:", err)
|
||||
@@ -72,14 +71,14 @@ func (s *Tunnel) Read(p []byte) (int, error) {
|
||||
d = s.outcache
|
||||
} else {
|
||||
pkt := <-s.out
|
||||
if pkt == nil {
|
||||
if !pkt.D.HasInit() {
|
||||
return 0, io.EOF
|
||||
}
|
||||
if pkt.Pointer().BodyLen() < 4 {
|
||||
logrus.Warnln("[tunnel] unexpected packet data len", pkt.Pointer().BodyLen(), "content", hex.EncodeToString(pkt.Pointer().UnsafeBody()))
|
||||
if pkt.H.Size() < 4 {
|
||||
logrus.Warnln("[tunnel] unexpected packet data len", pkt.H.Size(), "content", hex.EncodeToString(pkt.D.Trans()))
|
||||
return 0, io.EOF
|
||||
}
|
||||
d = pkt.Pointer().UnsafeBody()[4:]
|
||||
d = pkt.D.Trans()[4:]
|
||||
}
|
||||
if d != nil {
|
||||
if len(p) >= len(d) {
|
||||
@@ -126,37 +125,25 @@ func (s *Tunnel) handleWrite() {
|
||||
binary.LittleEndian.PutUint32(buf[:4], seq)
|
||||
seq++
|
||||
copy(buf[4:], b[:s.mtu-4])
|
||||
_, err := s.l.WritePacket(
|
||||
head.NewPacketPartial(head.ProtoData, s.src, s.peerip, s.dest, pbuf.ParseBytes(buf...)), false,
|
||||
)
|
||||
if err != nil {
|
||||
logrus.Errorln("[tunnel] seq", seq-1, "write err:", err)
|
||||
return
|
||||
}
|
||||
s.l.WritePacket(head.ProtoData, buf)
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[tunnel] seq", seq-1, "write succeeded")
|
||||
logrus.Debugln("[tunnel] seq", seq-1, "written")
|
||||
}
|
||||
b = b[s.mtu-4:]
|
||||
}
|
||||
binary.LittleEndian.PutUint32(buf[:4], seq)
|
||||
seq++
|
||||
copy(buf[4:], b)
|
||||
_, err := s.l.WritePacket(
|
||||
head.NewPacketPartial(head.ProtoData, s.src, s.peerip, s.dest, pbuf.ParseBytes(buf[:len(b)+4]...)), false,
|
||||
)
|
||||
if err != nil {
|
||||
logrus.Errorln("[tunnel] seq", seq-1, "write err:", err)
|
||||
break
|
||||
}
|
||||
s.l.WritePacket(head.ProtoData, buf[:len(b)+4])
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[tunnel] seq", seq-1, "write succeeded")
|
||||
logrus.Debugln("[tunnel] seq", seq-1, "written")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Tunnel) handleRead() {
|
||||
seq := uint32(0)
|
||||
seqmap := make(map[uint32]*orbyte.Item[head.Packet])
|
||||
seqmap := make(map[uint32]link.LinkData)
|
||||
for {
|
||||
if p, ok := seqmap[seq]; ok {
|
||||
if config.ShowDebugLog {
|
||||
@@ -168,21 +155,24 @@ func (s *Tunnel) handleRead() {
|
||||
continue
|
||||
}
|
||||
p := s.l.Read()
|
||||
if p == nil {
|
||||
if !p.D.HasInit() {
|
||||
logrus.Errorln("[tunnel] read recv nil")
|
||||
break
|
||||
}
|
||||
end := 64
|
||||
endl := "..."
|
||||
pp := p.Pointer()
|
||||
if pp.BodyLen() < 64 {
|
||||
end = pp.BodyLen()
|
||||
pp := &p.H
|
||||
if pp.Size() < 64 {
|
||||
end = pp.Size()
|
||||
endl = "."
|
||||
}
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[tunnel] read recv", hex.EncodeToString(pp.UnsafeBody()[:end]), endl)
|
||||
}
|
||||
recvseq := binary.LittleEndian.Uint32(pp.UnsafeBody()[:4])
|
||||
var recvseq uint32
|
||||
p.D.V(func(b []byte) {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[tunnel] read recv", hex.EncodeToString(b[:end]), endl)
|
||||
}
|
||||
recvseq = binary.LittleEndian.Uint32(b[:4])
|
||||
})
|
||||
if recvseq == seq {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[tunnel] dispatch seq", seq)
|
||||
|
||||
@@ -12,11 +12,10 @@ import (
|
||||
"time"
|
||||
|
||||
curve "github.com/fumiama/go-x25519"
|
||||
"github.com/fumiama/orbyte"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/fumiama/WireGold/gold/link"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
"github.com/fumiama/WireGold/internal/bin"
|
||||
)
|
||||
|
||||
func TestTunnelUDP(t *testing.T) {
|
||||
@@ -432,7 +431,7 @@ type logFormat struct {
|
||||
func (f logFormat) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
// this writer will not be put back
|
||||
|
||||
buf := (*orbyte.Item[bytes.Buffer])(helper.SelectWriter()).Trans().Pointer()
|
||||
buf := bin.SelectWriter()
|
||||
|
||||
buf.WriteByte('[')
|
||||
if f.enableColor {
|
||||
@@ -446,7 +445,7 @@ func (f logFormat) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
buf.WriteString(entry.Message)
|
||||
buf.WriteString("\n")
|
||||
|
||||
return buf.Bytes(), nil
|
||||
return buf.ToBytes().Trans(), nil
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -13,10 +13,11 @@ import (
|
||||
_ "github.com/fumiama/WireGold/gold/p2p/tcp" // support tcp connection
|
||||
_ "github.com/fumiama/WireGold/gold/p2p/udp" // support udp connection
|
||||
_ "github.com/fumiama/WireGold/gold/p2p/udplite" // support udplite connection
|
||||
_ "github.com/fumiama/WireGold/gold/proto" // support basic protos
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
"github.com/fumiama/WireGold/gold/link"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
"github.com/fumiama/WireGold/internal/bin"
|
||||
)
|
||||
|
||||
const suffix32 = "㴄"
|
||||
@@ -32,7 +33,7 @@ func NewWireGold(c *config.Config) (wg WG, err error) {
|
||||
wg.c = c
|
||||
|
||||
var k []byte
|
||||
k, err = base14.UTF82UTF16BE(helper.StringToBytes(c.PrivateKey + suffix32))
|
||||
k, err = base14.UTF82UTF16BE(bin.StringToBytes(c.PrivateKey + suffix32))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -47,7 +48,7 @@ func NewWireGold(c *config.Config) (wg WG, err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
wg.PublicKey = helper.BytesToString(pubk[:57])
|
||||
wg.PublicKey = bin.BytesToString(pubk[:57])
|
||||
|
||||
return
|
||||
}
|
||||
@@ -118,7 +119,7 @@ func (wg *WG) init(srcport, dstport uint16) {
|
||||
|
||||
for _, peer := range wg.c.Peers {
|
||||
var peerkey [32]byte
|
||||
k, err := base14.UTF82UTF16BE(helper.StringToBytes(peer.PublicKey + suffix32))
|
||||
k, err := base14.UTF82UTF16BE(bin.StringToBytes(peer.PublicKey + suffix32))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -128,7 +129,7 @@ func (wg *WG) init(srcport, dstport uint16) {
|
||||
}
|
||||
var pshk *[32]byte
|
||||
if peer.PresharedKey != "" {
|
||||
k, err := base14.UTF82UTF16BE(helper.StringToBytes(peer.PresharedKey + suffix32))
|
||||
k, err := base14.UTF82UTF16BE(bin.StringToBytes(peer.PresharedKey + suffix32))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user