74 lines
1.6 KiB
Go
74 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/TransX/log"
|
|
"github.com/TransX/tscipher"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func server(t *testing.T) {
|
|
listener, err := net.Listen("tcp4", "127.0.0.1:1244")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for {
|
|
conn, err := listener.Accept()
|
|
log.Info("Test Server Incoming", conn.RemoteAddr().String())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bytes := make([]byte, 32)
|
|
XOR := tscipher.NewXOR([]byte("fasdfasdf"))
|
|
n, err := conn.Read(bytes)
|
|
decryped, _ := XOR.Decrypt(bytes[:n])
|
|
copy(bytes, decryped[:n])
|
|
log.Info("Test Server Receive ", string(bytes[:n]))
|
|
encrypted, _ := XOR.Encrypt([]byte("OK"))
|
|
_, err = conn.Write(encrypted)
|
|
log.Info("Test Server write")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conn.Close()
|
|
log.Info("Test Server closed")
|
|
}
|
|
}
|
|
|
|
func client(t *testing.T) {
|
|
for {
|
|
conn, err := net.Dial("tcp4", "127.0.0.1:1200")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
XOR := tscipher.NewXOR([]byte("fasdfasdf"))
|
|
encrypted, _ := XOR.Encrypt([]byte("Client"))
|
|
conn.Write(encrypted)
|
|
log.Info("Test Client write")
|
|
bytes := make([]byte, 32)
|
|
n, err := conn.Read(bytes)
|
|
decryped, _ := XOR.Decrypt(bytes[:n])
|
|
copy(bytes, decryped[:n])
|
|
log.Info("Test Client read")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
log.Info("Test Client Receive ", string(bytes[:n]))
|
|
time.Sleep(time.Second * 2)
|
|
conn.Close()
|
|
log.Info("Test Client closed")
|
|
|
|
}
|
|
}
|
|
|
|
func TestTunnel(t *testing.T) {
|
|
log.LogTo("stdout", "FINEST")
|
|
// t.Log("Start testing.")
|
|
log.Info("Test Start testing.")
|
|
go server(t)
|
|
go client(t)
|
|
trans := NewTransTCP()
|
|
trans.Start("1200", "127.0.0.1", "1244")
|
|
}
|