-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
server.go
111 lines (100 loc) · 2.22 KB
/
server.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//go:build windows
// +build windows
package main
import (
"encoding/gob"
"fmt"
"io"
"net"
"os"
"os/signal"
"syscall"
)
func server(args []string) int {
// make listener to communicate child process
lis, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
fmt.Fprintf(os.Stderr, "%v: cannot make listener\n", os.Args[0])
return 1
}
defer lis.Close()
// make sure executable name to avoid detecting same executable name
exe, err := os.Executable()
if err != nil {
fmt.Fprintf(os.Stderr, "%v: cannot find executable\n", os.Args[0])
return 1
}
cmdargs := []string{"-mode", lis.Addr().String()}
cmdargs = append(cmdargs, args...)
var errExec error
go func() {
err = _ShellExecuteAndWait(0, "runas", exe, makeCmdLine(cmdargs), "", syscall.SW_HIDE)
if err != nil {
errExec = err
lis.Close()
}
}()
conn, err := lis.Accept()
if err != nil {
if errExec != nil {
fmt.Fprintf(os.Stderr, "%v: %v\n", os.Args[0], errExec)
} else {
fmt.Fprintf(os.Stderr, "%v: cannot execute command: %v\n", os.Args[0], makeCmdLine(args))
}
return 1
}
defer conn.Close()
enc, dec := gob.NewEncoder(conn), gob.NewDecoder(conn)
err = enc.Encode(os.Environ())
if err != nil {
if errExec != nil {
fmt.Fprintf(os.Stderr, "%v: %v\n", os.Args[0], errExec)
} else {
fmt.Fprintf(os.Stderr, "%v: cannot execute command: %v\n", os.Args[0], makeCmdLine(args))
}
return 1
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt)
go func() {
for range sc {
enc.Encode(&msg{Name: "ctrlc"})
}
}()
defer close(sc)
go func() {
var b [256]byte
for {
n, err := os.Stdin.Read(b[:])
if err != nil {
// stdin was closed
if err == io.EOF {
enc.Encode(&msg{Name: "close"})
}
continue
}
err = enc.Encode(&msg{Name: "stdin", Data: b[:n]})
if err != nil {
break
}
}
}()
for {
var m msg
err = dec.Decode(&m)
if err != nil {
fmt.Fprintf(os.Stderr, "%v: cannot execute command: %v\n", os.Args[0], makeCmdLine(args))
return 1
}
switch m.Name {
case "stdout":
syscall.Write(syscall.Stdout, m.Data)
case "stderr":
syscall.Write(syscall.Stderr, m.Data)
case "error":
fmt.Fprintln(os.Stderr, m.Error)
case "exit":
return m.Exit
}
}
}