1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 17:27:50 -04:00
v2fly/proxy/vmess/vmessin_udp.go

109 lines
2.8 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-21 16:38:53 -04:00
buffer := alloc.NewBuffer()
nBytes, addr, err := conn.ReadFromUDP(buffer.Value)
2015-10-02 10:08:01 -04:00
if err != nil {
log.Error("VMessIn failed to read UDP packets: %v", err)
2015-10-21 16:38:53 -04:00
buffer.Release()
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
2015-10-21 16:38:53 -04:00
reader := bytes.NewReader(buffer.Value[:nBytes])
2015-10-02 15:55:37 -04:00
requestReader := protocol.NewVMessRequestReader(handler.clients)
request, err := requestReader.Read(reader)
if err != nil {
2015-10-11 11:03:52 -04:00
log.Access(addr.String(), "", log.AccessRejected, err.Error())
2015-10-02 15:55:37 -04:00
log.Warning("VMessIn: Invalid request from (%s): %v", addr.String(), err)
2015-10-21 16:38:53 -04:00
buffer.Release()
2015-10-03 05:34:01 -04:00
continue
2015-10-02 15:55:37 -04:00
}
2015-10-11 11:03:52 -04:00
log.Access(addr.String(), request.Address.String(), log.AccessAccepted, "")
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-21 16:38:53 -04:00
buffer.Release()
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-21 16:38:53 -04:00
buffer.Release()
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-14 08:51:19 -04:00
ray := handler.dispatcher.DispatchToOutbound(packet)
2015-10-02 10:08:01 -04:00
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
2015-10-10 14:52:13 -04:00
buffer := alloc.NewBuffer().Clear()
defer buffer.Release()
2015-10-02 15:55:37 -04:00
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 {
2015-10-10 14:52:13 -04:00
conn.WriteToUDP(buffer.Value, clientAddr)
log.Info("VMessIn sending %d bytes to %s", buffer.Len(), clientAddr.String())
2015-10-02 10:08:01 -04:00
}
}