Fix: fakeip udp should not replace with another ip
This commit is contained in:
parent
5b51b8f727
commit
e6d496cc6e
2 changed files with 26 additions and 17 deletions
|
@ -3,26 +3,17 @@ package tunnel
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
N "github.com/Dreamacro/clash/common/net"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
"github.com/Dreamacro/clash/component/resolver"
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata) error {
|
func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata) error {
|
||||||
defer packet.Drop()
|
defer packet.Drop()
|
||||||
|
|
||||||
// local resolve UDP dns
|
|
||||||
if !metadata.Resolved() {
|
|
||||||
ip, err := resolver.ResolveIP(metadata.Host)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
|
||||||
|
|
||||||
addr := metadata.UDPAddr()
|
addr := metadata.UDPAddr()
|
||||||
if addr == nil {
|
if addr == nil {
|
||||||
return errors.New("udp addr invalid")
|
return errors.New("udp addr invalid")
|
||||||
|
@ -37,7 +28,7 @@ func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string, fAddr net.Addr) {
|
func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string, oAddr, fAddr netip.Addr) {
|
||||||
buf := pool.Get(pool.UDPBufferSize)
|
buf := pool.Get(pool.UDPBufferSize)
|
||||||
defer pool.Put(buf)
|
defer pool.Put(buf)
|
||||||
defer natTable.Delete(key)
|
defer natTable.Delete(key)
|
||||||
|
@ -50,11 +41,16 @@ func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string, fAddr n
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if fAddr != nil {
|
fromUDPAddr := from.(*net.UDPAddr)
|
||||||
from = fAddr
|
if fAddr.IsValid() {
|
||||||
|
fromAddr, _ := netip.AddrFromSlice(fromUDPAddr.IP)
|
||||||
|
fromAddr.Unmap()
|
||||||
|
if oAddr == fromAddr {
|
||||||
|
fromUDPAddr.IP = fAddr.AsSlice()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = packet.WriteBack(buf[:n], from)
|
_, err = packet.WriteBack(buf[:n], fromUDPAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -166,9 +167,10 @@ func handleUDPConn(packet *inbound.PacketAdapter) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// make a fAddr if request ip is fakeip
|
// make a fAddr if request ip is fakeip
|
||||||
var fAddr net.Addr
|
var fAddr netip.Addr
|
||||||
if resolver.IsExistFakeIP(metadata.DstIP) {
|
if resolver.IsExistFakeIP(metadata.DstIP) {
|
||||||
fAddr = metadata.UDPAddr()
|
fAddr, _ = netip.AddrFromSlice(metadata.DstIP)
|
||||||
|
fAddr = fAddr.Unmap()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := preHandleMetadata(metadata); err != nil {
|
if err := preHandleMetadata(metadata); err != nil {
|
||||||
|
@ -176,6 +178,15 @@ func handleUDPConn(packet *inbound.PacketAdapter) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// local resolve UDP dns
|
||||||
|
if !metadata.Resolved() {
|
||||||
|
ip, err := resolver.ResolveIP(metadata.Host)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
metadata.DstIP = ip
|
||||||
|
}
|
||||||
|
|
||||||
key := packet.LocalAddr().String()
|
key := packet.LocalAddr().String()
|
||||||
|
|
||||||
handle := func() bool {
|
handle := func() bool {
|
||||||
|
@ -240,7 +251,9 @@ func handleUDPConn(packet *inbound.PacketAdapter) {
|
||||||
log.Infoln("[UDP] %s --> %s doesn't match any rule using DIRECT", metadata.SourceAddress(), metadata.RemoteAddress())
|
log.Infoln("[UDP] %s --> %s doesn't match any rule using DIRECT", metadata.SourceAddress(), metadata.RemoteAddress())
|
||||||
}
|
}
|
||||||
|
|
||||||
go handleUDPToLocal(packet.UDPPacket, pc, key, fAddr)
|
oAddr, _ := netip.AddrFromSlice(metadata.DstIP)
|
||||||
|
oAddr = oAddr.Unmap()
|
||||||
|
go handleUDPToLocal(packet.UDPPacket, pc, key, oAddr, fAddr)
|
||||||
|
|
||||||
natTable.Set(key, pc)
|
natTable.Set(key, pc)
|
||||||
handle()
|
handle()
|
||||||
|
|
Loading…
Reference in a new issue