handleTCPConnection starts a loop that handles an incoming TCP connection. proto must be either [ProtoTCP] or [ProtoTLS].
( ctx context.Context, conn net.Conn, proto Proto, reqSema syncutil.Semaphore, )
| 124 | // handleTCPConnection starts a loop that handles an incoming TCP connection. |
| 125 | // proto must be either [ProtoTCP] or [ProtoTLS]. |
| 126 | func (p *Proxy) handleTCPConnection( |
| 127 | ctx context.Context, |
| 128 | conn net.Conn, |
| 129 | proto Proto, |
| 130 | reqSema syncutil.Semaphore, |
| 131 | ) { |
| 132 | defer slogutil.RecoverAndLog(ctx, p.logger) |
| 133 | defer reqSema.Release() |
| 134 | defer func() { |
| 135 | err := conn.Close() |
| 136 | if err != nil { |
| 137 | logWithNonCrit(ctx, err, "closing conn", ProtoTCP, p.logger) |
| 138 | } |
| 139 | }() |
| 140 | |
| 141 | p.logger.DebugContext(ctx, "handling new request", "proto", proto, "raddr", conn.RemoteAddr()) |
| 142 | |
| 143 | ctx, cancel := p.reqCtx.New(ctx) |
| 144 | defer cancel() |
| 145 | |
| 146 | for p.isStarted() { |
| 147 | err := conn.SetDeadline(p.time.Now().Add(defaultTimeout)) |
| 148 | if err != nil { |
| 149 | // Consider deadline errors non-critical. |
| 150 | logWithNonCrit(ctx, err, "setting deadline", ProtoTCP, p.logger) |
| 151 | } |
| 152 | |
| 153 | req := p.readDNSReq(ctx, conn) |
| 154 | if req == nil { |
| 155 | return |
| 156 | } |
| 157 | |
| 158 | d := p.newDNSContext(proto, req, netutil.NetAddrToAddrPort(conn.RemoteAddr())) |
| 159 | d.Conn = conn |
| 160 | |
| 161 | err = p.handleDNSRequest(ctx, d) |
| 162 | if err != nil { |
| 163 | logWithNonCrit(ctx, err, "handling request", ProtoTCP, p.logger) |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // readDNSReq returns DNS request message from the given connection or nil if |
| 169 | // it failed to read it. Properly logs the error if it happened. |
no test coverage detected