-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_unix.go
48 lines (42 loc) · 857 Bytes
/
client_unix.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
//go:build !windows
package drpc
import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"time"
)
func connect() (net.Conn, error) {
temp := "/tmp"
subDirs := []string{
"",
"app/com.discordapp.Discord",
"snap.discord",
}
for _, name := range []string{"XDG_RUNTIME_DIR", "TMPDIR", "TMP", "TEMP"} {
if value := os.Getenv(name); value != "" {
temp = value
break
}
}
for _, sd := range subDirs {
for i := 0; i < 10; i++ {
conn, err := net.DialTimeout(
"unix",
filepath.Join(temp, sd, "discord-ipc-"+strconv.Itoa(i)),
time.Second*5,
)
// socket exists, but it has a error.
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("%w: %w", ErrConnFailed, err)
}
if err == nil {
return conn, nil
}
}
}
return nil, errors.New("drpc: discord is not running")
}