WebDAV Share
WebDAV is an HTTP based file transfer protocol. It's integrated into Windows, GNOME, KDE and many programs for backups, file sharing and media players.
It's installed as a plugin for a web server. Unfortunately OpenWrt's uhttpd doesn't support it. If you have enough space you can install Lighttpd webserver and configure WebDAV share.
WebDAV with Lighttpd
If don't have yet the Lighttpd install it:
opkg install lighttpd
If you didn't uninstalled the default ughttpd
webserver you should change the Lighttpd port to avoid conflicts e.g. for the HTTP set to 2080 and for the HTTPS set port to 2443.
In the /etc/lighttpd/lighttpd.conf
by the server.port = 2080
. For the TLS you better to create a separate file /etc/lighttpd/conf.d/40-tls-enable_custom.conf
:
- /etc/lighttpd/conf.d/40-tls-enable_custom.conf
$SERVER["socket"] == ":2443" { ssl.engine = "enable" } $SERVER["socket"] == "[::]:2443" { ssl.engine = "enable" }
If you already have the Lighttpd that is used by a firmware then update it and it's modules to avoid conflicts:
opkg update opkg list-upgradable | cut -f 1 -d ' ' | grep lighttpd | xargs opkg upgrade
Install the Basic Auth and WebDAV modules:
opkg install lighttpd-mod-auth lighttpd-mod-authn_file lighttpd-mod-webdav
Assuming that USB drive is mounted to /mnt/disk/
folder:
mkdir -p /mnt/disk/dav /var/lib/lighttpd chown http:www-data /mnt/disk/dav /var/lib/lighttpd cat << "EOF" > /etc/lighttpd/conf.d/99-disk.conf $HTTP["url"] =~ "^/dav($|/)" { server.document-root := "/mnt/disk/" auth.backend = "plain" auth.backend.plain.userfile = "/etc/lighttpd/webdav.shadow" auth.require = ( "/dav/" => ("method" => "basic", "realm" => "disk", "require" => "valid-user") ) auth.cache = ("max-age" => "3600") } EOF
Now set a password:
echo "youruser:somesecret" > /etc/lighttpd/webdav.shadow
Note: your secret is not encoded and saved on router in clear text for a better performance. If a hacker get access to the file it can see your password. So don't put here a password that you are using anywhere else. Just generate a new one for example with the command:
< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32}; echo
And finally restart Lighttpd:
/etc/init.d/lighttpd restart
Troubleshooting
If you got the error:
(../src/mod_webdav.c.1327) sqlite3_open() '/var/lib/lighttpd/webdav.db': unable to open database file (../src/server.c.1943) Configuration of plugins failed. Going down.
Then you need to create the file manually with touch /var/lib/lighttpd/webdav.db
.
To test configuration use lighttpd -tt -f /etc/lighttpd/lighttpd.conf
.
Test it
Open in a browser http://192.168.1.1/dav/ and you'll be prompted for credentials. After login you'll see a directory listing but this not yet a WebDAV, just an additional feature.
To connect to WebDAV see Accessing WebDAV Server.
A quick test can be done with a curl
(replace the youruser
):
curl -u youruser:pass -X PROPFIND -H 'Depth: 1' http://192.168.1.1/dav/
You must see a 207 status code and XML response with directory listing.
See more details and examples WebDAV with curl
Don't forget about encryption!
Please note that you must configure HTTPS if you are going to access your files from internet. You'll need to get a TLS certificate and configure lighttpd for HTTPS
Browser UI for the WebDAV share
As with usual HTTP you can see (GET) any file directly from a browser but you can't see a listing of files in a folder. That's because for a listing is used a PROPFIND method but not just GET. But you can install a browser extension or more advanced app.
But a better is to install the small and nice UI webdav-js. Just create a file in dav folder's root `/mnt/disk/dav/index.html`
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebDAV</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/dom111/webdav-js/assets/css/style-min.css"/> </head> <body> <script src="https://cdn.jsdelivr.net/gh/dom111/webdav-js/src/webdav-min.js"></script> </body> </html>
Then open http://192.168.1.1/dav/ and you'll see a simple file manager. Internally it performs all webdav operations with AJAX e.g. sends a PROPFIND to list files.
The disadvantage of the method is that it's doesn't work for subfolders i.e. when you open http://192.168.1.1/dav/Documents/ then the index.html
file wont be show.
That's fine for 99% of usages.
Online apps and PWAs
You can also try an online apps that can connect directly to your WebDAV share to backup data. Some notable apps:
- Diffuse a music player Source code
- Supper Productivity a powerful TODO App. Source code
- Buttercup a password manager as a browser extension.
- KeeWeb a password manager as PWA
To allow online apps to connect with your WebDAV it need a CORS enabled. Here is an example https://gist.github.com/stokito/0a6274106d407ba6d9fb776e7773445d
Useful Apps
Useful mobile apps that supports WebDAV sync:
- Orgzly - Outliner for notes and to-do lists. Source code
- CloudBeats - a music player. Proprietary.
- EasySync - two way synchronization of images, videos, audio and downloads.
- FolderSync - a backup and sync e.g. you can upload photos automatically. Proprietary.
Links and docs
- Awesome WebDAV a list of software that works with WebDAV.
- openwrt-lighttpd-public OpenWrt configuration for Lighttpd with WebDAV and autosharing
ZeroConf autodiscovery
You can advertise a WebDAV share with umdns. Then it will be seen in Network folder of a file manager in GNOME and KDE and can be discovered from a Kodi media player.
Install the umdns package with opkg install umdns
and create a service description file:
- /etc/umdns/lighttpd_webdav.json
{ "lighttpd_webdav": { "service": "_webdav._tcp.local", "port": 80, "txt": [ "path=/dav/", "u=media" ] } }
The reload the umdns service with: ubus call umdns reload
or service umdns reload
.