webdav.go 4.9 KB

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