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.
 
 
 

328 lines
7.0 KiB

  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "github.com/dutchcoders/transfer.sh/server"
  7. "github.com/fatih/color"
  8. "github.com/minio/cli"
  9. )
  10. var Version = "0.1"
  11. var helpTemplate = `NAME:
  12. {{.Name}} - {{.Usage}}
  13. DESCRIPTION:
  14. {{.Description}}
  15. USAGE:
  16. {{.Name}} {{if .Flags}}[flags] {{end}}command{{if .Flags}}{{end}} [arguments...]
  17. COMMANDS:
  18. {{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
  19. {{end}}{{if .Flags}}
  20. FLAGS:
  21. {{range .Flags}}{{.}}
  22. {{end}}{{end}}
  23. VERSION:
  24. ` + Version +
  25. `{{ "\n"}}`
  26. var globalFlags = []cli.Flag{
  27. cli.StringFlag{
  28. Name: "listener",
  29. Usage: "127.0.0.1:8080",
  30. Value: "127.0.0.1:8080",
  31. },
  32. // redirect to https?
  33. // hostnames
  34. cli.StringFlag{
  35. Name: "profile-listener",
  36. Usage: "127.0.0.1:6060",
  37. Value: "",
  38. },
  39. cli.BoolFlag{
  40. Name: "force-https",
  41. Usage: "",
  42. },
  43. cli.StringFlag{
  44. Name: "tls-listener",
  45. Usage: "127.0.0.1:8443",
  46. Value: "",
  47. },
  48. cli.BoolFlag{
  49. Name: "tls-listener-only",
  50. Usage: "",
  51. },
  52. cli.StringFlag{
  53. Name: "tls-cert-file",
  54. Value: "",
  55. },
  56. cli.StringFlag{
  57. Name: "tls-private-key",
  58. Value: "",
  59. },
  60. cli.StringFlag{
  61. Name: "temp-path",
  62. Usage: "path to temp files",
  63. Value: os.TempDir(),
  64. },
  65. cli.StringFlag{
  66. Name: "web-path",
  67. Usage: "path to static web files",
  68. Value: "",
  69. },
  70. cli.StringFlag{
  71. Name: "ga-key",
  72. Usage: "key for google analytics (front end)",
  73. Value: "",
  74. },
  75. cli.StringFlag{
  76. Name: "uservoice-key",
  77. Usage: "key for user voice (front end)",
  78. Value: "",
  79. },
  80. cli.StringFlag{
  81. Name: "provider",
  82. Usage: "s3|gdrive|local",
  83. Value: "",
  84. },
  85. cli.StringFlag{
  86. Name: "s3-endpoint",
  87. Usage: "",
  88. Value: "http://s3-eu-west-1.amazonaws.com",
  89. EnvVar: "S3_ENDPOINT",
  90. },
  91. cli.StringFlag{
  92. Name: "aws-access-key",
  93. Usage: "",
  94. Value: "",
  95. EnvVar: "AWS_ACCESS_KEY",
  96. },
  97. cli.StringFlag{
  98. Name: "aws-secret-key",
  99. Usage: "",
  100. Value: "",
  101. EnvVar: "AWS_SECRET_KEY",
  102. },
  103. cli.StringFlag{
  104. Name: "bucket",
  105. Usage: "",
  106. Value: "",
  107. EnvVar: "BUCKET",
  108. },
  109. cli.StringFlag{
  110. Name: "gdrive-client-json-filepath",
  111. Usage: "",
  112. Value: "",
  113. },
  114. cli.StringFlag{
  115. Name: "gdrive-local-config-path",
  116. Usage: "",
  117. Value: "",
  118. },
  119. cli.IntFlag{
  120. Name: "rate-limit",
  121. Usage: "requests per minute",
  122. Value: 0,
  123. EnvVar: "",
  124. },
  125. cli.StringFlag{
  126. Name: "lets-encrypt-hosts",
  127. Usage: "host1, host2",
  128. Value: "",
  129. EnvVar: "HOSTS",
  130. },
  131. cli.StringFlag{
  132. Name: "log",
  133. Usage: "/var/log/transfersh.log",
  134. Value: "",
  135. },
  136. cli.StringFlag{
  137. Name: "basedir",
  138. Usage: "path to storage",
  139. Value: "",
  140. },
  141. cli.StringFlag{
  142. Name: "clamav-host",
  143. Usage: "clamav-host",
  144. Value: "",
  145. EnvVar: "CLAMAV_HOST",
  146. },
  147. cli.StringFlag{
  148. Name: "virustotal-key",
  149. Usage: "virustotal-key",
  150. Value: "",
  151. EnvVar: "VIRUSTOTAL_KEY",
  152. },
  153. cli.BoolFlag{
  154. Name: "profiler",
  155. Usage: "enable profiling",
  156. },
  157. cli.StringFlag{
  158. Name: "http-auth-user",
  159. Usage: "user for http basic auth",
  160. Value: "",
  161. },
  162. cli.StringFlag{
  163. Name: "http-auth-pass",
  164. Usage: "pass for http basic auth",
  165. Value: "",
  166. },
  167. }
  168. type Cmd struct {
  169. *cli.App
  170. }
  171. func VersionAction(c *cli.Context) {
  172. fmt.Println(color.YellowString(fmt.Sprintf("transfer.sh: Easy file sharing from the command line")))
  173. }
  174. func New() *Cmd {
  175. app := cli.NewApp()
  176. app.Name = "transfer.sh"
  177. app.Author = ""
  178. app.Usage = "transfer.sh"
  179. app.Description = `Easy file sharing from the command line`
  180. app.Flags = globalFlags
  181. app.CustomAppHelpTemplate = helpTemplate
  182. app.Commands = []cli.Command{
  183. {
  184. Name: "version",
  185. Action: VersionAction,
  186. },
  187. }
  188. app.Before = func(c *cli.Context) error {
  189. return nil
  190. }
  191. app.Action = func(c *cli.Context) {
  192. options := []server.OptionFn{}
  193. if v := c.String("listener"); v != "" {
  194. options = append(options, server.Listener(v))
  195. }
  196. if v := c.String("tls-listener"); v == "" {
  197. } else if c.Bool("tls-listener-only") {
  198. options = append(options, server.TLSListener(v, true))
  199. } else {
  200. options = append(options, server.TLSListener(v, false))
  201. }
  202. if v := c.String("profile-listener"); v != "" {
  203. options = append(options, server.ProfileListener(v))
  204. }
  205. if v := c.String("web-path"); v != "" {
  206. options = append(options, server.WebPath(v))
  207. }
  208. if v := c.String("ga-key"); v != "" {
  209. options = append(options, server.GoogleAnalytics(v))
  210. }
  211. if v := c.String("uservoice-key"); v != "" {
  212. options = append(options, server.UserVoice(v))
  213. }
  214. if v := c.String("temp-path"); v != "" {
  215. options = append(options, server.TempPath(v))
  216. }
  217. if v := c.String("lets-encrypt-hosts"); v != "" {
  218. options = append(options, server.UseLetsEncrypt(strings.Split(v, ",")))
  219. }
  220. if v := c.String("virustotal-key"); v != "" {
  221. options = append(options, server.VirustotalKey(v))
  222. }
  223. if v := c.String("clamav-host"); v != "" {
  224. options = append(options, server.ClamavHost(v))
  225. }
  226. if v := c.Int("rate-limit"); v > 0 {
  227. options = append(options, server.RateLimit(v))
  228. }
  229. if cert := c.String("tls-cert-file"); cert == "" {
  230. } else if pk := c.String("tls-private-key"); pk == "" {
  231. } else {
  232. options = append(options, server.TLSConfig(cert, pk))
  233. }
  234. if c.Bool("profiler") {
  235. options = append(options, server.EnableProfiler())
  236. }
  237. if c.Bool("force-https") {
  238. options = append(options, server.ForceHTTPs())
  239. }
  240. if httpAuthUser := c.String("http-auth-user"); httpAuthUser == "" {
  241. } else if httpAuthPass := c.String("http-auth-pass"); httpAuthPass == "" {
  242. } else {
  243. options = append(options, server.HttpAuthCredentials(httpAuthUser, httpAuthPass))
  244. }
  245. switch provider := c.String("provider"); provider {
  246. case "s3":
  247. if accessKey := c.String("aws-access-key"); accessKey == "" {
  248. panic("access-key not set.")
  249. } else if secretKey := c.String("aws-secret-key"); secretKey == "" {
  250. panic("secret-key not set.")
  251. } else if bucket := c.String("bucket"); bucket == "" {
  252. panic("bucket not set.")
  253. } else if storage, err := server.NewS3Storage(accessKey, secretKey, bucket, c.String("s3-endpoint")); err != nil {
  254. panic(err)
  255. } else {
  256. options = append(options, server.UseStorage(storage))
  257. }
  258. case "gdrive":
  259. if clientJsonFilepath := c.String("gdrive-client-json-filepath"); clientJsonFilepath == "" {
  260. panic("client-json-filepath not set.")
  261. } else if localConfigPath := c.String("gdrive-local-config-path"); localConfigPath == "" {
  262. panic("local-config-path not set.")
  263. } else if basedir := c.String("basedir"); basedir == "" {
  264. panic("basedir not set.")
  265. } else if storage, err := server.NewGDriveStorage(clientJsonFilepath, localConfigPath, basedir); err != nil {
  266. panic(err)
  267. } else {
  268. options = append(options, server.UseStorage(storage))
  269. }
  270. case "local":
  271. if v := c.String("basedir"); v == "" {
  272. panic("basedir not set.")
  273. } else if storage, err := server.NewLocalStorage(v); err != nil {
  274. panic(err)
  275. } else {
  276. options = append(options, server.UseStorage(storage))
  277. }
  278. default:
  279. panic("Provider not set or invalid.")
  280. }
  281. srvr, err := server.New(
  282. options...,
  283. )
  284. if err != nil {
  285. fmt.Println(color.RedString("Error starting server: %s", err.Error()))
  286. return
  287. }
  288. srvr.Run()
  289. }
  290. return &Cmd{
  291. App: app,
  292. }
  293. }