1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-05 07:50:24 +08:00

feat(p2p): support tcp protocol

This commit is contained in:
源文雨
2024-07-16 21:38:45 +09:00
parent 17e1f6cac9
commit 739cf863f1
19 changed files with 393 additions and 26 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/sirupsen/logrus"
_ "github.com/fumiama/WireGold/gold/p2p/tcp" // support tcp connection
_ "github.com/fumiama/WireGold/gold/p2p/udp" // support udp connection
"github.com/fumiama/WireGold/gold/head"

View File

@@ -16,7 +16,7 @@ import (
"github.com/fumiama/WireGold/helper"
)
func testTunnel(t *testing.T, isplain bool, pshk *[32]byte) {
func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte) {
selfpk, err := curve.New(nil)
if err != nil {
panic(err)
@@ -33,6 +33,7 @@ func testTunnel(t *testing.T, isplain bool, pshk *[32]byte) {
m := link.NewMe(&link.MyConfig{
MyIPwithMask: "192.168.1.2/32",
MyEndpoint: "127.0.0.1:0",
Network: nw,
PrivateKey: selfpk.Private(),
SrcPort: 1,
DstPort: 1,
@@ -43,6 +44,7 @@ func testTunnel(t *testing.T, isplain bool, pshk *[32]byte) {
p := link.NewMe(&link.MyConfig{
MyIPwithMask: "192.168.1.3/32",
MyEndpoint: "127.0.0.1:0",
Network: nw,
PrivateKey: peerpk.Private(),
SrcPort: 1,
DstPort: 1,
@@ -146,20 +148,36 @@ func testTunnel(t *testing.T, isplain bool, pshk *[32]byte) {
}
}
func TestTunnel(t *testing.T) {
func TestTunnelUDP(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logFormat{enableColor: false})
testTunnel(t, true, nil) // test plain text
testTunnel(t, "udp", true, nil) // test plain text
testTunnel(t, false, nil) // test normal
testTunnel(t, "udp", false, nil) // test normal
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
panic(err)
}
testTunnel(t, false, &buf) // test preshared
testTunnel(t, "udp", false, &buf) // test preshared
}
func TestTunnelTCP(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logFormat{enableColor: false})
testTunnel(t, "tcp", true, nil) // test plain text
testTunnel(t, "tcp", false, nil) // test normal
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
panic(err)
}
testTunnel(t, "tcp", false, &buf) // test preshared
}
// logFormat specialize for go-cqhttp

View File

@@ -9,6 +9,7 @@ import (
curve "github.com/fumiama/go-x25519"
"github.com/sirupsen/logrus"
_ "github.com/fumiama/WireGold/gold/p2p/tcp" // support tcp connection
_ "github.com/fumiama/WireGold/gold/p2p/udp" // support udp connection
"github.com/fumiama/WireGold/config"
@@ -36,7 +37,7 @@ func NewWireGold(c *config.Config) (wg WG, err error) {
}
n := copy(wg.key[:], base14.Decode(k))
if n != 32 {
err = errors.New("private key length is not 32")
err = errors.New("private key length != 32, got " + strconv.Itoa(n))
return
}