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.

415 lines
9.4 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/urfave/cli"
  10. "google.golang.org/api/googleapi"
  11. )
  12. var Version = "1.1.2"
  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: "",
  96. EnvVar: "S3_ENDPOINT",
  97. },
  98. cli.StringFlag{
  99. Name: "s3-region",
  100. Usage: "",
  101. Value: "eu-west-1",
  102. EnvVar: "S3_REGION",
  103. },
  104. cli.StringFlag{
  105. Name: "aws-access-key",
  106. Usage: "",
  107. Value: "",
  108. EnvVar: "AWS_ACCESS_KEY",
  109. },
  110. cli.StringFlag{
  111. Name: "aws-secret-key",
  112. Usage: "",
  113. Value: "",
  114. EnvVar: "AWS_SECRET_KEY",
  115. },
  116. cli.StringFlag{
  117. Name: "bucket",
  118. Usage: "",
  119. Value: "",
  120. EnvVar: "BUCKET",
  121. },
  122. cli.BoolFlag{
  123. Name: "s3-no-multipart",
  124. Usage: "Disables S3 Multipart Puts",
  125. },
  126. cli.BoolFlag{
  127. Name: "s3-path-style",
  128. Usage: "Forces path style URLs, required for Minio.",
  129. },
  130. cli.StringFlag{
  131. Name: "gdrive-client-json-filepath",
  132. Usage: "",
  133. Value: "",
  134. },
  135. cli.StringFlag{
  136. Name: "gdrive-local-config-path",
  137. Usage: "",
  138. Value: "",
  139. },
  140. cli.IntFlag{
  141. Name: "gdrive-chunk-size",
  142. Usage: "",
  143. Value: googleapi.DefaultUploadChunkSize / 1024 / 1024,
  144. },
  145. cli.StringFlag{
  146. Name: "storj-access",
  147. Usage: "Access for the project",
  148. Value: "",
  149. EnvVar: "STORJ_ACCESS",
  150. },
  151. cli.StringFlag{
  152. Name: "storj-bucket",
  153. Usage: "Bucket to use within the project",
  154. Value: "",
  155. EnvVar: "STORJ_BUCKET",
  156. },
  157. cli.IntFlag{
  158. Name: "rate-limit",
  159. Usage: "requests per minute",
  160. Value: 0,
  161. EnvVar: "",
  162. },
  163. cli.StringFlag{
  164. Name: "lets-encrypt-hosts",
  165. Usage: "host1, host2",
  166. Value: "",
  167. EnvVar: "HOSTS",
  168. },
  169. cli.StringFlag{
  170. Name: "log",
  171. Usage: "/var/log/transfersh.log",
  172. Value: "",
  173. },
  174. cli.StringFlag{
  175. Name: "basedir",
  176. Usage: "path to storage",
  177. Value: "",
  178. },
  179. cli.StringFlag{
  180. Name: "clamav-host",
  181. Usage: "clamav-host",
  182. Value: "",
  183. EnvVar: "CLAMAV_HOST",
  184. },
  185. cli.StringFlag{
  186. Name: "virustotal-key",
  187. Usage: "virustotal-key",
  188. Value: "",
  189. EnvVar: "VIRUSTOTAL_KEY",
  190. },
  191. cli.BoolFlag{
  192. Name: "profiler",
  193. Usage: "enable profiling",
  194. },
  195. cli.StringFlag{
  196. Name: "http-auth-user",
  197. Usage: "user for http basic auth",
  198. Value: "",
  199. },
  200. cli.StringFlag{
  201. Name: "http-auth-pass",
  202. Usage: "pass for http basic auth",
  203. Value: "",
  204. },
  205. cli.StringFlag{
  206. Name: "ip-whitelist",
  207. Usage: "comma separated list of ips allowed to connect to the service",
  208. Value: "",
  209. },
  210. cli.StringFlag{
  211. Name: "ip-blacklist",
  212. Usage: "comma separated list of ips not allowed to connect to the service",
  213. Value: "",
  214. },
  215. }
  216. type Cmd struct {
  217. *cli.App
  218. }
  219. func VersionAction(c *cli.Context) {
  220. fmt.Println(color.YellowString(fmt.Sprintf("transfer.sh: Easy file sharing from the command line")))
  221. }
  222. func New() *Cmd {
  223. logger := log.New(os.Stdout, "[transfer.sh]", log.LstdFlags)
  224. app := cli.NewApp()
  225. app.Name = "transfer.sh"
  226. app.Author = ""
  227. app.Usage = "transfer.sh"
  228. app.Description = `Easy file sharing from the command line`
  229. app.Version = Version
  230. app.Flags = globalFlags
  231. app.CustomAppHelpTemplate = helpTemplate
  232. app.Commands = []cli.Command{
  233. {
  234. Name: "version",
  235. Action: VersionAction,
  236. },
  237. }
  238. app.Before = func(c *cli.Context) error {
  239. return nil
  240. }
  241. app.Action = func(c *cli.Context) {
  242. options := []server.OptionFn{}
  243. if v := c.String("listener"); v != "" {
  244. options = append(options, server.Listener(v))
  245. }
  246. if v := c.String("tls-listener"); v == "" {
  247. } else if c.Bool("tls-listener-only") {
  248. options = append(options, server.TLSListener(v, true))
  249. } else {
  250. options = append(options, server.TLSListener(v, false))
  251. }
  252. if v := c.String("profile-listener"); v != "" {
  253. options = append(options, server.ProfileListener(v))
  254. }
  255. if v := c.String("web-path"); v != "" {
  256. options = append(options, server.WebPath(v))
  257. }
  258. if v := c.String("proxy-path"); v != "" {
  259. options = append(options, server.ProxyPath(v))
  260. }
  261. if v := c.String("ga-key"); v != "" {
  262. options = append(options, server.GoogleAnalytics(v))
  263. }
  264. if v := c.String("uservoice-key"); v != "" {
  265. options = append(options, server.UserVoice(v))
  266. }
  267. if v := c.String("temp-path"); v != "" {
  268. options = append(options, server.TempPath(v))
  269. }
  270. if v := c.String("log"); v != "" {
  271. options = append(options, server.LogFile(logger, v))
  272. } else {
  273. options = append(options, server.Logger(logger))
  274. }
  275. if v := c.String("lets-encrypt-hosts"); v != "" {
  276. options = append(options, server.UseLetsEncrypt(strings.Split(v, ",")))
  277. }
  278. if v := c.String("virustotal-key"); v != "" {
  279. options = append(options, server.VirustotalKey(v))
  280. }
  281. if v := c.String("clamav-host"); v != "" {
  282. options = append(options, server.ClamavHost(v))
  283. }
  284. if v := c.Int("rate-limit"); v > 0 {
  285. options = append(options, server.RateLimit(v))
  286. }
  287. if cert := c.String("tls-cert-file"); cert == "" {
  288. } else if pk := c.String("tls-private-key"); pk == "" {
  289. } else {
  290. options = append(options, server.TLSConfig(cert, pk))
  291. }
  292. if c.Bool("profiler") {
  293. options = append(options, server.EnableProfiler())
  294. }
  295. if c.Bool("force-https") {
  296. options = append(options, server.ForceHTTPs())
  297. }
  298. if httpAuthUser := c.String("http-auth-user"); httpAuthUser == "" {
  299. } else if httpAuthPass := c.String("http-auth-pass"); httpAuthPass == "" {
  300. } else {
  301. options = append(options, server.HttpAuthCredentials(httpAuthUser, httpAuthPass))
  302. }
  303. applyIPFilter := false
  304. ipFilterOptions := server.IPFilterOptions{}
  305. if ipWhitelist := c.String("ip-whitelist"); ipWhitelist != "" {
  306. applyIPFilter = true
  307. ipFilterOptions.AllowedIPs = strings.Split(ipWhitelist, ",")
  308. ipFilterOptions.BlockByDefault = true
  309. }
  310. if ipBlacklist := c.String("ip-blacklist"); ipBlacklist != "" {
  311. applyIPFilter = true
  312. ipFilterOptions.BlockedIPs = strings.Split(ipBlacklist, ",")
  313. }
  314. if applyIPFilter {
  315. options = append(options, server.FilterOptions(ipFilterOptions))
  316. }
  317. switch provider := c.String("provider"); provider {
  318. case "s3":
  319. if accessKey := c.String("aws-access-key"); accessKey == "" {
  320. panic("access-key not set.")
  321. } else if secretKey := c.String("aws-secret-key"); secretKey == "" {
  322. panic("secret-key not set.")
  323. } else if bucket := c.String("bucket"); bucket == "" {
  324. panic("bucket not set.")
  325. } else if storage, err := server.NewS3Storage(accessKey, secretKey, bucket, c.String("s3-region"), 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(storage))
  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 storage, err := server.NewGDriveStorage(clientJsonFilepath, localConfigPath, basedir, chunkSize, logger); err != nil {
  339. panic(err)
  340. } else {
  341. options = append(options, server.UseStorage(storage))
  342. }
  343. case "storj":
  344. if access := c.String("storj-access"); access == "" {
  345. panic("storj-access not set.")
  346. } else if bucket := c.String("storj-bucket"); bucket == "" {
  347. panic("storj-bucket not set.")
  348. } else if storage, err := server.NewStorjStorage(access, bucket, logger); err != nil {
  349. panic(err)
  350. } else {
  351. options = append(options, server.UseStorage(storage))
  352. }
  353. case "local":
  354. if v := c.String("basedir"); v == "" {
  355. panic("basedir not set.")
  356. } else if storage, err := server.NewLocalStorage(v, logger); err != nil {
  357. panic(err)
  358. } else {
  359. options = append(options, server.UseStorage(storage))
  360. }
  361. default:
  362. panic("Provider not set or invalid.")
  363. }
  364. srvr, err := server.New(
  365. options...,
  366. )
  367. if err != nil {
  368. logger.Println(color.RedString("Error starting server: %s", err.Error()))
  369. return
  370. }
  371. srvr.Run()
  372. }
  373. return &Cmd{
  374. App: app,
  375. }
  376. }