发现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 (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/TransX/log"
|
||||
"net"
|
||||
"os"
|
||||
|
|
@ -32,19 +33,20 @@ func serverBin(t *testing.T) {
|
|||
n := 0
|
||||
for {
|
||||
/////->
|
||||
n, err := conn.Read(_bytes)
|
||||
n, err = conn.Read(_bytes)
|
||||
if err != nil {
|
||||
log.Error("Test Server read %", err.Error())
|
||||
break
|
||||
}
|
||||
log.Info("Test Server read per time %d", n)
|
||||
////<-
|
||||
copy(bytes[add:add+n], _bytes[:n])
|
||||
add += n
|
||||
log.Info("add %d from %s", add, conn.RemoteAddr().String())
|
||||
if add == 4097 {
|
||||
break
|
||||
n = 4097
|
||||
break
|
||||
}
|
||||
log.Info("add %d", add)
|
||||
}
|
||||
////->
|
||||
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)
|
||||
|
||||
nCount++
|
||||
if n == 0 {
|
||||
log.Error("read n=0")
|
||||
os.Exit(-2)
|
||||
}
|
||||
binBytes := make([]byte, n-1)
|
||||
pos, _ := file.Seek(0, 1)
|
||||
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 Receive %s", string(bytes[:n]))
|
||||
|
|
@ -169,6 +179,7 @@ func clientBin(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
log.Info("Test Client Receive %s", bytes[:n])
|
||||
fmt.Println("Test Client Receive ", string(bytes[:n]))
|
||||
time.Sleep(time.Second * 2)
|
||||
conn.Close()
|
||||
log.Info("Test Client closed")
|
||||
|
|
|
|||
|
|
@ -17,9 +17,10 @@ type Cipher interface {
|
|||
}
|
||||
|
||||
type Carrier struct {
|
||||
Conn net.Conn
|
||||
Cipher Cipher
|
||||
Cache []byte
|
||||
Conn net.Conn
|
||||
Cipher Cipher
|
||||
Cache []byte
|
||||
AttachedTunnelID string
|
||||
}
|
||||
|
||||
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))
|
||||
if err == nil && packageSize == len(data) { //读到的是一个完整的包
|
||||
realData = data
|
||||
n = len(realData)
|
||||
log.Debug("read a complete package")
|
||||
} else {
|
||||
gotSize := len(data)
|
||||
|
|
@ -126,7 +128,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
|
|||
return
|
||||
}
|
||||
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)
|
||||
if err == nil {
|
||||
n = 0
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ type XOR struct {
|
|||
}
|
||||
|
||||
func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) {
|
||||
// decrypted = data
|
||||
// err = nil
|
||||
// return
|
||||
decrypted = data
|
||||
err = nil
|
||||
return
|
||||
decrypted = make([]byte, len(data))
|
||||
for i := 0; i < len(data); i++ {
|
||||
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) {
|
||||
// encryped = data
|
||||
// err = nil
|
||||
// return
|
||||
encryped = data
|
||||
err = nil
|
||||
return
|
||||
encryped = make([]byte, len(data))
|
||||
for i := 0; i < len(data); i++ {
|
||||
encryped[len(data)-i-1] = data[i] ^ this.key[i%len(this.key)]
|
||||
|
|
|
|||
|
|
@ -66,11 +66,12 @@ func (this *Tunnel) run() { //单向的,从src发送到dest
|
|||
src,
|
||||
tscipher.NewCipher("XOR"),
|
||||
cache,
|
||||
this.id,
|
||||
}
|
||||
log.Debug("Encrypt Direction %s ID %s", cipherDirection, id)
|
||||
// log.Debug("Encrypt Direction %s ID %s", cipherDirection, id)
|
||||
if cipherDirection != "receive" {
|
||||
revCarrier.Cipher = nil
|
||||
log.Debug("Read not crypted. Tunnel: %s", id)
|
||||
// log.Debug("Read not crypted. Tunnel: %s", id)
|
||||
}
|
||||
nByte, err := tscipher.ReceiveData(revCarrier)
|
||||
if err != nil {
|
||||
|
|
@ -81,10 +82,11 @@ func (this *Tunnel) run() { //单向的,从src发送到dest
|
|||
dest,
|
||||
tscipher.NewCipher("XOR"),
|
||||
cache, //TODO:危险,cache的容量容易被不小心修改
|
||||
this.id,
|
||||
}
|
||||
if cipherDirection != "send" {
|
||||
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)
|
||||
log.Info("Write %d bytes from %s to %s. Tunnel: %s", n, dest.LocalAddr(), dest.RemoteAddr().String(), id)
|
||||
|
|
|
|||
Loading…
Reference in New Issue