项目作者: ThomasLeister

项目描述 :
Golang mod_http_upload_external server for Prosody and Ejabberd
高级语言: Go
项目地址: git://github.com/ThomasLeister/prosody-filer.git
创建时间: 2018-06-19T18:38:21Z
项目社区:https://github.com/ThomasLeister/prosody-filer

开源协议:MIT License

下载


Prosody Filer

A simple file server for handling XMPP http_upload requests. This server is meant to be used with the Prosody mod_http_upload_external module.

Despite the name, this server is also compatible with Ejabberd and Ejabberd’s http_upload module!


Why should I use this server?

Originally this software was written to circumvent memory limitations / issues with the Prosody-internal http_upload implementation at the time. These limitations do not exist, anymore. Still this software can be used with Ejabberd and Prosody as an alternative to the internal http_upload servers.

Download

If you are using regular x86_64 Linux, you can download a finished binary for your system on the release page. No need to compile this application yourself.

Build (optional)

If you’re using something different than a x64 Linux, you need to compile this application yourself.

To compile the server, you need a full Golang development environment. This can be set up quickly: https://golang.org/doc/install#install

Then checkout this repo:

  1. go install github.com/ThomasLeister/prosody-filer

and switch to the new directory:

  1. cd $GOPATH/src/github.com/ThomasLeister/prosody-filer

The application can now be build:

  1. ### Build static binary
  2. ./build.sh
  3. ### OR regular Go build
  4. go build prosody-filer.go

Set up / configuration

Setup Prosody Filer environment

Create a new user for Prosody Filer to run as:

  1. adduser --disabled-login --disabled-password prosody-filer

Switch to the new user:

  1. su - prosody-filer

Copy

  • the binary prosody-filer and
  • config config.example.toml

to /home/prosody-filer/. Rename the configuration to config.toml.

Make sure the prosody-filer binary is executable:

  1. chmod u+x prosody-filer

Configure Prosody

Back in your root shell make sure mod_http_upload is disabled and mod_http_upload_external is enabled! Then configure the external upload module:

  1. Component "uploads.myserver.tld" "http_upload_external"
  2. http_upload_external_base_url = "https://uploads.myserver.tld/upload/"
  3. http_upload_external_secret = "mysecret"
  4. http_upload_external_file_size_limit = 50000000 -- 50 MB

Restart Prosody when you are finished:

  1. systemctl restart prosody

Alternative: Configure Ejabberd

Although this tool is named after Prosody, it can be used with Ejabberd, too! Make sure you have a Ejabberd configuration similar to this:

  1. mod_http_upload:
  2. put_url: "https://uploads.@HOST@/upload"
  3. external_secret: "mysecret"
  4. max_size: 52428800

Configure Prosody Filer

Prosody Filer configuration is done via the config.toml file in TOML syntax. There’s not much to be configured. The most important piece is the secret setting, which needs to match the secret defined in your mod_http_upload_external settings!

  1. ### IP address and port to listen to, e.g. "[::]:5050"
  2. listenport = "[::1]:5050"
  3. ### Secret (must match the one in prosody.conf.lua!)
  4. secret = "mysecret"
  5. ### Where to store the uploaded files
  6. storeDir = "./upload/"
  7. ### Subdirectory for HTTP upload / download requests (usually "upload/")
  8. uploadSubDir = "upload/"

In addition to that, make sure that the nginx user or group can read the files uploaded
via prosody-filer if you want to have them served by nginx directly.

Docker usage

To build container:

docker build . -t prosody-filer:latest

To run container use:

docker run -it --rm -v $PWD/config.example.toml:/config.toml prosody-filer -config /config.toml

Systemd service file

Create a new Systemd service file: /etc/systemd/system/prosody-filer.service

  1. [Unit]
  2. Description=Prosody file upload server
  3. [Service]
  4. Type=simple
  5. ExecStart=/home/prosody-filer/prosody-filer
  6. Restart=always
  7. WorkingDirectory=/home/prosody-filer
  8. User=prosody-filer
  9. Group=prosody-filer
  10. # Group=nginx # if the files should get served by nginx directly:
  11. [Install]
  12. WantedBy=multi-user.target

Reload the service definitions, enable the service and start it:

  1. systemctl daemon-reload
  2. systemctl enable prosody-filer
  3. systemctl start prosody-filer

Done! Prosody Filer is now listening on the specified port and waiting for requests.

