claudetui: Windows 10 gets Microsoft's ConPTY with the release, no folder to place (#5205)
a49f3745e used a conpty folder beside magpie.exe holding conpty.dll and
OpenConsole.exe, which a user had to fetch and place. Windows 10's own
ConPTY drops a paste's bracketed-paste marks and loses 1024-byte blocks,
so without that folder long messages reached Claude Code cut.
Now a Windows release carries the pair. internal/conptypair pins
Microsoft.Windows.Console.ConPTY 1.25.260930003 from nuget.org (MIT): the
package's SHA-512 as nuget.org publishes it (catalog packageHash) and
each file's SHA-256. `make conpty-pair` (go run ./build/conpty, before
release-cli and release-windows) downloads it, checks both, and writes
the pair to internal/conptypair/<arch>/ (gitignored), which -tags
conptypair embeds: about 1.2 MB per Windows binary. At run time, on a
build below 22000 with no conpty folder of the user's, the pair is
written to %LocalAppData%\magpie\conpty\<version>\<arch> (temp file and
rename), each file checked against its pin there before conpty.dll is
loaded; a file that doesn't match is written again. The order is the
user's conpty folder, then the bundled pair on Windows 10, then the
system's; any failure leaves the system's, where the whole-message check
(previous commit) catches a cut message. Windows 11 keeps its own.
1.25.260930003 is the version 3leg0ocat ran whole on Windows 10 19044;
CoreyKatzburg ran 1.25.261109001 on 19045 (under two weeks old).
Run:
- The package hash: the download's SHA-512 matches nuget.org's
packageHash; the four files' SHA-256 are taken from it.
- Authenticode on xiaomi-gpu (Get-AuthenticodeSignature): all four files
Valid, CN=Microsoft Corporation.
- go run ./build/conpty on mote: fetched and written; a second run keeps
them with no download; a package with the wrong hash is refused.
GOOS=windows go build -tags nogui,packedassets,conptypair for amd64
and arm64: built, 1.2 MB over the build without the tag.
- win-run (Windows 11 26200) go test -tags conptypair ./internal/conptypair:
ok; TestBundledOpens writes the pair from the embedded bytes and
CreatePseudoConsole through its conpty.dll returns S_OK.
- go test ./internal/conptypair ./internal/claudetui on mote: ok.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./... on mote: ok.
- Full suite on mote (-timeout 45m) was still running at push, per the
ship-at-once rule; a red it shows gets fixed or filed.
Not tried: Windows 10 itself (no Windows 10 box here; xiaomi-gpu is
Windows 11, whose own ConPTY doesn't have the bug, so the build gate
sends it to the system's), and a real Claude Code turn through the
bundled pair; the user folder route that loads the same way was
confirmed on Windows 10 by CoreyKatzburg (19045) and 3leg0ocat (19044).
This commit is contained in:
@@ -9,3 +9,5 @@
|
||||
/internal/update/launcher-*.com
|
||||
/internal/gui/assets.zip
|
||||
/.overlay/
|
||||
/internal/conptypair/amd64/
|
||||
/internal/conptypair/arm64/
|
||||
|
||||
@@ -32,7 +32,7 @@ ifeq ($(shell go env GOOS),android)
|
||||
TAGS = nogui
|
||||
endif
|
||||
|
||||
.PHONY: assets-pack build-release build cli install test test-ui app icons release release-cli release-windows release-linux clean dev dev-once
|
||||
.PHONY: assets-pack conpty-pair build-release build cli install test test-ui app icons release release-cli release-windows release-linux clean dev dev-once
|
||||
|
||||
# A release build embeds the UI's files as one zip instead of the tree
|
||||
# (internal/gui/assets_packed.go): a Windows or Linux download is the raw
|
||||
@@ -41,6 +41,12 @@ endif
|
||||
assets-pack:
|
||||
@go run ./build/assetpack internal/gui/assets internal/gui/assets.zip
|
||||
|
||||
# The ConPTY a Windows release carries for Windows 10 (internal/conptypair,
|
||||
# #5205): Microsoft's, from nuget.org, checked against its pinned hashes and
|
||||
# embedded with -tags conptypair.
|
||||
conpty-pair:
|
||||
@go run ./build/conpty
|
||||
|
||||
# Every build but the test runs takes Go's runtime with the macOS page
|
||||
# release of build/darwin/footprint-overlay.sh: with Go's own, heap Go
|
||||
# gave back stayed in Activity Monitor's Memory on macOS 27. It replaces
|
||||
@@ -114,12 +120,12 @@ release: clean build-release
|
||||
@cp magpie dist/magpie-$(shell go env GOOS)-$(shell go env GOARCH)
|
||||
@$(MAKE) --no-print-directory release-cli
|
||||
|
||||
release-cli: assets-pack
|
||||
release-cli: assets-pack conpty-pair
|
||||
@mkdir -p dist
|
||||
@$(OVERLAY) for t in $(TARGETS); do \
|
||||
os=$${t%/*}; arch=$${t#*/}; ext=""; [ $$os = windows ] && ext=.exe; \
|
||||
echo " $$os/$$arch (cli)"; \
|
||||
CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch go build $$F -tags "nogui packedassets" -trimpath -ldflags="$(LDFLAGS)" -o dist/magpie-cli-$$os-$$arch$$ext . || exit 1; \
|
||||
CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch go build $$F -tags "nogui packedassets conptypair" -trimpath -ldflags="$(LDFLAGS)" -o dist/magpie-cli-$$os-$$arch$$ext . || exit 1; \
|
||||
done
|
||||
|
||||
# The desktop app for Windows: the system WebView2 needs no cgo, so both
|
||||
@@ -132,14 +138,14 @@ release-cli: assets-pack
|
||||
# commit in them every release wrote a new one though the launcher was the
|
||||
# same (TestLauncherSameAtEveryCommit).
|
||||
WINVER = $(shell echo $(VERSION) | sed -E 's/^v//; s/[^0-9.].*//')
|
||||
release-windows: assets-pack
|
||||
release-windows: assets-pack conpty-pair
|
||||
@mkdir -p dist
|
||||
@go run github.com/tc-hib/go-winres@v0.3.3 make --in build/windows/winres.json --arch amd64,arm64 --out rsrc \
|
||||
--file-version "$(or $(WINVER),0.0.0)" --product-version "$(or $(WINVER),0.0.0)"
|
||||
@for arch in amd64 arm64; do \
|
||||
echo " windows/$$arch (app)"; \
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=$$arch go build -trimpath -buildvcs=false -ldflags="-s -w" -o internal/update/launcher-$$arch.com ./internal/update/launcher || exit 1; \
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=$$arch go build -tags production,comlauncher,packedassets -trimpath -ldflags="$(LDFLAGS) -H windowsgui" -o dist/magpie-windows-$$arch.exe . || exit 1; \
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=$$arch go build -tags production,comlauncher,packedassets,conptypair -trimpath -ldflags="$(LDFLAGS) -H windowsgui" -o dist/magpie-windows-$$arch.exe . || exit 1; \
|
||||
done; rm -f rsrc_windows_*.syso internal/update/launcher-*.com
|
||||
|
||||
# The desktop app for Linux links GTK 3 and WebKitGTK 4.1 through cgo, so it
|
||||
@@ -150,7 +156,7 @@ release-linux: assets-pack
|
||||
@go build -tags "$(TAGS) packedassets" -trimpath -ldflags="$(LDFLAGS)" -o dist/magpie-linux-$(shell go env GOARCH) .
|
||||
|
||||
clean:
|
||||
rm -rf magpie magpie.exe magpie.app dist .overlay internal/gui/assets.zip rsrc_windows_*.syso internal/update/launcher-*.com
|
||||
rm -rf magpie magpie.exe magpie.app dist .overlay internal/gui/assets.zip rsrc_windows_*.syso internal/update/launcher-*.com internal/conptypair/amd64 internal/conptypair/arm64
|
||||
|
||||
# Development: the UI is served from internal/gui/assets and the window
|
||||
# reloads itself when a file there is saved. With fswatch installed, the
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
// conpty fetches the ConPTY a Windows release carries for Windows 10
|
||||
// (internal/conptypair, #5205): Microsoft.Windows.Console.ConPTY from
|
||||
// nuget.org, checked against the SHA-512 nuget.org publishes for it, and
|
||||
// writes each architecture's conpty.dll and OpenConsole.exe, each checked
|
||||
// against its pinned SHA-256, to internal/conptypair/<arch>/ for -tags
|
||||
// conptypair to embed. Files there already whole are kept, with no
|
||||
// download.
|
||||
//
|
||||
// Usage: go run ./build/conpty [package.nupkg]
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"crypto/sha512"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/conptypair"
|
||||
)
|
||||
|
||||
const out = "internal/conptypair"
|
||||
|
||||
func main() {
|
||||
if err := run(os.Args[1:]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "conpty:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(args []string) error {
|
||||
if whole() {
|
||||
return nil
|
||||
}
|
||||
var pkg []byte
|
||||
var err error
|
||||
if len(args) > 0 {
|
||||
pkg, err = os.ReadFile(args[0])
|
||||
} else {
|
||||
pkg, err = fetch()
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
got := sha512.Sum512(pkg)
|
||||
if h := base64.StdEncoding.EncodeToString(got[:]); h != conptypair.SHA512 {
|
||||
return fmt.Errorf("package SHA-512 %s, nuget.org publishes %s", h, conptypair.SHA512)
|
||||
}
|
||||
z, err := zip.NewReader(bytes.NewReader(pkg), int64(len(pkg)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for arch, files := range conptypair.Files {
|
||||
for _, f := range files {
|
||||
b, err := read(z, f.Zip)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
path := filepath.Join(out, arch, f.Name)
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(path, b, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := conptypair.Check(path, f.SHA256); err != nil {
|
||||
_ = os.Remove(path)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("conpty: ConPTY", conptypair.Version, "written to", out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// whole is whether every file is there and pinned already.
|
||||
func whole() bool {
|
||||
for arch, files := range conptypair.Files {
|
||||
for _, f := range files {
|
||||
if conptypair.Check(filepath.Join(out, arch, f.Name), f.SHA256) != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func fetch() ([]byte, error) {
|
||||
c := &http.Client{Timeout: 5 * time.Minute}
|
||||
var last error
|
||||
for try := 0; try < 3; try++ {
|
||||
if try > 0 {
|
||||
time.Sleep(time.Duration(try) * 5 * time.Second)
|
||||
}
|
||||
resp, err := c.Get(conptypair.URL)
|
||||
if err != nil {
|
||||
last = err
|
||||
continue
|
||||
}
|
||||
b, err := io.ReadAll(io.LimitReader(resp.Body, 64<<20))
|
||||
resp.Body.Close()
|
||||
if err == nil && resp.StatusCode != http.StatusOK {
|
||||
err = fmt.Errorf("%s: %s", conptypair.URL, resp.Status)
|
||||
}
|
||||
if err == nil {
|
||||
return b, nil
|
||||
}
|
||||
last = err
|
||||
}
|
||||
return nil, last
|
||||
}
|
||||
|
||||
func read(z *zip.Reader, name string) ([]byte, error) {
|
||||
f, err := z.Open(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("package has no %s: %w", name, err)
|
||||
}
|
||||
defer f.Close()
|
||||
return io.ReadAll(io.LimitReader(f, 16<<20))
|
||||
}
|
||||
@@ -493,8 +493,8 @@ path and the padded one were each taken as `[Image #1]`, and the bare
|
||||
path with no marks stayed words, as the reporter saw; the helper itself
|
||||
on that claude.exe, padded and bracketed, put the image in the
|
||||
transcript. Windows 10 itself is not tried; the padded path doesn't
|
||||
need the marks. Long pasted text there still loses blocks (#5205); the
|
||||
whole-message check below catches a message cut so.
|
||||
need the marks. Long pasted text there lost blocks (#5205); the ConPTY
|
||||
below keeps them, and the whole-message check catches what is still cut.
|
||||
If the interactive mode still doesn't take the image, the
|
||||
turn goes to `-p` as below, which the plan counts at about twice the
|
||||
interactive mode's and which starts without the interactive run's
|
||||
@@ -514,7 +514,24 @@ run (Off meanwhile, the client gone) the failure is a 400, not a 502
|
||||
`TestShimPaddedImageTakenWithoutMarks` the padded paste on a console that
|
||||
drops the marks.
|
||||
|
||||
The helper checks that a message reached Claude
|
||||
On Windows 10 the helper doesn't use Windows' own ConPTY when it can
|
||||
avoid it (#5205; `console` in `child_windows.go`). In order: a `conpty`
|
||||
folder beside magpie.exe holding `conpty.dll` and `OpenConsole.exe`, the
|
||||
user's own; then, below build 22000 (`win11`), the pair a release carries
|
||||
(`internal/conptypair`): Microsoft.Windows.Console.ConPTY
|
||||
1.25.260930003 from nuget.org (MIT, both files signed by Microsoft), the
|
||||
version 3leg0ocat ran whole on 19044. The release fetches it
|
||||
(`go run ./build/conpty`, `make conpty-pair` before `release-cli` and
|
||||
`release-windows`), checks the package against the SHA-512 nuget.org
|
||||
publishes and each file against its pinned SHA-256, and embeds the pair
|
||||
(`-tags conptypair`, about 1.2MB per Windows binary). At run time
|
||||
`conptypair.Ready` writes it to `%LocalAppData%\magpie\conpty\<version>\<arch>`
|
||||
(temp file and rename) and checks both files there against their pins
|
||||
before `conpty.dll` is loaded; a file that doesn't match is written again.
|
||||
Anything that fails leaves the system's ConPTY, as does Windows 11, whose
|
||||
own keeps the marks. A dev build has no pair (`conptypair.Bundled`).
|
||||
|
||||
Whatever the console, the helper checks that a message reached Claude
|
||||
Code whole (`whole.go`, from 3leg0ocat's patch). It took one as sent once
|
||||
a row of it reached the transcript, whatever the row held, so a cut
|
||||
message's tail was answered as the message. Now, while a message just sent
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
|
||||
"github.com/yetone/magpie/internal/appdir"
|
||||
"github.com/yetone/magpie/internal/conptypair"
|
||||
"github.com/yetone/magpie/internal/proc"
|
||||
)
|
||||
|
||||
@@ -30,11 +32,15 @@ var endSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}
|
||||
|
||||
// pseudoConsole is where CreatePseudoConsole and ClosePseudoConsole come
|
||||
// from. Windows 10's own (conhost 10.0.19041) drops a paste's bracketed
|
||||
// paste marks, so Claude Code takes a long message as many pastes and
|
||||
// submits a part of it (yetone/magpie#5205); the ConPTY Windows Terminal
|
||||
// ships (conpty.dll, which runs the OpenConsole.exe beside it) keeps them.
|
||||
// dsh-cross: a conpty folder beside magpie's executable holding both is
|
||||
// used first; without it, the system's.
|
||||
// paste marks and loses 1024-byte blocks of a long write, so Claude Code
|
||||
// takes a long message as many pastes and gets a part of it
|
||||
// (yetone/magpie#5205); the ConPTY Windows Terminal ships (conpty.dll,
|
||||
// which runs the OpenConsole.exe beside it) keeps them. In order:
|
||||
// - dsh-cross: a conpty folder beside magpie's executable holding both,
|
||||
// the user's own;
|
||||
// - on Windows 10, the pair a release carries (internal/conptypair),
|
||||
// written to magpie's cache and checked against its pins there;
|
||||
// - the system's.
|
||||
type pseudoConsole struct {
|
||||
create, close *windows.LazyProc
|
||||
// system: Windows' own, kernel32's
|
||||
@@ -45,9 +51,17 @@ var console = sync.OnceValue(func() pseudoConsole {
|
||||
if exe, err := os.Executable(); err == nil {
|
||||
dir := filepath.Join(filepath.Dir(exe), "conpty")
|
||||
if _, err := os.Stat(filepath.Join(dir, "OpenConsole.exe")); err == nil {
|
||||
dll := windows.NewLazyDLL(filepath.Join(dir, "conpty.dll"))
|
||||
if dll.NewProc("CreatePseudoConsole").Find() == nil && dll.NewProc("ClosePseudoConsole").Find() == nil {
|
||||
return pseudoConsole{dll.NewProc("CreatePseudoConsole"), dll.NewProc("ClosePseudoConsole"), false}
|
||||
if c, ok := conptyIn(dir); ok {
|
||||
return c
|
||||
}
|
||||
}
|
||||
}
|
||||
if windows.RtlGetVersion().BuildNumber < win11 && conptypair.Bundled() {
|
||||
if cache, err := appdir.SystemCache(); err == nil {
|
||||
if dir, err := conptypair.Ready(cache); err == nil {
|
||||
if c, ok := conptyIn(dir); ok {
|
||||
return c
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,11 +69,25 @@ var console = sync.OnceValue(func() pseudoConsole {
|
||||
return pseudoConsole{k.NewProc("CreatePseudoConsole"), k.NewProc("ClosePseudoConsole"), true}
|
||||
})
|
||||
|
||||
// win11 is Windows 11's first build: the ConPTY of every one since keeps
|
||||
// a paste's marks and every block of it.
|
||||
const win11 = 22000
|
||||
|
||||
// conptyIn is the ConPTY of a folder's conpty.dll, ok when it has both.
|
||||
func conptyIn(dir string) (pseudoConsole, bool) {
|
||||
dll := windows.NewLazyDLL(filepath.Join(dir, "conpty.dll"))
|
||||
create, closePC := dll.NewProc("CreatePseudoConsole"), dll.NewProc("ClosePseudoConsole")
|
||||
if create.Find() != nil || closePC.Find() != nil {
|
||||
return pseudoConsole{}, false
|
||||
}
|
||||
return pseudoConsole{create, closePC, false}, true
|
||||
}
|
||||
|
||||
// cutHint is what a message Claude Code didn't get whole (whole.go) is
|
||||
// told of where it was lost: on Windows 10's own ConPTY, how to use
|
||||
// another (#5205).
|
||||
func cutHint() string {
|
||||
if !console().system || windows.RtlGetVersion().BuildNumber >= 22000 {
|
||||
if !console().system || windows.RtlGetVersion().BuildNumber >= win11 {
|
||||
return ""
|
||||
}
|
||||
// short, for the bridge's log line to hold it whole
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
//go:build conptypair
|
||||
|
||||
package conptypair
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
// TestBundledOpens: the pair a release carries is written whole and its
|
||||
// conpty.dll opens a pseudo console, which starts the OpenConsole.exe
|
||||
// beside it.
|
||||
func TestBundledOpens(t *testing.T) {
|
||||
// the loaded conpty.dll can't be removed while the test binary runs,
|
||||
// so the folder is removed as far as it can be, not by t.TempDir
|
||||
cache, err := os.MkdirTemp("", "conptypair-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.RemoveAll(cache) })
|
||||
dir, err := Ready(cache)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dll := windows.NewLazyDLL(filepath.Join(dir, "conpty.dll"))
|
||||
create, closePC := dll.NewProc("CreatePseudoConsole"), dll.NewProc("ClosePseudoConsole")
|
||||
if err := create.Find(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
inR, inW, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
outR, outW, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer inW.Close()
|
||||
defer outR.Close()
|
||||
size := windows.Coord{X: 80, Y: 25}
|
||||
var h windows.Handle
|
||||
r, _, _ := create.Call(uintptr(*(*uint32)(unsafe.Pointer(&size))), inR.Fd(), outW.Fd(), 0, uintptr(unsafe.Pointer(&h)))
|
||||
inR.Close()
|
||||
outW.Close()
|
||||
// ClosePseudoConsole waits for what the console draws to be read
|
||||
go func() { _, _ = io.Copy(io.Discard, outR) }()
|
||||
if r != 0 {
|
||||
t.Fatalf("CreatePseudoConsole: 0x%x", r)
|
||||
}
|
||||
_, _, _ = closePC.Call(uintptr(h))
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
//go:build conptypair
|
||||
|
||||
package conptypair
|
||||
|
||||
import "embed"
|
||||
|
||||
// The pair go run ./build/conpty fetched, checked and wrote here.
|
||||
//
|
||||
//go:embed amd64/conpty.dll amd64/OpenConsole.exe
|
||||
var amd64 embed.FS
|
||||
|
||||
func init() { bundled, arch = amd64, "amd64" }
|
||||
@@ -0,0 +1,12 @@
|
||||
//go:build conptypair
|
||||
|
||||
package conptypair
|
||||
|
||||
import "embed"
|
||||
|
||||
// The pair go run ./build/conpty fetched, checked and wrote here.
|
||||
//
|
||||
//go:embed arm64/conpty.dll arm64/OpenConsole.exe
|
||||
var arm64 embed.FS
|
||||
|
||||
func init() { bundled, arch = arm64, "arm64" }
|
||||
@@ -0,0 +1,145 @@
|
||||
// Package conptypair is the ConPTY a Windows release carries for Windows
|
||||
// 10, whose own drops a paste's bracketed-paste marks and loses 1024-byte
|
||||
// blocks of a long write (yetone/magpie#5205): Microsoft's
|
||||
// Microsoft.Windows.Console.ConPTY package from nuget.org (MIT), its
|
||||
// conpty.dll and the OpenConsole.exe it runs, both signed by Microsoft.
|
||||
//
|
||||
// The release fetches the package (go run ./build/conpty), checks it
|
||||
// against the SHA-512 nuget.org publishes for it and each file against the
|
||||
// SHA-256 pinned here, and embeds the pair (-tags conptypair). At run time
|
||||
// Ready writes them to a cache folder and checks them there again before
|
||||
// they are loaded. A build without the tag carries none (Bundled is
|
||||
// false), and the system's ConPTY is used.
|
||||
package conptypair
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Version is the package's version: the one 3leg0ocat ran on Windows 10
|
||||
// 19044 with every message whole (#5205).
|
||||
const Version = "1.25.260930003"
|
||||
|
||||
// URL is where the package is fetched, nuget.org's own.
|
||||
const URL = "https://api.nuget.org/v3-flatcontainer/microsoft.windows.console.conpty/" + Version +
|
||||
"/microsoft.windows.console.conpty." + Version + ".nupkg"
|
||||
|
||||
// SHA512 is the package's hash as nuget.org publishes it (its catalog
|
||||
// entry's packageHash, base64), checked against the download.
|
||||
const SHA512 = "LuqMk9r5oTzeRne35IOlUdSQ7DkjorOIbcnODGZ81MvmUpTr/1VBQz3fAdCNCwkfuR0Mluq+H7rS3Hes/A1fOw=="
|
||||
|
||||
// File is one of the pair: its name, where the package holds it and its
|
||||
// SHA-256.
|
||||
type File struct {
|
||||
Name, Zip, SHA256 string
|
||||
}
|
||||
|
||||
// Files are each architecture's pair.
|
||||
var Files = map[string][]File{
|
||||
"amd64": {
|
||||
{"conpty.dll", "runtimes/win-x64/native/conpty.dll", "feeef341d891643c62d30b6b07800bc70f0bc148f44cb8c3bee84aa557ae805a"},
|
||||
{"OpenConsole.exe", "build/native/runtimes/x64/OpenConsole.exe", "3d66b23d0a71bb8eed2b77edc8b9df9bf54ce6c8fb74c863a30e760997f80586"},
|
||||
},
|
||||
"arm64": {
|
||||
{"conpty.dll", "runtimes/win-arm64/native/conpty.dll", "151a02355227710c20c9f8f1eaf68ac4668aa09cdc921adc7ec661804d8eb1bf"},
|
||||
{"OpenConsole.exe", "build/native/runtimes/arm64/OpenConsole.exe", "4313825a3ba634d113cc6ed3bc8ce735e3c19d360cecd985794787479469f91f"},
|
||||
},
|
||||
}
|
||||
|
||||
// bundled is the embedded pair, under <arch>/, nil without -tags
|
||||
// conptypair; arch is the one it holds.
|
||||
var (
|
||||
bundled fs.FS
|
||||
arch string
|
||||
)
|
||||
|
||||
// Bundled is whether this build carries the pair.
|
||||
func Bundled() bool { return bundled != nil }
|
||||
|
||||
// Ready writes the bundled pair under cache (cache/conpty/<Version>/<arch>)
|
||||
// unless it is there whole already, checks each file there against its
|
||||
// pin, and returns the folder, for its conpty.dll to be loaded. A file
|
||||
// that doesn't match is written again; one that still doesn't is an error,
|
||||
// and nothing there is to be loaded.
|
||||
func Ready(cache string) (string, error) {
|
||||
if bundled == nil {
|
||||
return "", errors.New("no ConPTY in this build")
|
||||
}
|
||||
dir := filepath.Join(cache, "conpty", Version, arch)
|
||||
return dir, write(bundled, arch, Files[arch], dir)
|
||||
}
|
||||
|
||||
// write puts src's <arch>/<name> of each file in dir, each checked against
|
||||
// its pin before it is written and after.
|
||||
func write(src fs.FS, arch string, files []File, dir string) error {
|
||||
if len(files) == 0 {
|
||||
return fmt.Errorf("no ConPTY pinned for %s", arch)
|
||||
}
|
||||
for _, f := range files {
|
||||
path := filepath.Join(dir, f.Name)
|
||||
if Check(path, f.SHA256) == nil {
|
||||
continue
|
||||
}
|
||||
b, err := fs.ReadFile(src, arch+"/"+f.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := matches(b, f.SHA256); err != nil {
|
||||
return fmt.Errorf("bundled %s: %w", f.Name, err)
|
||||
}
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
tmp, err := os.CreateTemp(dir, f.Name+".*.tmp")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tmp.Write(b)
|
||||
if cerr := tmp.Close(); err == nil {
|
||||
err = cerr
|
||||
}
|
||||
if err == nil {
|
||||
// a file in use (an OpenConsole.exe running) can't be
|
||||
// replaced: then what is there is checked below
|
||||
err = os.Rename(tmp.Name(), path)
|
||||
}
|
||||
if err != nil {
|
||||
_ = os.Remove(tmp.Name())
|
||||
}
|
||||
if err := Check(path, f.SHA256); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check is whether the file at path is the one pinned by sum.
|
||||
func Check(path, sum string) error {
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := matches(b, sum); err != nil {
|
||||
return fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func matches(b []byte, sum string) error {
|
||||
want, err := hex.DecodeString(sum)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
got := sha256.Sum256(b)
|
||||
if !bytes.Equal(got[:], want) {
|
||||
return fmt.Errorf("SHA-256 %x, pinned %s", got, sum)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package conptypair
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func sum(s string) string {
|
||||
b := sha256.Sum256([]byte(s))
|
||||
return hex.EncodeToString(b[:])
|
||||
}
|
||||
|
||||
func pair() (fstest.MapFS, []File) {
|
||||
src := fstest.MapFS{
|
||||
"amd64/conpty.dll": {Data: []byte("dll")},
|
||||
"amd64/OpenConsole.exe": {Data: []byte("exe")},
|
||||
}
|
||||
return src, []File{{"conpty.dll", "", sum("dll")}, {"OpenConsole.exe", "", sum("exe")}}
|
||||
}
|
||||
|
||||
// TestWrite: the pair is written where it is to be loaded, and a file
|
||||
// there that isn't the pinned one (half-written, or put there by another)
|
||||
// is written again.
|
||||
func TestWrite(t *testing.T) {
|
||||
src, files := pair()
|
||||
dir := filepath.Join(t.TempDir(), "x")
|
||||
if err := write(src, "amd64", files, dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, f := range files {
|
||||
if err := Check(filepath.Join(dir, f.Name), f.SHA256); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "conpty.dll"), []byte("other"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := write(src, "amd64", files, dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b, _ := os.ReadFile(filepath.Join(dir, "conpty.dll")); string(b) != "dll" {
|
||||
t.Fatalf("a changed conpty.dll was left: %q", b)
|
||||
}
|
||||
left, _ := filepath.Glob(filepath.Join(dir, "*.tmp"))
|
||||
if len(left) > 0 {
|
||||
t.Fatalf("temp files left: %v", left)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWriteRefusesUnpinned: bundled bytes that aren't the pinned ones are
|
||||
// never written, and the folder is not to be loaded.
|
||||
func TestWriteRefusesUnpinned(t *testing.T) {
|
||||
src, files := pair()
|
||||
src["amd64/OpenConsole.exe"] = &fstest.MapFile{Data: []byte("changed")}
|
||||
dir := t.TempDir()
|
||||
err := write(src, "amd64", files, dir)
|
||||
if err == nil || !strings.Contains(err.Error(), "SHA-256") {
|
||||
t.Fatalf("got %v, want a SHA-256 mismatch", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "OpenConsole.exe")); err == nil {
|
||||
t.Fatal("an unpinned OpenConsole.exe was written")
|
||||
}
|
||||
if err := write(src, "386", nil, dir); err == nil {
|
||||
t.Fatal("an architecture with no pin was written")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPins: each architecture pins both files, with a full SHA-256.
|
||||
func TestPins(t *testing.T) {
|
||||
for _, a := range []string{"amd64", "arm64"} {
|
||||
fs := Files[a]
|
||||
if len(fs) != 2 || fs[0].Name != "conpty.dll" || fs[1].Name != "OpenConsole.exe" {
|
||||
t.Fatalf("%s pins %v", a, fs)
|
||||
}
|
||||
for _, f := range fs {
|
||||
if b, err := hex.DecodeString(f.SHA256); err != nil || len(b) != 32 {
|
||||
t.Fatalf("%s %s: %q isn't a SHA-256", a, f.Name, f.SHA256)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !strings.Contains(URL, "/"+Version+"/") || !strings.HasPrefix(URL, "https://api.nuget.org/") {
|
||||
t.Fatalf("URL %s", URL)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReadyWithoutPair: a build without -tags conptypair has nothing to
|
||||
// write, and says so.
|
||||
func TestReadyWithoutPair(t *testing.T) {
|
||||
if Bundled() {
|
||||
t.Skip("built with the pair")
|
||||
}
|
||||
if _, err := Ready(t.TempDir()); err == nil {
|
||||
t.Fatal("Ready without a pair")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user