2015-10-08 20:40:36 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2015-10-10 18:28:19 +08:00
|
|
|
|
"bytes"
|
|
|
|
|
|
"crypto/md5"
|
|
|
|
|
|
"encoding/hex"
|
2015-10-16 23:21:38 +08:00
|
|
|
|
"github.com/TransX/log"
|
2015-10-08 23:11:07 +08:00
|
|
|
|
"github.com/TransX/tscipher"
|
2015-10-08 20:40:36 +08:00
|
|
|
|
"net"
|
2015-10-16 23:21:38 +08:00
|
|
|
|
"os"
|
2015-10-10 18:28:19 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
"time"
|
2015-10-08 20:40:36 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2015-10-18 17:44:56 +08:00
|
|
|
|
var seed int32
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
seed = 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-08 20:40:36 +08:00
|
|
|
|
type TransTCP struct {
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewTransTCP() *TransTCP {
|
2015-10-10 18:28:19 +08:00
|
|
|
|
|
2015-10-18 17:44:56 +08:00
|
|
|
|
return &TransTCP{}
|
2015-10-08 20:40:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-08 23:11:07 +08:00
|
|
|
|
func (this *TransTCP) createTCPClient(ip, port string) (conn net.Conn, err error) {
|
|
|
|
|
|
conn, err = net.Dial("tcp4", ip+":"+port)
|
2015-10-08 20:40:36 +08:00
|
|
|
|
if err == nil {
|
2015-10-08 23:11:07 +08:00
|
|
|
|
|
2015-10-08 20:40:36 +08:00
|
|
|
|
} else {
|
2015-10-08 23:11:07 +08:00
|
|
|
|
conn = nil
|
2015-10-08 20:40:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-08 23:11:07 +08:00
|
|
|
|
func (this *TransTCP) createTCPListener(ip, port string) (listen net.Listener, err error) {
|
2015-10-08 20:40:36 +08:00
|
|
|
|
listener, _err := net.Listen("tcp4", ip+":"+port)
|
|
|
|
|
|
if _err == nil {
|
2015-10-08 23:11:07 +08:00
|
|
|
|
listen = listener
|
|
|
|
|
|
err = nil
|
|
|
|
|
|
return
|
2015-10-08 20:40:36 +08:00
|
|
|
|
} else {
|
2015-10-08 23:11:07 +08:00
|
|
|
|
listen = nil
|
|
|
|
|
|
err = _err
|
2015-10-08 20:40:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-18 17:44:56 +08:00
|
|
|
|
func (this *TransTCP) tunnel(src, dest net.Conn, id string, encrypDirection string) { //单向的,从src发送到dest
|
2015-10-08 23:11:07 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
|
if src != nil {
|
|
|
|
|
|
src.Close()
|
2015-10-08 20:40:36 +08:00
|
|
|
|
}
|
2015-10-08 23:11:07 +08:00
|
|
|
|
if dest != nil {
|
|
|
|
|
|
dest.Close()
|
2015-10-08 20:40:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-08 23:11:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}()
|
2015-10-21 22:59:27 +08:00
|
|
|
|
cache := make([]byte, 1024*128) //128kB
|
2015-10-08 23:11:07 +08:00
|
|
|
|
for {
|
|
|
|
|
|
//构建Carrier
|
|
|
|
|
|
revCarrier := &tscipher.Carrier{
|
|
|
|
|
|
src,
|
2015-10-15 21:30:52 +08:00
|
|
|
|
tscipher.NewCipher("XOR"),
|
2015-10-08 23:11:07 +08:00
|
|
|
|
cache,
|
|
|
|
|
|
}
|
2015-10-18 17:44:56 +08:00
|
|
|
|
log.Debug("Encrypt Direction %s ID %s", encrypDirection, id)
|
|
|
|
|
|
if encrypDirection != "receive" {
|
|
|
|
|
|
revCarrier.Cipher = nil
|
|
|
|
|
|
log.Debug("Read not crypted. Tunnel: %s", id)
|
|
|
|
|
|
}
|
2015-10-08 23:11:07 +08:00
|
|
|
|
nByte, err := tscipher.ReceiveData(revCarrier)
|
|
|
|
|
|
if err != nil {
|
2015-10-17 23:14:43 +08:00
|
|
|
|
log.Panic("Read panic. Tunnel id: %s. Remote Add: %s. Err:%s", id, src.RemoteAddr().String(), err)
|
2015-10-08 23:11:07 +08:00
|
|
|
|
}
|
2015-10-21 22:59:27 +08:00
|
|
|
|
log.Info("Reived %d bytes from %s. Tunnel: id %s", nByte, src.RemoteAddr().String(), id)
|
2015-10-18 17:44:56 +08:00
|
|
|
|
log.Debug("Reived %s %s", id, cache[:nByte])
|
2015-10-08 23:11:07 +08:00
|
|
|
|
sendCarrier := &tscipher.Carrier{
|
|
|
|
|
|
dest,
|
2015-10-15 21:30:52 +08:00
|
|
|
|
tscipher.NewCipher("XOR"),
|
|
|
|
|
|
cache, //TODO:危险,cache的容量容易被不小心修改
|
2015-10-08 23:11:07 +08:00
|
|
|
|
}
|
2015-10-18 17:44:56 +08:00
|
|
|
|
if encrypDirection != "send" {
|
|
|
|
|
|
sendCarrier.Cipher = nil
|
|
|
|
|
|
log.Debug("Write not crypted. Tunnel: %s", id)
|
|
|
|
|
|
}
|
2015-10-22 15:07:51 +08:00
|
|
|
|
n, err := tscipher.SendData(sendCarrier, nByte)
|
|
|
|
|
|
log.Info("Write %d bytes from %s to %s. Tunnel: %s", n, dest.LocalAddr(), dest.RemoteAddr().String(), id)
|
2015-10-18 17:44:56 +08:00
|
|
|
|
log.Debug("Write %s %s", id, cache[:nByte])
|
2015-10-08 23:11:07 +08:00
|
|
|
|
if err != nil {
|
2015-10-17 23:14:43 +08:00
|
|
|
|
log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String())
|
2015-10-08 23:11:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2015-10-08 20:40:36 +08:00
|
|
|
|
|
2015-10-10 18:28:19 +08:00
|
|
|
|
func (this *TransTCP) tunnelID() string {
|
2015-10-18 17:44:56 +08:00
|
|
|
|
nowString := time.Now().String() + strconv.Itoa(int(seed))
|
|
|
|
|
|
atomic.AddInt32(&seed, 1) //避免多线程情况下获得的种子相同
|
2015-10-10 18:28:19 +08:00
|
|
|
|
md5Byte := md5.Sum(bytes.NewBufferString(nowString).Bytes())
|
2015-10-18 17:44:56 +08:00
|
|
|
|
// log.Info("seed %d %s", seed, hex.EncodeToString(md5Byte[:]))
|
2015-10-10 18:28:19 +08:00
|
|
|
|
return hex.EncodeToString(md5Byte[:])
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-18 17:44:56 +08:00
|
|
|
|
func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer string) {
|
2015-10-08 23:11:07 +08:00
|
|
|
|
listener, err := this.createTCPListener("0.0.0.0", listenPort)
|
|
|
|
|
|
if err != nil {
|
2015-10-17 23:14:43 +08:00
|
|
|
|
log.Panic("Failed to create listener. %s", err)
|
2015-10-16 23:21:38 +08:00
|
|
|
|
os.Exit(0)
|
2015-10-08 23:11:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
for {
|
|
|
|
|
|
if listenerConn, err := listener.Accept(); err == nil {
|
2015-10-17 23:14:43 +08:00
|
|
|
|
log.Info("Incoming %s", listenerConn.RemoteAddr().String())
|
2015-10-08 23:11:07 +08:00
|
|
|
|
//创建到目标的连接
|
|
|
|
|
|
destConn, err := this.createTCPClient(destIP, destPort)
|
|
|
|
|
|
if err != nil {
|
2015-10-17 23:14:43 +08:00
|
|
|
|
log.Panic("Failed to connect to destination. %s", err)
|
2015-10-16 23:21:38 +08:00
|
|
|
|
os.Exit(0)
|
2015-10-08 23:11:07 +08:00
|
|
|
|
}
|
2015-10-17 23:14:43 +08:00
|
|
|
|
log.Info("Dial %s", destConn.RemoteAddr().String())
|
2015-10-18 17:44:56 +08:00
|
|
|
|
//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")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-08 20:40:36 +08:00
|
|
|
|
} else {
|
2015-10-17 23:14:43 +08:00
|
|
|
|
log.Info("Failed to accept incoming connection. %s", err)
|
2015-10-08 23:11:07 +08:00
|
|
|
|
}
|
2015-10-08 20:40:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|