Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TODO to implement Port-Based Filtering in getProxies Route Function #47

Open
Mehul-Kumar-27 opened this issue Feb 27, 2024 · 1 comment

Comments

@Mehul-Kumar-27
Copy link

There is a TODO mentioned at riotpot/pkg/api/proxy.go in the getProxies route function which requires implementing functionality to filter proxies based on their ports.

I would like to work on this and plan to implement it in the following manner:

  • First I would be changing the path in the proxiesRoutes like below :
proxiesRoutes = []Route{
		// GET and POST proxies
		NewRoute("/getProxies/:port", "GET", getProxies),
		NewRoute("", "POST", createProxy),
	}
  • Then the getProxies function would look like this -:
func getProxies(ctx *gin.Context) {
	// Get the port parameter as a string
	portStr := ctx.Param("port")
	// Check if a port is provided
	if portStr != "all" {
		// Convert the port string to an integer
		port, err := strconv.Atoi(portStr)
		if err != nil {
			// Handle the error, for example, return a bad request response
			ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid port"})
			return
		}

		casted := []GetProxy{}

		// Iterate through the proxies registered
		for _, px := range proxy.Proxies.GetProxies() {
			// Serialize the proxy
			pr := NewProxy(px)
			// Append the proxy to the casted if the port matches
			if port == pr.Port {
				casted = append(casted, *pr)
			}
		}

		// Set the header and transform the struct to JSON format
		ctx.JSON(http.StatusOK, casted)
	} else {
		// No port provided, return all proxies
		allProxies := []GetProxy{}

		// Iterate through the proxies registered
		for _, px := range proxy.Proxies.GetProxies() {
			// Serialize the proxy
			pr := NewProxy(px)
			// Append the proxy to allProxies
			allProxies = append(allProxies, *pr)
		}

		// Set the header and transform the struct to JSON format
		ctx.JSON(http.StatusOK, allProxies)
	}
}

@RicYaben , sir, could you please provide your views on this?

@RicYaben
Copy link
Collaborator

RicYaben commented Feb 28, 2024

Hey there, please join the Honeynet Slack group. Please don't mention me in the comments, I may participate in this project but I do not own it.

For the first part, routes do use upper cases or verbs. These go in the requests, you either send a get request, post, or patch. I do not see how is this an improvement over the path proxies with a get command. I believe there is already a handler for this use-case?

Example of that same code a bit cleaner:

portStr := ctx.Param("port")
if port == '' {
 return proxy.Proxies.GetProxies()
}

// iterator here and the current logic
...
return ctx.JSON(ok, proxies)

As for the handler, the logic you mention does the same in both cases except the one below returns everything? the code does not look very good. If you want to add filters, I would expect the handler to actually handle different cases where we need to find a specific proxy based on other characteristics (e.g., assigned port, id, name, etc.).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants