parent
d4c3e2ada5
commit
f0d343cebb
|
|
@ -8,10 +8,12 @@ var DestIP string
|
|||
var DestPort int
|
||||
var ListenPort int
|
||||
var EncryptSide string
|
||||
var LogTo string
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&DestIP, "destip", "", "Destination IP")
|
||||
flag.StringVar(&EncryptSide, "encrypt", "", "Encrypt Side")
|
||||
flag.StringVar(&LogTo, "log", "stdout", "Log to where")
|
||||
flag.IntVar(&DestPort, "destport", 0, "Destination Port")
|
||||
flag.IntVar(&ListenPort, "listenport", 0, "Listen Port")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
.\TransX -destip 127.0.0.1 -destport 1082 -listenport 1080 -encrypt client
|
||||
.\TransX -destip 127.0.0.1 -destport 1082 -listenport 1080 -encrypt client
|
||||
4
main.go
4
main.go
|
|
@ -22,8 +22,8 @@ func tunnel() {
|
|||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello World!")
|
||||
log.LogTo("stdout", "INFO")
|
||||
flag.Parse()
|
||||
fmt.Println("Hello World!")
|
||||
log.LogTo(cli.LogTo, "INFO")
|
||||
tunnel()
|
||||
}
|
||||
|
|
|
|||
30
main_test.go
30
main_test.go
|
|
@ -24,12 +24,29 @@ func serverBin(t *testing.T) {
|
|||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
}
|
||||
|
||||
bytes := make([]byte, 1024*32)
|
||||
n, err := conn.Read(bytes)
|
||||
if err != nil {
|
||||
log.Error("Test Server read %", err.Error())
|
||||
break
|
||||
/////<- 当发送端的cache大于4097时,把这些注释掉
|
||||
_bytes := make([]byte, 1024*32)
|
||||
add := 0
|
||||
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())
|
||||
if 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
|
||||
}
|
||||
|
||||
log.Info("Test Server read file %d, it should be %d", nBinByte, n-1)
|
||||
for i := 0; i < nBinByte; i++ {
|
||||
if binBytes[i] != bytes[i+1] {
|
||||
|
|
@ -142,7 +160,7 @@ func clientBin(t *testing.T) {
|
|||
log.Error("client not write enough bytes")
|
||||
}
|
||||
// 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++
|
||||
bytes := make([]byte, 1024*32)
|
||||
n, err = conn.Read(bytes)
|
||||
|
|
@ -190,7 +208,7 @@ func clientText(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestTunnel(t *testing.T) {
|
||||
log.LogTo("log.txt", "INFO")
|
||||
log.LogTo("log.txt", "DEBUG")
|
||||
log.Info("Test Start testing.")
|
||||
go serverBin(t)
|
||||
go clientBin(t)
|
||||
|
|
|
|||
49
tcp.go
49
tcp.go
|
|
@ -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 {
|
||||
//构建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.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{
|
||||
dest,
|
||||
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)
|
||||
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 {
|
||||
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))
|
||||
atomic.AddInt32(&seed, 1) //避免多线程情况下获得的种子相同
|
||||
md5Byte := md5.Sum(bytes.NewBufferString(nowString).Bytes())
|
||||
// log.Info("seed %d %s", seed, hex.EncodeToString(md5Byte[:]))
|
||||
return hex.EncodeToString(md5Byte[:])
|
||||
}
|
||||
|
||||
|
|
@ -116,27 +113,29 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer
|
|||
}
|
||||
for {
|
||||
if listenerConn, err := listener.Accept(); err == nil {
|
||||
log.Info("Incoming %s", listenerConn.RemoteAddr().String())
|
||||
//创建到目标的连接
|
||||
destConn, err := this.createTCPClient(destIP, destPort)
|
||||
if err != nil {
|
||||
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)
|
||||
tunnelIDA := this.tunnelID()
|
||||
tunnelIDB := this.tunnelID()
|
||||
if clientOrServer == "client" {
|
||||
go this.tunnel(listenerConn, destConn, tunnelIDA, "send")
|
||||
go this.tunnel(destConn, listenerConn, tunnelIDB, "receive")
|
||||
log.Debug("tow tunnel created: %s %s %s %s", tunnelIDA, "send", tunnelIDB, "receive")
|
||||
}
|
||||
if clientOrServer == "server" {
|
||||
go this.tunnel(listenerConn, destConn, tunnelIDA, "receive")
|
||||
go this.tunnel(destConn, listenerConn, tunnelIDB, "send")
|
||||
log.Debug("tow tunnel created: %s %s %s %s", tunnelIDA, "receive", tunnelIDB, "send")
|
||||
}
|
||||
go func() {
|
||||
log.Info("Incoming %s", listenerConn.RemoteAddr().String())
|
||||
//创建到目标的连接
|
||||
destConn, err := this.createTCPClient(destIP, destPort)
|
||||
if err != nil {
|
||||
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)
|
||||
tunnelIDA := this.tunnelID()
|
||||
tunnelIDB := this.tunnelID()
|
||||
if clientOrServer == "client" {
|
||||
go this.tunnel(listenerConn, destConn, tunnelIDA, "send")
|
||||
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")
|
||||
go this.tunnel(destConn, listenerConn, tunnelIDB, "send")
|
||||
log.Debug("two tunnel created: %s %s %s %s", tunnelIDA, "receive", tunnelIDB, "send")
|
||||
}
|
||||
}()
|
||||
|
||||
} else {
|
||||
log.Info("Failed to accept incoming connection. %s", err)
|
||||
|
|
|
|||
|
|
@ -73,6 +73,9 @@ func UnwrapPackage(pacakge []byte) (packageSize int, data []byte, 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 {
|
||||
n, err = carrier.Conn.Write(carrier.Cache[:nByte])
|
||||
return
|
||||
|
|
@ -113,7 +116,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
|
||||
log.Info("read a complete package")
|
||||
log.Debug("read a complete package")
|
||||
} else {
|
||||
gotSize := len(data)
|
||||
for {
|
||||
|
|
@ -123,7 +126,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
|
|||
return
|
||||
}
|
||||
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)
|
||||
if err == nil {
|
||||
n = 0
|
||||
|
|
@ -133,7 +136,8 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
|
|||
gotSize += len(data)
|
||||
realData = append(realData, data...)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
@ -144,7 +148,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
|
|||
n = 0
|
||||
return
|
||||
}
|
||||
n = len(decrypted)
|
||||
// n = len(decrypted)
|
||||
copy(carrier.Cache, decrypted)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,20 +7,26 @@ type XOR struct {
|
|||
}
|
||||
|
||||
func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) {
|
||||
// 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)]
|
||||
// decrypted[i] = data[i]
|
||||
// decrypted[i] = data[i]
|
||||
}
|
||||
err = nil
|
||||
return
|
||||
}
|
||||
|
||||
func (this *XOR) Encrypt(data []byte) (encryped []byte, err error) {
|
||||
// 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)]
|
||||
// encryped[i] = data[i]
|
||||
// encryped[i] = data[i]
|
||||
}
|
||||
err = nil
|
||||
return
|
||||
|
|
|
|||
Loading…
Reference in New Issue