1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-06 10:20:44 -05:00
v2fly/proxy/vmess/vmessin_udp.go

99 lines
2.5 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"
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
}
cryptReader, err := v2io.NewAesDecryptReader(request.RequestKey[:], request.RequestIV[:], reader)
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 := make([]byte, bufferSize)
nBytes, err = cryptReader.Read(data)
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-03 05:34:01 -04:00
continue
2015-10-02 10:08:01 -04:00
}
2015-10-03 05:34:01 -04:00
packet := v2net.NewPacket(request.Destination(), data[:nBytes], 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-02 15:55:37 -04:00
responseKey := md5.Sum(request.RequestKey[:])
responseIV := md5.Sum(request.RequestIV[:])
buffer := bytes.NewBuffer(make([]byte, 0, bufferSize))
response := protocol.NewVMessResponse(request)
responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], buffer)
if err != nil {
log.Error("VMessIn: Failed to create encrypt writer: %v", err)
return
}
responseWriter.Write(response[:])
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)
}
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
}
}