Skip to content

[Bug]: ipv6 url error #206

@zephyr-profile

Description

@zephyr-profile

TLS client version

v1.11.2

System information

MacOS 26
go 1.25.1

Issue description

error at:

func (rt *roundTripper) getDialTLSAddr(req *http.Request) string {
	host, port, err := net.SplitHostPort(req.URL.Host)
	if err == nil {
		return net.JoinHostPort(host, port)
	}

	return net.JoinHostPort(req.URL.Host, "443")
}

at:
bogdanfinn/tls-client@v1.11.2/roundtripper.go

request url:
https://[2001:9000::2]/test.html

When requesting the following address:

[2001:9000::2]

The error "missing port in address" occurs because the code calls net.SplitHostPort(req.URL.Host).

This system function requires the hostname to include a port.

However, a host without a port is passed in here.

As a result, the IP address for the Dial operation becomes: [[2001:9000::2]]

https://[2001:9000::2]/test.html is a valid URL.

Image

Steps to reproduce / Code Sample

func (rt *roundTripper) getDialTLSAddr(req *http.Request) (string, error) {
    host := req.URL.Hostname() // 使用 URL.Hostname() 可以更可靠地获取主机部分(自动去除端口和IPv6的方括号)
    port := req.URL.Port()     // 使用 URL.Port() 直接获取端口部分

    // 如果 URL 中明确指定了端口,则使用该端口
    if port != "" {
        return net.JoinHostPort(host, port), nil
    }

    // 如果没有指定端口,则根据 Scheme 设置默认端口
    defaultPort := "443"
    if req.URL.Scheme == "http" {
        defaultPort = "80"
    }
    return net.JoinHostPort(host, defaultPort), nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions