mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-11 20:20:27 +08:00
chore: remove debug log at build
This commit is contained in:
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@@ -23,7 +23,7 @@ jobs:
|
|||||||
run: go build -v ./...
|
run: go build -v ./...
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: sudo go test $(go list ./...) # ip test needs sudo
|
run: sudo go test -X github.com/fumiama/WireGold/config.ShowDebugLog=true $(go list ./...) # ip test needs sudo
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
name: Lint
|
name: Lint
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,11 +42,11 @@ type Peer struct {
|
|||||||
func Parse(path string) (c Config) {
|
func Parse(path string) (c Config) {
|
||||||
file, err := os.ReadFile(path)
|
file, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("open config file failed:", err)
|
logrus.Fatal("open config file failed:", err)
|
||||||
}
|
}
|
||||||
err = yaml.NewDecoder(bytes.NewReader(file)).Decode(&c)
|
err = yaml.NewDecoder(bytes.NewReader(file)).Decode(&c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("invalid config file:", err)
|
logrus.Fatal("invalid config file:", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
3
config/global.go
Normal file
3
config/global.go
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
const ShowDebugLog = false
|
||||||
@@ -7,9 +7,11 @@ import (
|
|||||||
"hash/crc64"
|
"hash/crc64"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/fumiama/WireGold/helper"
|
|
||||||
blake2b "github.com/fumiama/blake2b-simd"
|
blake2b "github.com/fumiama/blake2b-simd"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/fumiama/WireGold/config"
|
||||||
|
"github.com/fumiama/WireGold/helper"
|
||||||
)
|
)
|
||||||
|
|
||||||
const PacketHeadLen = 60
|
const PacketHeadLen = 60
|
||||||
@@ -89,7 +91,6 @@ type Packet struct {
|
|||||||
|
|
||||||
// NewPacket 生成一个新包
|
// NewPacket 生成一个新包
|
||||||
func NewPacket(proto uint8, srcPort uint16, dst net.IP, dstPort uint16, data []byte) (p *Packet) {
|
func NewPacket(proto uint8, srcPort uint16, dst net.IP, dstPort uint16, data []byte) (p *Packet) {
|
||||||
// logrus.Debugln("[packet] new: [proto:", proto, ", srcport:", srcPort, ", dstport:", dstPort, ", dst:", dst, ", data:", data)
|
|
||||||
p = SelectPacket()
|
p = SelectPacket()
|
||||||
p.Proto = proto
|
p.Proto = proto
|
||||||
p.TTL = 16
|
p.TTL = 16
|
||||||
@@ -147,7 +148,9 @@ func (p *Packet) Unmarshal(data []byte) (complete bool, err error) {
|
|||||||
|
|
||||||
if p.rembytes > 0 {
|
if p.rembytes > 0 {
|
||||||
p.rembytes -= copy(p.data[flags.Offset():], data[PacketHeadLen:])
|
p.rembytes -= copy(p.data[flags.Offset():], data[PacketHeadLen:])
|
||||||
logrus.Debugln("[packet] copied frag", hex.EncodeToString(p.Hash[:]), "rembytes:", p.rembytes)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[packet] copied frag", hex.EncodeToString(p.Hash[:]), "rembytes:", p.rembytes)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
complete = p.rembytes == 0
|
complete = p.rembytes == 0
|
||||||
@@ -199,7 +202,10 @@ func (p *Packet) FillHash() {
|
|||||||
logrus.Error("[packet] err when fill hash:", err)
|
logrus.Error("[packet] err when fill hash:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logrus.Debugln("[packet] sum calulated:", hex.EncodeToString(h.Sum(p.Hash[:0])))
|
hsh := h.Sum(p.Hash[:0])
|
||||||
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[packet] sum calulated:", hex.EncodeToString(hsh))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsVaildHash 验证 packet 合法性
|
// IsVaildHash 验证 packet 合法性
|
||||||
@@ -211,8 +217,11 @@ func (p *Packet) IsVaildHash() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
var sum [32]byte
|
var sum [32]byte
|
||||||
logrus.Debugln("[packet] sum calulated:", hex.EncodeToString(h.Sum(sum[:0])))
|
_ = h.Sum(sum[:0])
|
||||||
logrus.Debugln("[packet] sum in packet:", hex.EncodeToString(p.Hash[:]))
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[packet] sum calulated:", hex.EncodeToString(sum[:]))
|
||||||
|
logrus.Debugln("[packet] sum in packet:", hex.EncodeToString(p.Hash[:]))
|
||||||
|
}
|
||||||
return sum == p.Hash
|
return sum == p.Hash
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/klauspost/compress/zstd"
|
"github.com/klauspost/compress/zstd"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/fumiama/WireGold/config"
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
"github.com/fumiama/WireGold/gold/p2p"
|
"github.com/fumiama/WireGold/gold/p2p"
|
||||||
"github.com/fumiama/WireGold/helper"
|
"github.com/fumiama/WireGold/helper"
|
||||||
@@ -48,7 +49,9 @@ func (m *Me) listen() (conn p2p.Conn, err error) {
|
|||||||
time.Sleep(time.Millisecond * 10)
|
time.Sleep(time.Millisecond * 10)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logrus.Debugln("[listen] lock index", i)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[listen] lock index", i)
|
||||||
|
}
|
||||||
lbf := listenbuff[i*lstnbufgragsz : (i+1)*lstnbufgragsz]
|
lbf := listenbuff[i*lstnbufgragsz : (i+1)*lstnbufgragsz]
|
||||||
n, addr, err := conn.ReadFromPeer(lbf)
|
n, addr, err := conn.ReadFromPeer(lbf)
|
||||||
if m.connections == nil || errors.Is(err, net.ErrClosed) {
|
if m.connections == nil || errors.Is(err, net.ErrClosed) {
|
||||||
@@ -62,7 +65,9 @@ func (m *Me) listen() (conn p2p.Conn, err error) {
|
|||||||
logrus.Errorln("[listen] reconnect udp err:", err)
|
logrus.Errorln("[listen] reconnect udp err:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logrus.Debugln("[listen] unlock index", i)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[listen] unlock index", i)
|
||||||
|
}
|
||||||
hasntfinished[i].Unlock()
|
hasntfinished[i].Unlock()
|
||||||
i--
|
i--
|
||||||
continue
|
continue
|
||||||
@@ -77,7 +82,9 @@ func (m *Me) listen() (conn p2p.Conn, err error) {
|
|||||||
}
|
}
|
||||||
packet := m.wait(lbf[:n:lstnbufgragsz])
|
packet := m.wait(lbf[:n:lstnbufgragsz])
|
||||||
if packet == nil {
|
if packet == nil {
|
||||||
logrus.Debugln("[listen] waiting, unlock index", i)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[listen] waiting, unlock index", i)
|
||||||
|
}
|
||||||
hasntfinished[i].Unlock()
|
hasntfinished[i].Unlock()
|
||||||
i--
|
i--
|
||||||
continue
|
continue
|
||||||
@@ -90,8 +97,10 @@ func (m *Me) listen() (conn p2p.Conn, err error) {
|
|||||||
|
|
||||||
func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish func()) {
|
func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish func()) {
|
||||||
defer finish()
|
defer finish()
|
||||||
defer logrus.Debugln("[listen] dispatched, unlock index", index)
|
if config.ShowDebugLog {
|
||||||
logrus.Debugln("[listen] start dispatching index", index)
|
defer logrus.Debugln("[listen] dispatched, unlock index", index)
|
||||||
|
logrus.Debugln("[listen] start dispatching index", index)
|
||||||
|
}
|
||||||
r := packet.Len() - packet.BodyLen()
|
r := packet.Len() - packet.BodyLen()
|
||||||
if r > 0 {
|
if r > 0 {
|
||||||
logrus.Warnln("[listen] @", index, "packet from endpoint", addr, "len", packet.BodyLen(), "is smaller than it declared len", packet.Len(), ", drop it")
|
logrus.Warnln("[listen] @", index, "packet from endpoint", addr, "len", packet.BodyLen(), "is smaller than it declared len", packet.Len(), ", drop it")
|
||||||
@@ -99,7 +108,9 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
p, ok := m.IsInPeer(packet.Src.String())
|
p, ok := m.IsInPeer(packet.Src.String())
|
||||||
logrus.Debugln("[listen] @", index, "recv from endpoint", addr, "src", packet.Src, "dst", packet.Dst)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[listen] @", index, "recv from endpoint", addr, "src", packet.Src, "dst", packet.Dst)
|
||||||
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
logrus.Warnln("[listen] @", index, "packet from", packet.Src, "to", packet.Dst, "is refused")
|
logrus.Warnln("[listen] @", index, "packet from", packet.Src, "to", packet.Dst, "is refused")
|
||||||
packet.Put()
|
packet.Put()
|
||||||
@@ -125,7 +136,9 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
|
|||||||
var err error
|
var err error
|
||||||
data, err := p.Decode(packet.CipherIndex(), addt, packet.Body())
|
data, err := p.Decode(packet.CipherIndex(), addt, packet.Body())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Debugln("[listen] @", index, "drop invalid packet key idx:", packet.CipherIndex(), "addt:", addt, "err:", err)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[listen] @", index, "drop invalid packet key idx:", packet.CipherIndex(), "addt:", addt, "err:", err)
|
||||||
|
}
|
||||||
packet.Put()
|
packet.Put()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -137,14 +150,18 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
|
|||||||
_, err = io.Copy(w, dec)
|
_, err = io.Copy(w, dec)
|
||||||
dec.Close()
|
dec.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Debugln("[listen] @", index, "drop invalid zstd packet:", err)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[listen] @", index, "drop invalid zstd packet:", err)
|
||||||
|
}
|
||||||
packet.Put()
|
packet.Put()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
packet.SetBody(w.Bytes(), true)
|
packet.SetBody(w.Bytes(), true)
|
||||||
}
|
}
|
||||||
if !packet.IsVaildHash() {
|
if !packet.IsVaildHash() {
|
||||||
logrus.Debugln("[listen] @", index, "drop invalid hash packet")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[listen] @", index, "drop invalid hash packet")
|
||||||
|
}
|
||||||
packet.Put()
|
packet.Put()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -154,7 +171,9 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
|
|||||||
case LINK_STATUS_DOWN:
|
case LINK_STATUS_DOWN:
|
||||||
n, err := p.WriteAndPut(head.NewPacket(head.ProtoHello, m.SrcPort(), p.peerip, m.DstPort(), nil), false)
|
n, err := p.WriteAndPut(head.NewPacket(head.ProtoHello, m.SrcPort(), p.peerip, m.DstPort(), nil), false)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
logrus.Debugln("[listen] @", index, "send", n, "bytes hello ack packet")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[listen] @", index, "send", n, "bytes hello ack packet")
|
||||||
|
}
|
||||||
p.status = LINK_STATUS_HALFUP
|
p.status = LINK_STATUS_HALFUP
|
||||||
} else {
|
} else {
|
||||||
logrus.Errorln("[listen] @", index, "send hello ack packet error:", err)
|
logrus.Errorln("[listen] @", index, "send hello ack packet error:", err)
|
||||||
@@ -175,12 +194,14 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
|
|||||||
case head.ProtoData:
|
case head.ProtoData:
|
||||||
if p.pipe != nil {
|
if p.pipe != nil {
|
||||||
p.pipe <- packet
|
p.pipe <- packet
|
||||||
logrus.Debugln("[listen] @", index, "deliver to pipe of", p.peerip)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[listen] @", index, "deliver to pipe of", p.peerip)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
_, err := m.nic.Write(packet.Body())
|
_, err := m.nic.Write(packet.Body())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("[listen] @", index, "deliver", packet.BodyLen(), "bytes data to nic err:", err)
|
logrus.Errorln("[listen] @", index, "deliver", packet.BodyLen(), "bytes data to nic err:", err)
|
||||||
} else {
|
} else if config.ShowDebugLog {
|
||||||
logrus.Debugln("[listen] @", index, "deliver", packet.BodyLen(), "bytes data to nic")
|
logrus.Debugln("[listen] @", index, "deliver", packet.BodyLen(), "bytes data to nic")
|
||||||
}
|
}
|
||||||
packet.Put()
|
packet.Put()
|
||||||
@@ -204,7 +225,9 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
|
|||||||
}
|
}
|
||||||
n, err := lnk.WriteAndPut(packet, true)
|
n, err := lnk.WriteAndPut(packet, true)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
logrus.Debugln("[listen] @", index, "trans", n, "bytes packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)))
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[listen] @", index, "trans", n, "bytes packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
logrus.Errorln("[listen] @", index, "trans packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)), "err:", err)
|
logrus.Errorln("[listen] @", index, "trans packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)), "err:", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/fumiama/water/waterutil"
|
"github.com/fumiama/water/waterutil"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/fumiama/WireGold/config"
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
"github.com/fumiama/WireGold/gold/p2p"
|
"github.com/fumiama/WireGold/gold/p2p"
|
||||||
"github.com/fumiama/WireGold/lower"
|
"github.com/fumiama/WireGold/lower"
|
||||||
@@ -151,7 +152,9 @@ func (m *Me) Close() error {
|
|||||||
|
|
||||||
func (m *Me) Write(packet []byte) (n int, err error) {
|
func (m *Me) Write(packet []byte) (n int, err error) {
|
||||||
n = m.sendAllSameDst(packet)
|
n = m.sendAllSameDst(packet)
|
||||||
logrus.Debugln("[me] writer ate", len(packet), "bytes, remain", len(packet)-n, "bytes")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[me] writer ate", len(packet), "bytes, remain", len(packet)-n, "bytes")
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,7 +183,9 @@ func (m *Me) sendAllSameDst(packet []byte) (n int) {
|
|||||||
}
|
}
|
||||||
n += pktl
|
n += pktl
|
||||||
rem = packet[n:]
|
rem = packet[n:]
|
||||||
logrus.Debugln("[me] skip to send", len(packet), "bytes ipv6 packet")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[me] skip to send", len(packet), "bytes ipv6 packet")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if len(rem) == 0 || !waterutil.IsIPv4(rem) {
|
if len(rem) == 0 || !waterutil.IsIPv4(rem) {
|
||||||
logrus.Warnln("[me] skip to send", len(packet), "bytes full packet")
|
logrus.Warnln("[me] skip to send", len(packet), "bytes full packet")
|
||||||
@@ -193,12 +198,16 @@ func (m *Me) sendAllSameDst(packet []byte) (n int) {
|
|||||||
for len(ptr) > 20 && p.issame(ptr) {
|
for len(ptr) > 20 && p.issame(ptr) {
|
||||||
totl := waterutil.IPv4TotalLength(ptr)
|
totl := waterutil.IPv4TotalLength(ptr)
|
||||||
if int(totl) > len(ptr) {
|
if int(totl) > len(ptr) {
|
||||||
logrus.Debugln("[me] wrap got invalid totl, break")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[me] wrap got invalid totl, break")
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
i += int(totl)
|
i += int(totl)
|
||||||
ptr = rem[i:]
|
ptr = rem[i:]
|
||||||
logrus.Debugln("[me] wrap", totl, "bytes packet to send together")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[me] wrap", totl, "bytes packet to send together")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
return
|
return
|
||||||
@@ -207,9 +216,13 @@ func (m *Me) sendAllSameDst(packet []byte) (n int) {
|
|||||||
packet = rem[:i]
|
packet = rem[:i]
|
||||||
rem = rem[i:]
|
rem = rem[i:]
|
||||||
dst := waterutil.IPv4Destination(packet)
|
dst := waterutil.IPv4Destination(packet)
|
||||||
logrus.Debugln("[me] sending", len(packet), "bytes packet from :"+strconv.Itoa(int(m.SrcPort())), "to", dst.String()+":"+strconv.Itoa(int(m.DstPort())), "remain:", len(rem), "bytes")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[me] sending", len(packet), "bytes packet from :"+strconv.Itoa(int(m.SrcPort())), "to", dst.String()+":"+strconv.Itoa(int(m.DstPort())), "remain:", len(rem), "bytes")
|
||||||
|
}
|
||||||
if m.me.Equal(dst) { // is to myself, write to nic (pipe not allow loopback)
|
if m.me.Equal(dst) { // is to myself, write to nic (pipe not allow loopback)
|
||||||
logrus.Debugln("[me] loopback packet")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[me] loopback packet")
|
||||||
|
}
|
||||||
_, err := m.nic.Write(packet)
|
_, err := m.nic.Write(packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Warnln("[me] write to loopback err:", err)
|
logrus.Warnln("[me] write to loopback err:", err)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/fumiama/WireGold/config"
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
"github.com/fumiama/WireGold/gold/p2p"
|
"github.com/fumiama/WireGold/gold/p2p"
|
||||||
"github.com/fumiama/WireGold/helper"
|
"github.com/fumiama/WireGold/helper"
|
||||||
@@ -63,7 +64,9 @@ func (l *Link) onNotify(packet []byte) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logrus.Debugln("[nat] notify drop invalid peer:", peer, "ep:", ep)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[nat] notify drop invalid peer:", peer, "ep:", ep)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"hash/crc64"
|
"hash/crc64"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/fumiama/WireGold/config"
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@@ -25,23 +26,33 @@ func (m *Me) wait(data []byte) *head.Packet {
|
|||||||
bound = len(data)
|
bound = len(data)
|
||||||
endl = "."
|
endl = "."
|
||||||
}
|
}
|
||||||
logrus.Debugln("[recv] data bytes", hex.EncodeToString(data[:bound]), endl)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[recv] data bytes", hex.EncodeToString(data[:bound]), endl)
|
||||||
|
}
|
||||||
seq, data := m.xordec(data)
|
seq, data := m.xordec(data)
|
||||||
logrus.Debugln("[recv] data xored", hex.EncodeToString(data[:bound]), endl)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[recv] data xored", hex.EncodeToString(data[:bound]), endl)
|
||||||
|
}
|
||||||
flags := head.Flags(data)
|
flags := head.Flags(data)
|
||||||
if !flags.IsValid() {
|
if !flags.IsValid() {
|
||||||
logrus.Debugln("[recv] drop invalid flags packet:", hex.EncodeToString(data[11:12]), hex.EncodeToString(data[10:11]))
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[recv] drop invalid flags packet:", hex.EncodeToString(data[11:12]), hex.EncodeToString(data[10:11]))
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
crc := binary.LittleEndian.Uint64(data[52:head.PacketHeadLen])
|
crc := binary.LittleEndian.Uint64(data[52:head.PacketHeadLen])
|
||||||
crclog := crc
|
crclog := crc
|
||||||
crc ^= (uint64(seq) << 16)
|
crc ^= (uint64(seq) << 16)
|
||||||
logrus.Debugf("[recv] packet crc %016x, seq %08x, xored crc %016x", crclog, seq, crc)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugf("[recv] packet crc %016x, seq %08x, xored crc %016x", crclog, seq, crc)
|
||||||
|
}
|
||||||
if m.recved.Get(crc) {
|
if m.recved.Get(crc) {
|
||||||
logrus.Warnln("[recv] ignore duplicated crc packet", strconv.FormatUint(crc, 16))
|
logrus.Warnln("[recv] ignore duplicated crc packet", strconv.FormatUint(crc, 16))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
logrus.Debugln("[recv]", len(data), "bytes data with flag", hex.EncodeToString(data[11:12]), hex.EncodeToString(data[10:11]))
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[recv]", len(data), "bytes data with flag", hex.EncodeToString(data[11:12]), hex.EncodeToString(data[10:11]))
|
||||||
|
}
|
||||||
if flags.IsSingle() || flags.NoFrag() {
|
if flags.IsSingle() || flags.NoFrag() {
|
||||||
h := head.SelectPacket()
|
h := head.SelectPacket()
|
||||||
_, err := h.Unmarshal(data)
|
_, err := h.Unmarshal(data)
|
||||||
@@ -61,13 +72,17 @@ func (m *Me) wait(data []byte) *head.Packet {
|
|||||||
hsh := crchash.Sum64()
|
hsh := crchash.Sum64()
|
||||||
h := m.recving.Get(hsh)
|
h := m.recving.Get(hsh)
|
||||||
if h != nil {
|
if h != nil {
|
||||||
logrus.Debugln("[recv] get another frag part of", strconv.FormatUint(hsh, 16))
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[recv] get another frag part of", strconv.FormatUint(hsh, 16))
|
||||||
|
}
|
||||||
ok, err := h.Unmarshal(data)
|
ok, err := h.Unmarshal(data)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if ok {
|
if ok {
|
||||||
m.recving.Delete(hsh)
|
m.recving.Delete(hsh)
|
||||||
m.recved.Set(crc, true)
|
m.recved.Set(crc, true)
|
||||||
logrus.Debugln("[recv] all parts of", strconv.FormatUint(hsh, 16), "has reached")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[recv] all parts of", strconv.FormatUint(hsh, 16), "has reached")
|
||||||
|
}
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -76,7 +91,9 @@ func (m *Me) wait(data []byte) *head.Packet {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
logrus.Debugln("[recv] get new frag part of", strconv.FormatUint(hsh, 16))
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[recv] get new frag part of", strconv.FormatUint(hsh, 16))
|
||||||
|
}
|
||||||
h = head.SelectPacket()
|
h = head.SelectPacket()
|
||||||
_, err := h.Unmarshal(data)
|
_, err := h.Unmarshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
|
|
||||||
"github.com/FloatTech/ttl"
|
"github.com/FloatTech/ttl"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/fumiama/WireGold/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Router struct {
|
type Router struct {
|
||||||
@@ -45,7 +47,9 @@ func (r *Router) SetDefault(l *Link) {
|
|||||||
func (r *Router) NextHop(ip string) (l *Link) {
|
func (r *Router) NextHop(ip string) (l *Link) {
|
||||||
l = r.cache.Get(ip)
|
l = r.cache.Get(ip)
|
||||||
if l != nil {
|
if l != nil {
|
||||||
logrus.Debugln("[router] get cached nexthop to", ip, "link", l)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[router] get cached nexthop to", ip, "link", l)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ipb := net.ParseIP(ip)
|
ipb := net.ParseIP(ip)
|
||||||
@@ -62,7 +66,9 @@ func (r *Router) NextHop(ip string) (l *Link) {
|
|||||||
for _, c := range r.list {
|
for _, c := range r.list {
|
||||||
if c.Contains(ipb) {
|
if c.Contains(ipb) {
|
||||||
l = r.table[c.String()]
|
l = r.table[c.String()]
|
||||||
logrus.Debugln("[router] get nexthop to", ipb, "-->", c, "link", l)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[router] get nexthop to", ipb, "-->", c, "link", l)
|
||||||
|
}
|
||||||
r.cache.Set(ip, l)
|
r.cache.Set(ip, l)
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,10 +11,12 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
|
||||||
"github.com/fumiama/WireGold/helper"
|
|
||||||
"github.com/klauspost/compress/zstd"
|
"github.com/klauspost/compress/zstd"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/fumiama/WireGold/config"
|
||||||
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
|
"github.com/fumiama/WireGold/helper"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -34,7 +36,9 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
|||||||
if l.mturandomrange > 0 {
|
if l.mturandomrange > 0 {
|
||||||
mtu -= uint16(rand.Intn(int(l.mturandomrange)))
|
mtu -= uint16(rand.Intn(int(l.mturandomrange)))
|
||||||
}
|
}
|
||||||
logrus.Debugln("[send] mtu:", mtu, ", addt:", sndcnt&0x07ff, ", key index:", teatype)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[send] mtu:", mtu, ", addt:", sndcnt&0x07ff, ", key index:", teatype)
|
||||||
|
}
|
||||||
if !istransfer {
|
if !istransfer {
|
||||||
l.encrypt(p, sndcnt, teatype)
|
l.encrypt(p, sndcnt, teatype)
|
||||||
}
|
}
|
||||||
@@ -56,7 +60,9 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
|||||||
packet := p.Copy()
|
packet := p.Copy()
|
||||||
for remlen > delta {
|
for remlen > delta {
|
||||||
remlen -= delta
|
remlen -= delta
|
||||||
logrus.Debugln("[send] split frag [", pos, "~", pos+delta, "], remain:", remlen)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[send] split frag [", pos, "~", pos+delta, "], remain:", remlen)
|
||||||
|
}
|
||||||
packet.CropBody(pos, pos+delta)
|
packet.CropBody(pos, pos+delta)
|
||||||
cnt, err := l.write(packet, teatype, sndcnt, totl, uint16(pos>>3), istransfer, true, seq)
|
cnt, err := l.write(packet, teatype, sndcnt, totl, uint16(pos>>3), istransfer, true, seq)
|
||||||
n += cnt
|
n += cnt
|
||||||
@@ -68,7 +74,9 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
|||||||
}
|
}
|
||||||
packet.Put()
|
packet.Put()
|
||||||
if remlen > 0 {
|
if remlen > 0 {
|
||||||
logrus.Debugln("[send] last frag [", pos, "~", pos+remlen, "]")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[send] last frag [", pos, "~", pos+remlen, "]")
|
||||||
|
}
|
||||||
p.CropBody(pos, pos+remlen)
|
p.CropBody(pos, pos+remlen)
|
||||||
cnt := 0
|
cnt := 0
|
||||||
cnt, err = l.write(p, teatype, sndcnt, totl, uint16(pos>>3), istransfer, false, seq)
|
cnt, err = l.write(p, teatype, sndcnt, totl, uint16(pos>>3), istransfer, false, seq)
|
||||||
@@ -79,7 +87,9 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
|||||||
|
|
||||||
func (l *Link) encrypt(p *head.Packet, sndcnt uint16, teatype uint8) {
|
func (l *Link) encrypt(p *head.Packet, sndcnt uint16, teatype uint8) {
|
||||||
p.FillHash()
|
p.FillHash()
|
||||||
logrus.Debugln("[send] data len before encrypt:", p.BodyLen())
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[send] data len before encrypt:", p.BodyLen())
|
||||||
|
}
|
||||||
data := p.Body()
|
data := p.Body()
|
||||||
if l.usezstd {
|
if l.usezstd {
|
||||||
w := helper.SelectWriter()
|
w := helper.SelectWriter()
|
||||||
@@ -88,10 +98,14 @@ func (l *Link) encrypt(p *head.Packet, sndcnt uint16, teatype uint8) {
|
|||||||
_, _ = io.Copy(enc, bytes.NewReader(data))
|
_, _ = io.Copy(enc, bytes.NewReader(data))
|
||||||
enc.Close()
|
enc.Close()
|
||||||
data = w.Bytes()
|
data = w.Bytes()
|
||||||
logrus.Debugln("[send] data len after zstd:", len(data))
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[send] data len after zstd:", len(data))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
p.SetBody(l.Encode(teatype, sndcnt&0x07ff, data), true)
|
p.SetBody(l.Encode(teatype, sndcnt&0x07ff, data), true)
|
||||||
logrus.Debugln("[send] data len after xchacha20:", p.BodyLen(), "addt:", sndcnt)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[send] data len after xchacha20:", p.BodyLen(), "addt:", sndcnt)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write 向 peer 发包
|
// write 向 peer 发包
|
||||||
@@ -132,10 +146,14 @@ func (l *Link) writeonce(p *head.Packet, teatype uint8, additional uint16, datas
|
|||||||
bound = len(d)
|
bound = len(d)
|
||||||
endl = "."
|
endl = "."
|
||||||
}
|
}
|
||||||
logrus.Debugln("[send] write", len(d), "bytes data from ep", l.me.conn.LocalAddr(), "to", peerep, "offset:", fmt.Sprintf("%04x", offset))
|
if config.ShowDebugLog {
|
||||||
logrus.Debugln("[send] data bytes", hex.EncodeToString(d[:bound]), endl)
|
logrus.Debugln("[send] write", len(d), "bytes data from ep", l.me.conn.LocalAddr(), "to", peerep, "offset:", fmt.Sprintf("%04x", offset))
|
||||||
|
logrus.Debugln("[send] data bytes", hex.EncodeToString(d[:bound]), endl)
|
||||||
|
}
|
||||||
d = l.me.xorenc(d, seq)
|
d = l.me.xorenc(d, seq)
|
||||||
logrus.Debugln("[send] data xored", hex.EncodeToString(d[:bound]), endl)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[send] data xored", hex.EncodeToString(d[:bound]), endl)
|
||||||
|
}
|
||||||
defer helper.PutBytes(d)
|
defer helper.PutBytes(d)
|
||||||
return l.me.conn.WriteToPeer(d, peerep)
|
return l.me.conn.WriteToPeer(d, peerep)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/fumiama/WireGold/config"
|
||||||
"github.com/fumiama/WireGold/helper"
|
"github.com/fumiama/WireGold/helper"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@@ -103,21 +104,29 @@ func isvalid(tcpconn *net.TCPConn) bool {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-stopch:
|
case <-stopch:
|
||||||
logrus.Debugln("[tcp] validate recv from", tcpconn.RemoteAddr(), "timeout")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tcp] validate recv from", tcpconn.RemoteAddr(), "timeout")
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
case <-copych:
|
case <-copych:
|
||||||
t.Stop()
|
t.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Debugln("[tcp] validate recv from", tcpconn.RemoteAddr(), "err:", err)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tcp] validate recv from", tcpconn.RemoteAddr(), "err:", err)
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if pckt.typ != packetTypeKeepAlive {
|
if pckt.typ != packetTypeKeepAlive {
|
||||||
logrus.Debugln("[tcp] validate got invalid typ", pckt.typ, "from", tcpconn.RemoteAddr())
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tcp] validate got invalid typ", pckt.typ, "from", tcpconn.RemoteAddr())
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Debugln("[tcp] passed validate recv from", tcpconn.RemoteAddr())
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tcp] passed validate recv from", tcpconn.RemoteAddr())
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,9 +10,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/FloatTech/ttl"
|
"github.com/FloatTech/ttl"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/fumiama/WireGold/config"
|
||||||
"github.com/fumiama/WireGold/gold/p2p"
|
"github.com/fumiama/WireGold/gold/p2p"
|
||||||
"github.com/fumiama/WireGold/helper"
|
"github.com/fumiama/WireGold/helper"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type EndPoint struct {
|
type EndPoint struct {
|
||||||
@@ -64,10 +66,12 @@ func (ep *EndPoint) Listen() (p2p.Conn, error) {
|
|||||||
peers: ttl.NewCacheOn(peerstimeout, [4]func(string, *net.TCPConn){
|
peers: ttl.NewCacheOn(peerstimeout, [4]func(string, *net.TCPConn){
|
||||||
nil, nil, func(_ string, t *net.TCPConn) {
|
nil, nil, func(_ string, t *net.TCPConn) {
|
||||||
err := t.CloseWrite()
|
err := t.CloseWrite()
|
||||||
if err != nil {
|
if config.ShowDebugLog {
|
||||||
logrus.Debugln("[tcp] close write from", t.LocalAddr(), "to", t.RemoteAddr(), "err:", err)
|
if err != nil {
|
||||||
} else {
|
logrus.Debugln("[tcp] close write from", t.LocalAddr(), "to", t.RemoteAddr(), "err:", err)
|
||||||
logrus.Debugln("[tcp] close write from", t.LocalAddr(), "to", t.RemoteAddr())
|
} else {
|
||||||
|
logrus.Debugln("[tcp] close write from", t.LocalAddr(), "to", t.RemoteAddr())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, nil,
|
}, nil,
|
||||||
}),
|
}),
|
||||||
@@ -130,7 +134,9 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
|
|||||||
if !isvalid(tcpconn) {
|
if !isvalid(tcpconn) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logrus.Debugln("[tcp] accept from", ep)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tcp] accept from", ep)
|
||||||
|
}
|
||||||
conn.peers.Set(ep.String(), tcpconn)
|
conn.peers.Set(ep.String(), tcpconn)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,7 +171,9 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-stopch:
|
case <-stopch:
|
||||||
logrus.Debugln("[tcp] recv from", ep, "timeout")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tcp] recv from", ep, "timeout")
|
||||||
|
}
|
||||||
_ = tcpconn.CloseRead()
|
_ = tcpconn.CloseRead()
|
||||||
return
|
return
|
||||||
case <-copych:
|
case <-copych:
|
||||||
@@ -173,16 +181,22 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Debugln("[tcp] recv from", ep, "err:", err)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tcp] recv from", ep, "err:", err)
|
||||||
|
}
|
||||||
_ = tcpconn.CloseRead()
|
_ = tcpconn.CloseRead()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if r.pckt.typ >= packetTypeTop {
|
if r.pckt.typ >= packetTypeTop {
|
||||||
logrus.Debugln("[tcp] close reading invalid conn from", ep, "typ", r.pckt.typ, "len", r.pckt.len)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tcp] close reading invalid conn from", ep, "typ", r.pckt.typ, "len", r.pckt.len)
|
||||||
|
}
|
||||||
_ = tcpconn.CloseRead()
|
_ = tcpconn.CloseRead()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logrus.Debugln("[tcp] dispatch packet from", ep, "typ", r.pckt.typ, "len", r.pckt.len)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tcp] dispatch packet from", ep, "typ", r.pckt.typ, "len", r.pckt.len)
|
||||||
|
}
|
||||||
conn.recv <- r
|
conn.recv <- r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -248,7 +262,9 @@ RECONNECT:
|
|||||||
if dialtimeout < time.Second {
|
if dialtimeout < time.Second {
|
||||||
dialtimeout = time.Second
|
dialtimeout = time.Second
|
||||||
}
|
}
|
||||||
logrus.Debugln("[tcp] dial to", tcpep.addr, "timeout", dialtimeout)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tcp] dial to", tcpep.addr, "timeout", dialtimeout)
|
||||||
|
}
|
||||||
var cn net.Conn
|
var cn net.Conn
|
||||||
// must use another port to send because there's no exsiting conn
|
// must use another port to send because there's no exsiting conn
|
||||||
cn, err = net.DialTimeout(tcpep.Network(), tcpep.addr.String(), dialtimeout)
|
cn, err = net.DialTimeout(tcpep.Network(), tcpep.addr.String(), dialtimeout)
|
||||||
@@ -263,13 +279,17 @@ RECONNECT:
|
|||||||
typ: packetTypeKeepAlive,
|
typ: packetTypeKeepAlive,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Debugln("[tcp] dial to", tcpep.addr, "success, but write err:", err)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tcp] dial to", tcpep.addr, "success, but write err:", err)
|
||||||
|
}
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
logrus.Debugln("[tcp] dial to", tcpep.addr, "success, local:", tcpconn.LocalAddr())
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tcp] dial to", tcpep.addr, "success, local:", tcpconn.LocalAddr())
|
||||||
|
}
|
||||||
conn.peers.Set(tcpep.String(), tcpconn)
|
conn.peers.Set(tcpep.String(), tcpconn)
|
||||||
go conn.receive(tcpconn, true)
|
go conn.receive(tcpconn, true)
|
||||||
} else {
|
} else if config.ShowDebugLog {
|
||||||
logrus.Debugln("[tcp] reuse tcpconn from", tcpconn.LocalAddr(), "to", tcpconn.RemoteAddr())
|
logrus.Debugln("[tcp] reuse tcpconn from", tcpconn.LocalAddr(), "to", tcpconn.RemoteAddr())
|
||||||
}
|
}
|
||||||
cnt, err := io.Copy(tcpconn, &packet{
|
cnt, err := io.Copy(tcpconn, &packet{
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
_ "github.com/fumiama/WireGold/gold/p2p/udp" // support udp 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/p2p/udplite" // support udplite connection
|
||||||
|
|
||||||
|
"github.com/fumiama/WireGold/config"
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
"github.com/fumiama/WireGold/gold/link"
|
"github.com/fumiama/WireGold/gold/link"
|
||||||
)
|
)
|
||||||
@@ -107,14 +108,20 @@ func (s *Tunnel) handleWrite() {
|
|||||||
end = len(b)
|
end = len(b)
|
||||||
endl = "."
|
endl = "."
|
||||||
}
|
}
|
||||||
logrus.Debugln("[tunnel] write send", hex.EncodeToString(b[:end]), endl)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tunnel] write send", hex.EncodeToString(b[:end]), endl)
|
||||||
|
}
|
||||||
if b == nil {
|
if b == nil {
|
||||||
logrus.Errorln("[tunnel] write recv nil")
|
logrus.Errorln("[tunnel] write recv nil")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
logrus.Debugln("[tunnel] writing", len(b), "bytes...")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tunnel] writing", len(b), "bytes...")
|
||||||
|
}
|
||||||
for len(b) > int(s.mtu)-4 {
|
for len(b) > int(s.mtu)-4 {
|
||||||
logrus.Debugln("[tunnel] seq", seq, "split buffer")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tunnel] seq", seq, "split buffer")
|
||||||
|
}
|
||||||
binary.LittleEndian.PutUint32(buf[:4], seq)
|
binary.LittleEndian.PutUint32(buf[:4], seq)
|
||||||
seq++
|
seq++
|
||||||
copy(buf[4:], b[:s.mtu-4])
|
copy(buf[4:], b[:s.mtu-4])
|
||||||
@@ -125,7 +132,9 @@ func (s *Tunnel) handleWrite() {
|
|||||||
logrus.Errorln("[tunnel] seq", seq-1, "write err:", err)
|
logrus.Errorln("[tunnel] seq", seq-1, "write err:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logrus.Debugln("[tunnel] seq", seq-1, "write succeeded")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tunnel] seq", seq-1, "write succeeded")
|
||||||
|
}
|
||||||
b = b[s.mtu-4:]
|
b = b[s.mtu-4:]
|
||||||
}
|
}
|
||||||
binary.LittleEndian.PutUint32(buf[:4], seq)
|
binary.LittleEndian.PutUint32(buf[:4], seq)
|
||||||
@@ -138,7 +147,9 @@ func (s *Tunnel) handleWrite() {
|
|||||||
logrus.Errorln("[tunnel] seq", seq-1, "write err:", err)
|
logrus.Errorln("[tunnel] seq", seq-1, "write err:", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
logrus.Debugln("[tunnel] seq", seq-1, "write succeeded")
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tunnel] seq", seq-1, "write succeeded")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,7 +158,9 @@ func (s *Tunnel) handleRead() {
|
|||||||
seqmap := make(map[uint32]*head.Packet)
|
seqmap := make(map[uint32]*head.Packet)
|
||||||
for {
|
for {
|
||||||
if p, ok := seqmap[seq]; ok {
|
if p, ok := seqmap[seq]; ok {
|
||||||
logrus.Debugln("[tunnel] dispatch cached seq", seq)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tunnel] dispatch cached seq", seq)
|
||||||
|
}
|
||||||
delete(seqmap, seq)
|
delete(seqmap, seq)
|
||||||
seq++
|
seq++
|
||||||
s.out <- p
|
s.out <- p
|
||||||
@@ -164,15 +177,21 @@ func (s *Tunnel) handleRead() {
|
|||||||
end = p.BodyLen()
|
end = p.BodyLen()
|
||||||
endl = "."
|
endl = "."
|
||||||
}
|
}
|
||||||
logrus.Debugln("[tunnel] read recv", hex.EncodeToString(p.Body()[:end]), endl)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tunnel] read recv", hex.EncodeToString(p.Body()[:end]), endl)
|
||||||
|
}
|
||||||
recvseq := binary.LittleEndian.Uint32(p.Body()[:4])
|
recvseq := binary.LittleEndian.Uint32(p.Body()[:4])
|
||||||
if recvseq == seq {
|
if recvseq == seq {
|
||||||
logrus.Debugln("[tunnel] dispatch seq", seq)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tunnel] dispatch seq", seq)
|
||||||
|
}
|
||||||
seq++
|
seq++
|
||||||
s.out <- p
|
s.out <- p
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
seqmap[recvseq] = p
|
seqmap[recvseq] = p
|
||||||
logrus.Debugln("[tunnel] cache seq", recvseq)
|
if config.ShowDebugLog {
|
||||||
|
logrus.Debugln("[tunnel] cache seq", recvseq)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user