webdav.go 4.8 KB

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