router.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "imuslab.com/zoraxy/mod/sshprox"
  7. )
  8. /*
  9. router.go
  10. This script holds the static resources router
  11. for the reverse proxy service
  12. */
  13. func AuthFsHandler(handler http.Handler) http.Handler {
  14. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  15. // Allow access to /script/*, /img/pubic/* and /login.html without authentication
  16. if strings.HasPrefix(r.URL.Path, "/script/") || strings.HasPrefix(r.URL.Path, "/img/public/") || r.URL.Path == "/login.html" || r.URL.Path == "/favicon.png" {
  17. handler.ServeHTTP(w, r)
  18. return
  19. }
  20. // check authentication
  21. if !authAgent.CheckAuth(r) {
  22. http.Redirect(w, r, "/login.html", http.StatusTemporaryRedirect)
  23. return
  24. }
  25. //For WebSSH Routing
  26. //Example URL Path: /web.ssh/{{instance_uuid}}/*
  27. if strings.HasPrefix(r.URL.Path, "/web.ssh/") {
  28. requestPath := r.URL.Path
  29. if !strings.HasSuffix(requestPath, "/") {
  30. requestPath = requestPath + "/"
  31. }
  32. parts := strings.Split(requestPath, "/")
  33. if len(parts) > 2 {
  34. //Extract the instance ID from the request path
  35. instanceUUID := parts[2]
  36. fmt.Println(instanceUUID)
  37. //Rewrite the url so the proxy knows how to serve stuffs
  38. r.URL, _ = sshprox.RewriteURL("/web.ssh/"+instanceUUID, r.RequestURI)
  39. webSshManager.HandleHttpByInstanceId(instanceUUID, w, r)
  40. } else {
  41. fmt.Println(parts)
  42. http.Error(w, "Invalid Usage", http.StatusInternalServerError)
  43. }
  44. return
  45. }
  46. //Authenticated
  47. handler.ServeHTTP(w, r)
  48. })
  49. }