-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathredirector.go
More file actions
160 lines (140 loc) · 3.66 KB
/
Copy pathredirector.go
File metadata and controls
160 lines (140 loc) · 3.66 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package redirector
import (
"bytes"
"context"
"fmt"
"io"
"net"
"reflect"
"strings"
"github.com/p4gefau1t/trojan-go/common"
"github.com/p4gefau1t/trojan-go/log"
)
type Dial func(net.Addr) (net.Conn, error)
func defaultDial(addr net.Addr) (net.Conn, error) {
return net.Dial("tcp", addr.String())
}
type Redirection struct {
Dial
RedirectTo net.Addr
InboundConn net.Conn
ClientIP string
}
type Redirector struct {
ctx context.Context
redirectionChan chan *Redirection
}
func (r *Redirector) Redirect(redirection *Redirection) {
select {
case r.redirectionChan <- redirection:
log.Debug("redirect request")
case <-r.ctx.Done():
log.Debug("exiting")
}
}
func injectForwardedHeader(inbound net.Conn, outbound net.Conn, clientIP string) error {
var headerBuf bytes.Buffer
buf := make([]byte, 4096)
for {
n, err := inbound.Read(buf)
if err != nil {
return err
}
headerBuf.Write(buf[:n])
if bytes.Contains(headerBuf.Bytes(), []byte("\r\n\r\n")) {
break
}
if headerBuf.Len() > 65536 {
return fmt.Errorf("headers too large")
}
}
headerBytes := headerBuf.Bytes()
idx := bytes.Index(headerBytes, []byte("\r\n\r\n"))
headers := headerBytes[:idx]
remaining := headerBytes[idx+4:]
headerStr := string(headers)
lines := strings.Split(headerStr, "\r\n")
xffFound := false
for i, line := range lines {
if strings.HasPrefix(strings.ToLower(line), "x-forwarded-for:") {
lines[i] = line + ", " + clientIP
xffFound = true
break
}
}
if !xffFound {
lines = append(lines, "X-Forwarded-For: "+clientIP)
}
lines = append(lines, "X-Real-IP: "+clientIP)
var out bytes.Buffer
for _, line := range lines {
out.WriteString(line)
out.WriteString("\r\n")
}
out.WriteString("\r\n")
out.Write(remaining)
_, err := outbound.Write(out.Bytes())
return err
}
func (r *Redirector) worker() {
for {
select {
case redirection := <-r.redirectionChan:
handle := func(redirection *Redirection) {
if redirection.InboundConn == nil || reflect.ValueOf(redirection.InboundConn).IsNil() {
log.Error("nil inbound conn")
return
}
defer redirection.InboundConn.Close()
if redirection.RedirectTo == nil || reflect.ValueOf(redirection.RedirectTo).IsNil() {
log.Error("nil redirection addr")
return
}
if redirection.Dial == nil {
redirection.Dial = defaultDial
}
log.Warn("redirecting connection from", redirection.InboundConn.RemoteAddr(), "to", redirection.RedirectTo.String())
outboundConn, err := redirection.Dial(redirection.RedirectTo)
if err != nil {
log.Error(common.NewError("failed to redirect to target address").Base(err))
return
}
defer outboundConn.Close()
if redirection.ClientIP != "" {
if err := injectForwardedHeader(redirection.InboundConn, outboundConn, redirection.ClientIP); err != nil {
log.Debug("failed to inject X-Forwarded-For header, using plain TCP forwarding:", err)
}
}
errChan := make(chan error, 2)
copyConn := func(a, b net.Conn) {
_, err := io.Copy(a, b)
errChan <- err
}
go copyConn(outboundConn, redirection.InboundConn)
go copyConn(redirection.InboundConn, outboundConn)
select {
case err := <-errChan:
if err != nil {
log.Error(common.NewError("failed to redirect").Base(err))
}
log.Info("redirection done")
case <-r.ctx.Done():
log.Debug("exiting")
return
}
}
go handle(redirection)
case <-r.ctx.Done():
log.Debug("shutting down redirector")
return
}
}
}
func NewRedirector(ctx context.Context) *Redirector {
r := &Redirector{
ctx: ctx,
redirectionChan: make(chan *Redirection, 64),
}
go r.worker()
return r
}