1.添加cli

2.采用log4go
3.消除XOR的一个bug

Signed-off-by: dmy@lab <dmy@lab.com>
This commit is contained in:
dmy@lab 2015-10-17 23:14:43 +08:00
parent 3f37565c5f
commit 3be197ebba
7 changed files with 51 additions and 22 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
TransX.exe
TransX.exe
TransX

15
cli/cli.go Normal file
View File

@ -0,0 +1,15 @@
package cli
import (
"flag"
)
var DestIP string
var DestPort int
var ListenPort int
func init() {
flag.StringVar(&DestIP, "destip", "", "Destination IP")
flag.IntVar(&DestPort, "desport", 0, "Destination Port")
flag.IntVar(&ListenPort, "listenport", 0, "Listen Port")
}

18
main.go
View File

@ -2,17 +2,27 @@
package main
import (
"flag"
"fmt"
"github.com/TransX/cli"
"github.com/TransX/log"
"strconv"
)
func Tunnel() {
func tunnel() {
trans := NewTransTCP()
trans.Start("1200", "192.168.0.120", "8118")
if cli.DestPort != 0 {
log.Info("Listening on 127.0.0.1:%d. Forward %s:%d",cli.ListenPort, cli.DestIP, cli.DestPort)
trans.Start(strconv.Itoa(cli.ListenPort), cli.DestIP, strconv.Itoa(cli.DestPort))
} else {
trans.Start("1200", "192.168.0.120", "8118")
}
}
func main() {
fmt.Println("Hello World!")
log.LogTo("stdout", "FINEST")
Tunnel()
log.LogTo("stdout", "INFO")
flag.Parse()
tunnel()
}

View File

@ -15,7 +15,7 @@ func server(t *testing.T) {
}
for {
conn, err := listener.Accept()
log.Info("Test Server Incoming", conn.RemoteAddr().String())
log.Info("Test Server Incoming %s", conn.RemoteAddr().String())
if err != nil {
t.Fatal(err)
}
@ -24,7 +24,7 @@ func server(t *testing.T) {
n, err := conn.Read(bytes)
decryped, _ := XOR.Decrypt(bytes[:n])
copy(bytes, decryped[:n])
log.Info("Test Server Receive ", string(bytes[:n]))
log.Info("Test Server Receive %s", string(bytes[:n]))
encrypted, _ := XOR.Encrypt([]byte("OK"))
_, err = conn.Write(encrypted)
log.Info("Test Server write")
@ -45,7 +45,7 @@ func client(t *testing.T) {
XOR := tscipher.NewXOR([]byte("fasdfasdf"))
encrypted, _ := XOR.Encrypt([]byte("Client"))
conn.Write(encrypted)
log.Info("Test Client write")
log.Info("Test Client write %s",string(encrypted))
bytes := make([]byte, 32)
n, err := conn.Read(bytes)
decryped, _ := XOR.Decrypt(bytes[:n])
@ -54,7 +54,7 @@ func client(t *testing.T) {
if err != nil {
t.Fatal(err)
}
log.Info("Test Client Receive ", string(bytes[:n]))
log.Info("Test Client Receive")
time.Sleep(time.Second * 2)
conn.Close()
log.Info("Test Client closed")
@ -63,7 +63,7 @@ func client(t *testing.T) {
}
func TestTunnel(t *testing.T) {
log.LogTo("stdout", "FINEST")
log.LogTo("stdout", "INFO")
// t.Log("Start testing.")
log.Info("Test Start testing.")
go server(t)

19
tcp.go
View File

@ -69,18 +69,19 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string) {
}
nByte, err := tscipher.ReceiveData(revCarrier)
if err != nil {
log.Panic("Read panic", id, err, src.RemoteAddr().String())
log.Panic("Read panic. Tunnel id: %s. Remote Add: %s. Err:%s", id, src.RemoteAddr().String(), err)
}
log.Info("Reived ", nByte, "bytes:", id, string(cache[:nByte]))
log.Info("Reived %d bytes. Tunnel: id %s", nByte,id)
log.Debug("Reived %s",cache[:nByte])
sendCarrier := &tscipher.Carrier{
dest,
tscipher.NewCipher("XOR"),
cache, //TODO:危险cache的容量容易被不小心修改
}
_, err = tscipher.SendData(sendCarrier, nByte)
log.Info("Write")
log.Info("Write %d bytes. Tunnel: %s",nByte,id)
if err != nil {
log.Panic("Write panic", id, err, dest.RemoteAddr().String())
log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String())
}
}
@ -97,23 +98,23 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string) {
listener, err := this.createTCPListener("0.0.0.0", listenPort)
if err != nil {
log.Panic("Failed to create listener.", err)
log.Panic("Failed to create listener. %s", err)
os.Exit(0)
}
for {
if listenerConn, err := listener.Accept(); err == nil {
log.Info("Incoming ", listenerConn.RemoteAddr().String())
log.Info("Incoming %s", listenerConn.RemoteAddr().String())
//创建到目标的连接
destConn, err := this.createTCPClient(destIP, destPort)
if err != nil {
log.Panic("Failed to connect to destination.", err)
log.Panic("Failed to connect to destination. %s", err)
os.Exit(0)
}
log.Info("Dial", destConn.RemoteAddr().String())
log.Info("Dial %s", destConn.RemoteAddr().String())
go this.tunnel(listenerConn, destConn, this.tunnelID())
go this.tunnel(destConn, listenerConn, this.tunnelID())
} else {
log.Info("Failed to accept incoming connection.", err)
log.Info("Failed to accept incoming connection. %s", err)
}
}
}

View File

@ -44,10 +44,8 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
n = 0
return
}
//TODO:err
decrypted, err := carrier.Cipher.Decrypt(carrier.Cache[:n])
copy(carrier.Cache, decrypted[:n])
if err != nil {
n = 0
return

View File

@ -10,7 +10,9 @@ func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) {
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] ^ this.key[i%len(this.key)]
}
// copy(decrypted,data)
err = nil
return
}
@ -18,8 +20,10 @@ func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) {
func (this *XOR) Encrypt(data []byte) (encryped []byte, err error) {
encryped = make([]byte, len(data))
for i := 0; i < len(data); i++ {
encryped[i] = data[len(data)-i-1] ^ this.key[i%len(this.key)]
encryped[len(data)-i-1] = data[i] ^ this.key[i%len(this.key)]
// encryped[i] = data[i] ^ this.key[i%len(this.key)]
}
// copy(encryped,data)
err = nil
return
}