internal.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // Define the internal rc functions
  2. package rc
  3. import (
  4. "context"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "os/exec"
  9. "runtime"
  10. "strings"
  11. "time"
  12. "github.com/coreos/go-semver/semver"
  13. "github.com/rclone/rclone/fs"
  14. "github.com/rclone/rclone/fs/config/obscure"
  15. "github.com/rclone/rclone/lib/atexit"
  16. "github.com/rclone/rclone/lib/buildinfo"
  17. "github.com/rclone/rclone/lib/debug"
  18. )
  19. func init() {
  20. Add(Call{
  21. Path: "rc/noopauth",
  22. AuthRequired: true,
  23. Fn: rcNoop,
  24. Title: "Echo the input to the output parameters requiring auth",
  25. Help: `
  26. This echoes the input parameters to the output parameters for testing
  27. purposes. It can be used to check that rclone is still alive and to
  28. check that parameter passing is working properly.`,
  29. })
  30. Add(Call{
  31. Path: "rc/noop",
  32. Fn: rcNoop,
  33. Title: "Echo the input to the output parameters",
  34. Help: `
  35. This echoes the input parameters to the output parameters for testing
  36. purposes. It can be used to check that rclone is still alive and to
  37. check that parameter passing is working properly.`,
  38. })
  39. }
  40. // Echo the input to the output parameters
  41. func rcNoop(ctx context.Context, in Params) (out Params, err error) {
  42. return in, nil
  43. }
  44. func init() {
  45. Add(Call{
  46. Path: "rc/error",
  47. Fn: rcError,
  48. Title: "This returns an error",
  49. Help: `
  50. This returns an error with the input as part of its error string.
  51. Useful for testing error handling.`,
  52. })
  53. }
  54. // Return an error regardless
  55. func rcError(ctx context.Context, in Params) (out Params, err error) {
  56. return nil, fmt.Errorf("arbitrary error on input %+v", in)
  57. }
  58. func init() {
  59. Add(Call{
  60. Path: "rc/list",
  61. Fn: rcList,
  62. Title: "List all the registered remote control commands",
  63. Help: `
  64. This lists all the registered remote control commands as a JSON map in
  65. the commands response.`,
  66. })
  67. }
  68. // List the registered commands
  69. func rcList(ctx context.Context, in Params) (out Params, err error) {
  70. out = make(Params)
  71. out["commands"] = Calls.List()
  72. return out, nil
  73. }
  74. func init() {
  75. Add(Call{
  76. Path: "core/pid",
  77. Fn: rcPid,
  78. Title: "Return PID of current process",
  79. Help: `
  80. This returns PID of current process.
  81. Useful for stopping rclone process.`,
  82. })
  83. }
  84. // Return PID of current process
  85. func rcPid(ctx context.Context, in Params) (out Params, err error) {
  86. out = make(Params)
  87. out["pid"] = os.Getpid()
  88. return out, nil
  89. }
  90. func init() {
  91. Add(Call{
  92. Path: "core/memstats",
  93. Fn: rcMemStats,
  94. Title: "Returns the memory statistics",
  95. Help: `
  96. This returns the memory statistics of the running program. What the values mean
  97. are explained in the go docs: https://golang.org/pkg/runtime/#MemStats
  98. The most interesting values for most people are:
  99. - HeapAlloc - this is the amount of memory rclone is actually using
  100. - HeapSys - this is the amount of memory rclone has obtained from the OS
  101. - Sys - this is the total amount of memory requested from the OS
  102. - It is virtual memory so may include unused memory
  103. `,
  104. })
  105. }
  106. // Return the memory statistics
  107. func rcMemStats(ctx context.Context, in Params) (out Params, err error) {
  108. out = make(Params)
  109. var m runtime.MemStats
  110. runtime.ReadMemStats(&m)
  111. out["Alloc"] = m.Alloc
  112. out["TotalAlloc"] = m.TotalAlloc
  113. out["Sys"] = m.Sys
  114. out["Mallocs"] = m.Mallocs
  115. out["Frees"] = m.Frees
  116. out["HeapAlloc"] = m.HeapAlloc
  117. out["HeapSys"] = m.HeapSys
  118. out["HeapIdle"] = m.HeapIdle
  119. out["HeapInuse"] = m.HeapInuse
  120. out["HeapReleased"] = m.HeapReleased
  121. out["HeapObjects"] = m.HeapObjects
  122. out["StackInuse"] = m.StackInuse
  123. out["StackSys"] = m.StackSys
  124. out["MSpanInuse"] = m.MSpanInuse
  125. out["MSpanSys"] = m.MSpanSys
  126. out["MCacheInuse"] = m.MCacheInuse
  127. out["MCacheSys"] = m.MCacheSys
  128. out["BuckHashSys"] = m.BuckHashSys
  129. out["GCSys"] = m.GCSys
  130. out["OtherSys"] = m.OtherSys
  131. return out, nil
  132. }
  133. func init() {
  134. Add(Call{
  135. Path: "core/gc",
  136. Fn: rcGc,
  137. Title: "Runs a garbage collection.",
  138. Help: `
  139. This tells the go runtime to do a garbage collection run. It isn't
  140. necessary to call this normally, but it can be useful for debugging
  141. memory problems.
  142. `,
  143. })
  144. }
  145. // Do a garbage collection run
  146. func rcGc(ctx context.Context, in Params) (out Params, err error) {
  147. runtime.GC()
  148. return nil, nil
  149. }
  150. func init() {
  151. Add(Call{
  152. Path: "core/version",
  153. Fn: rcVersion,
  154. Title: "Shows the current version of rclone and the go runtime.",
  155. Help: `
  156. This shows the current version of go and the go runtime:
  157. - version - rclone version, e.g. "v1.53.0"
  158. - decomposed - version number as [major, minor, patch]
  159. - isGit - boolean - true if this was compiled from the git version
  160. - isBeta - boolean - true if this is a beta version
  161. - os - OS in use as according to Go
  162. - arch - cpu architecture in use according to Go
  163. - goVersion - version of Go runtime in use
  164. - linking - type of rclone executable (static or dynamic)
  165. - goTags - space separated build tags or "none"
  166. `,
  167. })
  168. }
  169. // Return version info
  170. func rcVersion(ctx context.Context, in Params) (out Params, err error) {
  171. version, err := semver.NewVersion(fs.Version[1:])
  172. if err != nil {
  173. return nil, err
  174. }
  175. linking, tagString := buildinfo.GetLinkingAndTags()
  176. out = Params{
  177. "version": fs.Version,
  178. "decomposed": version.Slice(),
  179. "isGit": strings.HasSuffix(fs.Version, "-DEV"),
  180. "isBeta": version.PreRelease != "",
  181. "os": runtime.GOOS,
  182. "arch": runtime.GOARCH,
  183. "goVersion": runtime.Version(),
  184. "linking": linking,
  185. "goTags": tagString,
  186. }
  187. return out, nil
  188. }
  189. func init() {
  190. Add(Call{
  191. Path: "core/obscure",
  192. Fn: rcObscure,
  193. Title: "Obscures a string passed in.",
  194. Help: `
  195. Pass a clear string and rclone will obscure it for the config file:
  196. - clear - string
  197. Returns:
  198. - obscured - string
  199. `,
  200. })
  201. }
  202. // Return obscured string
  203. func rcObscure(ctx context.Context, in Params) (out Params, err error) {
  204. clear, err := in.GetString("clear")
  205. if err != nil {
  206. return nil, err
  207. }
  208. obscured, err := obscure.Obscure(clear)
  209. if err != nil {
  210. return nil, err
  211. }
  212. out = Params{
  213. "obscured": obscured,
  214. }
  215. return out, nil
  216. }
  217. func init() {
  218. Add(Call{
  219. Path: "core/quit",
  220. Fn: rcQuit,
  221. Title: "Terminates the app.",
  222. Help: `
  223. (Optional) Pass an exit code to be used for terminating the app:
  224. - exitCode - int
  225. `,
  226. })
  227. }
  228. // Terminates app
  229. func rcQuit(ctx context.Context, in Params) (out Params, err error) {
  230. code, err := in.GetInt64("exitCode")
  231. if IsErrParamInvalid(err) {
  232. return nil, err
  233. }
  234. if IsErrParamNotFound(err) {
  235. code = 0
  236. }
  237. exitCode := int(code)
  238. go func(exitCode int) {
  239. time.Sleep(time.Millisecond * 1500)
  240. atexit.Run()
  241. os.Exit(exitCode)
  242. }(exitCode)
  243. return nil, nil
  244. }
  245. func init() {
  246. Add(Call{
  247. Path: "debug/set-mutex-profile-fraction",
  248. Fn: rcSetMutexProfileFraction,
  249. Title: "Set runtime.SetMutexProfileFraction for mutex profiling.",
  250. Help: `
  251. SetMutexProfileFraction controls the fraction of mutex contention
  252. events that are reported in the mutex profile. On average 1/rate
  253. events are reported. The previous rate is returned.
  254. To turn off profiling entirely, pass rate 0. To just read the current
  255. rate, pass rate < 0. (For n>1 the details of sampling may change.)
  256. Once this is set you can look use this to profile the mutex contention:
  257. go tool pprof http://localhost:5572/debug/pprof/mutex
  258. Parameters:
  259. - rate - int
  260. Results:
  261. - previousRate - int
  262. `,
  263. })
  264. }
  265. func rcSetMutexProfileFraction(ctx context.Context, in Params) (out Params, err error) {
  266. rate, err := in.GetInt64("rate")
  267. if err != nil {
  268. return nil, err
  269. }
  270. previousRate := runtime.SetMutexProfileFraction(int(rate))
  271. out = make(Params)
  272. out["previousRate"] = previousRate
  273. return out, nil
  274. }
  275. func init() {
  276. Add(Call{
  277. Path: "debug/set-block-profile-rate",
  278. Fn: rcSetBlockProfileRate,
  279. Title: "Set runtime.SetBlockProfileRate for blocking profiling.",
  280. Help: `
  281. SetBlockProfileRate controls the fraction of goroutine blocking events
  282. that are reported in the blocking profile. The profiler aims to sample
  283. an average of one blocking event per rate nanoseconds spent blocked.
  284. To include every blocking event in the profile, pass rate = 1. To turn
  285. off profiling entirely, pass rate <= 0.
  286. After calling this you can use this to see the blocking profile:
  287. go tool pprof http://localhost:5572/debug/pprof/block
  288. Parameters:
  289. - rate - int
  290. `,
  291. })
  292. }
  293. func rcSetBlockProfileRate(ctx context.Context, in Params) (out Params, err error) {
  294. rate, err := in.GetInt64("rate")
  295. if err != nil {
  296. return nil, err
  297. }
  298. runtime.SetBlockProfileRate(int(rate))
  299. return nil, nil
  300. }
  301. func init() {
  302. Add(Call{
  303. Path: "debug/set-soft-memory-limit",
  304. Fn: rcSetSoftMemoryLimit,
  305. Title: "Call runtime/debug.SetMemoryLimit for setting a soft memory limit for the runtime.",
  306. Help: `
  307. SetMemoryLimit provides the runtime with a soft memory limit.
  308. The runtime undertakes several processes to try to respect this memory limit, including
  309. adjustments to the frequency of garbage collections and returning memory to the underlying
  310. system more aggressively. This limit will be respected even if GOGC=off (or, if SetGCPercent(-1) is executed).
  311. The input limit is provided as bytes, and includes all memory mapped, managed, and not
  312. released by the Go runtime. Notably, it does not account for space used by the Go binary
  313. and memory external to Go, such as memory managed by the underlying system on behalf of
  314. the process, or memory managed by non-Go code inside the same process.
  315. Examples of excluded memory sources include: OS kernel memory held on behalf of the process,
  316. memory allocated by C code, and memory mapped by syscall.Mmap (because it is not managed by the Go runtime).
  317. A zero limit or a limit that's lower than the amount of memory used by the Go runtime may cause
  318. the garbage collector to run nearly continuously. However, the application may still make progress.
  319. The memory limit is always respected by the Go runtime, so to effectively disable this behavior,
  320. set the limit very high. math.MaxInt64 is the canonical value for disabling the limit, but values
  321. much greater than the available memory on the underlying system work just as well.
  322. See https://go.dev/doc/gc-guide for a detailed guide explaining the soft memory limit in more detail,
  323. as well as a variety of common use-cases and scenarios.
  324. SetMemoryLimit returns the previously set memory limit. A negative input does not adjust the limit,
  325. and allows for retrieval of the currently set memory limit.
  326. Parameters:
  327. - mem-limit - int
  328. `,
  329. })
  330. }
  331. func rcSetSoftMemoryLimit(ctx context.Context, in Params) (out Params, err error) {
  332. memLimit, err := in.GetInt64("mem-limit")
  333. if err != nil {
  334. return nil, err
  335. }
  336. oldMemLimit := debug.SetMemoryLimit(memLimit)
  337. out = Params{
  338. "existing-mem-limit": oldMemLimit,
  339. }
  340. return out, nil
  341. }
  342. func init() {
  343. Add(Call{
  344. Path: "debug/set-gc-percent",
  345. Fn: rcSetGCPercent,
  346. Title: "Call runtime/debug.SetGCPercent for setting the garbage collection target percentage.",
  347. Help: `
  348. SetGCPercent sets the garbage collection target percentage: a collection is triggered
  349. when the ratio of freshly allocated data to live data remaining after the previous collection
  350. reaches this percentage. SetGCPercent returns the previous setting. The initial setting is the
  351. value of the GOGC environment variable at startup, or 100 if the variable is not set.
  352. This setting may be effectively reduced in order to maintain a memory limit.
  353. A negative percentage effectively disables garbage collection, unless the memory limit is reached.
  354. See https://pkg.go.dev/runtime/debug#SetMemoryLimit for more details.
  355. Parameters:
  356. - gc-percent - int
  357. `,
  358. })
  359. }
  360. func rcSetGCPercent(ctx context.Context, in Params) (out Params, err error) {
  361. gcPercent, err := in.GetInt64("gc-percent")
  362. if err != nil {
  363. return nil, err
  364. }
  365. oldGCPercent := debug.SetGCPercent(int(gcPercent))
  366. out = Params{
  367. "existing-gc-percent": oldGCPercent,
  368. }
  369. return out, nil
  370. }
  371. func init() {
  372. Add(Call{
  373. Path: "core/command",
  374. AuthRequired: true,
  375. Fn: rcRunCommand,
  376. NeedsRequest: true,
  377. NeedsResponse: true,
  378. Title: "Run a rclone terminal command over rc.",
  379. Help: `This takes the following parameters:
  380. - command - a string with the command name.
  381. - arg - a list of arguments for the backend command.
  382. - opt - a map of string to string of options.
  383. - returnType - one of ("COMBINED_OUTPUT", "STREAM", "STREAM_ONLY_STDOUT", "STREAM_ONLY_STDERR").
  384. - Defaults to "COMBINED_OUTPUT" if not set.
  385. - The STREAM returnTypes will write the output to the body of the HTTP message.
  386. - The COMBINED_OUTPUT will write the output to the "result" parameter.
  387. Returns:
  388. - result - result from the backend command.
  389. - Only set when using returnType "COMBINED_OUTPUT".
  390. - error - set if rclone exits with an error code.
  391. - returnType - one of ("COMBINED_OUTPUT", "STREAM", "STREAM_ONLY_STDOUT", "STREAM_ONLY_STDERR").
  392. Example:
  393. rclone rc core/command command=ls -a mydrive:/ -o max-depth=1
  394. rclone rc core/command -a ls -a mydrive:/ -o max-depth=1
  395. Returns:
  396. ` + "```" + `
  397. {
  398. "error": false,
  399. "result": "<Raw command line output>"
  400. }
  401. OR
  402. {
  403. "error": true,
  404. "result": "<Raw command line output>"
  405. }
  406. ` + "```" + `
  407. `,
  408. })
  409. }
  410. // rcRunCommand runs an rclone command with the given args and flags
  411. func rcRunCommand(ctx context.Context, in Params) (out Params, err error) {
  412. command, err := in.GetString("command")
  413. if err != nil {
  414. command = ""
  415. }
  416. var opt = map[string]string{}
  417. err = in.GetStructMissingOK("opt", &opt)
  418. if err != nil {
  419. return nil, err
  420. }
  421. var arg = []string{}
  422. err = in.GetStructMissingOK("arg", &arg)
  423. if err != nil {
  424. return nil, err
  425. }
  426. returnType, err := in.GetString("returnType")
  427. if err != nil {
  428. returnType = "COMBINED_OUTPUT"
  429. }
  430. var httpResponse http.ResponseWriter
  431. httpResponse, err = in.GetHTTPResponseWriter()
  432. if err != nil {
  433. return nil, fmt.Errorf("response object is required\n" + err.Error())
  434. }
  435. var allArgs = []string{}
  436. if command != "" {
  437. // Add the command e.g.: ls to the args
  438. allArgs = append(allArgs, command)
  439. }
  440. // Add all from arg
  441. allArgs = append(allArgs, arg...)
  442. // Add flags to args for e.g. --max-depth 1 comes in as { max-depth 1 }.
  443. // Convert it to [ max-depth, 1 ] and append to args list
  444. for key, value := range opt {
  445. if len(key) == 1 {
  446. allArgs = append(allArgs, "-"+key)
  447. } else {
  448. allArgs = append(allArgs, "--"+key)
  449. }
  450. allArgs = append(allArgs, value)
  451. }
  452. // Get the path for the current executable which was used to run rclone.
  453. ex, err := os.Executable()
  454. if err != nil {
  455. return nil, err
  456. }
  457. cmd := exec.CommandContext(ctx, ex, allArgs...)
  458. if returnType == "COMBINED_OUTPUT" {
  459. // Run the command and get the output for error and stdout combined.
  460. out, err := cmd.CombinedOutput()
  461. if err != nil {
  462. return Params{
  463. "result": string(out),
  464. "error": true,
  465. }, nil
  466. }
  467. return Params{
  468. "result": string(out),
  469. "error": false,
  470. }, nil
  471. } else if returnType == "STREAM_ONLY_STDOUT" {
  472. cmd.Stdout = httpResponse
  473. } else if returnType == "STREAM_ONLY_STDERR" {
  474. cmd.Stderr = httpResponse
  475. } else if returnType == "STREAM" {
  476. cmd.Stdout = httpResponse
  477. cmd.Stderr = httpResponse
  478. } else {
  479. return nil, fmt.Errorf("unknown returnType %q", returnType)
  480. }
  481. err = cmd.Run()
  482. return nil, err
  483. }