You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.3 KiB

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