Changeset 698 in code for trunk/downstream.go


Ignore:
Timestamp:
Nov 17, 2021, 11:10:56 AM (4 years ago)
Author:
contact
Message:

Add context arg to sanityCheckServer

As a bonus, the timeout now applies to the whole TLS dial
operation. Before the timeout only applied to the net dial
operation, making it possible for a bad server to stall the request
by making the TLS handshake extremely slow.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/downstream.go

    r694 r698  
    10311031}
    10321032
    1033 func sanityCheckServer(addr string) error {
    1034         dialer := net.Dialer{Timeout: 30 * time.Second}
    1035         conn, err := tls.DialWithDialer(&dialer, "tcp", addr, nil)
     1033func sanityCheckServer(ctx context.Context, addr string) error {
     1034        ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
     1035        defer cancel()
     1036
     1037        conn, err := new(tls.Dialer).DialContext(ctx, "tcp", addr)
    10361038        if err != nil {
    10371039                return err
    10381040        }
     1041
    10391042        return conn.Close()
    10401043}
     
    11311134
    11321135                dc.logger.Printf("trying to connect to new network %q", addr)
    1133                 if err := sanityCheckServer(addr); err != nil {
     1136                if err := sanityCheckServer(context.TODO(), addr); err != nil {
    11341137                        dc.logger.Printf("failed to connect to %q: %v", addr, err)
    11351138                        return ircError{&irc.Message{
Note: See TracChangeset for help on using the changeset viewer.