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.
 
 
 

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