From 8215abb695a7739f7f81eab8f2b8d2f03ca91977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Fri, 12 Jul 2024 23:18:02 +0900 Subject: [PATCH] fix(crypto): encode and decode of nil data --- gold/link/crypto.go | 7 +++++-- lower/nic.go | 3 ++- upper/services/wg/wg.go | 6 ++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/gold/link/crypto.go b/gold/link/crypto.go index 070faf9..810a85c 100644 --- a/gold/link/crypto.go +++ b/gold/link/crypto.go @@ -7,6 +7,8 @@ import ( "errors" "math/bits" mrand "math/rand" + + "github.com/sirupsen/logrus" ) var ( @@ -51,7 +53,7 @@ func expandkeyunit(v1, v2 byte) (v uint16) { // Encode 使用 xchacha20poly1305 和密钥序列加密 func (l *Link) Encode(teatype uint8, additional uint16, b []byte) (eb []byte) { - if b == nil || teatype >= 32 { + if len(b) == 0 || teatype >= 32 { return } if l.keys[0] == nil { @@ -61,6 +63,7 @@ func (l *Link) Encode(teatype uint8, additional uint16, b []byte) (eb []byte) { } aead := l.keys[teatype] if aead == nil { + logrus.Warnln("[crypto] cipher key at index", teatype, "is empty") return } eb = encode(aead, additional, b) @@ -69,7 +72,7 @@ func (l *Link) Encode(teatype uint8, additional uint16, b []byte) (eb []byte) { // Decode 使用 xchacha20poly1305 和密钥序列解密 func (l *Link) Decode(teatype uint8, additional uint16, b []byte) (db []byte, err error) { - if b == nil || teatype >= 32 { + if len(b) == 0 || teatype >= 32 { return } if l.keys[0] == nil { diff --git a/lower/nic.go b/lower/nic.go index e6495a9..c9d416b 100644 --- a/lower/nic.go +++ b/lower/nic.go @@ -31,7 +31,8 @@ type NIC struct { func NewNIC(ip, subnet, mtu string, cidrs ...string) NICIO { ifce, err := water.New(water.Config{DeviceType: water.TUN}) if err != nil { - panic(err) + logrus.Error(err) + os.Exit(1) } n := &NIC{ ifce: ifce, diff --git a/upper/services/wg/wg.go b/upper/services/wg/wg.go index d9632a2..0feffbb 100644 --- a/upper/services/wg/wg.go +++ b/upper/services/wg/wg.go @@ -54,10 +54,8 @@ func (wg *WG) Start(srcport, destport uint16) { func (wg *WG) Run(srcport, destport uint16) { wg.init(srcport, destport) - _, err := wg.me.ListenNIC() - if err != nil { - logrus.Warnln(err) - } + _, _ = wg.me.ListenNIC() + logrus.Info("[wg] stopped") } func (wg *WG) Stop() {