-
Notifications
You must be signed in to change notification settings - Fork 0
/
xavior.go
126 lines (114 loc) · 2.44 KB
/
xavior.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"crypto/sha512"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"time"
)
const BUFFER = 16 * 1024
const TIMEOUT = 30 * time.Second
const DEBUG = true
func printError(err error) {
if DEBUG {
fmt.Println(err, err.Error())
}
}
func computeKey(str string) []byte {
bytes := []byte(str)
hasher := sha512.New()
hasher.Write(bytes)
return hasher.Sum(nil)
}
func xorCopy(dst net.Conn, src net.Conn, key []byte) (err error) {
defer func(dst net.Conn) {
_ = dst.Close()
}(dst)
defer func(src net.Conn) {
_ = src.Close()
}(src)
buf := make([]byte, BUFFER)
xorKeyIndex := 0
for {
nr, er := src.Read(buf)
if nr > 0 {
goodBuf := buf[:nr]
for i := 0; i < len(goodBuf); i++ {
// don't just do `goodBuf[i] ^= key[i%len(key)]` cause `nr < len(buf)` happens
goodBuf[i] ^= key[xorKeyIndex]
xorKeyIndex = (xorKeyIndex + 1) % len(key)
}
nw, ew := dst.Write(goodBuf)
if nw < 0 || nr < nw {
nw = 0
if ew == nil {
err = errors.New("errInvalidWrite")
}
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
return err
}
func main() {
listenHost := flag.String("l", "127.0.0.1:1234", "listened address")
remoteHost := flag.String("r", "127.0.0.1:22", "remote address forwarding to")
sendPassword := flag.String("sp", "SendP@ssw0rd", "password when sending data")
receivePassword := flag.String("rp", "ReceiveP@ssw0rd", "password when receving data")
flag.Parse()
sendKey := computeKey(*sendPassword)
receiveKey := computeKey(*receivePassword)
if DEBUG {
println(fmt.Sprintf("sendKey :%x", sendKey))
println(fmt.Sprintf("receiveKey: %x", receiveKey))
}
log.Printf("Listening on %s ...", *listenHost)
listen, err := net.Listen("tcp", *listenHost)
if err != nil {
printError(err)
os.Exit(0)
}
for {
localConnection, err := listen.Accept()
if err != nil {
printError(err)
continue
}
log.Printf("Establishing connection to %s", *remoteHost)
remoteConnection, err := net.DialTimeout("tcp", *remoteHost, TIMEOUT)
if err != nil {
printError(err)
_ = localConnection.Close()
continue
}
go func() {
err := xorCopy(remoteConnection, localConnection, sendKey)
if err != nil {
printError(err)
}
}()
go func() {
err := xorCopy(localConnection, remoteConnection, receiveKey)
if err != nil {
printError(err)
}
}()
}
}