-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfarclone-menu.lua
More file actions
285 lines (235 loc) · 7.04 KB
/
Copy pathfarclone-menu.lua
File metadata and controls
285 lines (235 loc) · 7.04 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
-- farclone-menu.lua
-- Simplified version of farclone-disk.lua for manual invocation.
-- This version is triggered via the 'farclone:' command line prefix
-- or from the F11 Plugins menu. It does not interfere with
-- the disks menu or panel focus.
-- Configuration (use \ for paths in Windows)
local RclonePath = "C:\\Applications\\System\\rclone\\rclone.exe" -- Path to rclone.exe
local ConfigPath = "" -- Empty = use default rclone config location
local Timeout = 10 -- Server timeout in minutes
-- Server settings (SFTP protocol)
local ServPort = 2022
local ServPass = "rclone"
local RCPort = 5572
_G.G = _G.G or {}
G.rclone_server = G.rclone_server or { running = false, remote = nil }
local function executeRclone(args)
local configArg = ConfigPath ~= ""
and string.format('--config "%s"', ConfigPath)
or ""
local command = string.format('"%s" %s %s', RclonePath, configArg, args)
local handle = io.popen(command)
local output = {}
if handle then
for line in handle:lines() do
output[#output + 1] = line
end
handle:close()
end
return output
end
local function getRemotes()
local remotes_raw = executeRclone("listremotes")
local remotes = {}
for _, remote in ipairs(remotes_raw) do
local name = remote:match("^(.+):$") or remote
if name ~= "" then
table.insert(remotes, name)
end
end
return remotes
end
local function isPortInUse(port)
local handle = io.popen(string.format('netstat -an | find ":%d"', port))
if handle then
local output = handle:read("*a")
handle:close()
return output:find("LISTENING") ~= nil
end
return false
end
local function stopServer()
if not G.rclone_server.running then
return true
end
local cmd = string.format('curl -X POST http://127.0.0.1:%d/core/quit 2>nul', RCPort)
os.execute(cmd)
local maxWait = 10
local waited = 0
while (isPortInUse(ServPort) or isPortInUse(RCPort)) and waited < maxWait do
win.Sleep(500)
waited = waited + 1
end
G.rclone_server.running = false
G.rclone_server.remote = nil
return true
end
local function openNetBox(remoteName)
-- Use remote name as username so NetBox shows "remoteName@127.0.0.1"
local url = string.format("sftp://%s:%s@127.0.0.1:%d/", remoteName, ServPass, ServPort)
-- This macro is posted to Far's input queue.
-- It opens NetBox (which is assumed to be on the default Ctrl+F shortcut
-- in the Disks menu), enters the URL, and connects.
-- We use a function with mf.postmacro to ensure it runs after the current script context.
mf.postmacro(function()
Keys("Esc")
print(url)
Keys("Enter")
end)
return true
end
local function startServer(remoteName)
if G.rclone_server.running and G.rclone_server.remote == remoteName then
local result = far.Message(
string.format("Server already running for: %s\n\nStop server?", remoteName),
"Rclone",
";YesNo",
"w"
)
if result == 1 then
stopServer()
end
return true
end
if G.rclone_server.running then
local result = far.Message(
string.format("Server running for: %s\n\nReconnect to: %s ?\n\n(Server will restart)",
G.rclone_server.remote, remoteName),
"Switch Remote",
";YesNo",
"w"
)
if result == 1 then
stopServer()
win.Sleep(500)
else
return false
end
end
if isPortInUse(ServPort) or isPortInUse(RCPort) then
far.Message(string.format("Ports %d or %d still in use!\n\nWait a moment and try again.",
ServPort, RCPort), "Port Conflict", "OK", "w")
return false
end
local configArg = ConfigPath ~= ""
and string.format('--config "%s"', ConfigPath)
or ""
local cmd = string.format(
'start /MIN "rclone_%s" "%s" %s serve sftp %s: --addr 127.0.0.1:%d --user %s --pass %s --rc --rc-addr 127.0.0.1:%d --rc-no-auth --vfs-cache-mode writes --timeout %dm',
remoteName, RclonePath, configArg, remoteName, ServPort, remoteName, ServPass, RCPort, Timeout
)
os.execute(cmd)
win.Sleep(1500)
if not isPortInUse(ServPort) then
far.Message("Failed to start server!", "Error", "OK", "w")
return false
end
G.rclone_server.running = true
G.rclone_server.remote = remoteName
openNetBox(remoteName)
return true
end
local function makeHotkey(index)
if index >= 1 and index <= 9 then
return tostring(index)
elseif index == 10 then
return "0"
else
-- For items 11 and beyond, use letters A, B, C...
return string.char(64 + index - 10)
end
end
local function openRcloneConfig()
local configArg = ConfigPath ~= ""
and string.format('--config "%s"', ConfigPath)
or ""
os.execute(string.format('start "Rclone Config" "%s" %s config', RclonePath, configArg))
end
-- This is the main action, extracted into a reusable function.
local function mainAction()
local remotes = getRemotes()
if #remotes == 0 then
far.Message("No remotes found!", "Error", "OK", "w")
return
end
-- Build menu items with hotkeys and [running] marker
local items = {}
for i, remote in ipairs(remotes) do
local hotkey = makeHotkey(i)
local marker = (G.rclone_server.running and G.rclone_server.remote == remote)
and " [running]"
or ""
table.insert(items, {
text = string.format("&%s. %s%s", hotkey, remote, marker),
remote = remote
})
end
local bottomHint = G.rclone_server.running
and "Shift+F4: Config F8: Serv Stop"
or "Shift+F4: Config"
local menuProps = {
Title = "Select Rclone Remote",
Bottom = bottomHint,
Flags = far.Flags.FMENU_WRAPMODE
}
local breakKeys = {
{ BreakKey = "SHIFTF4" },
{ BreakKey = "F8" },
}
local result = far.Menu(menuProps, items, breakKeys)
if result then
if result.BreakKey == "SHIFTF4" then
openRcloneConfig()
elseif result.BreakKey == "F8" then
if G.rclone_server.running then
stopServer()
end
else
startServer(result.remote)
end
end
end
-- START: New Entry Points --
-- Entry Point 1: CommandLine prefix 'farclone:'
CommandLine {
description = "Rclone Remotes Launcher",
prefixes = "farclone",
action = function()
mainAction()
end
}
-- Entry Point 2: F11 Plugins Menu
MenuItem {
menu = "Plugins",
area = "Shell",
guid = "51E74971-6C9A-469D-93CF-95B9B1E059AF", -- Reusing guid from old script
text = "Rclone Remotes",
action = function()
mainAction()
end
}
-- END: New Entry Points --
-- This menu item for stopping the server is still useful.
MenuItem {
menu = "Plugins",
area = "Shell",
guid = "61F74971-7C9A-469D-93CF-95B9B1E059AF",
text = "Stop Rclone Server",
action = function()
if G.rclone_server.running then
local remote = G.rclone_server.remote
stopServer()
far.Message("Server stopped:\n" .. remote, "Rclone", "OK")
else
far.Message("No server running", "Rclone", "OK")
end
end
}
-- Event handlers remain unchanged, they are crucial for cleanup.
Event {
description = "Stop Rclone server on Far exit",
group = "ExitFAR",
action = function()
stopServer()
end
}