2015-10-08 20:40:36 +08:00
|
|
|
package main
|
|
|
|
|
|
2015-10-08 23:11:07 +08:00
|
|
|
import (
|
2015-10-15 21:30:52 +08:00
|
|
|
"github.com/TransX/tscipher"
|
|
|
|
|
"log"
|
2015-10-08 20:40:36 +08:00
|
|
|
"net"
|
2015-10-15 21:30:52 +08:00
|
|
|
"os"
|
2015-10-08 23:11:07 +08:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
2015-10-08 20:40:36 +08:00
|
|
|
)
|
|
|
|
|
|
2015-10-08 23:11:07 +08:00
|
|
|
func server(t *testing.T) {
|
|
|
|
|
listener, err := net.Listen("tcp4", "127.0.0.1:1244")
|
|
|
|
|
if err != nil {
|
2015-10-08 20:40:36 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
for {
|
2015-10-08 23:11:07 +08:00
|
|
|
conn, err := listener.Accept()
|
2015-10-15 21:30:52 +08:00
|
|
|
log.Println("Test Server Incoming", conn.RemoteAddr().String())
|
2015-10-08 23:11:07 +08:00
|
|
|
if err != nil {
|
2015-10-08 20:40:36 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2015-10-08 23:11:07 +08:00
|
|
|
bytes := make([]byte, 32)
|
2015-10-15 21:30:52 +08:00
|
|
|
XOR := tscipher.NewXOR([]byte("fasdfasdf"))
|
2015-10-08 23:11:07 +08:00
|
|
|
n, err := conn.Read(bytes)
|
2015-10-15 21:30:52 +08:00
|
|
|
decryped, _ := XOR.Decrypt(bytes[:n])
|
|
|
|
|
copy(bytes, decryped[:n])
|
|
|
|
|
log.Println("Test Server Receive ", string(bytes[:n]))
|
|
|
|
|
encrypted, _ := XOR.Encrypt([]byte("OK"))
|
|
|
|
|
_, err = conn.Write(encrypted)
|
|
|
|
|
log.Println("Test Server write")
|
2015-10-08 23:11:07 +08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
2015-10-08 20:40:36 +08:00
|
|
|
}
|
|
|
|
|
conn.Close()
|
2015-10-15 21:30:52 +08:00
|
|
|
log.Println("Test Server closed")
|
2015-10-08 20:40:36 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-08 23:11:07 +08:00
|
|
|
func client(t *testing.T) {
|
|
|
|
|
for {
|
|
|
|
|
conn, err := net.Dial("tcp4", "127.0.0.1:1200")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
2015-10-08 20:40:36 +08:00
|
|
|
}
|
2015-10-15 21:30:52 +08:00
|
|
|
XOR := tscipher.NewXOR([]byte("fasdfasdf"))
|
|
|
|
|
encrypted, _ := XOR.Encrypt([]byte("Client"))
|
|
|
|
|
conn.Write(encrypted)
|
|
|
|
|
log.Println("Test Client write")
|
2015-10-08 23:11:07 +08:00
|
|
|
bytes := make([]byte, 32)
|
|
|
|
|
n, err := conn.Read(bytes)
|
2015-10-15 21:30:52 +08:00
|
|
|
decryped, _ := XOR.Decrypt(bytes[:n])
|
|
|
|
|
copy(bytes, decryped[:n])
|
|
|
|
|
log.Println("Test Client read")
|
2015-10-08 23:11:07 +08:00
|
|
|
if err != nil {
|
2015-10-08 20:40:36 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2015-10-15 21:30:52 +08:00
|
|
|
log.Println("Test Client Receive ", string(bytes[:n]))
|
2015-10-08 23:11:07 +08:00
|
|
|
time.Sleep(time.Second * 2)
|
2015-10-08 20:40:36 +08:00
|
|
|
conn.Close()
|
2015-10-15 21:30:52 +08:00
|
|
|
log.Println("Test Client closed")
|
2015-10-08 23:11:07 +08:00
|
|
|
|
2015-10-08 20:40:36 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-08 23:11:07 +08:00
|
|
|
func TestTunnel(t *testing.T) {
|
2015-10-15 21:30:52 +08:00
|
|
|
file, _ := os.Create("log.txt")
|
|
|
|
|
log.SetOutput(file)
|
2015-10-08 23:11:07 +08:00
|
|
|
// t.Log("Start testing.")
|
2015-10-15 21:30:52 +08:00
|
|
|
log.Println("Test Start testing.")
|
2015-10-08 23:11:07 +08:00
|
|
|
go server(t)
|
|
|
|
|
go client(t)
|
|
|
|
|
trans := NewTransTCP()
|
|
|
|
|
trans.Start("1200", "127.0.0.1", "1244")
|
|
|
|
|
}
|