|
| 1 | +package zoraxy_plugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +type PathRouter struct { |
| 11 | + enableDebugPrint bool |
| 12 | + pathHandlers map[string]http.Handler |
| 13 | + defaultHandler http.Handler |
| 14 | +} |
| 15 | + |
| 16 | +// NewPathRouter creates a new PathRouter |
| 17 | +func NewPathRouter() *PathRouter { |
| 18 | + return &PathRouter{ |
| 19 | + enableDebugPrint: false, |
| 20 | + pathHandlers: make(map[string]http.Handler), |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// RegisterPathHandler registers a handler for a path |
| 25 | +func (p *PathRouter) RegisterPathHandler(path string, handler http.Handler) { |
| 26 | + path = strings.TrimSuffix(path, "/") |
| 27 | + p.pathHandlers[path] = handler |
| 28 | +} |
| 29 | + |
| 30 | +// RemovePathHandler removes a handler for a path |
| 31 | +func (p *PathRouter) RemovePathHandler(path string) { |
| 32 | + delete(p.pathHandlers, path) |
| 33 | +} |
| 34 | + |
| 35 | +// SetDefaultHandler sets the default handler for the router |
| 36 | +// This handler will be called if no path handler is found |
| 37 | +func (p *PathRouter) SetDefaultHandler(handler http.Handler) { |
| 38 | + p.defaultHandler = handler |
| 39 | +} |
| 40 | + |
| 41 | +// SetDebugPrintMode sets the debug print mode |
| 42 | +func (p *PathRouter) SetDebugPrintMode(enable bool) { |
| 43 | + p.enableDebugPrint = enable |
| 44 | +} |
| 45 | + |
| 46 | +func (p *PathRouter) RegisterHandle(capture_ingress string, mux *http.ServeMux) { |
| 47 | + if !strings.HasSuffix(capture_ingress, "/") { |
| 48 | + capture_ingress = capture_ingress + "/" |
| 49 | + } |
| 50 | + mux.Handle(capture_ingress, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 51 | + p.ServeHTTP(w, r) |
| 52 | + })) |
| 53 | +} |
| 54 | + |
| 55 | +func (p *PathRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 56 | + capturePath := r.Header.Get("X-Zoraxy-Capture") |
| 57 | + if capturePath != "" { |
| 58 | + if p.enableDebugPrint { |
| 59 | + fmt.Printf("Using capture path: %s\n", capturePath) |
| 60 | + } |
| 61 | + originalURI := r.Header.Get("X-Zoraxy-Uri") |
| 62 | + r.URL.Path = originalURI |
| 63 | + if handler, ok := p.pathHandlers[capturePath]; ok { |
| 64 | + handler.ServeHTTP(w, r) |
| 65 | + return |
| 66 | + } |
| 67 | + } |
| 68 | + p.defaultHandler.ServeHTTP(w, r) |
| 69 | +} |
| 70 | + |
| 71 | +func (p *PathRouter) PrintRequestDebugMessage(r *http.Request) { |
| 72 | + if p.enableDebugPrint { |
| 73 | + fmt.Printf("Capture Request with path: %s \n\n**Request Headers** \n\n", r.URL.Path) |
| 74 | + keys := make([]string, 0, len(r.Header)) |
| 75 | + for key := range r.Header { |
| 76 | + keys = append(keys, key) |
| 77 | + } |
| 78 | + sort.Strings(keys) |
| 79 | + for _, key := range keys { |
| 80 | + for _, value := range r.Header[key] { |
| 81 | + fmt.Printf("%s: %s\n", key, value) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + fmt.Printf("\n\n**Request Details**\n\n") |
| 86 | + fmt.Printf("Method: %s\n", r.Method) |
| 87 | + fmt.Printf("URL: %s\n", r.URL.String()) |
| 88 | + fmt.Printf("Proto: %s\n", r.Proto) |
| 89 | + fmt.Printf("Host: %s\n", r.Host) |
| 90 | + fmt.Printf("RemoteAddr: %s\n", r.RemoteAddr) |
| 91 | + fmt.Printf("RequestURI: %s\n", r.RequestURI) |
| 92 | + fmt.Printf("ContentLength: %d\n", r.ContentLength) |
| 93 | + fmt.Printf("TransferEncoding: %v\n", r.TransferEncoding) |
| 94 | + fmt.Printf("Close: %v\n", r.Close) |
| 95 | + fmt.Printf("Form: %v\n", r.Form) |
| 96 | + fmt.Printf("PostForm: %v\n", r.PostForm) |
| 97 | + fmt.Printf("MultipartForm: %v\n", r.MultipartForm) |
| 98 | + fmt.Printf("Trailer: %v\n", r.Trailer) |
| 99 | + fmt.Printf("RemoteAddr: %s\n", r.RemoteAddr) |
| 100 | + fmt.Printf("RequestURI: %s\n", r.RequestURI) |
| 101 | + |
| 102 | + } |
| 103 | +} |
0 commit comments