mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-06-17 14:45:06 +03:00
fix
This commit is contained in:
parent
aba03d590e
commit
68fbfc6f4b
1 changed files with 98 additions and 88 deletions
|
@ -2,14 +2,14 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/ed25519"
|
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
|
"crypto/ed25519"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"io"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -34,7 +34,7 @@ type Core struct {
|
||||||
phony.Inbox
|
phony.Inbox
|
||||||
*iwe.PacketConn
|
*iwe.PacketConn
|
||||||
config *config.NodeConfig // Config
|
config *config.NodeConfig // Config
|
||||||
sgcm map[string] cipher.AEAD
|
sgcm map[string]cipher.AEAD
|
||||||
secret ed25519.PrivateKey
|
secret ed25519.PrivateKey
|
||||||
public ed25519.PublicKey
|
public ed25519.PublicKey
|
||||||
links links
|
links links
|
||||||
|
@ -56,16 +56,20 @@ func (c *Core) _init() error {
|
||||||
c.log = log.New(ioutil.Discard, "", 0)
|
c.log = log.New(ioutil.Discard, "", 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.sgcm = make(map[string] cipher.AEAD)
|
c.sgcm = make(map[string]cipher.AEAD)
|
||||||
|
|
||||||
for addr, csecret := range c.config.Secrets {
|
for addr, csecret := range c.config.Secrets {
|
||||||
var gcm cipher.AEAD
|
var gcm cipher.AEAD
|
||||||
switch len(csecret) {
|
switch len(csecret) {
|
||||||
case 16, 24, 32: // Generate GCM
|
case 16, 24, 32: // Generate GCM
|
||||||
ch, err := aes.NewCipher([]byte(csecret))
|
ch, err := aes.NewCipher([]byte(csecret))
|
||||||
if err != nil { return fmt.Errorf("aes.NewCipher: %w", err) }
|
if err != nil {
|
||||||
|
return fmt.Errorf("aes.NewCipher: %w", err)
|
||||||
|
}
|
||||||
gcm, err = cipher.NewGCM(ch)
|
gcm, err = cipher.NewGCM(ch)
|
||||||
if err != nil { return fmt.Errorf("cipher.NewGCM: %w", err) }
|
if err != nil {
|
||||||
|
return fmt.Errorf("cipher.NewGCM: %w", err)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Secret for %s is incorrect length. Should be 16, 24 or 32 bytes", addr)
|
return fmt.Errorf("Secret for %s is incorrect length. Should be 16, 24 or 32 bytes", addr)
|
||||||
}
|
}
|
||||||
|
@ -74,7 +78,9 @@ func (c *Core) _init() error {
|
||||||
c.sgcm["0"] = gcm
|
c.sgcm["0"] = gcm
|
||||||
} else {
|
} else {
|
||||||
saddr, err := hex.DecodeString(addr)
|
saddr, err := hex.DecodeString(addr)
|
||||||
if err != nil { return err }
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if len(saddr) != ed25519.PublicKeySize {
|
if len(saddr) != ed25519.PublicKeySize {
|
||||||
return fmt.Errorf("PublicKey '%s' has the wrong length", addr)
|
return fmt.Errorf("PublicKey '%s' has the wrong length", addr)
|
||||||
}
|
}
|
||||||
|
@ -239,13 +245,17 @@ func (c *Core) ReadFrom(p []byte) (n int, from net.Addr, err error) {
|
||||||
case typeSessionTraffic:
|
case typeSessionTraffic:
|
||||||
// This is what we want to handle here
|
// This is what we want to handle here
|
||||||
gcm := c.getSecretForAddr(from)
|
gcm := c.getSecretForAddr(from)
|
||||||
if gcm != nil { continue }
|
if gcm != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
bs = bs[1:n]
|
bs = bs[1:n]
|
||||||
|
|
||||||
case typeSessionEncTraffic:
|
case typeSessionEncTraffic:
|
||||||
// Encoded traddic. Decode first
|
// Encoded traddic. Decode first
|
||||||
gcm := c.getSecretForAddr(from)
|
gcm := c.getSecretForAddr(from)
|
||||||
if gcm == nil { continue }
|
if gcm == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
bs, err = gcm.Open(nil, bs[1:gcm.NonceSize()+1], bs[gcm.NonceSize()+1:n], nil)
|
bs, err = gcm.Open(nil, bs[1:gcm.NonceSize()+1], bs[gcm.NonceSize()+1:n], nil)
|
||||||
if err != nil { // If we failed to decrypt the packet, we silently skip it.
|
if err != nil { // If we failed to decrypt the packet, we silently skip it.
|
||||||
err = nil
|
err = nil
|
||||||
|
@ -290,7 +300,7 @@ func (c *Core) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
||||||
buf = append(buf, gcm.Seal(nonce, nonce, p, nil)...)
|
buf = append(buf, gcm.Seal(nonce, nonce, p, nil)...)
|
||||||
n, err = c.PacketConn.WriteTo(buf, addr)
|
n, err = c.PacketConn.WriteTo(buf, addr)
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
n -= 1+len(nonce)
|
n -= 1 + len(nonce)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue