@@ -6,6 +6,7 @@ package proxy
66import (
77 "context"
88 "net"
9+ "reflect"
910 "strings"
1011 "sync"
1112 "time"
@@ -166,11 +167,12 @@ func (s *SQLServer) onConn(ctx context.Context, conn net.Conn, addr string) {
166167 zap .String ("addr" , addr ))
167168 clientConn := client .NewClientConnection (logger .Named ("conn" ), conn , s .certMgr .ServerSQLTLS (), s .certMgr .SQLTLS (),
168169 s .hsHandler , s .cpt , connID , addr , & backend.BCConfig {
169- ProxyProtocol : s .mu .proxyProtocol ,
170- RequireBackendTLS : s .mu .requireBackendTLS ,
171- HealthyKeepAlive : s .mu .healthyKeepAlive ,
172- UnhealthyKeepAlive : s .mu .unhealthyKeepAlive ,
173- ConnBufferSize : s .mu .connBufferSize ,
170+ ProxyProtocol : s .mu .proxyProtocol ,
171+ RequireBackendTLS : s .mu .requireBackendTLS ,
172+ HealthyKeepAlive : s .mu .healthyKeepAlive ,
173+ UnhealthyKeepAlive : s .mu .unhealthyKeepAlive ,
174+ ConnBufferSize : s .mu .connBufferSize ,
175+ FromPublicEndpoints : s .fromPublicEndpoint ,
174176 }, s .meter )
175177 s .mu .clients [connID ] = clientConn
176178 logger .Debug ("new connection" , zap .Bool ("proxy-protocol" , s .mu .proxyProtocol ), zap .Bool ("require_backend_tls" , s .mu .requireBackendTLS ))
@@ -204,6 +206,31 @@ func (s *SQLServer) onConn(ctx context.Context, conn net.Conn, addr string) {
204206 clientConn .Run (ctx )
205207}
206208
209+ func (s * SQLServer ) fromPublicEndpoint (addr net.Addr ) bool {
210+ if addr == nil || reflect .ValueOf (addr ).IsNil () {
211+ return false
212+ }
213+ s .mu .RLock ()
214+ publicEndpoints := s .mu .publicEndpoints
215+ s .mu .RUnlock ()
216+ ip , err := netutil .NetAddr2IP (addr )
217+ if err != nil {
218+ s .logger .Warn ("failed to check public endpoint" , zap .Any ("addr" , addr ), zap .Error (err ))
219+ return false
220+ }
221+ contains , err := netutil .CIDRContainsIP (publicEndpoints , ip )
222+ if err != nil {
223+ s .logger .Warn ("failed to check public endpoint" , zap .Any ("ip" , ip ), zap .Error (err ))
224+ return false
225+ }
226+ if contains {
227+ return true
228+ }
229+ // The public NLB may enable preserveIP, and the incoming address is the client address, which may be a public address.
230+ // Even if the private NLB enables preserveIP, the client address is still a private address.
231+ return ! netutil .IsPrivate (ip )
232+ }
233+
207234func (s * SQLServer ) PreClose () {
208235 // Step 1: HTTP status returns unhealthy so that NLB takes this instance offline and then new connections won't come.
209236 s .mu .Lock ()
0 commit comments