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.
 
 
 

321 lines
6.9 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: "ga-key",
  68. Usage: "key for google analytics (front end)",
  69. Value: "",
  70. },
  71. cli.StringFlag{
  72. Name: "uservoice-key",
  73. Usage: "key for user voice (front end)",
  74. Value: "",
  75. },
  76. cli.StringFlag{
  77. Name: "provider",
  78. Usage: "s3|gdrive|local",
  79. Value: "",
  80. },
  81. cli.StringFlag{
  82. Name: "s3-endpoint",
  83. Usage: "",
  84. Value: "http://s3-eu-west-1.amazonaws.com",
  85. EnvVar: "S3_ENDPOINT",
  86. },
  87. cli.StringFlag{
  88. Name: "aws-access-key",
  89. Usage: "",
  90. Value: "",
  91. EnvVar: "AWS_ACCESS_KEY",
  92. },
  93. cli.StringFlag{
  94. Name: "aws-secret-key",
  95. Usage: "",
  96. Value: "",
  97. EnvVar: "AWS_SECRET_KEY",
  98. },
  99. cli.StringFlag{
  100. Name: "bucket",
  101. Usage: "",
  102. Value: "",
  103. EnvVar: "BUCKET",
  104. },
  105. cli.StringFlag{
  106. Name: "gdrive-client-json-filepath",
  107. Usage: "",
  108. Value: "",
  109. },
  110. cli.StringFlag{
  111. Name: "gdrive-local-config-path",
  112. Usage: "",
  113. Value: "",
  114. },
  115. cli.IntFlag{
  116. Name: "rate-limit",
  117. Usage: "requests per minute",
  118. Value: 0,
  119. EnvVar: "",
  120. },
  121. cli.StringFlag{
  122. Name: "lets-encrypt-hosts",
  123. Usage: "host1, host2",
  124. Value: "",
  125. EnvVar: "HOSTS",
  126. },
  127. cli.StringFlag{
  128. Name: "log",
  129. Usage: "/var/log/transfersh.log",
  130. Value: "",
  131. },
  132. cli.StringFlag{
  133. Name: "basedir",
  134. Usage: "path to storage",
  135. Value: "",
  136. },
  137. cli.StringFlag{
  138. Name: "clamav-host",
  139. Usage: "clamav-host",
  140. Value: "",
  141. EnvVar: "CLAMAV_HOST",
  142. },
  143. cli.StringFlag{
  144. Name: "virustotal-key",
  145. Usage: "virustotal-key",
  146. Value: "",
  147. EnvVar: "VIRUSTOTAL_KEY",
  148. },
  149. cli.BoolFlag{
  150. Name: "profiler",
  151. Usage: "enable profiling",
  152. },
  153. cli.StringFlag{
  154. Name: "http-auth-user",
  155. Usage: "user for http basic auth",
  156. Value: "",
  157. },
  158. cli.StringFlag{
  159. Name: "http-auth-pass",
  160. Usage: "pass for http basic auth",
  161. Value: "",
  162. },
  163. }
  164. type Cmd struct {
  165. *cli.App
  166. }
  167. func VersionAction(c *cli.Context) {
  168. fmt.Println(color.YellowString(fmt.Sprintf("transfer.sh: Easy file sharing from the command line")))
  169. }
  170. func New() *Cmd {
  171. app := cli.NewApp()
  172. app.Name = "transfer.sh"
  173. app.Author = ""
  174. app.Usage = "transfer.sh"
  175. app.Description = `Easy file sharing from the command line`
  176. app.Flags = globalFlags
  177. app.CustomAppHelpTemplate = helpTemplate
  178. app.Commands = []cli.Command{
  179. {
  180. Name: "version",
  181. Action: VersionAction,
  182. },
  183. }
  184. app.Before = func(c *cli.Context) error {
  185. return nil
  186. }
  187. app.Action = func(c *cli.Context) {
  188. options := []server.OptionFn{}
  189. if v := c.String("listener"); v != "" {
  190. options = append(options, server.Listener(v))
  191. }
  192. if v := c.String("tls-listener"); v != "" {
  193. options = append(options, server.TLSListener(v))
  194. }
  195. if v := c.String("profile-listener"); v != "" {
  196. options = append(options, server.ProfileListener(v))
  197. }
  198. if v := c.String("web-path"); v != "" {
  199. options = append(options, server.WebPath(v))
  200. }
  201. if v := c.String("ga-key"); v != "" {
  202. options = append(options, server.GoogleAnalytics(v))
  203. }
  204. if v := c.String("uservoice-key"); v != "" {
  205. options = append(options, server.UserVoice(v))
  206. }
  207. if v := c.String("temp-path"); v != "" {
  208. options = append(options, server.TempPath(v))
  209. }
  210. if v := c.String("lets-encrypt-hosts"); v != "" {
  211. options = append(options, server.UseLetsEncrypt(strings.Split(v, ",")))
  212. }
  213. if v := c.String("virustotal-key"); v != "" {
  214. options = append(options, server.VirustotalKey(v))
  215. }
  216. if v := c.String("clamav-host"); v != "" {
  217. options = append(options, server.ClamavHost(v))
  218. }
  219. if v := c.Int("rate-limit"); v > 0 {
  220. options = append(options, server.RateLimit(v))
  221. }
  222. if cert := c.String("tls-cert-file"); cert == "" {
  223. } else if pk := c.String("tls-private-key"); pk == "" {
  224. } else {
  225. options = append(options, server.TLSConfig(cert, pk))
  226. }
  227. if c.Bool("profiler") {
  228. options = append(options, server.EnableProfiler())
  229. }
  230. if c.Bool("force-https") {
  231. options = append(options, server.ForceHTTPs())
  232. }
  233. if httpAuthUser := c.String("http-auth-user"); httpAuthUser == "" {
  234. } else if httpAuthPass := c.String("http-auth-pass"); httpAuthPass == "" {
  235. } else {
  236. options = append(options, server.HttpAuthCredentials(httpAuthUser, httpAuthPass))
  237. }
  238. switch provider := c.String("provider"); provider {
  239. case "s3":
  240. if accessKey := c.String("aws-access-key"); accessKey == "" {
  241. panic("access-key not set.")
  242. } else if secretKey := c.String("aws-secret-key"); secretKey == "" {
  243. panic("secret-key not set.")
  244. } else if bucket := c.String("bucket"); bucket == "" {
  245. panic("bucket not set.")
  246. } else if storage, err := server.NewS3Storage(accessKey, secretKey, bucket, c.String("s3-endpoint")); err != nil {
  247. panic(err)
  248. } else {
  249. options = append(options, server.UseStorage(storage))
  250. }
  251. case "gdrive":
  252. if clientJsonFilepath := c.String("gdrive-client-json-filepath"); clientJsonFilepath == "" {
  253. panic("client-json-filepath not set.")
  254. } else if localConfigPath := c.String("gdrive-local-config-path"); localConfigPath == "" {
  255. panic("local-config-path not set.")
  256. } else if basedir := c.String("basedir"); basedir == "" {
  257. panic("basedir not set.")
  258. } else if storage, err := server.NewGDriveStorage(clientJsonFilepath, localConfigPath, basedir); err != nil {
  259. panic(err)
  260. } else {
  261. options = append(options, server.UseStorage(storage))
  262. }
  263. case "local":
  264. if v := c.String("basedir"); v == "" {
  265. panic("basedir not set.")
  266. } else if storage, err := server.NewLocalStorage(v); err != nil {
  267. panic(err)
  268. } else {
  269. options = append(options, server.UseStorage(storage))
  270. }
  271. default:
  272. panic("Provider not set or invalid.")
  273. }
  274. srvr, err := server.New(
  275. options...,
  276. )
  277. if err != nil {
  278. fmt.Println(color.RedString("Error starting server: %s", err.Error()))
  279. return
  280. }
  281. srvr.Run()
  282. }
  283. return &Cmd{
  284. App: app,
  285. }
  286. }