mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-06-17 21:59:47 +03:00
Freedom: Cache UDP resolve result
This commit is contained in:
@ -346,6 +346,12 @@ type PacketWriter struct {
|
||||
*Handler
|
||||
context.Context
|
||||
UDPOverride net.Destination
|
||||
|
||||
// Dest of udp packets might be a domain, we will resolve them to IP
|
||||
// But resolver will return a random one if the domain has many IPs
|
||||
// Resulting in these packets being sent to many different IPs randomly
|
||||
// So, cache and keep the resolve result
|
||||
resolvedUDPAddr map[string]net.Address
|
||||
}
|
||||
|
||||
func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
||||
@ -365,9 +371,14 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
||||
b.UDP.Port = w.UDPOverride.Port
|
||||
}
|
||||
if w.Handler.config.hasStrategy() && b.UDP.Address.Family().IsDomain() {
|
||||
if ip := w.resolvedUDPAddr[b.UDP.Address.Domain()]; ip != nil {
|
||||
b.UDP.Address = ip
|
||||
}
|
||||
} else {
|
||||
ip := w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil)
|
||||
if ip != nil {
|
||||
b.UDP.Address = ip
|
||||
w.resolvedUDPAddr[b.UDP.Address.Domain()] = ip
|
||||
}
|
||||
}
|
||||
destAddr, _ := net.ResolveUDPAddr("udp", b.UDP.NetAddr())
|
||||
|
Reference in New Issue
Block a user