2015-12-04 06:42:56 -05:00
|
|
|
package inbound
|
2015-09-10 18:24:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"io"
|
|
|
|
"net"
|
2015-09-23 08:14:53 -04:00
|
|
|
"sync"
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-10-14 08:51:19 -04:00
|
|
|
"github.com/v2ray/v2ray-core/app"
|
2015-10-08 08:46:18 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2015-11-03 15:26:16 -05:00
|
|
|
v2crypto "github.com/v2ray/v2ray-core/common/crypto"
|
2015-09-19 18:50:21 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2015-09-19 17:54:36 -04:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-10-15 07:42:43 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/retry"
|
2016-01-18 06:31:27 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/serial"
|
2016-01-02 17:32:18 -05:00
|
|
|
"github.com/v2ray/v2ray-core/proxy"
|
2016-01-02 17:08:36 -05:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/internal"
|
2015-12-07 14:32:38 -05:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess"
|
2015-09-19 18:11:14 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
2015-09-10 18:24:18 -04:00
|
|
|
)
|
|
|
|
|
2015-12-01 08:54:49 -05:00
|
|
|
// Inbound connection handler that handles messages in VMess format.
|
2015-09-10 18:24:18 -04:00
|
|
|
type VMessInboundHandler struct {
|
2016-01-03 17:30:37 -05:00
|
|
|
sync.Mutex
|
2016-01-19 17:41:40 -05:00
|
|
|
space app.Space
|
|
|
|
clients protocol.UserSet
|
|
|
|
user *vmess.User
|
|
|
|
accepting bool
|
|
|
|
listener *net.TCPListener
|
|
|
|
features *FeaturesConfig
|
|
|
|
listeningPort v2net.Port
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2016-01-19 17:41:40 -05:00
|
|
|
func (this *VMessInboundHandler) Port() v2net.Port {
|
|
|
|
return this.listeningPort
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2016-01-03 17:30:37 -05:00
|
|
|
func (this *VMessInboundHandler) Close() {
|
|
|
|
this.accepting = false
|
|
|
|
if this.listener != nil {
|
2016-01-22 10:50:31 -05:00
|
|
|
this.listener.Close()
|
2016-01-22 11:56:19 -05:00
|
|
|
this.Lock()
|
2016-01-03 18:33:25 -05:00
|
|
|
this.listener = nil
|
2016-01-03 17:30:37 -05:00
|
|
|
this.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 17:41:40 -05:00
|
|
|
func (this *VMessInboundHandler) GetUser() *vmess.User {
|
|
|
|
return this.user
|
2016-01-08 18:10:57 -05:00
|
|
|
}
|
|
|
|
|
2015-12-02 15:44:01 -05:00
|
|
|
func (this *VMessInboundHandler) Listen(port v2net.Port) error {
|
2016-01-19 17:41:40 -05:00
|
|
|
if this.accepting {
|
|
|
|
if this.listeningPort == port {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return proxy.ErrorAlreadyListening
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.listeningPort = port
|
|
|
|
|
2015-10-07 19:39:50 -04:00
|
|
|
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
|
|
|
IP: []byte{0, 0, 0, 0},
|
|
|
|
Port: int(port),
|
|
|
|
Zone: "",
|
|
|
|
})
|
2015-09-10 18:24:18 -04:00
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("Unable to listen tcp port ", port, ": ", err)
|
2015-10-13 12:27:29 -04:00
|
|
|
return err
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
2015-11-27 15:57:15 -05:00
|
|
|
this.accepting = true
|
2016-01-04 02:40:24 -05:00
|
|
|
this.Lock()
|
2016-01-03 17:30:37 -05:00
|
|
|
this.listener = listener
|
2016-01-04 02:40:24 -05:00
|
|
|
this.Unlock()
|
2016-01-03 17:30:37 -05:00
|
|
|
go this.AcceptConnections()
|
2015-09-10 18:24:18 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-03 17:30:37 -05:00
|
|
|
func (this *VMessInboundHandler) AcceptConnections() error {
|
2015-11-27 15:57:15 -05:00
|
|
|
for this.accepting {
|
2015-10-15 07:42:43 -04:00
|
|
|
retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
|
2016-01-03 19:19:27 -05:00
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
2016-01-03 17:30:37 -05:00
|
|
|
if !this.accepting {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-03 19:19:27 -05:00
|
|
|
connection, err := this.listener.AcceptTCP()
|
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("Failed to accpet connection: ", err)
|
2016-01-03 19:19:27 -05:00
|
|
|
return err
|
2015-10-15 07:42:43 -04:00
|
|
|
}
|
2016-01-03 19:19:27 -05:00
|
|
|
go this.HandleConnection(connection)
|
2015-10-15 07:42:43 -04:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-27 15:57:15 -05:00
|
|
|
func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error {
|
2015-09-10 18:24:18 -04:00
|
|
|
defer connection.Close()
|
2015-09-14 12:19:17 -04:00
|
|
|
|
2015-10-21 17:10:03 -04:00
|
|
|
connReader := v2net.NewTimeOutReader(16, connection)
|
2015-11-27 15:57:15 -05:00
|
|
|
requestReader := protocol.NewVMessRequestReader(this.clients)
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-09-24 08:51:19 -04:00
|
|
|
request, err := requestReader.Read(connReader)
|
2015-09-10 18:24:18 -04:00
|
|
|
if err != nil {
|
2016-01-18 06:31:27 -05:00
|
|
|
log.Access(connection.RemoteAddr(), serial.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err)
|
2015-09-10 18:24:18 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-01-18 06:31:27 -05:00
|
|
|
log.Access(connection.RemoteAddr(), request.Address, log.AccessAccepted, serial.StringLiteral(""))
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Debug("VMessIn: Received request for ", request.Address)
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-12-05 16:55:45 -05:00
|
|
|
ray := this.space.PacketDispatcher().DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true))
|
2015-09-17 17:00:17 -04:00
|
|
|
input := ray.InboundInput()
|
|
|
|
output := ray.InboundOutput()
|
2015-09-23 08:14:53 -04:00
|
|
|
var readFinish, writeFinish sync.Mutex
|
|
|
|
readFinish.Lock()
|
|
|
|
writeFinish.Lock()
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2016-01-15 06:43:06 -05:00
|
|
|
userSettings := vmess.GetUserSettings(request.User.Level)
|
2015-10-31 04:39:45 -04:00
|
|
|
connReader.SetTimeOut(userSettings.PayloadReadTimeout)
|
2015-09-24 08:51:19 -04:00
|
|
|
go handleInput(request, connReader, input, &readFinish)
|
2015-09-17 17:00:17 -04:00
|
|
|
|
2015-10-07 08:58:31 -04:00
|
|
|
responseKey := md5.Sum(request.RequestKey)
|
|
|
|
responseIV := md5.Sum(request.RequestIV)
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-11-03 15:26:16 -05:00
|
|
|
aesStream, err := v2crypto.NewAesEncryptionStream(responseKey[:], responseIV[:])
|
2015-09-10 18:24:18 -04:00
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("VMessIn: Failed to create AES decryption stream: ", err)
|
2016-01-10 19:01:52 -05:00
|
|
|
close(input)
|
2015-10-13 12:27:29 -04:00
|
|
|
return err
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-11-03 15:26:16 -05:00
|
|
|
responseWriter := v2crypto.NewCryptionWriter(aesStream, connection)
|
|
|
|
|
2015-09-17 17:00:17 -04:00
|
|
|
// Optimize for small response packet
|
2015-10-10 10:50:19 -04:00
|
|
|
buffer := alloc.NewLargeBuffer().Clear()
|
2016-01-04 07:01:32 -05:00
|
|
|
defer buffer.Release()
|
2016-01-21 04:08:00 -05:00
|
|
|
buffer.AppendBytes(request.ResponseHeader, byte(0))
|
2016-01-21 11:22:56 -05:00
|
|
|
this.generateCommand(buffer)
|
2015-09-18 06:31:42 -04:00
|
|
|
|
2015-09-18 06:31:26 -04:00
|
|
|
if data, open := <-output; open {
|
2015-10-08 11:41:38 -04:00
|
|
|
buffer.Append(data.Value)
|
|
|
|
data.Release()
|
|
|
|
responseWriter.Write(buffer.Value)
|
2015-09-23 11:13:50 -04:00
|
|
|
go handleOutput(request, responseWriter, output, &writeFinish)
|
2015-09-23 08:14:53 -04:00
|
|
|
writeFinish.Lock()
|
2015-09-17 17:00:17 -04:00
|
|
|
}
|
2015-09-18 06:31:42 -04:00
|
|
|
|
2015-10-07 19:39:50 -04:00
|
|
|
connection.CloseWrite()
|
2015-09-23 08:14:53 -04:00
|
|
|
readFinish.Lock()
|
2015-09-10 18:24:18 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-08 08:46:18 -04:00
|
|
|
func handleInput(request *protocol.VMessRequest, reader io.Reader, input chan<- *alloc.Buffer, finish *sync.Mutex) {
|
2015-09-17 17:00:17 -04:00
|
|
|
defer close(input)
|
2015-09-23 08:14:53 -04:00
|
|
|
defer finish.Unlock()
|
2015-09-17 17:00:17 -04:00
|
|
|
|
2015-11-03 15:26:16 -05:00
|
|
|
aesStream, err := v2crypto.NewAesDecryptionStream(request.RequestKey, request.RequestIV)
|
2015-09-17 17:00:17 -04:00
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("VMessIn: Failed to create AES decryption stream: ", err)
|
2015-09-17 17:00:17 -04:00
|
|
|
return
|
|
|
|
}
|
2015-11-03 15:26:16 -05:00
|
|
|
requestReader := v2crypto.NewCryptionReader(aesStream, reader)
|
2015-09-17 17:00:17 -04:00
|
|
|
v2net.ReaderToChan(input, requestReader)
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-10-08 08:46:18 -04:00
|
|
|
func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-chan *alloc.Buffer, finish *sync.Mutex) {
|
2015-09-13 14:01:50 -04:00
|
|
|
v2net.ChanToWriter(writer, output)
|
2015-09-23 08:14:53 -04:00
|
|
|
finish.Unlock()
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2016-01-01 17:44:11 -05:00
|
|
|
func init() {
|
2016-01-06 10:23:54 -05:00
|
|
|
internal.MustRegisterInboundConnectionHandlerCreator("vmess",
|
2016-01-25 11:18:24 -05:00
|
|
|
func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
|
2016-01-15 06:43:06 -05:00
|
|
|
config := rawConfig.(*Config)
|
2015-09-11 11:27:36 -04:00
|
|
|
|
2016-01-12 05:52:40 -05:00
|
|
|
allowedClients := protocol.NewTimedUserSet()
|
2016-01-15 06:43:06 -05:00
|
|
|
for _, user := range config.AllowedUsers {
|
2016-01-06 10:23:54 -05:00
|
|
|
allowedClients.AddUser(user)
|
|
|
|
}
|
2015-10-06 17:11:08 -04:00
|
|
|
|
2016-01-19 17:41:40 -05:00
|
|
|
return &VMessInboundHandler{
|
|
|
|
space: space,
|
|
|
|
clients: allowedClients,
|
|
|
|
features: config.Features,
|
|
|
|
user: config.AllowedUsers[0],
|
|
|
|
}, nil
|
2016-01-06 10:23:54 -05:00
|
|
|
})
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|