Skip to content

Commit

Permalink
Fix Exchange may still modify original requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jul 25, 2024
1 parent 1cff486 commit 8a9cd58
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
5 changes: 3 additions & 2 deletions transport_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (t *TCPTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg
return nil, err
}
defer conn.Close()
err = writeMessage(conn, message)
err = writeMessage(conn, 0, message)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -110,12 +110,13 @@ func readMessage(reader io.Reader) (*dns.Msg, error) {
return &message, err
}

func writeMessage(writer io.Writer, message *dns.Msg) error {
func writeMessage(writer io.Writer, messageId uint16, message *dns.Msg) error {
requestLen := message.Len()
buffer := buf.NewSize(3 + requestLen)
defer buffer.Release()
common.Must(binary.Write(buffer, binary.BigEndian, uint16(requestLen)))
exMessage := *message
exMessage.Id = messageId
exMessage.Compress = true
rawMessage, err := exMessage.PackBuffer(buffer.FreeBytes())
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions transport_tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ func (t *TLSTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg
}

func (t *TLSTransport) exchange(message *dns.Msg, conn *tlsDNSConn) (*dns.Msg, error) {
messageId := message.Id
conn.queryId++
message.Id = conn.queryId
err := writeMessage(conn, message)
err := writeMessage(conn, conn.queryId, message)
if err != nil {
conn.Close()
return nil, E.Cause(err, "write request")
Expand All @@ -128,7 +126,6 @@ func (t *TLSTransport) exchange(message *dns.Msg, conn *tlsDNSConn) (*dns.Msg, e
conn.Close()
return nil, E.Cause(err, "read response")
}
response.Id = messageId
t.access.Lock()
t.connections.PushBack(conn)
t.access.Unlock()
Expand Down

0 comments on commit 8a9cd58

Please sign in to comment.