server.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "log"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. "github.com/cloudflare/cfssl/transport"
  10. "github.com/cloudflare/cfssl/transport/core"
  11. "github.com/cloudflare/cfssl/transport/example/exlib"
  12. "gopkg.in/errgo.v1"
  13. "golang.org/x/net/webdav"
  14. )
  15. var (
  16. addr = flag.String("addr", "acab.mobi:4444", "address of the server")
  17. dir = flag.String("dir", "./davOne", "where to save the files")
  18. cfCfg = flag.String("cfgFile", "cfssl_config.json", "cloudflare ssl config")
  19. )
  20. func main() {
  21. flag.Parse()
  22. log.SetFlags(0)
  23. lis, err := parseAndSetupListener(*addr, *cfCfg)
  24. if err != nil {
  25. log.Fatal("parseAndSetupListen:", err)
  26. }
  27. h := &webdav.Handler{
  28. FileSystem: webdav.Dir(*dir),
  29. LockSystem: webdav.NewMemLS(),
  30. Logger: func(r *http.Request, err error) {
  31. switch r.Method {
  32. case "COPY", "MOVE":
  33. dst := ""
  34. if u, err := url.Parse(r.Header.Get("Destination")); err == nil {
  35. dst = u.Path
  36. }
  37. o := r.Header.Get("Overwrite")
  38. log.Printf("%-10s%-30s%-30so=%-2s%v", r.Method, r.URL.Path, dst, o, err)
  39. default:
  40. log.Printf("%-10s%-30s%v", r.Method, r.URL.Path, err)
  41. }
  42. },
  43. }
  44. http.Handle("/", h)
  45. cfg, err := lis.TLSClientAuthServerConfig()
  46. if err != nil {
  47. exlib.Err(1, err, "tlsconfig")
  48. }
  49. server := &http.Server{
  50. Addr: ":https",
  51. TLSConfig: cfg,
  52. }
  53. log.Fatal(server.Serve(lis))
  54. }
  55. func parseAndSetupListener(addr, cfg string) (*transport.Listener, error) {
  56. f, err := os.Open(cfg)
  57. if err != nil {
  58. return nil, errgo.Notef(err, "opening config file failed")
  59. }
  60. var id = new(core.Identity)
  61. err = json.NewDecoder(f).Decode(id)
  62. if err != nil {
  63. return nil, errgo.Notef(err, "parsing config json failed")
  64. }
  65. tr, err := transport.New(exlib.Before, id)
  66. if err != nil {
  67. return nil, errgo.Notef(err, "parsing config json failed")
  68. }
  69. l, err := transport.Listen(addr, tr)
  70. if err != nil {
  71. return nil, errgo.Notef(err, "failed to create listener")
  72. }
  73. var errChan = make(chan error, 0)
  74. go func(ec <-chan error) {
  75. for {
  76. err, ok := <-ec
  77. if !ok {
  78. log.Println("error channel closed, future errors will not be reported")
  79. break
  80. }
  81. log.Fatal("auto update error: %v", err)
  82. }
  83. }(errChan)
  84. log.Print("setting up auto-update")
  85. go l.AutoUpdate(nil, errChan)
  86. return l, nil
  87. }