客户端Websocket SSL如何绕过DNS轮询连接指定IP地址?
Great question—this is a common scenario when you need to target a specific backend node behind a DNS-routed domain. Here are the most reliable methods to achieve this, while ensuring proper SSL certificate validation (since each IP holds an example.com certificate):
1. Modify Your Local Hosts File (Quickest for Testing)
The simplest way to bypass DNS polling is to force your system to resolve example.com to your target IP directly by editing the hosts file:
- Windows: Open
C:\Windows\System32\drivers\etc\hostsas Administrator - Linux/macOS: Edit
/etc/hostswith sudo privileges
Add a line like this, replacing 192.168.1.100 with your target IP:
192.168.1.100 example.com
Save the file, flush your system's DNS cache (e.g., ipconfig /flushdns on Windows, sudo dscacheutil -flushcache on macOS), then connect normally using wss://example.com. Your client will now route to the specified IP automatically.
Note: This affects all applications on your system that resolve
example.com. Remember to revert the change when you're done testing.
2. Manually Specify IP + SNI in Client Code (Best for Production/Controlled Apps)
If you can't modify the hosts file (e.g., in a deployed application), you can directly connect to the target IP while explicitly setting the SNI (Server Name Indication) to example.com—this ensures the server presents the correct SSL certificate, and your client validates it properly.
Python Example (using websockets library)
import websockets import asyncio async def connect_to_target_ip(): target_ip = "192.168.1.100" # Connect directly to the IP, but tell the server we're requesting example.com async with websockets.connect( f"wss://{target_ip}:443", host="example.com", # Sets the Host header for HTTP handshake ssl={"server_hostname": "example.com"} # Enables SNI for SSL validation ) as websocket: await websocket.send("Hello from targeted client!") response = await websocket.recv() print(f"Received: {response}") asyncio.run(connect_to_target_ip())
Node.js Example (using ws library)
const WebSocket = require('ws'); const tls = require('tls'); const targetIp = '192.168.1.100'; // First establish a TLS connection with SNI set to example.com const tlsSocket = tls.connect({ host: targetIp, port: 443, servername: 'example.com' // Critical: tells the server which certificate to use }, () => { // Attach WebSocket to the pre-configured TLS socket const ws = new WebSocket('wss://example.com/', { socket: tlsSocket }); ws.on('open', () => { ws.send('Hello targeted server!'); }); ws.on('message', (data) => { console.log(`Received: ${data.toString()}`); }); });
3. Custom DNS Resolution in Client Applications
For larger applications, you can implement custom DNS resolution logic to override the system's default DNS lookup. For example:
- In Java, use
InetAddresswith a custom resolver, or configure OkHttp'sDnsinterface to return your target IP forexample.com. - In C#, use
ServicePointManageror a customDnsResolverto mapexample.comto your desired IP.
Key Notes for All Methods
- SNI is Mandatory: Without setting the SNI to
example.com, the server may present the wrong certificate (or no valid certificate), causing your client to reject the connection. - Certificate Validation: Ensure your client verifies the certificate (don't disable validation unless testing)—since each IP holds an
example.comcertificate, setting SNI will make validation pass as normal.
内容的提问来源于stack exchange,提问作者Geige V

