-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathoutbound.go
More file actions
165 lines (145 loc) · 4.58 KB
/
outbound.go
File metadata and controls
165 lines (145 loc) · 4.58 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
161
162
163
164
165
package xray
import (
"context"
goerrors "errors"
"io"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/common/task"
"github.com/xtls/xray-core/transport"
"github.com/xtls/xray-core/transport/internet"
"go.uber.org/zap"
)
// meteredOutbound implements outbound.Handler. It replaces xray's default
// freedom outbound so we can register every dialed conn into connTracker
// (for kill API) and accumulate per-user byte counts in the local UserPool
// (replacing xray's gRPC StatsService).
type meteredOutbound struct {
tracker *connTracker
pool *UserPool
l *zap.Logger
}
func newMeteredOutbound(tracker *connTracker, pool *UserPool) *meteredOutbound {
return &meteredOutbound{tracker: tracker, pool: pool, l: zap.L().Named("xray_outbound")}
}
func (h *meteredOutbound) Tag() string { return "" }
func (h *meteredOutbound) Start() error { return nil }
func (h *meteredOutbound) Close() error { return nil }
func (h *meteredOutbound) SenderSettings() *serial.TypedMessage { return nil }
func (h *meteredOutbound) ProxySettings() *serial.TypedMessage { return nil }
// Dispatch implements outbound.Handler.
func (h *meteredOutbound) Dispatch(ctx context.Context, link *transport.Link) {
obs := session.OutboundsFromContext(ctx)
if len(obs) == 0 {
_ = common.Interrupt(link.Reader)
_ = common.Interrupt(link.Writer)
return
}
ob := obs[len(obs)-1]
target := ob.Target
if !target.IsValid() {
_ = common.Interrupt(link.Reader)
_ = common.Interrupt(link.Writer)
return
}
ob.Name = "metered"
inb := session.InboundFromContext(ctx)
userID := userIDFromInbound(inb)
email := ""
if inb != nil && inb.User != nil {
email = inb.User.Email
}
dialCtx, cancel := context.WithCancel(ctx)
defer cancel()
rawConn, err := internet.DialSystem(dialCtx, target, nil)
if err != nil {
h.l.Debug("dial failed",
zap.String("target", target.NetAddr()),
zap.String("email", email),
zap.Error(err),
)
_ = common.Interrupt(link.Writer)
_ = common.Interrupt(link.Reader)
return
}
// pool may be nil if SyncTrafficEndPoint wasn't configured; counters disabled.
var user *User
if h.pool != nil && userID > 0 {
user, _ = h.pool.GetUser(userID)
}
// Record the access IP for this cycle's traffic report. Source should
// always be populated by xray on inbound accept; if it isn't, that's a
// real anomaly worth surfacing.
if user != nil {
srcIP := sourceIPFromInbound(inb)
if srcIP == "" {
h.l.Warn("inbound source address missing, skipping IP record",
zap.String("email", email),
)
} else {
user.RecordIP(srcIP)
}
}
connID := h.tracker.Register(inb, ob, rawConn, cancel)
defer h.tracker.Unregister(connID)
defer rawConn.Close()
var reader buf.Reader = buf.NewReader(rawConn)
var writer buf.Writer = buf.NewWriter(rawConn)
if user != nil {
reader = &meteringReader{inner: reader, user: user}
writer = &meteringWriter{inner: writer, user: user}
}
requestDone := func() error {
if err := buf.Copy(link.Reader, writer); err != nil {
return errors.New("failed to send request").Base(err)
}
return nil
}
responseDone := func() error {
if err := buf.Copy(reader, link.Writer); err != nil {
return errors.New("failed to send response").Base(err)
}
return nil
}
if err := task.Run(dialCtx, requestDone, task.OnSuccess(responseDone, task.Close(link.Writer))); err != nil {
errC := errors.Cause(err)
if !goerrors.Is(errC, io.EOF) && !goerrors.Is(errC, io.ErrClosedPipe) && !goerrors.Is(errC, context.Canceled) {
h.l.Debug("connection ended with error",
zap.String("target", target.NetAddr()),
zap.String("email", email),
zap.Error(err),
)
_ = common.Interrupt(link.Writer)
}
}
_ = common.Interrupt(link.Reader)
}
// meteringReader / meteringWriter wrap the dialed conn at the buf layer so
// every chunk going through bumps the user's atomic counter.
type meteringReader struct {
inner buf.Reader
user *User
}
func (r *meteringReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
mb, err := r.inner.ReadMultiBuffer()
r.user.AddDownloadTraffic(mbLen(mb))
return mb, err
}
type meteringWriter struct {
inner buf.Writer
user *User
}
func (w *meteringWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
w.user.AddUploadTraffic(mbLen(mb))
return w.inner.WriteMultiBuffer(mb)
}
func mbLen(mb buf.MultiBuffer) int64 {
var n int64
for _, b := range mb {
n += int64(b.Len())
}
return n
}