阶段十:完成 Go 基础包注释基线清零

This commit is contained in:
Christ committed 2026-08-15 05:42:49 +08:00
1 parent ea1f2ec430
commit d1a165f897
14 files changed
+135 -924

No files matched your search

+16
View File
@@ -13,6 +13,7 @@ import (
"time"
)
// nonPublicPrefixes 保存nonPublicPrefixes,供当前处理流程使用
var nonPublicPrefixes = []netip.Prefix{
netip.MustParsePrefix("0.0.0.0/8"), netip.MustParsePrefix("10.0.0.0/8"),
netip.MustParsePrefix("100.64.0.0/10"), netip.MustParsePrefix("127.0.0.0/8"),
@@ -28,6 +29,7 @@ var nonPublicPrefixes = []netip.Prefix{
// PublicHTTPClient 返回只允许连接公网 IP 的客户端。DNS 与每次重定向都会重新校验。
func PublicHTTPClient(timeout time.Duration) *http.Client {
// transport 保存transport,供当前处理流程使用
transport := &http.Transport{
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
return DialPublicContext(ctx, network, address, 10*time.Second)
@@ -51,7 +53,9 @@ func PublicHTTPClient(timeout time.Duration) *http.Client {
// TrustedEndpointHTTPClient 用于管理员明确配置的集成端点。目标地址完全由管理员
// 信任并负责,因此不限制 IP 类型、DNS 结果或 HTTP 重定向;仅校验 HTTP(S) URL。
// TrustedEndpointHTTPClient 负责TrustedEndpointHTTPClient相关处理。
func TrustedEndpointHTTPClient(rawBaseURL string, timeout time.Duration) (*http.Client, error) {
// baseURL、err 保存baseURL、err,供当前处理流程使用
baseURL, err := url.Parse(strings.TrimSpace(rawBaseURL))
if err != nil || baseURL.Hostname() == "" {
return nil, fmt.Errorf("服务地址无效")
@@ -59,6 +63,7 @@ func TrustedEndpointHTTPClient(rawBaseURL string, timeout time.Duration) (*http.
if baseURL.Scheme != "http" && baseURL.Scheme != "https" {
return nil, fmt.Errorf("服务地址只支持 http 或 https")
}
// transport 保存transport,供当前处理流程使用
transport := http.DefaultTransport.(*http.Transport).Clone()
return &http.Client{
Transport: transport,
@@ -66,7 +71,9 @@ func TrustedEndpointHTTPClient(rawBaseURL string, timeout time.Duration) (*http.
}, nil
}
// IsPublicIP 负责IsPublicIP相关处理。
func IsPublicIP(ip net.IP) bool {
// addr、ok 保存addr、ok,供当前处理流程使用
addr, ok := netip.AddrFromSlice(ip)
if !ok {
return false
@@ -75,6 +82,7 @@ func IsPublicIP(ip net.IP) bool {
if !addr.IsGlobalUnicast() {
return false
}
// prefix 表示当前遍历过程中的prefix
for _, prefix := range nonPublicPrefixes {
if prefix.Contains(addr) {
return false
@@ -89,23 +97,31 @@ func DialPublicContext(ctx context.Context, network, address string, timeout tim
"拒绝访问非公网地址", "连接公网地址失败")
}
// dialContextWithPolicy 负责dial上下文WithPolicy相关处理。
func dialContextWithPolicy(ctx context.Context, network, address string, timeout time.Duration,
allow func(net.IP) bool, deniedMessage, connectErrorMessage string,
) (net.Conn, error) {
// host、port、err 保存host、port、err,供当前处理流程使用
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
// ips、err 保存ips、err,供当前处理流程使用
ips, err := net.DefaultResolver.LookupIPAddr(ctx, host)
if err != nil {
return nil, err
}
// dialer 保存dialer,供当前处理流程使用
dialer := &net.Dialer{Timeout: timeout, KeepAlive: 30 * time.Second}
// lastErr 保存lastErr,供当前处理流程使用
var lastErr error
// foundAllowed 保存foundAllowed,供当前处理流程使用
foundAllowed := false
// resolved 表示当前遍历过程中的resolved
for _, resolved := range ips {
if allow(resolved.IP) {
foundAllowed = true
// conn、dialErr 保存conn、dialErr,供当前处理流程使用
conn, dialErr := dialer.DialContext(ctx, network, net.JoinHostPort(resolved.IP.String(), port))
if dialErr == nil {
return conn, nil
+21 -2
View File
@@ -9,7 +9,9 @@ import (
"testing"
)
// TestIsPublicIP 负责TestIsPublicIP相关处理。
func TestIsPublicIP(t *testing.T) {
// raw 表示当前遍历过程中的原始
for _, raw := range []string{
"127.0.0.1", "10.0.0.1", "172.16.1.1", "192.168.1.1", "169.254.169.254", "::1",
"100.64.0.1", "198.18.0.1", "192.0.2.1", "198.51.100.1", "203.0.113.1", "2001:db8::1",
@@ -23,30 +25,41 @@ func TestIsPublicIP(t *testing.T) {
}
}
// TestPublicHTTPClientRejectsLoopback 负责TestPublicHTTPClientRejectsLoopback相关处理。
func TestPublicHTTPClientRejectsLoopback(t *testing.T) {
// client 保存client,供当前处理流程使用
client := PublicHTTPClient(0)
// req 保存req,供当前处理流程使用
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://127.0.0.1:1", nil)
if _, err := client.Do(req); err == nil {
if // err 保存err,供当前处理流程使用
_, err := client.Do(req); err == nil {
t.Fatal("loopback request must be rejected")
}
}
// TestTrustedEndpointHTTPClientAllowsLoopbackAndUnspecifiedAddress 负责TestTrustedEndpointHTTPClientAllowsLoopbackAndUnspecifiedAddress相关处理。
func TestTrustedEndpointHTTPClientAllowsLoopbackAndUnspecifiedAddress(t *testing.T) {
// server 保存server,供当前处理流程使用
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNoContent)
}))
t.Cleanup(server.Close)
// port、err 保存port、err,供当前处理流程使用
_, port, err := net.SplitHostPort(strings.TrimPrefix(server.URL, "http://"))
if err != nil {
t.Fatal(err)
}
// host 表示当前遍历过程中的host
for _, host := range []string{"127.0.0.1", "0.0.0.0"} {
// baseURL 保存baseURL,供当前处理流程使用
baseURL := "http://" + net.JoinHostPort(host, port)
// client、clientErr 保存client、clientErr,供当前处理流程使用
client, clientErr := TrustedEndpointHTTPClient(baseURL+"/v1", 0)
if clientErr != nil {
t.Fatal(clientErr)
}
// resp、requestErr 保存resp、requestErr,供当前处理流程使用
resp, requestErr := client.Get(baseURL + "/v1/models")
if requestErr != nil {
t.Fatalf("trusted endpoint should reach %s: %v", host, requestErr)
@@ -58,11 +71,14 @@ func TestTrustedEndpointHTTPClientAllowsLoopbackAndUnspecifiedAddress(t *testing
}
}
// TestTrustedEndpointHTTPClientDoesNotApplyAddressPolicy 负责TestTrustedEndpointHTTPClientDoesNotApplyAddressPolicy相关处理。
func TestTrustedEndpointHTTPClientDoesNotApplyAddressPolicy(t *testing.T) {
// raw 表示当前遍历过程中的原始
for _, raw := range []string{
"http://0.0.0.0:8080/v1", "http://127.0.0.1:8080/v1", "http://169.254.169.254/v1",
"http://192.168.0.220/v1", "http://[::1]:8080/v1", "https://user:[email protected]/v1",
} {
// client、err 保存client、err,供当前处理流程使用
client, err := TrustedEndpointHTTPClient(raw, 0)
if err != nil {
t.Fatalf("admin-configured address should be accepted (%s): %v", raw, err)
@@ -73,9 +89,12 @@ func TestTrustedEndpointHTTPClientDoesNotApplyAddressPolicy(t *testing.T) {
}
}
// TestTrustedEndpointHTTPClientValidatesBaseURL 负责TestTrustedEndpointHTTPClientValidatesBaseURL相关处理。
func TestTrustedEndpointHTTPClientValidatesBaseURL(t *testing.T) {
// raw 表示当前遍历过程中的原始
for _, raw := range []string{"", "file:///tmp/model", "://bad"} {
if _, err := TrustedEndpointHTTPClient(raw, 0); err == nil {
if // err 保存err,供当前处理流程使用
_, err := TrustedEndpointHTTPClient(raw, 0); err == nil {
t.Fatalf("invalid base URL should fail: %q", raw)
}
}