fix(windows): start a responsive tray immediately after installation
This commit is contained in:
1 parent
14cb3570c1
commit
d59b2d5463
13 files changed
+850
-106
No files matched your search
@@ -13,7 +13,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
@@ -23,6 +23,8 @@ import (
|
||||
const (
|
||||
pipeBufferBytes = 64 << 10
|
||||
pipeInstances = 8
|
||||
requestTimeout = 3 * time.Second
|
||||
peerTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -34,41 +36,53 @@ var (
|
||||
// run before any interactive user logs in; in that state no client can pass
|
||||
// the interactive-peer authorization check.
|
||||
func Serve(ctx context.Context, handler Handler) error {
|
||||
return serveNamedTrayPipe(ctx, PipeName, handler)
|
||||
}
|
||||
|
||||
func serveNamedTrayPipe(ctx context.Context, name string, handler Handler) error {
|
||||
if handler == nil {
|
||||
return errors.New("tray handler is required")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
var workers sync.WaitGroup
|
||||
defer func() { cancel(); workers.Wait() }()
|
||||
active := make(chan struct{}, pipeInstances)
|
||||
for {
|
||||
pipe, err := newTrayPipe()
|
||||
if err != nil {
|
||||
return fmt.Errorf("create tray pipe: %w", err)
|
||||
}
|
||||
connected := make(chan error, 1)
|
||||
go func() { connected <- winapi.ConnectNamedPipe(winapi.Handle(pipe.Fd()), nil) }()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
_ = pipe.Close()
|
||||
return nil
|
||||
case err := <-connected:
|
||||
if err != nil && !errors.Is(err, winapi.ERROR_PIPE_CONNECTED) {
|
||||
_ = pipe.Close()
|
||||
if ctx.Err() != nil {
|
||||
return nil
|
||||
}
|
||||
continue
|
||||
}
|
||||
go serveTrayPipe(ctx, pipe, handler)
|
||||
case active <- struct{}{}:
|
||||
}
|
||||
pipe, err := newTrayPipe(name)
|
||||
if err != nil {
|
||||
<-active
|
||||
return fmt.Errorf("create tray pipe: %w", err)
|
||||
}
|
||||
if err := connectTrayPipe(ctx, pipe); err != nil {
|
||||
_ = winapi.CloseHandle(pipe)
|
||||
<-active
|
||||
if ctx.Err() != nil {
|
||||
return nil
|
||||
}
|
||||
continue
|
||||
}
|
||||
workers.Add(1)
|
||||
go func() {
|
||||
defer workers.Done()
|
||||
defer func() { <-active }()
|
||||
serveTrayPipe(ctx, pipe, handler)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func newTrayPipe() (*os.File, error) {
|
||||
name, err := winapi.UTF16PtrFromString(PipeName)
|
||||
func newTrayPipe(pipeName string) (winapi.Handle, error) {
|
||||
name, err := winapi.UTF16PtrFromString(pipeName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return 0, err
|
||||
}
|
||||
descriptor, err := winapi.SecurityDescriptorFromString(pipeSDDL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return 0, err
|
||||
}
|
||||
attributes := &winapi.SecurityAttributes{
|
||||
Length: uint32(unsafe.Sizeof(winapi.SecurityAttributes{})),
|
||||
@@ -78,47 +92,49 @@ func newTrayPipe() (*os.File, error) {
|
||||
// client rejection belongs to the latter; placing it in open mode produces
|
||||
// ERROR_INVALID_PARAMETER on Windows. We inspect the client token directly
|
||||
// and never impersonate it, so no SQOS/impersonation flag is needed here.
|
||||
openMode := uint32(winapi.PIPE_ACCESS_DUPLEX)
|
||||
openMode := uint32(winapi.PIPE_ACCESS_DUPLEX | winapi.FILE_FLAG_OVERLAPPED)
|
||||
pipeMode := uint32(winapi.PIPE_TYPE_MESSAGE | winapi.PIPE_READMODE_MESSAGE | winapi.PIPE_WAIT | winapi.PIPE_REJECT_REMOTE_CLIENTS)
|
||||
handle, err := winapi.CreateNamedPipe(name, openMode, pipeMode, pipeInstances, pipeBufferBytes, pipeBufferBytes, 0, attributes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return 0, err
|
||||
}
|
||||
return os.NewFile(uintptr(handle), "rvbox-tray-pipe"), nil
|
||||
return handle, nil
|
||||
}
|
||||
|
||||
func serveTrayPipe(ctx context.Context, pipe *os.File, handler Handler) {
|
||||
if pipe == nil {
|
||||
return
|
||||
}
|
||||
defer pipe.Close()
|
||||
// A blocked Read must be interrupted when service shutdown cancels ctx.
|
||||
readDone := make(chan struct{})
|
||||
go func() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
_ = pipe.Close()
|
||||
case <-readDone:
|
||||
func serveTrayPipe(ctx context.Context, pipe winapi.Handle, handler Handler) {
|
||||
defer winapi.CloseHandle(pipe)
|
||||
ctx, cancel := context.WithTimeout(ctx, peerTimeout)
|
||||
defer cancel()
|
||||
respond := func(value any) {
|
||||
writeTrayResponse(ctx, pipe, value)
|
||||
// Keep buffered response bytes alive until the one-shot client closes.
|
||||
// Unlike FlushFileBuffers this read is cancellable and deadline-bounded;
|
||||
// no additional acknowledgement frame is required on the wire.
|
||||
buffer := make([]byte, maxFrameBytes)
|
||||
for ctx.Err() == nil {
|
||||
n, err := transferTrayPipe(ctx, pipe, buffer, false)
|
||||
if err != nil && !errors.Is(err, winapi.ERROR_MORE_DATA) || n == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
defer close(readDone)
|
||||
peer, err := peerFromPipe(winapi.Handle(pipe.Fd()))
|
||||
}
|
||||
peer, err := peerFromPipe(pipe)
|
||||
if err != nil {
|
||||
writeTrayResponse(pipe, err)
|
||||
respond(err)
|
||||
return
|
||||
}
|
||||
request, err := readTrayFrame(pipe)
|
||||
request, err := readTrayFrame(ctx, pipe)
|
||||
if err != nil {
|
||||
writeTrayResponse(pipe, err)
|
||||
respond(err)
|
||||
return
|
||||
}
|
||||
if err := Authorize(peer, request.Action); err != nil {
|
||||
writeTrayResponse(pipe, err)
|
||||
respond(err)
|
||||
return
|
||||
}
|
||||
response, err := handler(ctx, peer, request)
|
||||
if err != nil {
|
||||
writeTrayResponse(pipe, err)
|
||||
respond(err)
|
||||
return
|
||||
}
|
||||
if response.Action == 0 {
|
||||
@@ -127,22 +143,25 @@ func serveTrayPipe(ctx context.Context, pipe *os.File, handler Handler) {
|
||||
if response.Action != ActionStatus {
|
||||
response = Frame{Action: ActionStatus}
|
||||
}
|
||||
writeTrayResponse(pipe, response)
|
||||
respond(response)
|
||||
}
|
||||
|
||||
func readTrayFrame(reader io.Reader) (Frame, error) {
|
||||
func readTrayFrame(ctx context.Context, pipe winapi.Handle) (Frame, error) {
|
||||
buffer := make([]byte, maxFrameBytes)
|
||||
count, err := reader.Read(buffer)
|
||||
count, err := transferTrayPipe(ctx, pipe, buffer, false)
|
||||
if errors.Is(err, winapi.ERROR_MORE_DATA) {
|
||||
return Frame{}, ErrFrameTooLarge
|
||||
}
|
||||
if err != nil {
|
||||
return Frame{}, err
|
||||
}
|
||||
if count == len(buffer) {
|
||||
if int(count) == len(buffer) {
|
||||
return Frame{}, ErrFrameTooLarge
|
||||
}
|
||||
return Decode(buffer[:count])
|
||||
}
|
||||
|
||||
func writeTrayResponse(writer *os.File, value any) {
|
||||
func writeTrayResponse(ctx context.Context, pipe winapi.Handle, value any) {
|
||||
frame := Frame{Action: ActionStatus}
|
||||
switch response := value.(type) {
|
||||
case Frame:
|
||||
@@ -158,8 +177,7 @@ func writeTrayResponse(writer *os.File, value any) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, _ = writer.Write(encoded)
|
||||
_ = winapi.FlushFileBuffers(winapi.Handle(writer.Fd()))
|
||||
_, _ = transferTrayPipe(ctx, pipe, encoded, true)
|
||||
}
|
||||
|
||||
func peerFromPipe(pipe winapi.Handle) (Peer, error) {
|
||||
@@ -193,10 +211,19 @@ func peerFromPipe(pipe winapi.Handle) (Peer, error) {
|
||||
if err != nil {
|
||||
return Peer{}, ErrInvalidPeer
|
||||
}
|
||||
admin, err := token.IsMember(adminSID)
|
||||
groups, err := token.GetTokenGroups()
|
||||
if err != nil {
|
||||
return Peer{}, ErrInvalidPeer
|
||||
}
|
||||
// OpenProcessToken returns a primary token; CheckTokenMembership requires
|
||||
// an impersonation token. Inspect enabled groups directly, excluding the
|
||||
// deny-only Administrators SID in a normal split-token user's token.
|
||||
admin := false
|
||||
for _, group := range groups.AllGroups() {
|
||||
if group.Sid != nil && group.Sid.Equals(adminSID) && group.Attributes&winapi.SE_GROUP_ENABLED != 0 && group.Attributes&winapi.SE_GROUP_USE_FOR_DENY_ONLY == 0 {
|
||||
admin = true
|
||||
}
|
||||
}
|
||||
sid := user.User.Sid.String()
|
||||
return Peer{PID: pid, SessionID: sessionID, SID: sid, TokenVerified: true, Interactive: sessionID != 0, Administrator: admin, System: sid == "S-1-5-18"}, nil
|
||||
}
|
||||
@@ -205,22 +232,28 @@ func peerFromPipe(pipe winapi.Handle) (Peer, error) {
|
||||
// It retries only the transient ERROR_PIPE_BUSY state and never falls back to
|
||||
// an arbitrary filesystem/socket path.
|
||||
func Request(ctx context.Context, request Frame) (Frame, error) {
|
||||
return requestNamedTrayPipe(ctx, PipeName, request)
|
||||
}
|
||||
|
||||
func requestNamedTrayPipe(ctx context.Context, pipeName string, request Frame) (Frame, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, requestTimeout)
|
||||
defer cancel()
|
||||
encoded, err := Encode(request)
|
||||
if err != nil {
|
||||
return Frame{}, err
|
||||
}
|
||||
var pipe *os.File
|
||||
var pipe winapi.Handle
|
||||
for {
|
||||
if ctx.Err() != nil {
|
||||
return Frame{}, ctx.Err()
|
||||
}
|
||||
name, nameErr := winapi.UTF16PtrFromString(PipeName)
|
||||
name, nameErr := winapi.UTF16PtrFromString(pipeName)
|
||||
if nameErr != nil {
|
||||
return Frame{}, nameErr
|
||||
}
|
||||
handle, openErr := winapi.CreateFile(name, winapi.GENERIC_READ|winapi.GENERIC_WRITE, 0, nil, winapi.OPEN_EXISTING, 0, 0)
|
||||
handle, openErr := winapi.CreateFile(name, winapi.GENERIC_READ|winapi.GENERIC_WRITE, 0, nil, winapi.OPEN_EXISTING, winapi.FILE_FLAG_OVERLAPPED, 0)
|
||||
if openErr == nil {
|
||||
pipe = os.NewFile(uintptr(handle), "rvbox-tray-client")
|
||||
pipe = handle
|
||||
break
|
||||
}
|
||||
if !errors.Is(openErr, winapi.ERROR_PIPE_BUSY) {
|
||||
@@ -234,14 +267,15 @@ func Request(ctx context.Context, request Frame) (Frame, error) {
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
defer pipe.Close()
|
||||
defer winapi.CloseHandle(pipe)
|
||||
state := uint32(winapi.PIPE_READMODE_MESSAGE)
|
||||
_ = winapi.SetNamedPipeHandleState(winapi.Handle(pipe.Fd()), &state, nil, nil)
|
||||
if _, err := pipe.Write(encoded); err != nil {
|
||||
if err := winapi.SetNamedPipeHandleState(pipe, &state, nil, nil); err != nil {
|
||||
return Frame{}, err
|
||||
}
|
||||
if err := winapi.FlushFileBuffers(winapi.Handle(pipe.Fd())); err != nil {
|
||||
if n, err := transferTrayPipe(ctx, pipe, encoded, true); err != nil {
|
||||
return Frame{}, err
|
||||
} else if int(n) != len(encoded) {
|
||||
return Frame{}, io.ErrShortWrite
|
||||
}
|
||||
return readTrayFrame(pipe)
|
||||
return readTrayFrame(ctx, pipe)
|
||||
}
|
||||
Reference in new issue
Block a user