写到一半发现之前的代码逻辑有问题

Signed-off-by: dmy@lab <dmy@lab.com>
This commit is contained in:
dmy@lab 2015-10-08 20:40:36 +08:00
parent 57e17afaf3
commit fc533656a0
8 changed files with 226 additions and 0 deletions

BIN
TransX.exe Normal file

Binary file not shown.

20
cipher/chacha.go Normal file
View File

@ -0,0 +1,20 @@
package cipher
type ChaCha struct {
}
func (*ChaCha) Decrypt(data []byte) (decrypted []byte, err error) {
decrypted = data
err = nil
return
}
func (*ChaCha) Encrypt(data []byte) (encryped []byte, err error) {
encryped = data
err = nil
return
}
func NewChaCha() (cipher Cipher) {
return &ChaCha{}
}

23
cipher/cipher.go Normal file
View File

@ -0,0 +1,23 @@
package cipher
import (
"net"
)
type Cipher interface {
Decrypt(data []byte) (decrypted []byte, err error)
Encrypt(data []byte) (encryped []byte, err error)
}
type Carrier struct {
Conn net.Conn
Cipher Cipher
Cache []byte
}
func NewCipher(cipherName string) (cipher Cipher) {
if cipherName == "default" {
return NewChaCha()
}
return nil //TODO:临时这样处理
}

15
cipher/communication.go Normal file
View File

@ -0,0 +1,15 @@
package cipher
import (
// "github.com/TransX/cipher"
)
func SendData(carrier *Carrier, nByte int) (n int, err error) {
n, err = carrier.Conn.Write(carrier.Cache[:nByte])
return
}
func ReceiveData(carrier *Carrier) (n int, err error) {
n, err = carrier.Conn.Read(carrier.Cache)
return
}

10
main.go Normal file
View File

@ -0,0 +1,10 @@
// main.go
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello World!")
}

49
main_test.go Normal file
View File

@ -0,0 +1,49 @@
package main
import(
"testing"
"net"
)
func server(){
listener,err=net.Listen("tcp4","0.0.0.0:1234")
if err!=nil{
t.Fatal(err)
}
for {
conn,err:=listener.Accept()
if err!=nil{
t.Fatal(err)
}
bytes:=make([]byte,32)
n,err:=conn.Read(bytes)
t.Log("Server Receive ",bytes[:n])
_,err:=conn.Write([]byte("OK"))
if err!=nil{
t.Fatail(err)
}
conn.Close()
}
}
func client(){
conn,err:=net.Dial("tcp4","0.0.0.0:1234")
if err!=nil{
t.Fatail(err)
}
for{
conn.Write([]byte("Client"))
bytes:=make([]byte,32)
n,err:=conn.Read(byte)
if err!=nil{
t.Fatal(err)
}
t.Log("Client Receive ",bytes[:n])
conn.Close()
}
}
func TestTunnel(t *testing.T)
{
StartTunnel("0.0.0.0","1200","0.0.0.0",)
}

91
tcp.go Normal file
View File

@ -0,0 +1,91 @@
package main
import (
"github.com/TransX/cipher"
"io"
"log"
"net"
)
type TransTCP struct {
// serverConn *net.Conn
clientConn net.Conn
listener net.Listener
// cache []byte
}
func NewTransTCP() *TransTCP {
return &TransTCP{}
}
func (this *TransTCP) CreateTCPClient(ip, port string) (err error) {
conn, err := net.Dial("tcp4", ip+":"+port)
if err == nil {
this.clientConn = conn
} else {
this.clientConn = nil
}
return
}
func (this *TransTCP) CreateTCPListener(ip, port string) (err error) {
listener, _err := net.Listen("tcp4", ip+":"+port)
if _err == nil {
this.listener = listener
return nil
} else {
return _err
}
return
}
func (this *TransTCP) Start(remoteAddr chan net.Addr) (err error) {
cache := make([]byte, 1024*128) //128kB
// this.cache = cache
for {
if listenerConn, _err := this.listener.Accept(); err == nil {
remoteAddr <- listenerConn.RemoteAddr()
//构建Carrier
revCarrier := &cipher.Carrier{
listenerConn,
cipher.NewCipher("default"),
cache,
}
sendCarrier := &cipher.Carrier{
this.clientConn,
cipher.NewCipher("default"),
cache,
}
go func() {
for {
nByte, _err := cipher.ReceiveData(revCarrier)
if _err != nil {
if _err == io.EOF {
revCarrier.Conn.Close()
sendCarrier.Conn.Close()
log.Println("EOF")
return
} else {
log.Println(revCarrier.Conn.RemoteAddr().String(), _err)
return //TODO:还需要处理
}
}
log.Println("Received ", nByte, " From ", revCarrier.Conn.RemoteAddr().String())
nByte, _err = cipher.SendData(sendCarrier, nByte)
if _err == nil {
log.Println("Send ", nByte, " To ", sendCarrier.Conn.RemoteAddr().String())
return
} else {
log.Println(sendCarrier.Conn.RemoteAddr().String(), _err)
}
}
}()
} else {
err = _err
}
}
return nil
}

18
tunnel.go Normal file
View File

@ -0,0 +1,18 @@
package main
func StartTunnel(sourceIP, desIP, desPort string) {
remoteAddr := make(chan net.Addr)
transSrcToDes := NewTransTCP()
transSrcToDes.CreateTCPListener("0.0.0.0", desPort)
transSrcToDes.CreateTCPClient(desIP, desPort)
go transSrcToDes.Start(remoteAddr)
sourcePort := <-remoteAddr
transDesToSrc := NewTransTCP()
transDesToSrc.CreateTCPListener("0.0.0.0", sourcePort)
transSrcToDes.CreateTCPClient(sourceIP, sourcePort)
go transDesToSrc.Start()
c := make(chan byte)
<-c
}