FreeBSD service file

  • First, install go and build the static binary with go install github.com/ThomasLeister/prosody-filer.
    The binary should be in ~/go/bin/prosody-filer.
  • Create an user with pw user add -n prosodyfiler -c 'Prosody Filer' -d /home/prosodyfiler -m -s /usr/sbin/nologin
    and copy the binary into /home/prosodyfiler/.
  • Put this rc script in /etc/rc.d/prosody-filer:
    ```

    !/bin/sh

PROVIDE: prosody_filer

REQUIRE: FILESYSTEMS networking

KEYWORDS: upload

. /etc/rc.subr

name=”prosody_filer”
program_name=”prosody-filer”
title=”Prosody-Filer”
rcvar=prosody_filer_enable
prosody_filer_user=”prosodyfiler”
prosody_filer_group=”prosodyfiler”
prosody_filer_chdir=”/home/prosodyfiler/“

pidfile=”/home/prosodyfiler/${program_name}.pid”
required_files=”/home/prosodyfiler/config.toml”
exec_path=”/home/prosodyfiler/${program_name}”
output_file=”/var/log/${program_name}.log”

command=”/usr/sbin/daemon”
command_args=”-r -t ${title} -o ${output_file} -P ${pidfile} -f ${exec_path}”

load_rc_config $name

  1. - Execute `sysrc prosody_filer_enable=YES`
  2. You can now start the service via `service prosody_filer start` and check
  3. it's log via `/var/log/prosody-filer.log`.
  4. ### Configure Nginx
  5. Create a new config file ```/etc/nginx/sites-available/uploads.myserver.tld```:
  6. ```nginx
  7. server {
  8. listen 80;
  9. listen [::]:80;
  10. listen 443 ssl;
  11. listen [::]:443 ssl;
  12. server_name uploads.myserver.tld;
  13. ssl_certificate /etc/letsencrypt/live/uploads.myserver.tld/fullchain.pem;
  14. ssl_certificate_key /etc/letsencrypt/live/uploads.myserver.tld/privkey.pem;
  15. client_max_body_size 50m;
  16. location /upload/ {
  17. if ( $request_method = OPTIONS ) {
  18. add_header Access-Control-Allow-Origin '*';
  19. add_header Access-Control-Allow-Methods 'PUT, GET, OPTIONS, HEAD';
  20. add_header Access-Control-Allow-Headers 'Authorization, Content-Type';
  21. add_header Access-Control-Allow-Credentials 'true';
  22. add_header Content-Length 0;
  23. add_header Content-Type text/plain;
  24. return 200;
  25. }
  26. proxy_pass http://[::]:5050/upload/;
  27. proxy_request_buffering off;
  28. }
  29. }

Enable the new config:

  1. ln -s /etc/nginx/sites-available/uploads.myserver.tld /etc/nginx/sites-enabled/

Check Nginx config:

  1. nginx -t

Reload Nginx:

  1. systemctl reload nginx

Alternative configuration for letting Nginx serve the uploaded files

(not officially supported - user contribution!)

  1. server {
  2. listen 80;
  3. listen [::]:80;
  4. listen 443 ssl;
  5. listen [::]:443 ssl;
  6. server_name uploads.myserver.tld;
  7. ssl_certificate /etc/letsencrypt/live/uploads.myserver.tld/fullchain.pem;
  8. ssl_certificate_key /etc/letsencrypt/live/uploads.myserver.tld/privkey.pem;
  9. location /upload/ {
  10. if ( $request_method = OPTIONS ) {
  11. add_header Access-Control-Allow-Origin '*';
  12. add_header Access-Control-Allow-Methods 'PUT, GET, OPTIONS, HEAD';
  13. add_header Access-Control-Allow-Headers 'Authorization, Content-Type';
  14. add_header Access-Control-Allow-Credentials 'true';
  15. add_header Content-Length 0;
  16. add_header Content-Type text/plain;
  17. return 200;
  18. }
  19. root /home/prosody-filer;
  20. autoindex off;
  21. client_max_body_size 51m;
  22. client_body_buffer_size 51m;
  23. try_files $uri $uri/ @prosodyfiler;
  24. }
  25. location @prosodyfiler {
  26. proxy_pass http://[::1]:5050;
  27. proxy_buffering off;
  28. proxy_set_header Host $host;
  29. proxy_set_header X-Real-IP $remote_addr;
  30. proxy_set_header X-Forwarded-Proto $scheme;
  31. proxy_set_header X-Forwarded-Host $host:$server_port;
  32. proxy_set_header X-Forwarded-Server $host;
  33. proxy_set_header X-Forwarded-For $remote_addr;
  34. }
  35. }

apache2 configuration (alternative to Nginx)

(This configuration was provided by a user and has never been tested by the author of Prosody Filer. It might be outdated and might not work anymore)

  1. <VirtualHost *:80>
  2. ServerName upload.example.eu
  3. RedirectPermanent / https://upload.example.eu/
  4. </VirtualHost>
  5. <VirtualHost *:443>
  6. ServerName upload.example.eu
  7. SSLEngine on
  8. SSLCertificateFile "Path to the ca file"
  9. SSLCertificateKeyFile "Path to the key file"
  10. Header always set Public-Key-Pins: ''
  11. Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
  12. H2Direct on
  13. <Location /upload>
  14. Header always set Access-Control-Allow-Origin "*"
  15. Header always set Access-Control-Allow-Headers "Content-Type"
  16. Header always set Access-Control-Allow-Methods "OPTIONS, PUT, GET"
  17. RewriteEngine On
  18. RewriteCond %{REQUEST_METHOD} OPTIONS
  19. RewriteRule ^(.*)$ $1 [R=200,L]
  20. </Location>
  21. SSLProxyEngine on
  22. ProxyPreserveHost On
  23. ProxyRequests Off
  24. ProxyPass / http://localhost:5050/
  25. ProxyPassReverse / http://localhost:5050/
  26. </VirtualHost>

Automatic purge

Prosody Filer has no immediate knowlegde over all the stored files and the time they were uploaded, since no database exists for that. Also Prosody is not capable to do auto deletion if mod_http_upload_external is used. Therefore the suggested way of purging the uploads directory is to execute a purge command via a cron job:

  1. @daily find /home/prosody-filer/upload/ -mindepth 1 -type d -mtime +28 -print0 | xargs -0 -- rm -rf

This will delete uploads older than 28 days.

Check if it works

Get the log via

  1. journalctl -f -u prosody-filer

If your XMPP clients uploads or downloads any file, there should be some log messages on the screen.