Compare commits

...
14 Commits
Author SHA1 Message Date
Henrique Dias 3d06904d1f style: replace Tls by TLS 2018-04-21 08:42:23 +01:00
Henrique Dias 4dc7bae6ea Update .travis.yml 2018-04-21 08:39:55 +01:00
Henrique Dias 6b139d375c feat: TLS support 2018-04-21 08:39:32 +01:00
Florian Walther 80e3ffd57b added tls options to readme example config 2018-04-20 17:07:52 +02:00
Florian Walther 893d3b429a added TLS support 2018-04-20 17:05:21 +02:00
Henrique Dias cdaf929a4b Update readme.md 2018-04-08 19:04:07 +01:00
Henrique Dias d216a2edef Merge pull request #4 from alex3305/master
Adds a Systemd service unit
2018-04-08 19:03:19 +01:00
Alex van den Hoogen eeae21cd29 Adds a Systemd service unit
I wanted to run this application as a service on my Debian based machine,
which uses systemd as a service manager. Since there was no default or
example service available in this repository, I've added a very simple
service unit.
2018-04-08 12:48:53 +02:00
Henrique Dias b645ac51eb Merge pull request #3 from fzoske/master
Add Support For BCrypt Storage of Passwords
2018-03-31 15:32:19 +01:00
Fabian Zoske 62aea27486 Add Support For BCrypt Storage of Passwords 2018-03-31 12:01:00 +02:00
Henrique Dias 541defc796 Update .travis.yml 2017-12-21 17:07:12 +00:00
Henrique Dias d2f2b1ccde Update Travis 2017-12-21 17:02:37 +00:00
Henrique Dias 6c10c35efe Merge pull request #2 from c35sys/master
Add *address* config option
2017-12-21 17:01:18 +00:00
Christophe Le Guern 8271975046 Add *address* config option 2017-12-21 17:50:03 +01:00
4 changed files with 76 additions and 23 deletions
+2 -5
View File
@@ -1,6 +1,6 @@
language: go language: go
go: 1.8.3 go: 1.10.x
env: env:
- "PATH=/home/travis/gopath/bin:$PATH" - "PATH=/home/travis/gopath/bin:$PATH"
@@ -9,10 +9,7 @@ install:
- go get ./... - go get ./...
# Install gometalinter and certain linters # Install gometalinter and certain linters
- go get github.com/alecthomas/gometalinter - go get github.com/alecthomas/gometalinter
- go get github.com/client9/misspell/cmd/misspell - gometalinter --install
- go get github.com/gordonklaus/ineffassign
- go get golang.org/x/tools/cmd/goimports
- go get github.com/tsenart/deadcode
script: script:
- gometalinter --disable-all -E vet -E gofmt -E misspell -E ineffassign -E goimports -E deadcode --tests ./... - gometalinter --disable-all -E vet -E gofmt -E misspell -E ineffassign -E goimports -E deadcode --tests ./...
+53 -17
View File
@@ -11,8 +11,10 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"github.com/hacdias/webdav" "github.com/hacdias/webdav"
"golang.org/x/crypto/bcrypt"
wd "golang.org/x/net/webdav" wd "golang.org/x/net/webdav"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
) )
@@ -133,24 +135,36 @@ func getConfig() []byte {
} }
type cfg struct { type cfg struct {
webdav *webdav.Config webdav *webdav.Config
port string address string
auth map[string]string port string
tls bool
cert string
key string
auth map[string]string
} }
func parseConfig() *cfg { func parseConfig() *cfg {
file := getConfig() file := getConfig()
data := struct { data := struct {
Port string `json:"port" yaml:"port"` Address string `json:"address" yaml:"address"`
Scope string `json:"scope" yaml:"scope"` Port string `json:"port" yaml:"port"`
Modify bool `json:"modify" yaml:"modify"` TLS bool `json:"tls" yaml:"tls"`
Rules []map[string]interface{} `json:"rules" yaml:"rules"` Cert string `json:"cert" yaml:"cert"`
Users []map[string]interface{} `json:"users" yaml:"users"` 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", Address: "0.0.0.0",
Scope: "./", Port: "0",
Modify: true, TLS: false,
Cert: "cert.pem",
Key: "key.pem",
Scope: "./",
Modify: true,
} }
var err error var err error
@@ -165,8 +179,12 @@ func parseConfig() *cfg {
} }
config := &cfg{ config := &cfg{
port: data.Port, address: data.Address,
auth: map[string]string{}, port: data.Port,
tls: data.TLS,
cert: data.Cert,
key: data.Key,
auth: map[string]string{},
webdav: &webdav.Config{ webdav: &webdav.Config{
User: &webdav.User{ User: &webdav.User{
Scope: data.Scope, Scope: data.Scope,
@@ -209,7 +227,8 @@ func basicAuth(c *cfg) http.Handler {
return return
} }
if password != p { if !checkPassword(p, password) {
log.Println("Wrong Password for user", username)
http.Error(w, "Not authorized", 401) http.Error(w, "Not authorized", 401)
return return
} }
@@ -218,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() { func main() {
flag.Parse() flag.Parse()
cfg := parseConfig() cfg := parseConfig()
handler := basicAuth(cfg) handler := basicAuth(cfg)
// Builds the address and a listener. // Builds the address and a listener.
laddr := ":" + cfg.port laddr := cfg.address + ":" + cfg.port
listener, err := net.Listen("tcp", laddr) listener, err := net.Listen("tcp", laddr)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@@ -234,7 +263,14 @@ func main() {
fmt.Println("Listening on", listener.Addr().String()) fmt.Println("Listening on", listener.Addr().String())
// Starts the server. // Starts the server.
if err := http.Serve(listener, handler); err != nil { if cfg.tls {
log.Fatal(err) 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)
}
} }
} }
+9 -1
View File
@@ -7,10 +7,16 @@
```yaml ```yaml
scope: /path/to/files scope: /path/to/files
address: 0.0.0.0
port: 8080 port: 8080
tls: false
cert: cert.pem
key: key.pem
users: users:
- username: admin - username: admin
password: admin password: admin
- username: encrypted
password: "{bcrypt}$2y$10$zEP6oofmXFeHaeMfBNLnP.DO8m.H.Mwhd24/TOX2MWLxAExXi4qgi"
- username: basic - username: basic
password: basic password: basic
modify: false 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. 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
View File
@@ -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