mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-22 03:20:41 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d06904d1f | ||
|
|
4dc7bae6ea | ||
|
|
6b139d375c | ||
|
|
80e3ffd57b | ||
|
|
893d3b429a | ||
|
|
cdaf929a4b | ||
|
|
d216a2edef | ||
|
|
eeae21cd29 | ||
|
|
b645ac51eb | ||
|
|
62aea27486 | ||
|
|
541defc796 | ||
|
|
d2f2b1ccde | ||
|
|
6c10c35efe | ||
|
|
8271975046 | ||
|
|
ac0e137b1d | ||
|
|
65fa394f98 |
+2
-5
@@ -1,6 +1,6 @@
|
||||
language: go
|
||||
|
||||
go: 1.8.3
|
||||
go: 1.10.x
|
||||
|
||||
env:
|
||||
- "PATH=/home/travis/gopath/bin:$PATH"
|
||||
@@ -9,10 +9,7 @@ install:
|
||||
- go get ./...
|
||||
# Install gometalinter and certain linters
|
||||
- go get github.com/alecthomas/gometalinter
|
||||
- go get github.com/client9/misspell/cmd/misspell
|
||||
- go get github.com/gordonklaus/ineffassign
|
||||
- go get golang.org/x/tools/cmd/goimports
|
||||
- go get github.com/tsenart/deadcode
|
||||
- gometalinter --install
|
||||
|
||||
script:
|
||||
- gometalinter --disable-all -E vet -E gofmt -E misspell -E ineffassign -E goimports -E deadcode --tests ./...
|
||||
|
||||
+61
-19
@@ -11,15 +11,24 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/hacdias/webdav"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
wd "golang.org/x/net/webdav"
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
config string
|
||||
defaultConfigs = []string{"config.json", "config.yaml", "config.yml"}
|
||||
defaultConfigs = []string{
|
||||
"config.json",
|
||||
"config.yaml",
|
||||
"config.yml",
|
||||
"/etc/webdav/config.json",
|
||||
"/etc/webdav/config.yaml",
|
||||
"/etc/webdav/config.yml",
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -126,24 +135,36 @@ func getConfig() []byte {
|
||||
}
|
||||
|
||||
type cfg struct {
|
||||
webdav *webdav.Config
|
||||
port string
|
||||
auth map[string]string
|
||||
webdav *webdav.Config
|
||||
address string
|
||||
port string
|
||||
tls bool
|
||||
cert string
|
||||
key string
|
||||
auth map[string]string
|
||||
}
|
||||
|
||||
func parseConfig() *cfg {
|
||||
file := getConfig()
|
||||
|
||||
data := struct {
|
||||
Port string `json:"port" yaml:"port"`
|
||||
Scope string `json:"scope" yaml:"scope"`
|
||||
Modify bool `json:"modify" yaml:"modify"`
|
||||
Rules []map[string]interface{} `json:"rules" yaml:"rules"`
|
||||
Users []map[string]interface{} `json:"users" yaml:"users"`
|
||||
Address string `json:"address" yaml:"address"`
|
||||
Port string `json:"port" yaml:"port"`
|
||||
TLS bool `json:"tls" yaml:"tls"`
|
||||
Cert string `json:"cert" yaml:"cert"`
|
||||
Key string `json:"key" yaml:"key"`
|
||||
Scope string `json:"scope" yaml:"scope"`
|
||||
Modify bool `json:"modify" yaml:"modify"`
|
||||
Rules []map[string]interface{} `json:"rules" yaml:"rules"`
|
||||
Users []map[string]interface{} `json:"users" yaml:"users"`
|
||||
}{
|
||||
Port: "0",
|
||||
Scope: "./",
|
||||
Modify: true,
|
||||
Address: "0.0.0.0",
|
||||
Port: "0",
|
||||
TLS: false,
|
||||
Cert: "cert.pem",
|
||||
Key: "key.pem",
|
||||
Scope: "./",
|
||||
Modify: true,
|
||||
}
|
||||
|
||||
var err error
|
||||
@@ -158,10 +179,13 @@ func parseConfig() *cfg {
|
||||
}
|
||||
|
||||
config := &cfg{
|
||||
port: data.Port,
|
||||
auth: map[string]string{},
|
||||
address: data.Address,
|
||||
port: data.Port,
|
||||
tls: data.TLS,
|
||||
cert: data.Cert,
|
||||
key: data.Key,
|
||||
auth: map[string]string{},
|
||||
webdav: &webdav.Config{
|
||||
BaseURL: "",
|
||||
User: &webdav.User{
|
||||
Scope: data.Scope,
|
||||
Modify: data.Modify,
|
||||
@@ -203,7 +227,8 @@ func basicAuth(c *cfg) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
if password != p {
|
||||
if !checkPassword(p, password) {
|
||||
log.Println("Wrong Password for user", username)
|
||||
http.Error(w, "Not authorized", 401)
|
||||
return
|
||||
}
|
||||
@@ -212,13 +237,23 @@ func basicAuth(c *cfg) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func checkPassword(saved, input string) bool {
|
||||
|
||||
if strings.HasPrefix(saved, "{bcrypt}") {
|
||||
savedPassword := strings.TrimPrefix(saved, "{bcrypt}")
|
||||
return bcrypt.CompareHashAndPassword([]byte(savedPassword), []byte(input)) == nil
|
||||
}
|
||||
|
||||
return saved == input
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
cfg := parseConfig()
|
||||
handler := basicAuth(cfg)
|
||||
|
||||
// Builds the address and a listener.
|
||||
laddr := ":" + cfg.port
|
||||
laddr := cfg.address + ":" + cfg.port
|
||||
listener, err := net.Listen("tcp", laddr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -228,7 +263,14 @@ func main() {
|
||||
fmt.Println("Listening on", listener.Addr().String())
|
||||
|
||||
// Starts the server.
|
||||
if err := http.Serve(listener, handler); err != nil {
|
||||
log.Fatal(err)
|
||||
if cfg.tls {
|
||||
if err := http.ServeTLS(listener, handler, cfg.cert, cfg.key); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
if err := http.Serve(listener, handler); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,16 @@
|
||||
|
||||
```yaml
|
||||
scope: /path/to/files
|
||||
address: 0.0.0.0
|
||||
port: 8080
|
||||
tls: false
|
||||
cert: cert.pem
|
||||
key: key.pem
|
||||
users:
|
||||
- username: admin
|
||||
password: admin
|
||||
- username: encrypted
|
||||
password: "{bcrypt}$2y$10$zEP6oofmXFeHaeMfBNLnP.DO8m.H.Mwhd24/TOX2MWLxAExXi4qgi"
|
||||
- username: basic
|
||||
password: basic
|
||||
modify: false
|
||||
@@ -22,4 +28,6 @@ users:
|
||||
|
||||
You can specify the path to the configuration file using the `--config` flag. By default, it will search for a `config.{yaml,json}` file on your current working directory.
|
||||
|
||||
Download it [here](https://github.com/hacdias/webdav/releases).
|
||||
An example of how to use this with `systemd` is on [webdav.service.example](/webdav.service.example).
|
||||
|
||||
Download it [here](https://github.com/hacdias/webdav/releases).
|
||||
|
||||
@@ -12,8 +12,7 @@ import (
|
||||
// Config is the configuration of a WebDAV instance.
|
||||
type Config struct {
|
||||
*User
|
||||
BaseURL string
|
||||
Users map[string]*User
|
||||
Users map[string]*User
|
||||
}
|
||||
|
||||
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
||||
@@ -28,9 +27,6 @@ func (c *Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the BaseURL from the url path.
|
||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, c.BaseURL)
|
||||
|
||||
// Checks for user permissions relatively to this PATH.
|
||||
if !u.Allowed(r.URL.Path) {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
[Unit]
|
||||
Description=WebDAV server
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=root
|
||||
ExecStart=/usr/bin/webdav --config /opt/webdav.config
|
||||
Restart=on-failure
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
Reference in New Issue
Block a user