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.
 
 
 

355 lines
7.8 KiB

  1. package cmd
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "strings"
  7. "github.com/dutchcoders/transfer.sh/server"
  8. "github.com/fatih/color"
  9. "github.com/minio/cli"
  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: "proxy-path",
  74. Usage: "path prefix when service is run behind a proxy",
  75. Value: "",
  76. },
  77. cli.StringFlag{
  78. Name: "ga-key",
  79. Usage: "key for google analytics (front end)",
  80. Value: "",
  81. },
  82. cli.StringFlag{
  83. Name: "uservoice-key",
  84. Usage: "key for user voice (front end)",
  85. Value: "",
  86. },
  87. cli.StringFlag{
  88. Name: "provider",
  89. Usage: "s3|gdrive|local",
  90. Value: "",
  91. },
  92. cli.StringFlag{
  93. Name: "s3-endpoint",
  94. Usage: "",
  95. Value: "http://s3-eu-west-1.amazonaws.com",
  96. EnvVar: "S3_ENDPOINT",
  97. },
  98. cli.StringFlag{
  99. Name: "aws-access-key",
  100. Usage: "",
  101. Value: "",
  102. EnvVar: "AWS_ACCESS_KEY",
  103. },
  104. cli.StringFlag{
  105. Name: "aws-secret-key",
  106. Usage: "",
  107. Value: "",
  108. EnvVar: "AWS_SECRET_KEY",
  109. },
  110. cli.StringFlag{
  111. Name: "bucket",
  112. Usage: "",
  113. Value: "",
  114. EnvVar: "BUCKET",
  115. },
  116. cli.BoolFlag{
  117. Name: "s3-no-multipart",
  118. Usage: "Disables S3 Multipart Puts",
  119. },
  120. cli.StringFlag{
  121. Name: "gdrive-client-json-filepath",
  122. Usage: "",
  123. Value: "",
  124. },
  125. cli.StringFlag{
  126. Name: "gdrive-local-config-path",
  127. Usage: "",
  128. Value: "",
  129. },
  130. cli.IntFlag{
  131. Name: "gdrive-chunk-size",
  132. Usage: "",
  133. Value: googleapi.DefaultUploadChunkSize / 1024 / 1024,
  134. },
  135. cli.IntFlag{
  136. Name: "rate-limit",
  137. Usage: "requests per minute",
  138. Value: 0,
  139. EnvVar: "",
  140. },
  141. cli.StringFlag{
  142. Name: "lets-encrypt-hosts",
  143. Usage: "host1, host2",
  144. Value: "",
  145. EnvVar: "HOSTS",
  146. },
  147. cli.StringFlag{
  148. Name: "log",
  149. Usage: "/var/log/transfersh.log",
  150. Value: "",
  151. },
  152. cli.StringFlag{
  153. Name: "basedir",
  154. Usage: "path to storage",
  155. Value: "",
  156. },
  157. cli.StringFlag{
  158. Name: "clamav-host",
  159. Usage: "clamav-host",
  160. Value: "",
  161. EnvVar: "CLAMAV_HOST",
  162. },
  163. cli.StringFlag{
  164. Name: "virustotal-key",
  165. Usage: "virustotal-key",
  166. Value: "",
  167. EnvVar: "VIRUSTOTAL_KEY",
  168. },
  169. cli.BoolFlag{
  170. Name: "profiler",
  171. Usage: "enable profiling",
  172. },
  173. cli.StringFlag{
  174. Name: "http-auth-user",
  175. Usage: "user for http basic auth",
  176. Value: "",
  177. },
  178. cli.StringFlag{
  179. Name: "http-auth-pass",
  180. Usage: "pass for http basic auth",
  181. Value: "",
  182. },
  183. }
  184. type Cmd struct {
  185. *cli.App
  186. }
  187. func VersionAction(c *cli.Context) {
  188. fmt.Println(color.YellowString(fmt.Sprintf("transfer.sh: Easy file sharing from the command line")))
  189. }
  190. func New() *Cmd {
  191. logger := log.New(os.Stdout, "[transfer.sh]", log.LstdFlags)
  192. app := cli.NewApp()
  193. app.Name = "transfer.sh"
  194. app.Author = ""
  195. app.Usage = "transfer.sh"
  196. app.Description = `Easy file sharing from the command line`
  197. app.Flags = globalFlags
  198. app.CustomAppHelpTemplate = helpTemplate
  199. app.Commands = []cli.Command{
  200. {
  201. Name: "version",
  202. Action: VersionAction,
  203. },
  204. }
  205. app.Before = func(c *cli.Context) error {
  206. return nil
  207. }
  208. app.Action = func(c *cli.Context) {
  209. options := []server.OptionFn{}
  210. if v := c.String("listener"); v != "" {
  211. options = append(options, server.Listener(v))
  212. }
  213. if v := c.String("tls-listener"); v == "" {
  214. } else if c.Bool("tls-listener-only") {
  215. options = append(options, server.TLSListener(v, true))
  216. } else {
  217. options = append(options, server.TLSListener(v, false))
  218. }
  219. if v := c.String("profile-listener"); v != "" {
  220. options = append(options, server.ProfileListener(v))
  221. }
  222. if v := c.String("web-path"); v != "" {
  223. options = append(options, server.WebPath(v))
  224. }
  225. if v := c.String("proxy-path"); v != "" {
  226. options = append(options, server.ProxyPath(v))
  227. }
  228. if v := c.String("ga-key"); v != "" {
  229. options = append(options, server.GoogleAnalytics(v))
  230. }
  231. if v := c.String("uservoice-key"); v != "" {
  232. options = append(options, server.UserVoice(v))
  233. }
  234. if v := c.String("temp-path"); v != "" {
  235. options = append(options, server.TempPath(v))
  236. }
  237. if v := c.String("log"); v != "" {
  238. options = append(options, server.LogFile(logger, v))
  239. } else {
  240. options = append(options, server.Logger(logger))
  241. }
  242. if v := c.String("lets-encrypt-hosts"); v != "" {
  243. options = append(options, server.UseLetsEncrypt(strings.Split(v, ",")))
  244. }
  245. if v := c.String("virustotal-key"); v != "" {
  246. options = append(options, server.VirustotalKey(v))
  247. }
  248. if v := c.String("clamav-host"); v != "" {
  249. options = append(options, server.ClamavHost(v))
  250. }
  251. if v := c.Int("rate-limit"); v > 0 {
  252. options = append(options, server.RateLimit(v))
  253. }
  254. if cert := c.String("tls-cert-file"); cert == "" {
  255. } else if pk := c.String("tls-private-key"); pk == "" {
  256. } else {
  257. options = append(options, server.TLSConfig(cert, pk))
  258. }
  259. if c.Bool("profiler") {
  260. options = append(options, server.EnableProfiler())
  261. }
  262. if c.Bool("force-https") {
  263. options = append(options, server.ForceHTTPs())
  264. }
  265. if httpAuthUser := c.String("http-auth-user"); httpAuthUser == "" {
  266. } else if httpAuthPass := c.String("http-auth-pass"); httpAuthPass == "" {
  267. } else {
  268. options = append(options, server.HttpAuthCredentials(httpAuthUser, httpAuthPass))
  269. }
  270. switch provider := c.String("provider"); provider {
  271. case "s3":
  272. if accessKey := c.String("aws-access-key"); accessKey == "" {
  273. panic("access-key not set.")
  274. } else if secretKey := c.String("aws-secret-key"); secretKey == "" {
  275. panic("secret-key not set.")
  276. } else if bucket := c.String("bucket"); bucket == "" {
  277. panic("bucket not set.")
  278. } else if storage, err := server.NewS3Storage(accessKey, secretKey, bucket, c.String("s3-endpoint"), logger, c.Bool("s3-no-multipart")); err != nil {
  279. panic(err)
  280. } else {
  281. options = append(options, server.UseStorage(storage))
  282. }
  283. case "gdrive":
  284. chunkSize := c.Int("gdrive-chunk-size")
  285. if clientJsonFilepath := c.String("gdrive-client-json-filepath"); clientJsonFilepath == "" {
  286. panic("client-json-filepath not set.")
  287. } else if localConfigPath := c.String("gdrive-local-config-path"); localConfigPath == "" {
  288. panic("local-config-path not set.")
  289. } else if basedir := c.String("basedir"); basedir == "" {
  290. panic("basedir not set.")
  291. } else if storage, err := server.NewGDriveStorage(clientJsonFilepath, localConfigPath, basedir, chunkSize, logger); err != nil {
  292. panic(err)
  293. } else {
  294. options = append(options, server.UseStorage(storage))
  295. }
  296. case "local":
  297. if v := c.String("basedir"); v == "" {
  298. panic("basedir not set.")
  299. } else if storage, err := server.NewLocalStorage(v, logger); err != nil {
  300. panic(err)
  301. } else {
  302. options = append(options, server.UseStorage(storage))
  303. }
  304. default:
  305. panic("Provider not set or invalid.")
  306. }
  307. srvr, err := server.New(
  308. options...,
  309. )
  310. if err != nil {
  311. logger.Println(color.RedString("Error starting server: %s", err.Error()))
  312. return
  313. }
  314. srvr.Run()
  315. }
  316. return &Cmd{
  317. App: app,
  318. }
  319. }