CodeSampleX

示例

golang.org/x/net v0.17.0: proxy.Direct

已验证示例 — golang golang.org/x/net v0.17.0: proxy.Direct. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: proxy.Direct implements proxy.Dialer and…

sha256:c84e07011aaf497e269decac1790c5bfbccb6f47b547ef526fe83d42ec3fa5f5

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-01

案例

HOW
目标
verify proxy.Direct in pkg:golang/golang.org/x/net@v0.17.0
包
符号
  • proxy.Direct
创建时间
2026-09-01T01:37:08Z

契约

  1. proxy.Direct implements proxy.Dialer and proxy.ContextDialer for direct network connections
  2. proxy.Direct.Dial connects to local TCP listeners and supports full duplex communication
  3. proxy.Direct.DialContext honors context cancellation and timeout deadlines
  4. proxy.Direct.Dial returns a network error when dialing an invalid or unreachable address

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify proxy.Direct in pkg:golang/golang.org/x/net@v0.17.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/net@v0.17.0
Demonstrate these symbols/APIs:
  - proxy.Direct
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:d13b9fa35ea9803d21058d0e754b31646631078dfddbca5881e1a3d0e3197aa5","contract":["proxy.Direct implements proxy.Dialer and proxy.ContextDialer for direct network connections","proxy.Direct.Dial connects to local TCP listeners and supports full duplex communication","proxy.Direct.DialContext honors context cancellation and timeout deadlines","proxy.Direct.Dial returns a network error when dialing an invalid or unreachable address"],"goal":"verify proxy.Direct in pkg:golang/golang.org/x/net@v0.17.0","kind":"HOW","packages":["pkg:golang/golang.org/x/net@v0.17.0"],"schemaVersion":1,"symbols":["proxy.Direct"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/net@v0.17.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/net@v0.17.0","symbols":["proxy.Direct"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.25.0

require golang.org/x/net v0.17.0
go.sum
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
main.go
package main

import (
	"context"
	"fmt"
	"io"
	"log"
	"net"
	"time"

	"golang.org/x/net/proxy"
)

func main() {
	listener, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		log.Fatalf("Failed to start listener: %v", err)
	}
	defer listener.Close()

	go func() {
		for {
			conn, err := listener.Accept()
			if err != nil {
				return
			}
			go func(c net.Conn) {
				defer c.Close()
				io.Copy(c, c)
			}(conn)
		}
	}()

	// 1. Direct dial using proxy.Direct.Dial
	conn, err := proxy.Direct.Dial("tcp", listener.Addr().String())
	if err != nil {
		log.Fatalf("proxy.Direct.Dial failed: %v", err)
	}
	defer conn.Close()

	message := "hello proxy.Direct"
	if _, err := conn.Write([]byte(message)); err != nil {
		log.Fatalf("Failed to write to connection: %v", err)
	}

	buf := make([]byte, len(message))
	if _, err := io.ReadFull(conn, buf); err != nil {
		log.Fatalf("Failed to read from connection: %v", err)
	}
	fmt.Printf("Received echo: %s\n", string(buf))

	// 2. Direct dial using proxy.ContextDialer interface
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()

	if ctxDialer, ok := any(proxy.Direct).(proxy.ContextDialer); ok {
		ctxConn, err := ctxDialer.DialContext(ctx, "tcp", listener.Addr().String())
		if err != nil {
			log.Fatalf("proxy.Direct.DialContext failed: %v", err)
		}
		defer ctxConn.Close()
	}

	fmt.Println("proxy.Direct verification complete.")
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify proxy.Direct in pkg:golang/golang.org/x/net@v0.17.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/net@v0.17.0"
  ],
  "symbols": [
    "proxy.Direct"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"context"
	"errors"
	"fmt"
	"io"
	"net"
	"os"
	"time"

	"golang.org/x/net/proxy"
)

