-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_fns.lua
190 lines (157 loc) · 5.16 KB
/
parse_fns.lua
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
184
185
186
187
188
189
190
-- Copyright (c) 2016 Etan Reisner
-- luacheck: read globals hs
local parse_fns = {}
local function do_entry(hosts, seen, newhosts)
local cEntry
for _, newhost in ipairs(newhosts) do
if seen[newhost] then
cEntry = seen[newhost]
break
end
end
if not cEntry then
cEntry = {
text = newhosts[1],
}
hosts[#hosts + 1] = cEntry
seen[newhosts[1]] = cEntry
end
for _, newhost in ipairs(newhosts) do
if not seen[newhost] then
if not cEntry.hosts then
cEntry.hosts = {}
end
cEntry.hosts[#cEntry.hosts + 1] = newhost
end
seen[newhost] = cEntry
end
if newhosts.username then
cEntry.username = newhosts.username
end
return cEntry
end
local function add_config_entry(hosts, seen, hostpats)
if (not hostpats) or (#hostpats == 0) then
return
end
-- Entries with %h in them need every Host to be its own chooser entry.
if hostpats.realhost and hostpats.realhost:find('%h', 1, true) then
for _, hostpat in ipairs(hostpats) do
local fullname = hostpats.realhost:gsub("%%h", hostpat)
do_entry(hosts, seen, {fullname, username = hostpats.username})
end
return
end
-- Otherwise just add the real hostname to the hostpats list.
hostpats[#hostpats + 1] = hostpats.realhost
hostpats.realhost = nil
do_entry(hosts, seen, hostpats)
end
function parse_fns.get_config_hosts(hosts, seen, configFile)
local f = io.open(configFile)
if not f then
return hosts
end
local hostpats = {}
for line in f:lines() do
-- Trim leading and trailing spaces.
line = line:gsub("^%s*", ""):gsub("%s*$", "")
local s, e = line:find("^Host%s+")
if s then
-- Found a new Host entry so add all previously seen hosts.
add_config_entry(hosts, seen, hostpats)
-- Reset current host information.
hostpats = {}
-- Extract host match patterns
local hoststr = line:sub(e+1)
for hostpat in hoststr:gmatch("%S+") do
-- Skip wildcard patterns, we can't use them
if not hostpat:find("[*?]", 1) then
hostpats[#hostpats + 1] = hostpat
end
end
else
-- Found real hostname for current host patterns
local tmp = line:match("^Hostname +(%S+)")
if tmp then
hostpats.realhost = tmp
end
-- Save User to format a leading `user@` on the labels.
tmp = line:match("^User +(%S+)")
if tmp then
hostpats.username = tmp
end
end
end
f:close()
-- Add entries for the last Host in the file.
add_config_entry(hosts, seen, hostpats)
return hosts
end
local function hashed_hostname(hostname)
return hostname:sub(1,1) == "|"
end
function parse_fns.get_known_hosts(hosts, seen, knownhostsfile)
local f = io.open(knownhostsfile)
if not f then
return hosts
end
local klines = {}
for line in f:lines() do
local khosts = {}
local _, e, hoststr = line:gsub('^%s*', ''):find('^([^%s]+)')
if not hoststr then
-- Ignore blank lines
hoststr = ''
elseif hoststr == "@revoked" then
-- Ignore @revoked marker lines
hoststr = ''
elseif hoststr == '@cert-authority' then
-- Skip the @cert-authority marker field
hoststr = line:sub(e+2):match('^([^%s]+)')
elseif hoststr:match('^#') then
-- Ignore comment lines
hoststr = ''
end
-- Collect host entries for this line
for hostpat in hoststr:gmatch("[^,]+") do
-- Only handle non-negated host patterns
if not hostpat:match("^!") and (not hashed_hostname(hostpat)) then
-- Skip wildcard patterns, we can't use them
if not hostpat:find("[*?]", 1) then
hostpat = hostpat:gsub('^%[', '')
hostpat = hostpat:gsub('%]:%d+$', '')
if not hashed_hostname(hostpat) then
khosts[#khosts + 1] = hostpat
end
end
end
end
if #khosts > 0 then
klines[#klines + 1] = khosts
end
end
f:close()
for _,kline in ipairs(klines) do
do_entry(hosts, seen, kline)
end
return hosts
end
function parse_fns.parse_config(sshDir)
local hosts, seen = {}, {}
parse_fns.get_config_hosts(hosts, seen, sshDir .. '/config')
parse_fns.get_known_hosts(hosts, seen, sshDir .. '/known_hosts')
for _, v in ipairs(hosts) do
if v.hosts then
v.subText = table.concat(v.hosts, ' ')
v.hosts = nil
end
if v.username then
local s = hs.styledtext.new(v.username..'@', {color = hs.drawing.color.x11.gray})
v.text = s .. hs.styledtext.new(v.text)
v.username = nil
end
end
return hosts
end
return parse_fns