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.
 
 
 

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