Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使可以指定发起连接使用的Interface以及Port #483

Open
brainiac19 opened this issue Dec 29, 2023 · 0 comments
Open

使可以指定发起连接使用的Interface以及Port #483

brainiac19 opened this issue Dec 29, 2023 · 0 comments
Labels
功能建议 功能与建议

Comments

@brainiac19
Copy link

功能需求

特别是在软路由环境下,若可以指定发起连接的Interface和Port,则可在各类代理中添加规则使流量Bypass,不再有必要关闭代理再测速。对我个人的情况来讲,若可以不关闭代理进行测速,外加MOSDNS接管cf的dns重定向,意味着可以几乎无感完成优选IP更新。

预期目标

我不懂Go。问了下AI,它说可以实现。

Yes, you can modify the downloadHandler function to allow specifying the network interface name and port. Here's an updated version of the function that includes these changes:

func downloadHandler(interfaceName string, localPort int) float64 {
	interfaces, err := net.Interfaces()
	if err != nil {
		// handle error
	}

	var localAddr net.IP
	for _, iface := range interfaces {
		if iface.Name == interfaceName {
			addrs, err := iface.Addrs()
			if err != nil {
				// handle error
			}
			for _, addr := range addrs {
				ip, _, err := net.ParseCIDR(addr.String())
				if err != nil {
					// handle error
				}
				if ip.To4() != nil {
					localAddr = ip
					break
				}
			}
		}
	}

	if localAddr == nil {
		// handle error, interface not found or no IPv4 address
	}

	transport := &http.Transport{
		DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
			localTCPAddr := &net.TCPAddr{
				IP:   localAddr,
				Port: localPort,
			}
			dialer := &net.Dialer{
				LocalAddr: localTCPAddr,
			}
			return dialer.DialContext(ctx, network, addr)
		},
		Timeout:   Timeout,
		Proxy:     http.ProxyFromEnvironment,
		// ... other transport configuration options
	}

	client := &http.Client{
		Transport: transport,
		Timeout:   Timeout,
		// ... other client configuration options
	}

	// ... rest of the function code
}

In the updated code, the downloadHandler function takes an additional interfaceName argument, which represents the name of the network interface you want to use. It then searches for the specified network interface and retrieves its IPv4 address.The DialContext function is modified to create a custom net.Dialer with a specific local address (localTCPAddr) that contains the desired local IP and port. This custom Dialer is then used to establish the network connection.To use this updated function, pass the network interface name and local port as arguments when calling the function, for example:

interfaceName := "eth0"
localPort := 12345
result := downloadHandler(interfaceName, localPort)
Make sure to replace interfaceName with the appropriate network interface name, adjust the other parts of the code as needed, and handle any error conditions.
@brainiac19 brainiac19 added the 功能建议 功能与建议 label Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
功能建议 功能与建议
Projects
None yet
Development

No branches or pull requests

1 participant