-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06bd3aa
commit ac6c4b0
Showing
8 changed files
with
251 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package route | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"time" | ||
|
||
"github.com/sagernet/sing-box/adapter" | ||
C "github.com/sagernet/sing-box/constant" | ||
"github.com/sagernet/sing-box/outbound" | ||
"github.com/sagernet/sing-dns" | ||
"github.com/sagernet/sing/common/buf" | ||
E "github.com/sagernet/sing/common/exceptions" | ||
M "github.com/sagernet/sing/common/metadata" | ||
N "github.com/sagernet/sing/common/network" | ||
"github.com/sagernet/sing/common/udpnat2" | ||
|
||
mDNS "github.com/miekg/dns" | ||
) | ||
|
||
func (r *Router) hijackDNSStream(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { | ||
metadata.Destination = M.Socksaddr{} | ||
for { | ||
conn.SetReadDeadline(time.Now().Add(C.DNSTimeout)) | ||
err := outbound.HandleStreamDNSRequest(ctx, r, conn, metadata) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
func (r *Router) hijackDNSPacket(ctx context.Context, conn N.PacketConn, packetBuffers []*N.PacketBuffer, metadata adapter.InboundContext) { | ||
if uConn, isUDPNAT2 := conn.(*udpnat.Conn); isUDPNAT2 { | ||
metadata.Destination = M.Socksaddr{} | ||
for _, packet := range packetBuffers { | ||
buffer := packet.Buffer | ||
destination := packet.Destination | ||
N.PutPacketBuffer(packet) | ||
go ExchangeDNSPacket(ctx, r, uConn, buffer, metadata, destination) | ||
} | ||
uConn.SetHandler(&dnsHijacker{ | ||
router: r, | ||
conn: conn, | ||
ctx: ctx, | ||
metadata: metadata, | ||
}) | ||
return | ||
} | ||
err := outbound.NewDNSPacketConnection(ctx, r, conn, packetBuffers, metadata) | ||
if err != nil && !E.IsClosedOrCanceled(err) { | ||
r.dnsLogger.ErrorContext(ctx, E.Cause(err, "process packet connection")) | ||
} | ||
} | ||
|
||
func ExchangeDNSPacket(ctx context.Context, router *Router, conn N.PacketConn, buffer *buf.Buffer, metadata adapter.InboundContext, destination M.Socksaddr) { | ||
err := exchangeDNSPacket(ctx, router, conn, buffer, metadata, destination) | ||
if err != nil && !E.IsClosedOrCanceled(err) { | ||
router.dnsLogger.ErrorContext(ctx, E.Cause(err, "process packet connection")) | ||
} | ||
} | ||
|
||
func exchangeDNSPacket(ctx context.Context, router *Router, conn N.PacketConn, buffer *buf.Buffer, metadata adapter.InboundContext, destination M.Socksaddr) error { | ||
var message mDNS.Msg | ||
err := message.Unpack(buffer.Bytes()) | ||
buffer.Release() | ||
if err != nil { | ||
return E.Cause(err, "unpack request") | ||
} | ||
response, err := router.Exchange(adapter.WithContext(ctx, &metadata), &message) | ||
if err != nil { | ||
return err | ||
} | ||
responseBuffer, err := dns.TruncateDNSMessage(&message, response, 1024) | ||
if err != nil { | ||
return err | ||
} | ||
err = conn.WritePacket(responseBuffer, destination) | ||
responseBuffer.Release() | ||
return err | ||
} | ||
|
||
type dnsHijacker struct { | ||
router *Router | ||
conn N.PacketConn | ||
ctx context.Context | ||
metadata adapter.InboundContext | ||
} | ||
|
||
func (h *dnsHijacker) NewPacketEx(buffer *buf.Buffer, destination M.Socksaddr) { | ||
go ExchangeDNSPacket(h.ctx, h.router, h.conn, buffer, h.metadata, destination) | ||
} |
Oops, something went wrong.