You need to enable JavaScript to run this app. 客户端Websocket SSL如何绕过DNS轮询连接指定IP地址?
优惠活动
大模型
产品
解决方案
定价
更多

客户端Websocket SSL如何绕过DNS轮询连接指定IP地址?

How to Connect to a Specific IP for example.com's WebSocket Server

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\hosts as Administrator
  • Linux/macOS: Edit /etc/hosts with 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 InetAddress with a custom resolver, or configure OkHttp's Dns interface to return your target IP for example.com.
  • In C#, use ServicePointManager or a custom DnsResolver to map example.com to 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.com certificate, setting SNI will make validation pass as normal.

内容的提问来源于stack exchange,提问作者Geige V

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:27:09