router.go 1.4 KB

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