重新组织目录结构
This commit is contained in:
8
protocol/constant.go
Normal file
8
protocol/constant.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package protocol
|
||||
|
||||
type Direction int
|
||||
|
||||
const (
|
||||
RECEIVE Direction = 0
|
||||
SEND Direction = 1
|
||||
)
|
||||
84
protocol/tcp.go
Normal file
84
protocol/tcp.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/TransX/log"
|
||||
"net"
|
||||
"os"
|
||||
)
|
||||
|
||||
type TransTCP struct {
|
||||
}
|
||||
|
||||
func NewTransTCP() *TransTCP {
|
||||
|
||||
return &TransTCP{}
|
||||
}
|
||||
|
||||
func (this *TransTCP) createTCPClient(ip, port string) (conn net.Conn, err error) {
|
||||
conn, err = net.Dial("tcp4", ip+":"+port)
|
||||
if err != nil {
|
||||
conn = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (this *TransTCP) createTCPClientWithRetry(ip, port string, retry int) (conn net.Conn, err error) {
|
||||
for i := 0; i < retry; i++ {
|
||||
c, e := this.createTCPClient(ip, port)
|
||||
if e == nil {
|
||||
return c, e
|
||||
}
|
||||
log.Error("Create Client Error: %s", e.Error())
|
||||
}
|
||||
//failed with retry
|
||||
return nil, errors.New(fmt.Sprintln("failed to create client after %d retry", retry))
|
||||
}
|
||||
|
||||
func (this *TransTCP) createTCPListener(ip, port string) (listen net.Listener, err error) {
|
||||
listener, _err := net.Listen("tcp4", ip+":"+port)
|
||||
if _err == nil {
|
||||
listen = listener
|
||||
err = nil
|
||||
return
|
||||
} else {
|
||||
listen = nil
|
||||
err = _err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer string) {
|
||||
listener, err := this.createTCPListener("0.0.0.0", listenPort)
|
||||
if err != nil {
|
||||
log.Panic("Failed to create listener. %s", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
for {
|
||||
if listenerConn, err := listener.Accept(); err == nil {
|
||||
go func() {
|
||||
log.Info("Incoming %s", listenerConn.RemoteAddr().String())
|
||||
//创建到目标的连接
|
||||
destConn, err := this.createTCPClientWithRetry(destIP, destPort, 3)
|
||||
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)
|
||||
if clientOrServer == "client" {
|
||||
go NewTunnel(listenerConn, destConn, SEND).run()
|
||||
go NewTunnel(destConn, listenerConn, RECEIVE).run()
|
||||
}
|
||||
if clientOrServer == "server" {
|
||||
go NewTunnel(listenerConn, destConn, RECEIVE).run()
|
||||
go NewTunnel(destConn, listenerConn, SEND).run()
|
||||
}
|
||||
}()
|
||||
|
||||
} else {
|
||||
log.Info("Failed to accept incoming connection. %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
96
protocol/tunnel.go
Normal file
96
protocol/tunnel.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"github.com/TransX/log"
|
||||
"github.com/TransX/tscipher"
|
||||
"net"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
var seed int32
|
||||
|
||||
func init() {
|
||||
seed = 0
|
||||
}
|
||||
|
||||
type Tunnel struct {
|
||||
id string
|
||||
src net.Conn
|
||||
dest net.Conn
|
||||
cipherDirection Direction
|
||||
}
|
||||
|
||||
func NewTunnel(src, dest net.Conn, cipherDirection Direction) *Tunnel {
|
||||
return &Tunnel{
|
||||
id: tunnelID(),
|
||||
src: src,
|
||||
dest: dest,
|
||||
cipherDirection: cipherDirection,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Tunnel) GetID(id string) string {
|
||||
return this.id
|
||||
}
|
||||
|
||||
func (this *Tunnel) SetID(id string) { //rarely used
|
||||
this.id = id
|
||||
}
|
||||
|
||||
//tunnel model : [ -->>server ---- client -->> ](this is a tunnel)
|
||||
func (this *Tunnel) run() { //单向的,从src发送到dest
|
||||
src := this.src
|
||||
dest := this.dest
|
||||
cipherDirection := this.cipherDirection
|
||||
id := this.id
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if src != nil {
|
||||
src.Close()
|
||||
}
|
||||
if dest != nil {
|
||||
dest.Close()
|
||||
}
|
||||
|
||||
}
|
||||
}()
|
||||
cache := make([]byte, 1024*4) //4kB
|
||||
//构建Carrier
|
||||
revCarrier := tscipher.NewCarrier(src, tscipher.NewCipher("XOR"), cache, this.id)
|
||||
sendCarrier := tscipher.NewCarrier(dest, tscipher.NewCipher("XOR"), cache, this.id)
|
||||
for {
|
||||
var nByte int
|
||||
var err error
|
||||
if cipherDirection != RECEIVE {
|
||||
revCarrier.Cipher = nil
|
||||
nByte, err = tscipher.RowReceiveData(revCarrier)
|
||||
} else {
|
||||
nByte, err = tscipher.ReceiveData(revCarrier)
|
||||
}
|
||||
if err != nil {
|
||||
log.Panic("Read panic. Tunnel id: %s. Remote Add: %s Local: %s. Err:%s", id, src.RemoteAddr().String(), src.LocalAddr().String(), err.Error())
|
||||
}
|
||||
log.Info("Reived %d bytes from %s. Tunnel: id %s", nByte, src.RemoteAddr().String(), id)
|
||||
if cipherDirection != SEND {
|
||||
sendCarrier.Cipher = nil
|
||||
}
|
||||
n, err := tscipher.SendData(sendCarrier, nByte)
|
||||
if err != nil {
|
||||
log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String())
|
||||
}
|
||||
log.Info("Write %d bytes from %s to %s. Tunnel: %s . 18 bytes %x", n, dest.LocalAddr(), dest.RemoteAddr().String(), id, sendCarrier.Cache[:18])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func tunnelID() string {
|
||||
nowString := time.Now().String() + strconv.Itoa(int(seed))
|
||||
atomic.AddInt32(&seed, 1) //避免多线程情况下获得的种子相同
|
||||
md5Byte := md5.Sum(bytes.NewBufferString(nowString).Bytes())
|
||||
return hex.EncodeToString(md5Byte[:])
|
||||
}
|
||||
Reference in New Issue
Block a user