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.
 
 
 

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