为了更好的调试,准备重构

Signed-off-by: dmy@lab <dmy@lab.com>
This commit is contained in:
dmy@lab 2015-10-24 16:07:23 +08:00
parent d4c3e2ada5
commit f0d343cebb
7 changed files with 69 additions and 40 deletions

View File

@ -8,10 +8,12 @@ var DestIP string
var DestPort int var DestPort int
var ListenPort int var ListenPort int
var EncryptSide string var EncryptSide string
var LogTo string
func init() { func init() {
flag.StringVar(&DestIP, "destip", "", "Destination IP") flag.StringVar(&DestIP, "destip", "", "Destination IP")
flag.StringVar(&EncryptSide, "encrypt", "", "Encrypt Side") flag.StringVar(&EncryptSide, "encrypt", "", "Encrypt Side")
flag.StringVar(&LogTo, "log", "stdout", "Log to where")
flag.IntVar(&DestPort, "destport", 0, "Destination Port") flag.IntVar(&DestPort, "destport", 0, "Destination Port")
flag.IntVar(&ListenPort, "listenport", 0, "Listen Port") flag.IntVar(&ListenPort, "listenport", 0, "Listen Port")
} }

View File

@ -22,8 +22,8 @@ func tunnel() {
} }
func main() { func main() {
fmt.Println("Hello World!")
log.LogTo("stdout", "INFO")
flag.Parse() flag.Parse()
fmt.Println("Hello World!")
log.LogTo(cli.LogTo, "INFO")
tunnel() tunnel()
} }

View File

