This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
tcp.go
93 lines (88 loc) · 2.31 KB
/
tcp.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
// tcp.go
package main
import (
"errors"
"log"
"net"
"strings"
)
func forwardLoop(fromConn *net.TCPConn, toConn *net.TCPConn, payload []byte, sub int) {
var len, err = fromConn.Read(payload)
if err == nil {
if CuteBi_XorCrypt_password != nil {
sub = CuteBi_XorCrypt(payload[:len], sub)
}
var _, err = toConn.Write(payload[:len])
if err == nil {
forwardLoop(fromConn, toConn, payload, sub)
} else {
log.Println("tcp-forward write failed.")
}
} else {
log.Println("tcp-forward read failed.")
}
}
func tcpForward(fromConn *net.TCPConn, toConn *net.TCPConn) {
var CuteBi_XorCrypt_passwordSub int = 0
var payload = make([]byte, 65536)
forwardLoop(fromConn, toConn, payload, CuteBi_XorCrypt_passwordSub)
fromConn.Close();
toConn.Close();
}
func getProxyHost(header []byte) (string, error) {
var found = hostRegex.FindSubmatch(header)
if len(found) >= 2 {
if CuteBi_XorCrypt_password != nil {
var host, err = CuteBi_decrypt_host(found[1])
if err != nil {
log.Println(err)
return "", err
} else {
return string(host), nil
}
} else {
return string(found[1]), nil
}
} else {
return "", errors.New("not found host in header")
}
}
func handleTcpSession(cConn *net.TCPConn, header []byte) {
var host, err = getProxyHost(header)
if err != nil {
log.Println("No proxy host: {" + string(header) + "}")
cConn.Write([]byte("No proxy host"))
} else {
log.Println("proxyHost: " + host)
if !(enable_dns_tcpOverUdp && strings.HasSuffix(host, ":53")) {
/* connecting to the destination host */
if !strings.Contains(host, ":") {
host += ":80"
}
var sAddr, resErr = net.ResolveTCPAddr("tcp", host)
if resErr != nil {
log.Println(resErr)
cConn.Write([]byte("Proxy address [" + host + "] ResolveTCP() error"))
} else {
var sConn, dialErr = net.DialTCP("tcp", nil, sAddr)
if dialErr != nil {
log.Println(dialErr)
cConn.Write([]byte("Proxy address [" + host + "] DialTCP() error"))
} else {
sConn.SetKeepAlive(true)
cConn.SetKeepAlive(true)
/* starting forward */
log.Println("Start tcpForward")
go tcpForward(cConn, sConn)
tcpForward(sConn, cConn)
sConn.Close()
}
}
} else {
// tcpDNS over udpDNS
dns_tcpOverUdp(cConn, host, header)
}
cConn.Close()
log.Println("A tcp client has been close")
}
}