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.

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