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.
 
 
 

264 lines
5.3 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.StringFlag{
  49. Name: "tls-cert-file",
  50. Value: "",
  51. },
  52. cli.StringFlag{
  53. Name: "tls-private-key",
  54. Value: "",
  55. },
  56. cli.StringFlag{
  57. Name: "temp-path",
  58. Usage: "path to temp files",
  59. Value: os.TempDir(),
  60. },
  61. cli.StringFlag{
  62. Name: "web-path",
  63. Usage: "path to static web files",
  64. Value: "",
  65. },
  66. cli.StringFlag{
  67. Name: "provider",
  68. Usage: "s3|local",
  69. Value: "",
  70. },
  71. cli.StringFlag{
  72. Name: "s3-endpoint",
  73. Usage: "",
  74. Value: "http://s3-eu-west-1.amazonaws.com",
  75. EnvVar: "S3_ENDPOINT",
  76. },
  77. cli.StringFlag{
  78. Name: "aws-access-key",
  79. Usage: "",
  80. Value: "",
  81. EnvVar: "AWS_ACCESS_KEY",
  82. },
  83. cli.StringFlag{
  84. Name: "aws-secret-key",
  85. Usage: "",
  86. Value: "",
  87. EnvVar: "AWS_SECRET_KEY",
  88. },
  89. cli.StringFlag{
  90. Name: "bucket",
  91. Usage: "",
  92. Value: "",
  93. EnvVar: "BUCKET",
  94. },
  95. cli.IntFlag{
  96. Name: "rate-limit",
  97. Usage: "requests per minute",
  98. Value: 0,
  99. EnvVar: "",
  100. },
  101. cli.StringFlag{
  102. Name: "lets-encrypt-hosts",
  103. Usage: "host1, host2",
  104. Value: "",
  105. EnvVar: "HOSTS",
  106. },
  107. cli.StringFlag{
  108. Name: "log",
  109. Usage: "/var/log/transfersh.log",
  110. Value: "",
  111. },
  112. cli.StringFlag{
  113. Name: "basedir",
  114. Usage: "path to storage",
  115. Value: "",
  116. },
  117. cli.StringFlag{
  118. Name: "clamav-host",
  119. Usage: "clamav-host",
  120. Value: "",
  121. EnvVar: "CLAMAV_HOST",
  122. },
  123. cli.StringFlag{
  124. Name: "virustotal-key",
  125. Usage: "virustotal-key",
  126. Value: "",
  127. EnvVar: "VIRUSTOTAL_KEY",
  128. },
  129. cli.BoolFlag{
  130. Name: "profiler",
  131. Usage: "enable profiling",
  132. },
  133. }
  134. type Cmd struct {
  135. *cli.App
  136. }
  137. func VersionAction(c *cli.Context) {
  138. fmt.Println(color.YellowString(fmt.Sprintf("transfer.sh: Easy file sharing from the command line")))
  139. }
  140. func New() *Cmd {
  141. app := cli.NewApp()
  142. app.Name = "transfer.sh"
  143. app.Author = ""
  144. app.Usage = "transfer.sh"
  145. app.Description = `Easy file sharing from the command line`
  146. app.Flags = globalFlags
  147. app.CustomAppHelpTemplate = helpTemplate
  148. app.Commands = []cli.Command{
  149. {
  150. Name: "version",
  151. Action: VersionAction,
  152. },
  153. }
  154. app.Before = func(c *cli.Context) error {
  155. return nil
  156. }
  157. app.Action = func(c *cli.Context) {
  158. options := []server.OptionFn{}
  159. if v := c.String("listener"); v != "" {
  160. options = append(options, server.Listener(v))
  161. }
  162. if v := c.String("tls-listener"); v != "" {
  163. options = append(options, server.TLSListener(v))
  164. }
  165. if v := c.String("profile-listener"); v != "" {
  166. options = append(options, server.ProfileListener(v))
  167. }
  168. if v := c.String("web-path"); v != "" {
  169. options = append(options, server.WebPath(v))
  170. }
  171. if v := c.String("temp-path"); v != "" {
  172. options = append(options, server.TempPath(v))
  173. }
  174. if v := c.String("lets-encrypt-hosts"); v != "" {
  175. options = append(options, server.UseLetsEncrypt(strings.Split(v, ",")))
  176. }
  177. if v := c.String("virustotal-key"); v != "" {
  178. options = append(options, server.VirustotalKey(v))
  179. }
  180. if v := c.String("clamav-host"); v != "" {
  181. options = append(options, server.ClamavHost(v))
  182. }
  183. if v := c.Int("rate-limit"); v > 0 {
  184. options = append(options, server.RateLimit(v))
  185. }
  186. if cert := c.String("tls-cert-file"); cert == "" {
  187. } else if pk := c.String("tls-private-key"); pk == "" {
  188. } else {
  189. options = append(options, server.TLSConfig(cert, pk))
  190. }
  191. if c.Bool("profiler") {
  192. options = append(options, server.EnableProfiler())
  193. }
  194. if c.Bool("force-https") {
  195. options = append(options, server.ForceHTTPs())
  196. }
  197. switch provider := c.String("provider"); provider {
  198. case "s3":
  199. if accessKey := c.String("aws-access-key"); accessKey == "" {
  200. panic("access-key not set.")
  201. } else if secretKey := c.String("aws-secret-key"); secretKey == "" {
  202. panic("secret-key not set.")
  203. } else if bucket := c.String("bucket"); bucket == "" {
  204. panic("bucket not set.")
  205. } else if storage, err := server.NewS3Storage(accessKey, secretKey, bucket, c.String("s3-endpoint")); err != nil {
  206. panic(err)
  207. } else {
  208. options = append(options, server.UseStorage(storage))
  209. }
  210. case "local":
  211. if v := c.String("basedir"); v == "" {
  212. panic("basedir not set.")
  213. } else if storage, err := server.NewLocalStorage(v); err != nil {
  214. panic(err)
  215. } else {
  216. options = append(options, server.UseStorage(storage))
  217. }
  218. default:
  219. panic("Provider not set or invalid.")
  220. }
  221. srvr, err := server.New(
  222. options...,
  223. )
  224. if err != nil {
  225. fmt.Println(color.RedString("Error starting server: %s", err.Error()))
  226. return
  227. }
  228. srvr.Run()
  229. }
  230. return &Cmd{
  231. App: app,
  232. }
  233. }