router.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. parts := strings.Split(requestPath, "/")
  30. if !strings.HasSuffix(requestPath, "/") && len(parts) == 3 {
  31. http.Redirect(w, r, requestPath+"/", http.StatusTemporaryRedirect)
  32. return
  33. }
  34. if len(parts) > 2 {
  35. //Extract the instance ID from the request path
  36. instanceUUID := parts[2]
  37. fmt.Println(instanceUUID)
  38. //Rewrite the url so the proxy knows how to serve stuffs
  39. r.URL, _ = sshprox.RewriteURL("/web.ssh/"+instanceUUID, r.RequestURI)
  40. webSshManager.HandleHttpByInstanceId(instanceUUID, w, r)
  41. } else {
  42. fmt.Println(parts)
  43. http.Error(w, "Invalid Usage", http.StatusInternalServerError)
  44. }
  45. return
  46. }
  47. //Authenticated
  48. handler.ServeHTTP(w, r)
  49. })
  50. }