File size: 2,310 Bytes
a9b851c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

set -e

# Logging function
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] [WebDAV] $*"
}

log "Initializing WebDAV service..."

# Load configuration
user_home="${HOME:-/home/user}"
config_file="$user_home/config/webdav.conf"

# Default configuration values
WEBDAV_HOST="${WEBDAV_HOST:-0.0.0.0}"
WEBDAV_PORT="${WEBDAV_PORT:-8081}"
WEBDAV_USERNAME="${WEBDAV_USERNAME:-webdav}"
WEBDAV_PASSWORD="${WEBDAV_PASSWORD:-webdav123}"
WEBDAV_ROOT_DIR="${WEBDAV_ROOT_DIR:-$user_home/webdav}"
WEBDAV_ENABLE_UI="${WEBDAV_ENABLE_UI:-true}"

# Load config file if exists
if [[ -f "$config_file" ]]; then
    log "Loading WebDAV configuration from: $config_file"
    source "$config_file"
else
    log "No WebDAV configuration file found, using defaults"
fi

# Ensure directories exist
mkdir -p "$user_home/webdav/data" "$user_home/webdav/config" "$WEBDAV_ROOT_DIR"

# Create FlyDAV configuration file
webdav_config_file="$user_home/webdav/config/flydav.toml"
log "Creating FlyDAV configuration file: $webdav_config_file"

# Generate SHA256 hash of the password
password_hash=$(echo -n "$WEBDAV_PASSWORD" | sha256sum | cut -d' ' -f1)

cat > "$webdav_config_file" << EOF
[server]
host = "$WEBDAV_HOST"
port = $WEBDAV_PORT
path = "/webdav"
fs_dir = "$WEBDAV_ROOT_DIR"

[auth]
[[auth.user]]
username = "$WEBDAV_USERNAME"
sub_fs_dir = ""
sub_path = ""
password_hash = "$password_hash"
password_crypt = "sha256"

[log]
level = "info"

[[log.stdout]]
format = "text"
output = "stdout"
EOF

log "WebDAV configuration created successfully"
log "Server: $WEBDAV_HOST:$WEBDAV_PORT"
log "Root directory: $WEBDAV_ROOT_DIR"
log "Username: $WEBDAV_USERNAME"
log "WebDAV URL: http://$WEBDAV_HOST:$WEBDAV_PORT/webdav"

# Check if FlyDAV is available
if ! command -v flydav >/dev/null 2>&1; then
    log "ERROR: FlyDAV command not found!"
    log "Please ensure WebDAV installation completed successfully"
    exit 1
fi

# Start WebDAV server
log "Starting WebDAV server..."

# Add UI flag if enabled
UI_FLAG=""
if [[ "${WEBDAV_ENABLE_UI}" == "true" ]]; then
    UI_FLAG="--with-ui"
    log "WebDAV Web UI enabled"
    log "Web UI URL: http://$WEBDAV_HOST:$WEBDAV_PORT/"
fi

log "Executing: flydav -c \"$webdav_config_file\" $UI_FLAG"

# Start the service with proper signal handling
exec flydav -c "$webdav_config_file" $UI_FLAG