1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-31 00:08:08 -04:00
v2fly/proxy/vmess/vmessin_udp.go

102 lines
2.6 KiB
Go
Raw Normal View History

2015-10-02 10:08:01 -04:00
package vmess
import (
2015-10-02 15:55:37 -04:00
"bytes"
"crypto/md5"
2015-10-02 10:08:01 -04:00
"net"
"github.com/v2ray/v2ray-core/common/alloc"
2015-10-02 15:55:37 -04:00
v2io "github.com/v2ray/v2ray-core/common/io"
2015-10-02 10:08:01 -04:00
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
)
const (
bufferSize = 2 * 1024
)
func (handler *VMessInboundHandler) ListenUDP(port uint16) error {
addr := &net.UDPAddr{
IP: net.IP{0, 0, 0, 0},
Port: int(port),
Zone: "",
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
log.Error("VMessIn failed to listen UDP on port %d: %v", port, err)
return err
}
go handler.AcceptPackets(conn)
return nil
}
2015-10-03 05:34:01 -04:00
func (handler *VMessInboundHandler) AcceptPackets(conn *net.UDPConn) {
2015-10-02 10:08:01 -04:00
for {
2015-10-03 05:34:01 -04:00
buffer := make([]byte, bufferSize)
2015-10-02 10:08:01 -04:00
nBytes, addr, err := conn.ReadFromUDP(buffer)
if err != nil {
log.Error("VMessIn failed to read UDP packets: %v", err)
2015-10-03 05:34:01 -04:00
continue
2015-10-02 10:08:01 -04:00
}
2015-10-02 15:55:37 -04:00
reader := bytes.NewReader(buffer[:nBytes])
requestReader := protocol.NewVMessRequestReader(handler.clients)
request, err := requestReader.Read(reader)
if err != nil {
log.Warning("VMessIn: Invalid request from (%s): %v", addr.String(), err)
2015-10-03 05:34:01 -04:00
continue
2015-10-02 15:55:37 -04:00
}
2015-10-07 08:58:31 -04:00
cryptReader, err := v2io.NewAesDecryptReader(request.RequestKey, request.RequestIV, reader)
2015-10-02 15:55:37 -04:00
if err != nil {
log.Error("VMessIn: Failed to create decrypt reader: %v", err)
2015-10-03 05:34:01 -04:00
continue
2015-10-02 15:55:37 -04:00
}
data := alloc.NewBuffer()
nBytes, err = cryptReader.Read(data.Value)
2015-10-02 10:08:01 -04:00
if err != nil {
2015-10-02 15:55:37 -04:00
log.Warning("VMessIn: Unable to decrypt data: %v", err)
2015-10-08 11:41:38 -04:00
data.Release()
2015-10-03 05:34:01 -04:00
continue
2015-10-02 10:08:01 -04:00
}
data.Slice(0, nBytes)
2015-10-02 10:08:01 -04:00
packet := v2net.NewPacket(request.Destination(), data, false)
2015-10-02 15:55:37 -04:00
go handler.handlePacket(conn, request, packet, addr)
2015-10-02 10:08:01 -04:00
}
}
2015-10-02 15:55:37 -04:00
func (handler *VMessInboundHandler) handlePacket(conn *net.UDPConn, request *protocol.VMessRequest, packet v2net.Packet, clientAddr *net.UDPAddr) {
2015-10-02 10:08:01 -04:00
ray := handler.vPoint.DispatchToOutbound(packet)
close(ray.InboundInput())
2015-10-07 08:58:31 -04:00
responseKey := md5.Sum(request.RequestKey)
responseIV := md5.Sum(request.RequestIV)
2015-10-02 15:55:37 -04:00
buffer := bytes.NewBuffer(make([]byte, 0, bufferSize))
responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], buffer)
if err != nil {
log.Error("VMessIn: Failed to create encrypt writer: %v", err)
return
}
2015-10-07 08:58:31 -04:00
responseWriter.Write(request.ResponseHeader)
2015-10-02 15:55:37 -04:00
hasData := false
2015-10-02 10:08:01 -04:00
if data, ok := <-ray.InboundOutput(); ok {
2015-10-02 15:55:37 -04:00
hasData = true
responseWriter.Write(data.Value)
data.Release()
2015-10-02 15:55:37 -04:00
}
if hasData {
conn.WriteToUDP(buffer.Bytes(), clientAddr)
2015-10-06 11:24:57 -04:00
log.Info("VMessIn sending %d bytes to %s", len(buffer.Bytes()), clientAddr.String())
2015-10-02 10:08:01 -04:00
}
}