1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-22 19:40:35 +08:00

fix(crypto): encode and decode of nil data

This commit is contained in:
源文雨
2024-07-12 23:18:02 +09:00
parent 677b11f440
commit 8215abb695
3 changed files with 9 additions and 7 deletions

View File

@@ -7,6 +7,8 @@ import (
"errors" "errors"
"math/bits" "math/bits"
mrand "math/rand" mrand "math/rand"
"github.com/sirupsen/logrus"
) )
var ( var (
@@ -51,7 +53,7 @@ func expandkeyunit(v1, v2 byte) (v uint16) {
// Encode 使用 xchacha20poly1305 和密钥序列加密 // Encode 使用 xchacha20poly1305 和密钥序列加密
func (l *Link) Encode(teatype uint8, additional uint16, b []byte) (eb []byte) { func (l *Link) Encode(teatype uint8, additional uint16, b []byte) (eb []byte) {
if b == nil || teatype >= 32 { if len(b) == 0 || teatype >= 32 {
return return
} }
if l.keys[0] == nil { 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] aead := l.keys[teatype]
if aead == nil { if aead == nil {
logrus.Warnln("[crypto] cipher key at index", teatype, "is empty")
return return
} }
eb = encode(aead, additional, b) eb = encode(aead, additional, b)
@@ -69,7 +72,7 @@ func (l *Link) Encode(teatype uint8, additional uint16, b []byte) (eb []byte) {
// Decode 使用 xchacha20poly1305 和密钥序列解密 // Decode 使用 xchacha20poly1305 和密钥序列解密
func (l *Link) Decode(teatype uint8, additional uint16, b []byte) (db []byte, err error) { 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 return
} }
if l.keys[0] == nil { if l.keys[0] == nil {

View File

@@ -31,7 +31,8 @@ type NIC struct {
func NewNIC(ip, subnet, mtu string, cidrs ...string) NICIO { func NewNIC(ip, subnet, mtu string, cidrs ...string) NICIO {
ifce, err := water.New(water.Config{DeviceType: water.TUN}) ifce, err := water.New(water.Config{DeviceType: water.TUN})
if err != nil { if err != nil {
panic(err) logrus.Error(err)
os.Exit(1)
} }
n := &NIC{ n := &NIC{
ifce: ifce, ifce: ifce,

View File

@@ -54,10 +54,8 @@ func (wg *WG) Start(srcport, destport uint16) {
func (wg *WG) Run(srcport, destport uint16) { func (wg *WG) Run(srcport, destport uint16) {
wg.init(srcport, destport) wg.init(srcport, destport)
_, err := wg.me.ListenNIC() _, _ = wg.me.ListenNIC()
if err != nil { logrus.Info("[wg] stopped")
logrus.Warnln(err)
}
} }
func (wg *WG) Stop() { func (wg *WG) Stop() {