-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
162 lines (126 loc) · 3.9 KB
/
discovery.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
package kdeconnect
import (
"bytes"
"log"
"os/exec"
"regexp"
"strings"
"github.com/pkg/errors"
)
var (
cliListAvailableRegex = regexp.MustCompile(`^- (.*): ([a-z0-9]+).*$`)
mountSFTPHostRegex = regexp.MustCompile(`^kdeconnect@(.+):.*$`)
ssSFTPPortRegex = regexp.MustCompile(`[^\s]+\s+[^\s]+\s+[^\s]+\s+[^\s]+\s+(?:[0-9.]+:([0-9]+))\s+[^\s]+`)
)
type Device struct {
// Name and ID of the device
Name string
ID string
// Host & port of the device's SFTP server. Will be empty strings if the
// device's filesystem is not yet mounted by KDE Connect via the
// "Browse this device" button.
SFTPHost string
SFTPPort string
}
// ListAvailableDevices returns a list of KDE Connect devices connected to
// the host
func ListAvailableDevices() ([]*Device, error) {
availableDevices, err := listAvailableDevices()
if err != nil {
return nil, err
}
for _, d := range availableDevices {
if err := populateSFTPHostPort(d); err != nil {
log.Printf("error getting KDE connect device host/port for device '%s'", d.Name)
}
}
return availableDevices, nil
}
// listAvailableDevices returns a list of KDE Connect devices connected to
// the host with `kdeconnect-cli -a`
func listAvailableDevices() ([]*Device, error) {
var out bytes.Buffer
cmd := exec.Command("kdeconnect-cli", "-a")
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return nil, errors.Wrap(err, "error listing available KDE Connect devices")
}
// Parse the output, example:
//
// - S6 Lite: 82c27bf0c8d7fbc5 (paired and reachable)
// - Asus Max Pro M1: cc06e6c222be2ff6 (paired and reachable)
// 2 devices found
devices := []*Device{}
for _, line := range strings.Split(out.String(), "\n") {
if !strings.HasPrefix(line, "- ") {
continue
}
// Parse format like this:
// ^- Asus Max Pro M1: cc06e6c222be2ff6 (paired and reachable)$
matches := cliListAvailableRegex.FindAllStringSubmatch(line, -1)
if len(matches) != 1 {
continue
}
if len(matches[0]) != 3 {
continue
}
devices = append(
devices,
&Device{Name: matches[0][1], ID: matches[0][2]},
)
}
return devices, nil
}
// populateSFTPHostPort populates the device's SFTP host/port field accordingly.
// The device's host is obtained from `mount | grep <device-id>`, while the port
// is obtained from `ss -plnaut | grep <device-ip>`.
func populateSFTPHostPort(device *Device) error {
// Get the device's host
var mountOut bytes.Buffer
mountCmd := exec.Command("mount")
mountCmd.Stdout = &mountOut
if err := mountCmd.Run(); err != nil {
return errors.Wrap(err, "error listing KDE Connect mounted volumes")
}
// Get device IP from expected output format:
// kdeconnect@192.168.0.188:/ on /run/user/1000/82c27bf0c8d7fbc5 type fuse.sshfs (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000)
for _, line := range strings.Split(mountOut.String(), "\n") {
if !strings.Contains(line, device.ID) || !strings.Contains(line, "kdeconnect") {
continue
}
matches := mountSFTPHostRegex.FindAllStringSubmatch(line, -1)
if len(matches) != 1 {
continue
}
if len(matches[0]) != 2 {
continue
}
device.SFTPHost = matches[0][1]
}
if device.SFTPHost == "" {
return nil
}
// Get device SFTP port
var ssOut bytes.Buffer
ssCmd := exec.Command("ss", "-plnta")
ssCmd.Stdout = &ssOut
if err := ssCmd.Run(); err != nil {
return errors.Wrap(err, "error listing KDE Connect established connections")
}
// Get device port from expected output format:
// ESTAB 0 0 192.168.0.138:41718 192.168.0.188:1739 users:(("ssh",pid=361762,fd=3))
for _, line := range strings.Split(ssOut.String(), "\n") {
if !strings.Contains(line, device.SFTPHost) || !strings.Contains(line, "ssh") {
continue
}
matches := ssSFTPPortRegex.FindAllStringSubmatch(line, -1)
if len(matches) != 1 {
continue
}
if len(matches[0]) != 2 {
continue
}
device.SFTPPort = matches[0][1]
}
return nil
}