@@ -19,7 +19,19 @@ local T = ffiutil.template
1919
2020local path = DataStorage :getFullDataDir ()
2121local plugPath = path .. " /plugins/filebrowser.koplugin/filebrowser"
22+ local configPath = path .. " /plugins/filebrowser.koplugin/config.json"
23+ local dbPath = path .. " /plugins/filebrowser.koplugin/filebrowser.db"
2224local binPath = plugPath .. " /filebrowser"
25+ local filebrowserArgs = string.format (
26+ " %s %s %s %s " ,
27+ " -d" , dbPath ,
28+ " -c " , configPath
29+ )
30+ local filebrowserCmd = string.format (" %s %s %s %s %s " ,
31+ binPath ,
32+ " -d" , dbPath ,
33+ " -c " , configPath
34+ )
2335local logPath = plugPath .. " /filebrowser.log"
2436local pidFilePath = " /tmp/filebrowser_koreader.pid"
2537
@@ -34,12 +46,53 @@ local Filebrowser = WidgetContainer:extend {
3446
3547function Filebrowser :init ()
3648 self .filebrowser_port = G_reader_settings :readSetting (" filebrowser_port" ) or " 80"
37- self .filebrowser_password_hash = G_reader_settings :readSetting (" filebrowser_password" ) or " $2a$10$x9hMP5e/71oPI/KCE9vqj.eUQqciNhFFNDzoC4idO.aNehTPlZJnK " -- admin
49+ self .filebrowser_password_hash = G_reader_settings :readSetting (" filebrowser_password" ) or " admin " -- admin
3850 self .ui .menu :registerToMainMenu (self )
3951 self :onDispatcherRegisterActions ()
4052end
4153
54+ -- Wipe configuration and set auth to noauth
55+ function Filebrowser :config ()
56+ os.remove (configPath )
57+ os.remove (dbPath )
58+ local init = string.format (" %s %s" ,
59+ filebrowserCmd ,
60+ " config init"
61+ )
62+ logger .info (" init: " .. init )
63+ local status = os.execute (init )
64+
65+ local create_user = string.format (
66+ " %s %s %s %s %s %s " ,
67+ filebrowserCmd ,
68+ " users" ,
69+ " add" ,
70+ " koreader" ,
71+ " koreader" ,
72+ " --perm.admin"
73+ )
74+ logger .info (" create_user: " .. create_user )
75+ local status = os.execute (create_user )
76+ logger .info (" status: " .. status )
77+
78+ local set_noauth = string.format (" %s %s %s %s %s %s %s %s " , binPath , " -d" , dbPath , " -c" , configPath , " config" , " set" , " --auth.method=noauth" )
79+ logger .info (" set_noauth: " .. set_noauth )
80+ local status = status + os.execute (set_noauth )
81+ if status == 0 then
82+ logger .info (" [Filebrowser] User has been reset to koreader and password has been reset to koreader and auth has been reset to noauth." )
83+ else
84+ logger .info (" [Filebrowser] Failed to reset admin password and auth, status Filebrowser, status: " , status )
85+ local info = InfoMessage :new {
86+ icon = " notice-warning" ,
87+ text = _ (" Failed to reset Filebrowser config." ),
88+ }
89+ UIManager :show (info )
90+ end
91+ end
92+
93+
4294function Filebrowser :start ()
95+ self :config ()
4396 -- Since Filebrowser doesn't start as a deamon by default and has no option to
4497 -- set a pidfile, we launch it using the start-stop-daemon helper. On Kobo and Kindle,
4598 -- this command is provided by BusyBox:
@@ -53,27 +106,18 @@ function Filebrowser:start()
53106 -- background process. On Filebrowser itself, set the root directory,
54107 -- and a log file.
55108 local cmd = string.format (
56- " start-stop-daemon -S "
57- .. " -m -p %s " -- %s: pidFilePath
58- .. " -o "
59- .. " -b "
60- .. " -x %s " -- %s: binPath
61- .. " -- " -- filebrowser arguments follow
62- .. " --noauth " -- disable authentication
63- .. " --password %s " -- %s: self.filebrowser_password: set password hash to admin
64- .. " -a 0.0.0.0 " -- ip to bind to (0.0.0.0 means all interfaces)
65- .. " -r %s " -- %s: dataPath
66- .. " -p %s " -- %s: filebrowser_port
67- .. " -l %s " -- %s: logPath
68- .. " -c ./plugins/filebrowser.koplugin/config.json "
69- .. " -d ./plugins/filebrowser.koplugin/filebrowser.db "
70- .. " " , -- end arguments
71- pidFilePath ,
72- binPath ,
73- self .filebrowser_password ,
74- dataPath ,
75- self .filebrowser_port ,
76- logPath
109+ " %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s " ,
110+ " start-stop-daemon -S " ,
111+ " -m -p " , pidFilePath ,
112+ " -o " ,
113+ " -b " ,
114+ " -x " , binPath ,
115+ " -- " , -- filebrowser arguments follow
116+ filebrowserArgs ,
117+ " -a 0.0.0.0 " , -- ip to bind to (0.0.0.0 means all interfaces)
118+ " -r " , dataPath ,
119+ " -p " , self .filebrowser_port ,
120+ " -l " , logPath
77121 )
78122
79123 -- Make a hole in the Kindle's firewall
@@ -87,14 +131,14 @@ function Filebrowser:start()
87131 " -m conntrack --ctstate ESTABLISHED -j ACCEPT" ))
88132 end
89133
90- logger .dbg (" [Filebrowser] Launching Filebrowser: " , cmd )
134+ logger .info (" [Filebrowser] Launching Filebrowser: " , cmd )
91135
92136 local status = os.execute (cmd )
93137 if status == 0 then
94138 logger .dbg (" [Filebrowser] Filebrowser started. Find Filebrowser logs at " , logPath )
95139 local info = InfoMessage :new {
96140 timeout = 2 ,
97- text = _ (" Filebrowser started. " )
141+ text = _ (" Filebrowser started! " )
98142 }
99143 UIManager :show (info )
100144 else
@@ -129,11 +173,19 @@ end
129173function Filebrowser :stop ()
130174 -- Use start-stop-daemon -K to stop the process, with --oknodo to exit with
131175 -- status code 0 if there are no matching processes in the first place.
176+ local cmd = string.format (
177+ " %s %s %s %s %s " ,
178+ " start-stop-daemon -K " ,
179+ " -o -p " , pidFilePath ,
180+ " -x " , binPath
181+ )
132182 local cmd = string.format (
133183 " start-stop-daemon -K -o -p %s -x %s" ,
134184 pidFilePath ,
135185 binPath
136186 )
187+ local cmd = string.format (" cat %s | xargs kill" , pidFilePath )
188+
137189
138190 logger .dbg (" [Filebrowser] Stopping Filebrowser: " , cmd )
139191
@@ -167,7 +219,7 @@ function Filebrowser:stop()
167219 icon = " notice-warning" ,
168220 text = _ (" Failed to stop Filebrowser." )
169221 })
170- end
222+ end
171223end
172224
173225function Filebrowser :onToggleFilebrowser ()
0 commit comments