appproxy.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package main
  2. /*
  3. appproxy.go
  4. Top-level wiring for container apps: web servers (usually Docker
  5. containers) that an administrator publishes through the built-in reverse
  6. proxy, so they are reachable on the same port as ArozOS (and therefore
  7. through Cloudflare / a single forwarded port) and can be pinned to the
  8. desktop by every user.
  9. - mrouter calls appProxyManager.HandleRequest first (see main.router.go)
  10. - admins publish / edit apps in the Container Apps web app, or from the
  11. "Publish" action of Docker Manager
  12. - users get a simplified list of the apps they may open and pin them as
  13. "app" desktop shortcuts, which always resolve through /app/<slug>/
  14. */
  15. import (
  16. "encoding/json"
  17. "net/http"
  18. "net/url"
  19. "strconv"
  20. "imuslab.com/arozos/mod/appproxy"
  21. "imuslab.com/arozos/mod/docker"
  22. module "imuslab.com/arozos/mod/modules"
  23. prout "imuslab.com/arozos/mod/prouter"
  24. "imuslab.com/arozos/mod/utils"
  25. )
  26. var appProxyManager *appproxy.Manager
  27. // appProxyIdentity converts an ArozOS user into the proxy's identity
  28. func appProxyIdentity(username string, groups []string, isAdmin bool) *appproxy.Identity {
  29. return &appproxy.Identity{Username: username, Groups: groups, IsAdmin: isAdmin}
  30. }
  31. func AppProxyInit() {
  32. manager, err := appproxy.NewManager(appproxy.Options{
  33. Store: sysdb,
  34. SelfPort: *listen_port,
  35. ResolveUser: func(w http.ResponseWriter, r *http.Request) *appproxy.Identity {
  36. if !authAgent.CheckAuth(r) {
  37. return nil
  38. }
  39. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  40. if err != nil {
  41. return nil
  42. }
  43. //Using an app counts as activity on the desktop session
  44. authAgent.UpdateSessionExpireTime(w, r)
  45. return appProxyIdentity(userinfo.Username, userinfo.GetUserPermissionGroupNames(), userinfo.IsAdmin())
  46. },
  47. LookupUser: func(username string) *appproxy.Identity {
  48. if !authAgent.UserExists(username) {
  49. return nil
  50. }
  51. userinfo, err := userHandler.GetUserInfoFromUsername(username)
  52. if err != nil {
  53. return nil
  54. }
  55. return appProxyIdentity(userinfo.Username, userinfo.GetUserPermissionGroupNames(), userinfo.IsAdmin())
  56. },
  57. LoginURL: func(returnTo string) string {
  58. return "/login.html?redirect=" + url.QueryEscape(returnTo)
  59. },
  60. StripCookies: []string{"ao_auth", "ao_acc"},
  61. Log: func(message string, err error) {
  62. systemWideLogger.PrintAndLog("AppProxy", message, err)
  63. },
  64. ContainersAvailable: func() bool {
  65. return dockerManager != nil
  66. },
  67. })
  68. if err != nil {
  69. systemWideLogger.PrintAndLog("AppProxy", "Unable to start the container app proxy", err)
  70. return
  71. }
  72. appProxyManager = manager
  73. //Any logged in user: the simplified app list and shortcut launch info
  74. userRouter := prout.NewModuleRouter(prout.RouterOption{
  75. ModuleName: "",
  76. UserHandler: userHandler,
  77. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  78. utils.SendErrorResponse(w, "Permission Denied")
  79. },
  80. })
  81. userRouter.HandleFunc("/system/appproxy/apps", manager.HandleUserApps)
  82. userRouter.HandleFunc("/system/appproxy/launch", manager.HandleLaunch)
  83. //Administrators: publish, edit, remove and analyse apps
  84. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  85. ModuleName: "",
  86. AdminOnly: true,
  87. UserHandler: userHandler,
  88. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  89. utils.SendErrorResponse(w, "Permission Denied")
  90. },
  91. })
  92. adminRouter.HandleFunc("/system/appproxy/admin/list", manager.HandleAdminList)
  93. adminRouter.HandleFunc("/system/appproxy/admin/save", manager.HandleAdminSave)
  94. adminRouter.HandleFunc("/system/appproxy/admin/delete", manager.HandleAdminDelete)
  95. adminRouter.HandleFunc("/system/appproxy/admin/probe", manager.HandleAdminProbe)
  96. adminRouter.HandleFunc("/system/appproxy/admin/containers", handleAppProxyContainers)
  97. //Every user opens and pins apps here; administrators also publish from it,
  98. //including web servers that are not Docker containers
  99. moduleHandler.RegisterModule(module.ModuleInfo{
  100. Name: "Container Apps",
  101. Desc: "Open web apps running in containers and pin them to the desktop",
  102. Group: "Utilities",
  103. IconPath: "ContainerApps/img/icon.svg",
  104. Version: "1.0",
  105. StartDir: "ContainerApps/index.html",
  106. SupportFW: true,
  107. LaunchFWDir: "ContainerApps/index.html",
  108. InitFWSize: []int{900, 600},
  109. })
  110. systemWideLogger.PrintAndLog("AppProxy", "Container app proxy ready with "+strconv.Itoa(manager.Count())+" published app(s)", nil)
  111. }
  112. // appProxyContainer is a Docker container offered for publishing
  113. type appProxyContainer struct {
  114. ID string
  115. Name string
  116. Image string
  117. State string
  118. Ports []docker.PublishedPort
  119. Targets []string
  120. Published []string //Slugs of apps already pointing at this container
  121. }
  122. // handleAppProxyContainers lists Docker containers with their published web ports
  123. func handleAppProxyContainers(w http.ResponseWriter, r *http.Request) {
  124. if dockerManager == nil {
  125. utils.SendJSONResponse(w, "[]")
  126. return
  127. }
  128. containers, err := dockerManager.ListContainers()
  129. if err != nil {
  130. utils.SendErrorResponse(w, err.Error())
  131. return
  132. }
  133. published := map[string][]string{}
  134. for _, ep := range appProxyManager.List() {
  135. if ep.Container != "" {
  136. published[ep.Container] = append(published[ep.Container], ep.Slug)
  137. }
  138. }
  139. results := []appProxyContainer{}
  140. for _, c := range containers {
  141. ports := docker.ParsePublishedPorts(c.Ports)
  142. targets := []string{}
  143. for _, p := range ports {
  144. targets = append(targets, p.Target())
  145. }
  146. results = append(results, appProxyContainer{
  147. ID: c.ID,
  148. Name: c.Names,
  149. Image: c.Image,
  150. State: c.State,
  151. Ports: ports,
  152. Targets: targets,
  153. Published: published[c.Names],
  154. })
  155. }
  156. js, _ := json.Marshal(results)
  157. utils.SendJSONResponse(w, string(js))
  158. }