mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-05 07:50:24 +08:00
add more logs
This commit is contained in:
24
gold/link/crypto_test.go
Normal file
24
gold/link/crypto_test.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package link
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/rand"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestXOR(t *testing.T) {
|
||||||
|
m := Me{
|
||||||
|
mask: 0x12345678_90abcdef,
|
||||||
|
}
|
||||||
|
buf := make([]byte, 65535)
|
||||||
|
for i := 1; i < 65536; i++ {
|
||||||
|
data := buf[:i]
|
||||||
|
_, err := rand.Read(data)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(m.xor(m.xor(data)), data) {
|
||||||
|
t.Fatal("unexpected xor at ", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"github.com/klauspost/compress/zstd"
|
"github.com/klauspost/compress/zstd"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@@ -20,6 +22,7 @@ func (m *Me) listen() (conn *net.UDPConn, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
logrus.Infoln("[listen] at", m.myend)
|
||||||
var mu sync.Mutex
|
var mu sync.Mutex
|
||||||
for i := 0; i < runtime.NumCPU()*4; i++ {
|
for i := 0; i < runtime.NumCPU()*4; i++ {
|
||||||
go m.listenthread(conn, &mu)
|
go m.listenthread(conn, &mu)
|
||||||
@@ -52,7 +55,6 @@ func (m *Me) listenthread(conn *net.UDPConn, mu *sync.Mutex) {
|
|||||||
}
|
}
|
||||||
p, ok := m.IsInPeer(packet.Src.String())
|
p, ok := m.IsInPeer(packet.Src.String())
|
||||||
logrus.Debugln("[listen] recv from endpoint", addr, "src", packet.Src, "dst", packet.Dst)
|
logrus.Debugln("[listen] recv from endpoint", addr, "src", packet.Src, "dst", packet.Dst)
|
||||||
// logrus.Debugln("[listen] recv:", hex.EncodeToString(lbf))
|
|
||||||
if !ok {
|
if !ok {
|
||||||
logrus.Warnln("[listen] packet from", packet.Src, "to", packet.Dst, "is refused")
|
logrus.Warnln("[listen] packet from", packet.Src, "to", packet.Dst, "is refused")
|
||||||
packet.Put()
|
packet.Put()
|
||||||
@@ -60,7 +62,7 @@ func (m *Me) listenthread(conn *net.UDPConn, mu *sync.Mutex) {
|
|||||||
}
|
}
|
||||||
if p.endpoint == nil || p.endpoint.String() != addr.String() {
|
if p.endpoint == nil || p.endpoint.String() != addr.String() {
|
||||||
logrus.Infoln("[listen] set endpoint of peer", p.peerip, "to", addr.String())
|
logrus.Infoln("[listen] set endpoint of peer", p.peerip, "to", addr.String())
|
||||||
p.endpoint = addr
|
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.endpoint)), unsafe.Pointer(addr))
|
||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
case p.IsToMe(packet.Dst):
|
case p.IsToMe(packet.Dst):
|
||||||
|
|||||||
@@ -18,7 +18,15 @@ func (m *Me) wait(data []byte) *head.Packet {
|
|||||||
if len(data) < 60 { // not a valid packet
|
if len(data) < 60 { // not a valid packet
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
bound := 256
|
||||||
|
endl := "..."
|
||||||
|
if len(data) < bound {
|
||||||
|
bound = len(data)
|
||||||
|
endl = "."
|
||||||
|
}
|
||||||
|
logrus.Debugln("[recv] data bytes", hex.EncodeToString(data[:bound]), endl)
|
||||||
data = m.xor(data)
|
data = m.xor(data)
|
||||||
|
logrus.Debugln("[recv] data xored", hex.EncodeToString(data[:bound]), endl)
|
||||||
flags := binary.LittleEndian.Uint16(data[10:12])
|
flags := binary.LittleEndian.Uint16(data[10:12])
|
||||||
if flags&0x8000 == 0x8000 { // not a valid packet
|
if flags&0x8000 == 0x8000 { // not a valid packet
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package link
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -98,8 +99,17 @@ func (l *Link) write(p *head.Packet, teatype uint8, additional, mtu uint16, data
|
|||||||
if peerep == nil {
|
if peerep == nil {
|
||||||
return 0, errors.New("[send] nil endpoint of " + p.Dst.String())
|
return 0, errors.New("[send] nil endpoint of " + p.Dst.String())
|
||||||
}
|
}
|
||||||
|
bound := 256
|
||||||
|
endl := "..."
|
||||||
|
if len(d) < bound {
|
||||||
|
bound = len(d)
|
||||||
|
endl = "."
|
||||||
|
}
|
||||||
logrus.Debugln("[send] write", len(d), "bytes data from ep", l.me.myep.LocalAddr(), "to", peerep, "offset:", fmt.Sprintf("%04x", offset))
|
logrus.Debugln("[send] write", len(d), "bytes data from ep", l.me.myep.LocalAddr(), "to", peerep, "offset:", fmt.Sprintf("%04x", offset))
|
||||||
n, err = l.me.myep.WriteToUDP(l.me.xor(d), peerep)
|
logrus.Debugln("[send] data bytes", hex.EncodeToString(d[:bound]), endl)
|
||||||
|
d = l.me.xor(d)
|
||||||
|
n, err = l.me.myep.WriteToUDP(d, peerep)
|
||||||
|
logrus.Debugln("[send] data xored", hex.EncodeToString(d[:bound]), endl)
|
||||||
cl()
|
cl()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
curve "github.com/fumiama/go-x25519"
|
curve "github.com/fumiama/go-x25519"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@@ -30,7 +31,7 @@ func TestTunnel(t *testing.T) {
|
|||||||
|
|
||||||
m := link.NewMe(&link.MyConfig{
|
m := link.NewMe(&link.MyConfig{
|
||||||
MyIPwithMask: "192.168.1.2/32",
|
MyIPwithMask: "192.168.1.2/32",
|
||||||
MyEndpoint: "127.0.0.1:1236",
|
MyEndpoint: "127.0.0.1:21236",
|
||||||
PrivateKey: selfpk.Private(),
|
PrivateKey: selfpk.Private(),
|
||||||
SrcPort: 1,
|
SrcPort: 1,
|
||||||
DstPort: 1,
|
DstPort: 1,
|
||||||
@@ -38,14 +39,14 @@ func TestTunnel(t *testing.T) {
|
|||||||
})
|
})
|
||||||
m.AddPeer(&link.PeerConfig{
|
m.AddPeer(&link.PeerConfig{
|
||||||
PeerIP: "192.168.1.3",
|
PeerIP: "192.168.1.3",
|
||||||
EndPoint: "127.0.0.1:1237",
|
EndPoint: "127.0.0.1:21237",
|
||||||
AllowedIPs: []string{"192.168.1.3/32"},
|
AllowedIPs: []string{"192.168.1.3/32"},
|
||||||
PubicKey: peerpk.Public(),
|
PubicKey: peerpk.Public(),
|
||||||
MTU: 4096,
|
MTU: 4096,
|
||||||
})
|
})
|
||||||
p := link.NewMe(&link.MyConfig{
|
p := link.NewMe(&link.MyConfig{
|
||||||
MyIPwithMask: "192.168.1.3/32",
|
MyIPwithMask: "192.168.1.3/32",
|
||||||
MyEndpoint: "127.0.0.1:1237",
|
MyEndpoint: "127.0.0.1:21237",
|
||||||
PrivateKey: peerpk.Private(),
|
PrivateKey: peerpk.Private(),
|
||||||
SrcPort: 1,
|
SrcPort: 1,
|
||||||
DstPort: 1,
|
DstPort: 1,
|
||||||
@@ -53,7 +54,7 @@ func TestTunnel(t *testing.T) {
|
|||||||
})
|
})
|
||||||
p.AddPeer(&link.PeerConfig{
|
p.AddPeer(&link.PeerConfig{
|
||||||
PeerIP: "192.168.1.2",
|
PeerIP: "192.168.1.2",
|
||||||
EndPoint: "127.0.0.1:1236",
|
EndPoint: "127.0.0.1:21236",
|
||||||
AllowedIPs: []string{"192.168.1.2/32"},
|
AllowedIPs: []string{"192.168.1.2/32"},
|
||||||
PubicKey: selfpk.Public(),
|
PubicKey: selfpk.Public(),
|
||||||
MTU: 4096,
|
MTU: 4096,
|
||||||
@@ -69,6 +70,8 @@ func TestTunnel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
tunnpeer.Start(1, 1, 4096)
|
tunnpeer.Start(1, 1, 4096)
|
||||||
|
|
||||||
|
time.Sleep(time.Second * 10) // wait link up
|
||||||
|
|
||||||
sendb := ([]byte)("1234")
|
sendb := ([]byte)("1234")
|
||||||
tunnme.Write(sendb)
|
tunnme.Write(sendb)
|
||||||
buf := make([]byte, 4)
|
buf := make([]byte, 4)
|
||||||
|
|||||||
Reference in New Issue
Block a user