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.
 
 
 

288 lines
6.0 KiB

  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "github.com/dutchcoders/transfer.sh/server"
  7. "github.com/fatih/color"
  8. "github.com/minio/cli"
  9. )
  10. var Version = "0.1"
  11. var helpTemplate = `NAME:
  12. {{.Name}} - {{.Usage}}
  13. DESCRIPTION:
  14. {{.Description}}
  15. USAGE:
  16. {{.Name}} {{if .Flags}}[flags] {{end}}command{{if .Flags}}{{end}} [arguments...]
  17. COMMANDS:
  18. {{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
  19. {{end}}{{if .Flags}}
  20. FLAGS:
  21. {{range .Flags}}{{.}}
  22. {{end}}{{end}}
  23. VERSION:
  24. ` + Version +
  25. `{{ "\n"}}`
  26. var globalFlags = []cli.Flag{
  27. cli.StringFlag{
  28. Name: "listener",
  29. Usage: "127.0.0.1:8080",
  30. Value: "127.0.0.1:8080",
  31. },
  32. // redirect to https?
  33. // hostnames
  34. cli.StringFlag{
  35. Name: "profile-listener",
  36. Usage: "127.0.0.1:6060",
  37. Value: "",
  38. },
  39. cli.BoolFlag{
  40. Name: "force-https",
  41. Usage: "",
  42. },
  43. cli.StringFlag{
  44. Name: "tls-listener",
  45. Usage: "127.0.0.1:8443",
  46. Value: "",
  47. },
  48. cli.StringFlag{
  49. Name: "tls-cert-file",
  50. Value: "",
  51. },
  52. cli.StringFlag{
  53. Name: "tls-private-key",
  54. Value: "",
  55. },
  56. cli.StringFlag{
  57. Name: "temp-path",
  58. Usage: "path to temp files",
  59. Value: os.TempDir(),
  60. },
  61. cli.StringFlag{
  62. Name: "web-path",
  63. Usage: "path to static web files",
  64. Value: "",
  65. },
  66. cli.StringFlag{
  67. Name: "provider",
  68. Usage: "s3|gdrive|local",
  69. Value: "",
  70. },
  71. cli.StringFlag{
  72. Name: "s3-endpoint",
  73. Usage: "",
  74. Value: "http://s3-eu-west-1.amazonaws.com",
  75. EnvVar: "S3_ENDPOINT",
  76. },
  77. cli.StringFlag{
  78. Name: "aws-access-key",
  79. Usage: "",
  80. Value: "",
  81. EnvVar: "AWS_ACCESS_KEY",
  82. },
  83. cli.StringFlag{
  84. Name: "aws-secret-key",
  85. Usage: "",
  86. Value: "",
  87. EnvVar: "AWS_SECRET_KEY",
  88. },
  89. cli.StringFlag{
  90. Name: "bucket",
  91. Usage: "",
  92. Value: "",
  93. EnvVar: "BUCKET",
  94. },
  95. cli.StringFlag{
  96. Name: "gdrive-client-json-filepath",
  97. Usage: "",
  98. Value: "",
  99. EnvVar: "",
  100. },
  101. cli.StringFlag{
  102. Name: "gdrive-local-config-path",
  103. Usage: "",
  104. Value: "",
  105. EnvVar: "",
  106. },
  107. cli.IntFlag{
  108. Name: "rate-limit",
  109. Usage: "requests per minute",
  110. Value: 0,
  111. EnvVar: "",
  112. },
  113. cli.StringFlag{
  114. Name: "lets-encrypt-hosts",
  115. Usage: "host1, host2",
  116. Value: "",
  117. EnvVar: "HOSTS",
  118. },
  119. cli.StringFlag{
  120. Name: "log",
  121. Usage: "/var/log/transfersh.log",
  122. Value: "",
  123. },
  124. cli.StringFlag{
  125. Name: "basedir",
  126. Usage: "path to storage",
  127. Value: "",
  128. },
  129. cli.StringFlag{
  130. Name: "clamav-host",
  131. Usage: "clamav-host",
  132. Value: "",
  133. EnvVar: "CLAMAV_HOST",
  134. },
  135. cli.StringFlag{
  136. Name: "virustotal-key",
  137. Usage: "virustotal-key",
  138. Value: "",
  139. EnvVar: "VIRUSTOTAL_KEY",
  140. },
  141. cli.BoolFlag{
  142. Name: "profiler",
  143. Usage: "enable profiling",
  144. },
  145. }
  146. type Cmd struct {
  147. *cli.App
  148. }
  149. func VersionAction(c *cli.Context) {
  150. fmt.Println(color.YellowString(fmt.Sprintf("transfer.sh: Easy file sharing from the command line")))
  151. }
  152. func New() *Cmd {
  153. app := cli.NewApp()
  154. app.Name = "transfer.sh"
  155. app.Author = ""
  156. app.Usage = "transfer.sh"
  157. app.Description = `Easy file sharing from the command line`
  158. app.Flags = globalFlags
  159. app.CustomAppHelpTemplate = helpTemplate
  160. app.Commands = []cli.Command{
  161. {
  162. Name: "version",
  163. Action: VersionAction,
  164. },
  165. }
  166. app.Before = func(c *cli.Context) error {
  167. return nil
  168. }
  169. app.Action = func(c *cli.Context) {
  170. options := []server.OptionFn{}
  171. if v := c.String("listener"); v != "" {
  172. options = append(options, server.Listener(v))
  173. }
  174. if v := c.String("tls-listener"); v != "" {
  175. options = append(options, server.TLSListener(v))
  176. }
  177. if v := c.String("profile-listener"); v != "" {
  178. options = append(options, server.ProfileListener(v))
  179. }
  180. if v := c.String("web-path"); v != "" {
  181. options = append(options, server.WebPath(v))
  182. }
  183. if v := c.String("temp-path"); v != "" {
  184. options = append(options, server.TempPath(v))
  185. }
  186. if v := c.String("lets-encrypt-hosts"); v != "" {
  187. options = append(options, server.UseLetsEncrypt(strings.Split(v, ",")))
  188. }
  189. if v := c.String("virustotal-key"); v != "" {
  190. options = append(options, server.VirustotalKey(v))
  191. }
  192. if v := c.String("clamav-host"); v != "" {
  193. options = append(options, server.ClamavHost(v))
  194. }
  195. if v := c.Int("rate-limit"); v > 0 {
  196. options = append(options, server.RateLimit(v))
  197. }
  198. if cert := c.String("tls-cert-file"); cert == "" {
  199. } else if pk := c.String("tls-private-key"); pk == "" {
  200. } else {
  201. options = append(options, server.TLSConfig(cert, pk))
  202. }
  203. if c.Bool("profiler") {
  204. options = append(options, server.EnableProfiler())
  205. }
  206. if c.Bool("force-https") {
  207. options = append(options, server.ForceHTTPs())
  208. }
  209. switch provider := c.String("provider"); provider {
  210. case "s3":
  211. if accessKey := c.String("aws-access-key"); accessKey == "" {
  212. panic("access-key not set.")
  213. } else if secretKey := c.String("aws-secret-key"); secretKey == "" {
  214. panic("secret-key not set.")
  215. } else if bucket := c.String("bucket"); bucket == "" {
  216. panic("bucket not set.")
  217. } else if storage, err := server.NewS3Storage(accessKey, secretKey, bucket, c.String("s3-endpoint")); err != nil {
  218. panic(err)
  219. } else {
  220. options = append(options, server.UseStorage(storage))
  221. }
  222. case "gdrive":
  223. if clientJsonFilepath := c.String("gdrive-client-json-filepath"); clientJsonFilepath == "" {
  224. panic("client-json-filepath not set.")
  225. } else if localConfigPath := c.String("gdrive-local-config-path"); localConfigPath == "" {
  226. panic("local-config-path not set.")
  227. } else if basedir := c.String("basedir"); basedir == "" {
  228. panic("basedir not set.")
  229. } else if storage, err := server.NewGDriveStorage(clientJsonFilepath, localConfigPath, basedir); err != nil {
  230. panic(err)
  231. } else {
  232. options = append(options, server.UseStorage(storage))
  233. }
  234. case "local":
  235. if v := c.String("basedir"); v == "" {
  236. panic("basedir not set.")
  237. } else if storage, err := server.NewLocalStorage(v); err != nil {
  238. panic(err)
  239. } else {
  240. options = append(options, server.UseStorage(storage))
  241. }
  242. default:
  243. panic("Provider not set or invalid.")
  244. }
  245. srvr, err := server.New(
  246. options...,
  247. )
  248. if err != nil {
  249. fmt.Println(color.RedString("Error starting server: %s", err.Error()))
  250. return
  251. }
  252. srvr.Run()
  253. }
  254. return &Cmd{
  255. App: app,
  256. }
  257. }