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.
 
 
 

405 lines
9.1 KiB

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