Skip to content

Commit

Permalink
Improve TLS transport
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jun 24, 2024
1 parent b1625ab commit 156d7da
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions transport_tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,28 @@ func (t *TLSTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg
t.access.Lock()
conn := t.connections.PopFront()
t.access.Unlock()
if conn == nil {
tcpConn, err := t.dialer.DialContext(ctx, N.NetworkTCP, t.serverAddr)
if err != nil {
return nil, err
if conn != nil {
response, err := t.exchange(message, conn)
if err == nil {
return response, nil
}
tlsConn := tls.Client(tcpConn, &tls.Config{
ServerName: t.serverAddr.AddrString(),
})
err = tlsConn.HandshakeContext(ctx)
if err != nil {
tcpConn.Close()
return nil, err
}
conn = &tlsDNSConn{Conn: tlsConn}
}
tcpConn, err := t.dialer.DialContext(ctx, N.NetworkTCP, t.serverAddr)
if err != nil {
return nil, err
}
tlsConn := tls.Client(tcpConn, &tls.Config{
ServerName: t.serverAddr.AddrString(),
})
err = tlsConn.HandshakeContext(ctx)
if err != nil {
tcpConn.Close()
return nil, err
}
return t.exchange(message, &tlsDNSConn{Conn: tlsConn})
}

func (t *TLSTransport) exchange(message *dns.Msg, conn *tlsDNSConn) (*dns.Msg, error) {
messageId := message.Id
conn.queryId++
message.Id = conn.queryId
Expand Down

0 comments on commit 156d7da

Please sign in to comment.