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.

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