func main() {
	// Assertion 1: proxy.Direct implements proxy.Dialer interface
	{
		var dialer proxy.Dialer = proxy.Direct
		if dialer == nil {
			fmt.Fprintf(os.Stderr, "Contract 1 failed: proxy.Direct does not satisfy proxy.Dialer\n")
			os.Exit(1)
		}
	}

	// Assertion 2: proxy.Direct implements proxy.ContextDialer interface
	{
		ctxDialer, ok := any(proxy.Direct).(proxy.ContextDialer)
		if !ok || ctxDialer == nil {
			fmt.Fprintf(os.Stderr, "Contract 2 failed: proxy.Direct does not satisfy proxy.ContextDialer\n")
			os.Exit(1)
		}
	}

	// Set up an offline in-process TCP listener
	listener, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Setup failed: net.Listen error: %v\n", err)
		os.Exit(1)
	}
	defer listener.Close()

	go func() {
		for {
			conn, err := listener.Accept()
			if err != nil {
				return
			}
			go func(c net.Conn) {
				defer c.Close()
				buf := make([]byte, 1024)
				n, err := c.Read(buf)
				if err == nil && n > 0 {
					c.Write(buf[:n])
				}
			}(conn)
		}
	}()

	addr := listener.Addr().String()

	// Assertion 3: proxy.Direct.Dial connects to local TCP listener and transfers data
	{
		conn, err := proxy.Direct.Dial("tcp", addr)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Contract 3 failed: proxy.Direct.Dial error: %v\n", err)
			os.Exit(1)
		}
		defer conn.Close()

		payload := []byte("ping-direct-dial")
		if _, err := conn.Write(payload); err != nil {
			fmt.Fprintf(os.Stderr, "Contract 3 failed: conn.Write error: %v\n", err)
			os.Exit(1)
		}

		reply := make([]byte, len(payload))
		if _, err := io.ReadFull(conn, reply); err != nil {
			fmt.Fprintf(os.Stderr, "Contract 3 failed: io.ReadFull error: %v\n", err)
			os.Exit(1)
		}

		if !bytes.Equal(reply, payload) {
			fmt.Fprintf(os.Stderr, "Contract 3 failed: reply mismatch: expected %q, got %q\n", payload, reply)
			os.Exit(1)
		}
	}

	// Assertion 4: proxy.Direct.DialContext connects with an active context
	{
		ctxDialer := any(proxy.Direct).(proxy.ContextDialer)
		ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
		defer cancel()

		conn, err := ctxDialer.DialContext(ctx, "tcp", addr)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Contract 4 failed: DialContext error: %v\n", err)
			os.Exit(1)
		}
		defer conn.Close()

		payload := []byte("ping-context-dial")
		if _, err := conn.Write(payload); err != nil {
			fmt.Fprintf(os.Stderr, "Contract 4 failed: conn.Write error: %v\n", err)
			os.Exit(1)
		}

		reply := make([]byte, len(payload))
		if _, err := io.ReadFull(conn, reply); err != nil {
			fmt.Fprintf(os.Stderr, "Contract 4 failed: io.ReadFull error: %v\n", err)
			os.Exit(1)
		}

		if !bytes.Equal(reply, payload) {
			fmt.Fprintf(os.Stderr, "Contract 4 failed: reply mismatch: expected %q, got %q\n", payload, reply)
			os.Exit(1)
		}
	}

	// Assertion 5: proxy.Direct.DialContext honors canceled context
	{
		ctxDialer := any(proxy.Direct).(proxy.ContextDialer)
		ctx, cancel := context.WithCancel(context.Background())
		cancel() // Cancel immediately before dial

		_, err := ctxDialer.DialContext(ctx, "tcp", addr)
		if err == nil {
			fmt.Fprintf(os.Stderr, "Contract 5 failed: expected error with canceled context, got nil\n")
			os.Exit(1)
		}
		if !errors.Is(err, context.Canceled) && !errors.Is(ctx.Err(), context.Canceled) {
			fmt.Fprintf(os.Stderr, "Contract 5 failed: expected context.Canceled error, got: %v\n", err)
			os.Exit(1)
		}
	}

	// Assertion 6: proxy.Direct.Dial fails when dialing an invalid network/address
	{
		_, err := proxy.Direct.Dial("invalid_network", "127.0.0.1:0")
		if err == nil {
			fmt.Fprintf(os.Stderr, "Contract 6 failed: expected error for unknown network, got nil\n")
			os.Exit(1)
		}
	}

	fmt.Println("All contract assertions passed.")
}

原始种子者

匿名