/
client.go
170 lines (154 loc) · 3.61 KB
/
client.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package agent
import (
"fmt"
"github.com/mitchellh/go-ps"
"github.com/pkg/errors"
"io"
"net"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
)
type gopsProc struct {
pid int
executable string
file string
}
func (p *gopsProc) String() string {
return fmt.Sprintf("%v (pid %v)", p.executable, p.pid)
}
func GetGopsProcesses() ([]*gopsProc, error) {
var result []*gopsProc
matches, err := filepath.Glob(path.Join(os.TempDir(), fmt.Sprintf("%v.*.sock", SockPrefix)))
if err != nil {
return nil, err
}
re, err := regexp.Compile(`.*\.(?P<PID>\d+)\.sock`)
if err != nil {
return nil, err
}
for _, match := range matches {
res := re.FindSubmatch([]byte(match))
if len(res) != 0 {
pid, err := strconv.Atoi(string(res[1]))
if err != nil {
return nil, err
}
// process is alive and is reachable by us
if process, err := os.FindProcess(pid); err == nil {
if err = process.Signal(syscall.Signal(0)); err == nil {
proc, err := ps.FindProcess(pid)
if err != nil {
return nil, err
}
result = append(result, &gopsProc{
pid: pid,
executable: proc.Executable(),
file: match,
})
}
}
}
}
return result, nil
}
func GetUnixSockForPid(pid int) string {
return path.Join(os.TempDir(), fmt.Sprintf("%v.%v.sock", SockPrefix, pid))
}
func tooManyProcsError(procs []*gopsProc) error {
var list []string
for _, v := range procs {
list = append(list, v.String())
}
builder := &strings.Builder{}
builder.WriteString("too many gops-agent process found, including [")
builder.WriteString(strings.Join(list, ", "))
builder.WriteString("]")
return errors.New(builder.String())
}
// ParseGopsAddress tries to parse the target string, be it remote host:port
// or local process's PID or executable name
func ParseGopsAddress(args []string) (string, error) {
if len(args) == 0 {
return "", nil
}
target := args[0]
if strings.Contains(target, ":") {
return target, nil
}
// try to find port by pid then, connect to local
num, err := strconv.Atoi(target)
if err == nil {
filePath := GetUnixSockForPid(num)
if _, err := os.Stat(filePath); err == nil {
return "unix:" + filePath, nil
}
return "tcp:127.0.0.1:" + target, nil
}
return target, nil
}
func MakeRequest(addr string, signal byte, params []byte, out io.Writer) error {
return MakeRequestF(addr, signal, params, func(conn net.Conn) error {
_, err := io.Copy(out, conn)
return err
})
}
func MakeRequestF(addr string, signal byte, params []byte, f func(conn net.Conn) error) error {
network := "tcp"
if addr == "" {
network = "unix"
procs, err := GetGopsProcesses()
if err != nil {
return err
}
if len(procs) == 0 {
return errors.New("no gops-agent processes found")
}
if len(procs) > 1 {
return tooManyProcsError(procs)
}
addr = procs[0].file
} else {
procs, err := GetGopsProcesses()
found := false
if err == nil {
var filtered []*gopsProc
for _, proc := range procs {
if strings.Contains(proc.executable, addr) {
filtered = append(filtered, proc)
}
}
if len(filtered) > 1 {
return tooManyProcsError(filtered)
}
if len(filtered) == 1 {
found = true
network = "unix"
addr = filtered[0].file
}
}
if !found {
parts := strings.SplitN(addr, ":", 2)
if len(parts) == 2 {
network = parts[0]
addr = parts[1]
}
}
}
conn, err := net.Dial(network, addr)
if err != nil {
return err
}
var buf []byte
buf = append(buf, Magic...)
buf = append(buf, signal)
buf = append(buf, params...)
if _, err := conn.Write(buf); err != nil {
return err
}
return f(conn)
}