mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-05 07:50:24 +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 ./...
|
||||
|
||||
- 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:
|
||||
name: Lint
|
||||
|
||||
@@ -2,9 +2,9 @@ package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -42,11 +42,11 @@ type Peer struct {
|
||||
func Parse(path string) (c Config) {
|
||||
file, err := os.ReadFile(path)
|
||||
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)
|
||||
if err != nil {
|
||||
log.Fatal("invalid config file:", err)
|
||||
logrus.Fatal("invalid config file:", err)
|
||||
}
|
||||
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"
|
||||
"net"
|
||||
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
blake2b "github.com/fumiama/blake2b-simd"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
)
|
||||
|
||||
const PacketHeadLen = 60
|
||||
@@ -89,7 +91,6 @@ type Packet struct {
|
||||
|
||||
// NewPacket 生成一个新包
|
||||
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.Proto = proto
|
||||
p.TTL = 16
|
||||
@@ -147,7 +148,9 @@ func (p *Packet) Unmarshal(data []byte) (complete bool, err error) {
|
||||
|
||||
if p.rembytes > 0 {
|
||||
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
|
||||
@@ -199,7 +202,10 @@ func (p *Packet) FillHash() {
|
||||
logrus.Error("[packet] err when fill hash:", err)
|
||||
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 合法性
|
||||
@@ -211,8 +217,11 @@ func (p *Packet) IsVaildHash() bool {
|
||||
return false
|
||||
}
|
||||
var sum [32]byte
|
||||
logrus.Debugln("[packet] sum calulated:", hex.EncodeToString(h.Sum(sum[:0])))
|
||||
logrus.Debugln("[packet] sum in packet:", hex.EncodeToString(p.Hash[:]))
|
||||
_ = h.Sum(sum[:0])
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/klauspost/compress/zstd"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
"github.com/fumiama/WireGold/gold/p2p"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
@@ -48,7 +49,9 @@ func (m *Me) listen() (conn p2p.Conn, err error) {
|
||||
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]
|
||||
n, addr, err := conn.ReadFromPeer(lbf)
|
||||
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)
|
||||
return
|
||||
}
|
||||
logrus.Debugln("[listen] unlock index", i)
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[listen] unlock index", i)
|
||||
}
|
||||
hasntfinished[i].Unlock()
|
||||
i--
|
||||
continue
|
||||
@@ -77,7 +82,9 @@ func (m *Me) listen() (conn p2p.Conn, err error) {
|
||||
}
|
||||
packet := m.wait(lbf[:n:lstnbufgragsz])
|
||||
if packet == nil {
|
||||
logrus.Debugln("[listen] waiting, unlock index", i)
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[listen] waiting, unlock index", i)
|
||||
}
|
||||
hasntfinished[i].Unlock()
|
||||
i--
|
||||
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()) {
|
||||
defer finish()
|
||||
defer logrus.Debugln("[listen] dispatched, unlock index", index)
|
||||
logrus.Debugln("[listen] start dispatching index", index)
|
||||
if config.ShowDebugLog {
|
||||
defer logrus.Debugln("[listen] dispatched, unlock index", index)
|
||||
logrus.Debugln("[listen] start dispatching index", index)
|
||||
}
|
||||
r := packet.Len() - packet.BodyLen()
|
||||
if r > 0 {
|
||||
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
|
||||
}
|
||||
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 {
|
||||
logrus.Warnln("[listen] @", index, "packet from", packet.Src, "to", packet.Dst, "is refused")
|
||||
packet.Put()
|
||||
@@ -125,7 +136,9 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
|
||||
var err error
|
||||
data, err := p.Decode(packet.CipherIndex(), addt, packet.Body())
|
||||
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()
|
||||
return
|
||||
}
|
||||
@@ -137,14 +150,18 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
|
||||
_, err = io.Copy(w, dec)
|
||||
dec.Close()
|
||||
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()
|
||||
return
|
||||
}
|
||||
packet.SetBody(w.Bytes(), true)
|
||||
}
|
||||
if !packet.IsVaildHash() {
|
||||
logrus.Debugln("[listen] @", index, "drop invalid hash packet")
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[listen] @", index, "drop invalid hash packet")
|
||||
}
|
||||
packet.Put()
|
||||
return
|
||||
}
|
||||
@@ -154,7 +171,9 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
|
||||
case LINK_STATUS_DOWN:
|
||||
n, err := p.WriteAndPut(head.NewPacket(head.ProtoHello, m.SrcPort(), p.peerip, m.DstPort(), nil), false)
|
||||
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
|
||||
} else {
|
||||
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:
|
||||
if p.pipe != nil {
|
||||
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 {
|
||||
_, err := m.nic.Write(packet.Body())
|
||||
if err != nil {
|
||||
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")
|
||||
}
|
||||
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)
|
||||
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 {
|
||||
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/sirupsen/logrus"
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
"github.com/fumiama/WireGold/gold/p2p"
|
||||
"github.com/fumiama/WireGold/lower"
|
||||
@@ -151,7 +152,9 @@ func (m *Me) Close() error {
|
||||
|
||||
func (m *Me) Write(packet []byte) (n int, err error) {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -180,7 +183,9 @@ func (m *Me) sendAllSameDst(packet []byte) (n int) {
|
||||
}
|
||||
n += pktl
|
||||
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) {
|
||||
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) {
|
||||
totl := waterutil.IPv4TotalLength(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
|
||||
}
|
||||
i += int(totl)
|
||||
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 {
|
||||
return
|
||||
@@ -207,9 +216,13 @@ func (m *Me) sendAllSameDst(packet []byte) (n int) {
|
||||
packet = rem[:i]
|
||||
rem = rem[i:]
|
||||
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)
|
||||
logrus.Debugln("[me] loopback packet")
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[me] loopback packet")
|
||||
}
|
||||
_, err := m.nic.Write(packet)
|
||||
if err != nil {
|
||||
logrus.Warnln("[me] write to loopback err:", err)
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
"github.com/fumiama/WireGold/gold/p2p"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
@@ -63,7 +64,9 @@ func (l *Link) onNotify(packet []byte) {
|
||||
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"
|
||||
"strconv"
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -25,23 +26,33 @@ func (m *Me) wait(data []byte) *head.Packet {
|
||||
bound = len(data)
|
||||
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)
|
||||
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)
|
||||
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
|
||||
}
|
||||
crc := binary.LittleEndian.Uint64(data[52:head.PacketHeadLen])
|
||||
crclog := crc
|
||||
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) {
|
||||
logrus.Warnln("[recv] ignore duplicated crc packet", strconv.FormatUint(crc, 16))
|
||||
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() {
|
||||
h := head.SelectPacket()
|
||||
_, err := h.Unmarshal(data)
|
||||
@@ -61,13 +72,17 @@ func (m *Me) wait(data []byte) *head.Packet {
|
||||
hsh := crchash.Sum64()
|
||||
h := m.recving.Get(hsh)
|
||||
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)
|
||||
if err == nil {
|
||||
if ok {
|
||||
m.recving.Delete(hsh)
|
||||
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
|
||||
}
|
||||
} else {
|
||||
@@ -76,7 +91,9 @@ func (m *Me) wait(data []byte) *head.Packet {
|
||||
}
|
||||
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()
|
||||
_, err := h.Unmarshal(data)
|
||||
if err != nil {
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
|
||||
"github.com/FloatTech/ttl"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
)
|
||||
|
||||
type Router struct {
|
||||
@@ -45,7 +47,9 @@ func (r *Router) SetDefault(l *Link) {
|
||||
func (r *Router) NextHop(ip string) (l *Link) {
|
||||
l = r.cache.Get(ip)
|
||||
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
|
||||
}
|
||||
ipb := net.ParseIP(ip)
|
||||
@@ -62,7 +66,9 @@ func (r *Router) NextHop(ip string) (l *Link) {
|
||||
for _, c := range r.list {
|
||||
if c.Contains(ipb) {
|
||||
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)
|
||||
return l
|
||||
}
|
||||
|
||||
@@ -11,10 +11,12 @@ import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
"github.com/klauspost/compress/zstd"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -34,7 +36,9 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
||||
if l.mturandomrange > 0 {
|
||||
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 {
|
||||
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()
|
||||
for 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)
|
||||
cnt, err := l.write(packet, teatype, sndcnt, totl, uint16(pos>>3), istransfer, true, seq)
|
||||
n += cnt
|
||||
@@ -68,7 +74,9 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
||||
}
|
||||
packet.Put()
|
||||
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)
|
||||
cnt := 0
|
||||
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) {
|
||||
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()
|
||||
if l.usezstd {
|
||||
w := helper.SelectWriter()
|
||||
@@ -88,10 +98,14 @@ func (l *Link) encrypt(p *head.Packet, sndcnt uint16, teatype uint8) {
|
||||
_, _ = io.Copy(enc, bytes.NewReader(data))
|
||||
enc.Close()
|
||||
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)
|
||||
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 发包
|
||||
@@ -132,10 +146,14 @@ func (l *Link) writeonce(p *head.Packet, teatype uint8, additional uint16, datas
|
||||
bound = len(d)
|
||||
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)
|
||||
if config.ShowDebugLog {
|
||||
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)
|
||||
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)
|
||||
return l.me.conn.WriteToPeer(d, peerep)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -103,21 +104,29 @@ func isvalid(tcpconn *net.TCPConn) bool {
|
||||
|
||||
select {
|
||||
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
|
||||
case <-copych:
|
||||
t.Stop()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
logrus.Debugln("[tcp] passed validate recv from", tcpconn.RemoteAddr())
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[tcp] passed validate recv from", tcpconn.RemoteAddr())
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -10,9 +10,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/FloatTech/ttl"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
"github.com/fumiama/WireGold/gold/p2p"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type EndPoint struct {
|
||||
@@ -64,10 +66,12 @@ func (ep *EndPoint) Listen() (p2p.Conn, error) {
|
||||
peers: ttl.NewCacheOn(peerstimeout, [4]func(string, *net.TCPConn){
|
||||
nil, nil, func(_ string, t *net.TCPConn) {
|
||||
err := t.CloseWrite()
|
||||
if err != nil {
|
||||
logrus.Debugln("[tcp] close write from", t.LocalAddr(), "to", t.RemoteAddr(), "err:", err)
|
||||
} else {
|
||||
logrus.Debugln("[tcp] close write from", t.LocalAddr(), "to", t.RemoteAddr())
|
||||
if config.ShowDebugLog {
|
||||
if err != nil {
|
||||
logrus.Debugln("[tcp] close write from", t.LocalAddr(), "to", t.RemoteAddr(), "err:", err)
|
||||
} else {
|
||||
logrus.Debugln("[tcp] close write from", t.LocalAddr(), "to", t.RemoteAddr())
|
||||
}
|
||||
}
|
||||
}, nil,
|
||||
}),
|
||||
@@ -130,7 +134,9 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
|
||||
if !isvalid(tcpconn) {
|
||||
return
|
||||
}
|
||||
logrus.Debugln("[tcp] accept from", ep)
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[tcp] accept from", ep)
|
||||
}
|
||||
conn.peers.Set(ep.String(), tcpconn)
|
||||
}
|
||||
|
||||
@@ -165,7 +171,9 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
|
||||
|
||||
select {
|
||||
case <-stopch:
|
||||
logrus.Debugln("[tcp] recv from", ep, "timeout")
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[tcp] recv from", ep, "timeout")
|
||||
}
|
||||
_ = tcpconn.CloseRead()
|
||||
return
|
||||
case <-copych:
|
||||
@@ -173,16 +181,22 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
logrus.Debugln("[tcp] recv from", ep, "err:", err)
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[tcp] recv from", ep, "err:", err)
|
||||
}
|
||||
_ = tcpconn.CloseRead()
|
||||
return
|
||||
}
|
||||
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()
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -248,7 +262,9 @@ RECONNECT:
|
||||
if 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
|
||||
// must use another port to send because there's no exsiting conn
|
||||
cn, err = net.DialTimeout(tcpep.Network(), tcpep.addr.String(), dialtimeout)
|
||||
@@ -263,13 +279,17 @@ RECONNECT:
|
||||
typ: packetTypeKeepAlive,
|
||||
})
|
||||
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
|
||||
}
|
||||
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)
|
||||
go conn.receive(tcpconn, true)
|
||||
} else {
|
||||
} else if config.ShowDebugLog {
|
||||
logrus.Debugln("[tcp] reuse tcpconn from", tcpconn.LocalAddr(), "to", tcpconn.RemoteAddr())
|
||||
}
|
||||
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/udplite" // support udplite connection
|
||||
|
||||
"github.com/fumiama/WireGold/config"
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
"github.com/fumiama/WireGold/gold/link"
|
||||
)
|
||||
@@ -107,14 +108,20 @@ func (s *Tunnel) handleWrite() {
|
||||
end = len(b)
|
||||
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 {
|
||||
logrus.Errorln("[tunnel] write recv nil")
|
||||
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 {
|
||||
logrus.Debugln("[tunnel] seq", seq, "split buffer")
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[tunnel] seq", seq, "split buffer")
|
||||
}
|
||||
binary.LittleEndian.PutUint32(buf[:4], seq)
|
||||
seq++
|
||||
copy(buf[4:], b[:s.mtu-4])
|
||||
@@ -125,7 +132,9 @@ func (s *Tunnel) handleWrite() {
|
||||
logrus.Errorln("[tunnel] seq", seq-1, "write err:", err)
|
||||
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:]
|
||||
}
|
||||
binary.LittleEndian.PutUint32(buf[:4], seq)
|
||||
@@ -138,7 +147,9 @@ func (s *Tunnel) handleWrite() {
|
||||
logrus.Errorln("[tunnel] seq", seq-1, "write err:", err)
|
||||
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)
|
||||
for {
|
||||
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)
|
||||
seq++
|
||||
s.out <- p
|
||||
@@ -164,15 +177,21 @@ func (s *Tunnel) handleRead() {
|
||||
end = p.BodyLen()
|
||||
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])
|
||||
if recvseq == seq {
|
||||
logrus.Debugln("[tunnel] dispatch seq", seq)
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[tunnel] dispatch seq", seq)
|
||||
}
|
||||
seq++
|
||||
s.out <- p
|
||||
continue
|
||||
}
|
||||
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