发现bug了,接受的时候并不是按发送的数据块接受的,所以接受处理有错位的地方。
Signed-off-by: dmy@lab <dmy@lab.com>
This commit is contained in:
parent
317743fed8
commit
c848299a38
19
main_test.go
19
main_test.go
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"github.com/TransX/log"
|
"github.com/TransX/log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -32,19 +33,20 @@ func serverBin(t *testing.T) {
|
||||||
n := 0
|
n := 0
|
||||||
for {
|
for {
|
||||||
/////->
|
/////->
|
||||||
n, err := conn.Read(_bytes)
|
n, err = conn.Read(_bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Test Server read %", err.Error())
|
log.Error("Test Server read %", err.Error())
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
log.Info("Test Server read per time %d", n)
|
||||||
////<-
|
////<-
|
||||||
copy(bytes[add:add+n], _bytes[:n])
|
copy(bytes[add:add+n], _bytes[:n])
|
||||||
add += n
|
add += n
|
||||||
|
log.Info("add %d from %s", add, conn.RemoteAddr().String())
|
||||||
if add == 4097 {
|
if add == 4097 {
|
||||||
break
|
|
||||||
n = 4097
|
n = 4097
|
||||||
|
break
|
||||||
}
|
}
|
||||||
log.Info("add %d", add)
|
|
||||||
}
|
}
|
||||||
////->
|
////->
|
||||||
log.Info("Test Server read %d bytes from %s", n, conn.RemoteAddr().String())
|
log.Info("Test Server read %d bytes from %s", n, conn.RemoteAddr().String())
|
||||||
|
|
@ -53,8 +55,11 @@ func serverBin(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Test Server receive with count %d", nCount)
|
log.Info("Test Server receive with count %d", nCount)
|
||||||
|
|
||||||
nCount++
|
nCount++
|
||||||
|
if n == 0 {
|
||||||
|
log.Error("read n=0")
|
||||||
|
os.Exit(-2)
|
||||||
|
}
|
||||||
binBytes := make([]byte, n-1)
|
binBytes := make([]byte, n-1)
|
||||||
pos, _ := file.Seek(0, 1)
|
pos, _ := file.Seek(0, 1)
|
||||||
log.Info("Test Server file pos %d", pos)
|
log.Info("Test Server file pos %d", pos)
|
||||||
|
|
@ -84,6 +89,11 @@ func serverBin(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
////
|
||||||
|
f, _ := os.Create("server.bin")
|
||||||
|
f.Write(bytes[1 : nBinByte+1])
|
||||||
|
f.Close()
|
||||||
|
////
|
||||||
log.Info("Test Server. All Matches.")
|
log.Info("Test Server. All Matches.")
|
||||||
|
|
||||||
// log.Info("Test Server Receive %s", string(bytes[:n]))
|
// log.Info("Test Server Receive %s", string(bytes[:n]))
|
||||||
|
|
@ -169,6 +179,7 @@ func clientBin(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
log.Info("Test Client Receive %s", bytes[:n])
|
log.Info("Test Client Receive %s", bytes[:n])
|
||||||
|
fmt.Println("Test Client Receive ", string(bytes[:n]))
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
conn.Close()
|
conn.Close()
|
||||||
log.Info("Test Client closed")
|
log.Info("Test Client closed")
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,10 @@ type Cipher interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Carrier struct {
|
type Carrier struct {
|
||||||
Conn net.Conn
|
Conn net.Conn
|
||||||
Cipher Cipher
|
Cipher Cipher
|
||||||
Cache []byte
|
Cache []byte
|
||||||
|
AttachedTunnelID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCipher(cipherName string) (cipher Cipher) {
|
func NewCipher(cipherName string) (cipher Cipher) {
|
||||||
|
|
@ -116,6 +117,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
|
||||||
// log.Info("packageSize %d data size %d", packageSize, len(data))
|
// log.Info("packageSize %d data size %d", packageSize, len(data))
|
||||||
if err == nil && packageSize == len(data) { //读到的是一个完整的包
|
if err == nil && packageSize == len(data) { //读到的是一个完整的包
|
||||||
realData = data
|
realData = data
|
||||||
|
n = len(realData)
|
||||||
log.Debug("read a complete package")
|
log.Debug("read a complete package")
|
||||||
} else {
|
} else {
|
||||||
gotSize := len(data)
|
gotSize := len(data)
|
||||||
|
|
@ -126,7 +128,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
wrapedPackage = carrier.Cache[:n]
|
wrapedPackage = carrier.Cache[:n]
|
||||||
log.Debug("got partial package size %d from %s", n, carrier.Conn.RemoteAddr().String())
|
log.Debug("got partial package size %d from %s ID: %s", n, carrier.Conn.RemoteAddr().String(), carrier.AttachedTunnelID)
|
||||||
_, data, err = UnwrapPackage(wrapedPackage)
|
_, data, err = UnwrapPackage(wrapedPackage)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
n = 0
|
n = 0
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,9 @@ type XOR struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) {
|
func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) {
|
||||||
// decrypted = data
|
decrypted = data
|
||||||
// err = nil
|
err = nil
|
||||||
// return
|
return
|
||||||
decrypted = make([]byte, len(data))
|
decrypted = make([]byte, len(data))
|
||||||
for i := 0; i < len(data); i++ {
|
for i := 0; i < len(data); i++ {
|
||||||
decrypted[i] = data[len(data)-i-1] ^ this.key[i%len(this.key)]
|
decrypted[i] = data[len(data)-i-1] ^ this.key[i%len(this.key)]
|
||||||
|
|
@ -20,9 +20,9 @@ func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *XOR) Encrypt(data []byte) (encryped []byte, err error) {
|
func (this *XOR) Encrypt(data []byte) (encryped []byte, err error) {
|
||||||
// encryped = data
|
encryped = data
|
||||||
// err = nil
|
err = nil
|
||||||
// return
|
return
|
||||||
encryped = make([]byte, len(data))
|
encryped = make([]byte, len(data))
|
||||||
for i := 0; i < len(data); i++ {
|
for i := 0; i < len(data); i++ {
|
||||||
encryped[len(data)-i-1] = data[i] ^ this.key[i%len(this.key)]
|
encryped[len(data)-i-1] = data[i] ^ this.key[i%len(this.key)]
|
||||||
|
|
|
||||||
|
|
@ -66,11 +66,12 @@ func (this *Tunnel) run() { //单向的,从src发送到dest
|
||||||
src,
|
src,
|
||||||
tscipher.NewCipher("XOR"),
|
tscipher.NewCipher("XOR"),
|
||||||
cache,
|
cache,
|
||||||
|
this.id,
|
||||||
}
|
}
|
||||||
log.Debug("Encrypt Direction %s ID %s", cipherDirection, id)
|
// log.Debug("Encrypt Direction %s ID %s", cipherDirection, id)
|
||||||
if cipherDirection != "receive" {
|
if cipherDirection != "receive" {
|
||||||
revCarrier.Cipher = nil
|
revCarrier.Cipher = nil
|
||||||
log.Debug("Read not crypted. Tunnel: %s", id)
|
// log.Debug("Read not crypted. Tunnel: %s", id)
|
||||||
}
|
}
|
||||||
nByte, err := tscipher.ReceiveData(revCarrier)
|
nByte, err := tscipher.ReceiveData(revCarrier)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -81,10 +82,11 @@ func (this *Tunnel) run() { //单向的,从src发送到dest
|
||||||
dest,
|
dest,
|
||||||
tscipher.NewCipher("XOR"),
|
tscipher.NewCipher("XOR"),
|
||||||
cache, //TODO:危险,cache的容量容易被不小心修改
|
cache, //TODO:危险,cache的容量容易被不小心修改
|
||||||
|
this.id,
|
||||||
}
|
}
|
||||||
if cipherDirection != "send" {
|
if cipherDirection != "send" {
|
||||||
sendCarrier.Cipher = nil
|
sendCarrier.Cipher = nil
|
||||||
log.Debug("Write not crypted. Tunnel: %s", id)
|
// log.Debug("Write not crypted. Tunnel: %s", id)
|
||||||
}
|
}
|
||||||
n, err := tscipher.SendData(sendCarrier, nByte)
|
n, err := tscipher.SendData(sendCarrier, nByte)
|
||||||
log.Info("Write %d bytes from %s to %s. Tunnel: %s", n, dest.LocalAddr(), dest.RemoteAddr().String(), id)
|
log.Info("Write %d bytes from %s to %s. Tunnel: %s", n, dest.LocalAddr(), dest.RemoteAddr().String(), id)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue