-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
vmessout.go
197 lines (166 loc) · 5.64 KB
/
vmessout.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package vmess
import (
"bytes"
"crypto/md5"
"crypto/rand"
mrand "math/rand"
"net"
"sync"
"github.com/v2ray/v2ray-core/common/alloc"
v2crypto "github.com/v2ray/v2ray-core/common/crypto"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
"github.com/v2ray/v2ray-core/transport/ray"
)
type VMessOutboundHandler struct {
vNextList []*config.OutboundTarget
}
func NewVMessOutboundHandler(vNextList []*config.OutboundTarget) *VMessOutboundHandler {
return &VMessOutboundHandler{
vNextList: vNextList,
}
}
func pickVNext(serverList []*config.OutboundTarget) (v2net.Destination, config.User) {
vNextLen := len(serverList)
if vNextLen == 0 {
panic("VMessOut: Zero vNext is configured.")
}
vNextIndex := 0
if vNextLen > 1 {
vNextIndex = mrand.Intn(vNextLen)
}
vNext := serverList[vNextIndex]
vNextUserLen := len(vNext.Accounts)
if vNextUserLen == 0 {
panic("VMessOut: Zero User account.")
}
vNextUserIndex := 0
if vNextUserLen > 1 {
vNextUserIndex = mrand.Intn(vNextUserLen)
}
vNextUser := vNext.Accounts[vNextUserIndex]
return vNext.Destination, vNextUser
}
func (handler *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
vNextList := handler.vNextList
vNextAddress, vNextUser := pickVNext(vNextList)
command := protocol.CmdTCP
if firstPacket.Destination().IsUDP() {
command = protocol.CmdUDP
}
request := &protocol.VMessRequest{
Version: protocol.Version,
User: vNextUser,
Command: command,
Address: firstPacket.Destination().Address(),
}
buffer := alloc.NewSmallBuffer()
defer buffer.Release()
v2net.ReadAllBytes(rand.Reader, buffer.Value[:36]) // 16 + 16 + 4
request.RequestIV = buffer.Value[:16]
request.RequestKey = buffer.Value[16:32]
request.ResponseHeader = buffer.Value[32:36]
return startCommunicate(request, vNextAddress, ray, firstPacket)
}
func startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ray ray.OutboundRay, firstPacket v2net.Packet) error {
conn, err := net.Dial(dest.Network(), dest.Address().String())
if err != nil {
log.Error("Failed to open %s: %v", dest.String(), err)
if ray != nil {
close(ray.OutboundOutput())
}
return err
}
log.Info("VMessOut: Tunneling request to %s via %s", request.Address.String(), dest.String())
defer conn.Close()
input := ray.OutboundInput()
output := ray.OutboundOutput()
var requestFinish, responseFinish sync.Mutex
requestFinish.Lock()
responseFinish.Lock()
go handleRequest(conn, request, firstPacket, input, &requestFinish)
go handleResponse(conn, request, output, &responseFinish, dest.IsUDP())
requestFinish.Lock()
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.CloseWrite()
}
responseFinish.Lock()
return nil
}
func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2net.Packet, input <-chan *alloc.Buffer, finish *sync.Mutex) {
defer finish.Unlock()
aesStream, err := v2crypto.NewAesEncryptionStream(request.RequestKey[:], request.RequestIV[:])
if err != nil {
log.Error("VMessOut: Failed to create AES encryption stream: %v", err)
return
}
encryptRequestWriter := v2crypto.NewCryptionWriter(aesStream, conn)
buffer := alloc.NewBuffer().Clear()
buffer, err = request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange, buffer)
if err != nil {
log.Error("VMessOut: Failed to serialize VMess request: %v", err)
return
}
// Send first packet of payload together with request, in favor of small requests.
firstChunk := firstPacket.Chunk()
moreChunks := firstPacket.MoreChunks()
if firstChunk == nil && moreChunks {
firstChunk, moreChunks = <-input
}
if firstChunk != nil {
aesStream.XORKeyStream(firstChunk.Value, firstChunk.Value)
buffer.Append(firstChunk.Value)
firstChunk.Release()
_, err = conn.Write(buffer.Value)
buffer.Release()
if err != nil {
log.Error("VMessOut: Failed to write VMess request: %v", err)
return
}
}
if moreChunks {
v2net.ChanToWriter(encryptRequestWriter, input)
}
return
}
func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<- *alloc.Buffer, finish *sync.Mutex, isUDP bool) {
defer finish.Unlock()
defer close(output)
responseKey := md5.Sum(request.RequestKey[:])
responseIV := md5.Sum(request.RequestIV[:])
aesStream, err := v2crypto.NewAesDecryptionStream(responseKey[:], responseIV[:])
if err != nil {
log.Error("VMessOut: Failed to create AES encryption stream: %v", err)
return
}
decryptResponseReader := v2crypto.NewCryptionReader(aesStream, conn)
buffer, err := v2net.ReadFrom(decryptResponseReader, nil)
if err != nil {
log.Error("VMessOut: Failed to read VMess response (%d bytes): %v", buffer.Len(), err)
return
}
if buffer.Len() < 4 || !bytes.Equal(buffer.Value[:4], request.ResponseHeader[:]) {
log.Warning("VMessOut: unexepcted response header. The connection is probably hijacked.")
return
}
log.Info("VMessOut received %d bytes from %s", buffer.Len()-4, conn.RemoteAddr().String())
buffer.SliceFrom(4)
output <- buffer
if !isUDP {
v2net.ReaderToChan(output, decryptResponseReader)
}
return
}
type VMessOutboundHandlerFactory struct {
}
func (factory *VMessOutboundHandlerFactory) Create(rawConfig interface{}) (connhandler.OutboundConnectionHandler, error) {
vOutConfig := rawConfig.(config.Outbound)
return NewVMessOutboundHandler(vOutConfig.Targets()), nil
}
func init() {
connhandler.RegisterOutboundConnectionHandlerFactory("vmess", &VMessOutboundHandlerFactory{})
}