forked from kryptco/kr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.go
183 lines (162 loc) · 3.55 KB
/
socket.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
171
172
173
174
175
176
177
178
179
180
181
182
183
package kr
import (
"bufio"
"fmt"
"net"
"net/http"
"strings"
"os"
"os/exec"
"os/user"
"path/filepath"
"time"
)
func User() string {
user := os.Getenv("USER")
if user == "" {
whoami, err := exec.Command("whoami").Output()
if err == nil {
user = strings.TrimSpace(string(whoami))
os.Setenv("USER", user)
}
}
return user
}
func HomeDir() (home string) {
user, err := user.Lookup(User())
if err == nil && user != nil {
home = user.HomeDir
} else {
home = os.Getenv("HOME")
err = nil
}
if os.Getenv("HOME") != home {
os.Setenv("HOME", home)
}
return
}
func KrDir() (krPath string, err error) {
home := HomeDir()
if err != nil {
return
}
krPath = filepath.Join(home, ".kr")
err = os.MkdirAll(krPath, os.FileMode(0700))
return
}
func NotifyDir() (krPath string, err error) {
home := HomeDir()
if err != nil {
return
}
krPath = filepath.Join(home, ".kr", "notify")
err = os.MkdirAll(krPath, os.FileMode(0700))
return
}
func NotifyDirFile(file string) (fullPath string, err error) {
notifyPath, err := NotifyDir()
if err != nil {
return
}
fullPath = filepath.Join(notifyPath, file)
return
}
func KrDirFile(file string) (fullPath string, err error) {
krPath, err := KrDir()
if err != nil {
return
}
fullPath = filepath.Join(krPath, file)
return
}
const AGENT_SOCKET_FILENAME = "krd-agent.sock"
func AgentListen() (listener net.Listener, err error) {
socketPath, err := KrDirFile(AGENT_SOCKET_FILENAME)
if err != nil {
return
}
// delete UNIX socket in case daemon was not killed cleanly
_ = os.Remove(socketPath)
listener, err = net.Listen("unix", socketPath)
return
}
const DAEMON_SOCKET_FILENAME = "krd.sock"
func DaemonListen() (listener net.Listener, err error) {
socketPath, err := KrDirFile(DAEMON_SOCKET_FILENAME)
if err != nil {
return
}
// delete UNIX socket in case daemon was not killed cleanly
_ = os.Remove(socketPath)
listener, err = net.Listen("unix", socketPath)
return
}
const HOST_AUTH_FILENAME = "krd-hostauth.sock"
func HostAuthListen() (listener net.Listener, err error) {
socketPath, err := KrDirFile(HOST_AUTH_FILENAME)
if err != nil {
return
}
// delete UNIX socket in case daemon was not killed cleanly
_ = os.Remove(socketPath)
listener, err = net.Listen("unix", socketPath)
return
}
func HostAuthDial() (conn net.Conn, err error) {
socketPath, err := KrDirFile(HOST_AUTH_FILENAME)
if err != nil {
return
}
conn, err = DaemonDial(socketPath)
return
}
func pingDaemon(unixFile string) (err error) {
conn, err := DaemonDial(unixFile)
if err != nil {
return
}
defer conn.Close()
pingRequest, err := http.NewRequest("GET", "/ping", nil)
if err != nil {
return
}
err = pingRequest.Write(conn)
if err != nil {
return
}
responseReader := bufio.NewReader(conn)
_, err = http.ReadResponse(responseReader, pingRequest)
if err != nil {
err = fmt.Errorf("Daemon Read error: %s", err.Error())
return
}
return
}
func DaemonDialWithTimeout(unixFile string) (conn net.Conn, err error) {
done := make(chan error, 1)
go func() {
done <- pingDaemon(unixFile)
}()
select {
case <-time.After(5*time.Second):
err = fmt.Errorf("ping timed out")
return
case err = <-done:
}
if err != nil {
return
}
conn, err = DaemonDial(unixFile)
return
}
func DaemonSocketOrFatal() (unixFile string) {
unixFile, err := KrDirFile(DAEMON_SOCKET_FILENAME)
if err != nil {
log.Fatal("Could not open connection to daemon. Make sure it is running by typing \"kr restart\".")
}
return
}
func IsKrdRunning() bool {
err := exec.Command("pgrep", "-U", User(), "krd").Run()
return nil == err
}