@ -24,12 +24,29 @@ func serverBin(t *testing.T) {
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
} }
bytes := make([]byte, 1024*32) bytes := make([]byte, 1024*32)
n, err := conn.Read(bytes) /////<- 当发送端的cache大于4097时把这些注释掉
if err != nil { _bytes := make([]byte, 1024*32)
log.Error("Test Server read %", err.Error()) add := 0
break n := 0
for {
/////->
n, err := conn.Read(_bytes)
if err != nil {
log.Error("Test Server read %", err.Error())
break
}
////<-
copy(bytes[add:add+n], _bytes[:n])
add += n
if add == 4097 {
break
n = 4097
}
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())
if bytes[0] != nCount { if bytes[0] != nCount {
log.Error("package sequence not right. it's %d. should be %d", bytes[0], nCount) log.Error("package sequence not right. it's %d. should be %d", bytes[0], nCount)
@ -59,6 +76,7 @@ func serverBin(t *testing.T) {
} }
break break
} }
log.Info("Test Server read file %d, it should be %d", nBinByte, n-1) log.Info("Test Server read file %d, it should be %d", nBinByte, n-1)
for i := 0; i < nBinByte; i++ { for i := 0; i < nBinByte; i++ {
if binBytes[i] != bytes[i+1] { if binBytes[i] != bytes[i+1] {
@ -142,7 +160,7 @@ func clientBin(t *testing.T) {
log.Error("client not write enough bytes") log.Error("client not write enough bytes")
} }
// log.Info("Test Client write %s", string(binBytes[:n])) // log.Info("Test Client write %s", string(binBytes[:n]))
log.Info("Test Client write with count %d", nCount) log.Info("Test Client write %d bytes with count %d", n, nCount)
nCount++ nCount++
bytes := make([]byte, 1024*32) bytes := make([]byte, 1024*32)
n, err = conn.Read(bytes) n, err = conn.Read(bytes)
@ -190,7 +208,7 @@ func clientText(t *testing.T) {
} }
func TestTunnel(t *testing.T) { func TestTunnel(t *testing.T) {
log.LogTo("log.txt", "INFO") log.LogTo("log.txt", "DEBUG")
log.Info("Test Start testing.") log.Info("Test Start testing.")
go serverBin(t) go serverBin(t)
go clientBin(t) go clientBin(t)

49
tcp.go
View File

@ -62,7 +62,7 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string, encrypDirection stri
} }
}() }()
cache := make([]byte, 1024*128) //128kB cache := make([]byte, 1024*2) //128kB
for { for {
//构建Carrier //构建Carrier
revCarrier := &tscipher.Carrier{ revCarrier := &tscipher.Carrier{
@ -80,7 +80,6 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string, encrypDirection stri
log.Panic("Read panic. Tunnel id: %s. Remote Add: %s. Err:%s", id, src.RemoteAddr().String(), err) log.Panic("Read panic. Tunnel id: %s. Remote Add: %s. Err:%s", id, src.RemoteAddr().String(), err)
} }
log.Info("Reived %d bytes from %s. Tunnel: id %s", nByte, src.RemoteAddr().String(), id) log.Info("Reived %d bytes from %s. Tunnel: id %s", nByte, src.RemoteAddr().String(), id)
log.Debug("Reived %s %s", id, cache[:nByte])
sendCarrier := &tscipher.Carrier{ sendCarrier := &tscipher.Carrier{
dest, dest,
tscipher.NewCipher("XOR"), tscipher.NewCipher("XOR"),
@ -92,7 +91,6 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string, encrypDirection stri
} }
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)
log.Debug("Write %s %s", id, cache[:nByte])
if err != nil { if err != nil {
log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String()) log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String())
} }
@ -104,7 +102,6 @@ func (this *TransTCP) tunnelID() string {
nowString := time.Now().String() + strconv.Itoa(int(seed)) nowString := time.Now().String() + strconv.Itoa(int(seed))
atomic.AddInt32(&seed, 1) //避免多线程情况下获得的种子相同 atomic.AddInt32(&seed, 1) //避免多线程情况下获得的种子相同
md5Byte := md5.Sum(bytes.NewBufferString(nowString).Bytes()) md5Byte := md5.Sum(bytes.NewBufferString(nowString).Bytes())
// log.Info("seed %d %s", seed, hex.EncodeToString(md5Byte[:]))
return hex.EncodeToString(md5Byte[:]) return hex.EncodeToString(md5Byte[:])
} }
@ -116,27 +113,29 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer
} }
for { for {
if listenerConn, err := listener.Accept(); err == nil { if listenerConn, err := listener.Accept(); err == nil {
log.Info("Incoming %s", listenerConn.RemoteAddr().String()) go func() {
//创建到目标的连接 log.Info("Incoming %s", listenerConn.RemoteAddr().String())
destConn, err := this.createTCPClient(destIP, destPort) //创建到目标的连接
if err != nil { destConn, err := this.createTCPClient(destIP, destPort)
log.Panic("Failed to connect to destination. %s", err) if err != nil {
os.Exit(0) log.Panic("Failed to connect to destination. %s", err)
} os.Exit(0)
log.Info("Dial %s", destConn.RemoteAddr().String()) }
//tunnel model : [ -->>server ---- client -->> ](this is a tunnel) log.Info("Dial %s", destConn.RemoteAddr().String())
tunnelIDA := this.tunnelID() //tunnel model : [ -->>server ---- client -->> ](this is a tunnel)
tunnelIDB := this.tunnelID() tunnelIDA := this.tunnelID()
if clientOrServer == "client" { tunnelIDB := this.tunnelID()
go this.tunnel(listenerConn, destConn, tunnelIDA, "send") if clientOrServer == "client" {
go this.tunnel(destConn, listenerConn, tunnelIDB, "receive") go this.tunnel(listenerConn, destConn, tunnelIDA, "send")
log.Debug("tow tunnel created: %s %s %s %s", tunnelIDA, "send", tunnelIDB, "receive") go this.tunnel(destConn, listenerConn, tunnelIDB, "receive")
} log.Debug("two tunnel created: %s %s %s %s", tunnelIDA, "send", tunnelIDB, "receive")
if clientOrServer == "server" { }
go this.tunnel(listenerConn, destConn, tunnelIDA, "receive") if clientOrServer == "server" {
go this.tunnel(destConn, listenerConn, tunnelIDB, "send") go this.tunnel(listenerConn, destConn, tunnelIDA, "receive")
log.Debug("tow tunnel created: %s %s %s %s", tunnelIDA, "receive", tunnelIDB, "send") go this.tunnel(destConn, listenerConn, tunnelIDB, "send")
} log.Debug("two tunnel created: %s %s %s %s", tunnelIDA, "receive", tunnelIDB, "send")
}
}()
} else { } else {
log.Info("Failed to accept incoming connection. %s", err) log.Info("Failed to accept incoming connection. %s", err)

View File

@ -73,6 +73,9 @@ func UnwrapPackage(pacakge []byte) (packageSize int, data []byte, err error) {
} }
func SendData(carrier *Carrier, nByte int) (n int, err error) { func SendData(carrier *Carrier, nByte int) (n int, err error) {
if len(carrier.Cache) < nByte {
log.Panic("Cache of send is too small")
}
if carrier.Cipher == nil { if carrier.Cipher == nil {
n, err = carrier.Conn.Write(carrier.Cache[:nByte]) n, err = carrier.Conn.Write(carrier.Cache[:nByte])
return return
@ -113,7 +116,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
log.Info("read a complete package") log.Debug("read a complete package")
} else { } else {
gotSize := len(data) gotSize := len(data)
for { for {
@ -123,7 +126,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
return return
} }
wrapedPackage = carrier.Cache[:n] wrapedPackage = carrier.Cache[:n]
log.Info("got partial package size %d", n) log.Debug("got partial package size %d from %s", n, carrier.Conn.RemoteAddr().String())
_, data, err = UnwrapPackage(wrapedPackage) _, data, err = UnwrapPackage(wrapedPackage)
if err == nil { if err == nil {
n = 0 n = 0
@ -133,7 +136,8 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
gotSize += len(data) gotSize += len(data)
realData = append(realData, data...) realData = append(realData, data...)
if gotSize == packageSize { if gotSize == packageSize {
log.Info("got enough:. packageSize %d, real size %d", packageSize, gotSize) log.Debug("got enough:. packageSize %d, real size %d. not include header", packageSize, gotSize)
n = gotSize
break break
} }
} }
@ -144,7 +148,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
n = 0 n = 0
return return
} }
n = len(decrypted) // n = len(decrypted)
copy(carrier.Cache, decrypted) copy(carrier.Cache, decrypted)
return return
} }

View File

@ -7,20 +7,26 @@ 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
// err = nil
// 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)]
// decrypted[i] = data[i] // decrypted[i] = data[i]
} }
err = nil err = nil
return return
} }
func (this *XOR) Encrypt(data []byte) (encryped []byte, err error) { func (this *XOR) Encrypt(data []byte) (encryped []byte, err error) {
// encryped = data
// err = nil
// 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)]
// encryped[i] = data[i] // encryped[i] = data[i]
} }
err = nil err = nil
return return