1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-12 21:00:27 +08:00

chore: remove debug log at build

This commit is contained in:
源文雨
2024-08-03 15:32:31 +08:00
parent 08688b584b
commit fa9abff1a8
13 changed files with 218 additions and 78 deletions

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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)
}
}
}

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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)
}