webdav.go 4.7 KB

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