webdav.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "os/user"
  7. "strconv"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/security"
  13. "github.com/chrislusf/seaweedfs/weed/server"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. var (
  17. webDavStandaloneOptions WebDavOption
  18. )
  19. type WebDavOption struct {
  20. filer *string
  21. port *int
  22. collection *string
  23. tlsPrivateKey *string
  24. tlsCertificate *string
  25. }
  26. func init() {
  27. cmdWebDav.Run = runWebDav // break init cycle
  28. webDavStandaloneOptions.filer = cmdWebDav.Flag.String("filer", "localhost:8888", "filer server address")
  29. webDavStandaloneOptions.port = cmdWebDav.Flag.Int("port", 7333, "webdav server http listen port")
  30. webDavStandaloneOptions.collection = cmdWebDav.Flag.String("collection", "", "collection to create the files")
  31. webDavStandaloneOptions.tlsPrivateKey = cmdWebDav.Flag.String("key.file", "", "path to the TLS private key file")
  32. webDavStandaloneOptions.tlsCertificate = cmdWebDav.Flag.String("cert.file", "", "path to the TLS certificate file")
  33. }
  34. var cmdWebDav = &Command{
  35. UsageLine: "webdav -port=7333 -filer=<ip:port>",
  36. Short: "start a webdav server that is backed by a filer",
  37. Long: `start a webdav server that is backed by a filer.
  38. `,
  39. }
  40. func runWebDav(cmd *Command, args []string) bool {
  41. util.LoadConfiguration("security", false)
  42. glog.V(0).Infof("Starting Seaweed WebDav Server %s at https port %d", util.VERSION, *webDavStandaloneOptions.port)
  43. return webDavStandaloneOptions.startWebDav()
  44. }
  45. func (wo *WebDavOption) startWebDav() bool {
  46. // detect current user
  47. uid, gid := uint32(0), uint32(0)
  48. if u, err := user.Current(); err == nil {
  49. if parsedId, pe := strconv.ParseUint(u.Uid, 10, 32); pe == nil {
  50. uid = uint32(parsedId)
  51. }
  52. if parsedId, pe := strconv.ParseUint(u.Gid, 10, 32); pe == nil {
  53. gid = uint32(parsedId)
  54. }
  55. }
  56. // parse filer grpc address
  57. filerGrpcAddress, err := pb.ParseFilerGrpcAddress(*wo.filer)
  58. if err != nil {
  59. glog.Fatal(err)
  60. return false
  61. }
  62. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  63. var cipher bool
  64. // connect to filer
  65. for {
  66. err = pb.WithGrpcFilerClient(filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  67. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  68. if err != nil {
  69. return fmt.Errorf("get filer %s configuration: %v", filerGrpcAddress, err)
  70. }
  71. cipher = resp.Cipher
  72. return nil
  73. })
  74. if err != nil {
  75. glog.V(0).Infof("wait to connect to filer %s grpc address %s", *wo.filer, filerGrpcAddress)
  76. time.Sleep(time.Second)
  77. } else {
  78. glog.V(0).Infof("connected to filer %s grpc address %s", *wo.filer, filerGrpcAddress)
  79. break
  80. }
  81. }
  82. ws, webdavServer_err := weed_server.NewWebDavServer(&weed_server.WebDavOption{
  83. Filer: *wo.filer,
  84. FilerGrpcAddress: filerGrpcAddress,
  85. GrpcDialOption: grpcDialOption,
  86. Collection: *wo.collection,
  87. Uid: uid,
  88. Gid: gid,
  89. Cipher: cipher,
  90. })
  91. if webdavServer_err != nil {
  92. glog.Fatalf("WebDav Server startup error: %v", webdavServer_err)
  93. }
  94. httpS := &http.Server{Handler: ws.Handler}
  95. listenAddress := fmt.Sprintf(":%d", *wo.port)
  96. webDavListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
  97. if err != nil {
  98. glog.Fatalf("WebDav Server listener on %s error: %v", listenAddress, err)
  99. }
  100. if *wo.tlsPrivateKey != "" {
  101. glog.V(0).Infof("Start Seaweed WebDav Server %s at https port %d", util.VERSION, *wo.port)
  102. if err = httpS.ServeTLS(webDavListener, *wo.tlsCertificate, *wo.tlsPrivateKey); err != nil {
  103. glog.Fatalf("WebDav Server Fail to serve: %v", err)
  104. }
  105. } else {
  106. glog.V(0).Infof("Start Seaweed WebDav Server %s at http port %d", util.VERSION, *wo.port)
  107. if err = httpS.Serve(webDavListener); err != nil {
  108. glog.Fatalf("WebDav Server Fail to serve: %v", err)
  109. }
  110. }
  111. return true
  112. }