-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_gateway_windows.go
97 lines (84 loc) · 1.78 KB
/
file_gateway_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package ipc
import (
"io"
"net"
"os"
"golang.org/x/sys/windows"
)
type fileData struct {
Handle windows.Handle
Name string
withData bool
}
func (f *fileData) serialize(w io.Writer) error {
bw := &bytesWriter{w, nil}
bw.write(uint64(f.Handle))
bw.writeBytes([]byte(f.Name))
bw.write(f.withData)
return bw.err
}
func (f *fileData) deserialize(r io.Reader) error {
var ui64 uint64
br := &bytesReader{r, nil}
br.read(&ui64)
f.Handle = windows.Handle(ui64)
if b := br.readBytes(); b != nil {
f.Name = string(b)
}
br.read(&f.withData)
return br.err
}
type fileGateway struct {
gateway
}
func (gw *fileGateway) send(conn net.Conn, f *os.File, msg []byte) (err error) {
rawFile, err := f.SyscallConn()
if err != nil {
return
}
rawFile.Control(func(fd uintptr) {
err = gw.sendImpl(conn, msg, func() (s serializer, err error) {
thisPHandle, err := windows.GetCurrentProcess()
if err != nil {
return
}
defer windows.CloseHandle(thisPHandle)
targetPHandle, err := windows.OpenProcess(windows.PROCESS_DUP_HANDLE, false, gw.pid)
if err != nil {
return
}
defer windows.CloseHandle(targetPHandle)
fdata := fileData{
Name: f.Name(),
withData: len(msg) > 0,
}
err = windows.DuplicateHandle(
thisPHandle,
windows.Handle(fd),
targetPHandle,
&fdata.Handle,
0,
false,
windows.DUPLICATE_SAME_ACCESS)
if err != nil {
return
}
return &fdata, nil
})
})
if err == nil {
f.Close()
}
return
}
func (gw *fileGateway) receive(conn net.Conn) (f *os.File, withData bool, err error) {
var fdata fileData
err = gw.receiveImpl(conn, &fdata)
if err != nil {
return
}
return os.NewFile(uintptr(fdata.Handle), fdata.Name), fdata.withData, nil
}
func newFileGateway() *fileGateway {
return &fileGateway{}
}