How should my proxy intercept packets

245 views Asked by At

I have two web servers A and C. Because of rules, policies etc A cannot communicate directly with C' (Crequires requests to have special security headers andA` does not support this)

I want to create a "proxy" called B in Java, to facilitate communication between A and B but I having trouble with it conceptually.

When you create a proxy, don't you have to change the hard coded URLs on all your services to now travel through the proxy? And then I suppose you must pass your destination as a query parameter somehow, for example

http://proxy.com/?destination=mydestination.com

Is this how a proxy would work?

(I have found a few similar questions but they do not resolve the fundamental question I am having i.e. How packets reaches Destination throgh proxy servers?)

1

There are 1 answers

2
Shadi On

Don't write your own proxy from scratch that will be a redo, there are a plenty of proxy implementations out there

Spring framework

Standard Servlet

in case if you are intrested in how you can build up your own proxy , here is a simple example using spring framework (Servlet implementation is not that far from spring implementation)

@RestController
public class ProxyController {

/**
 * request handler for all the requests on the registered context intended to be proxied
 *
 * @param request
 * @return
 */
@RequestMapping("/**")
public String defaultGateway(HttpServletRequest request) {
    // edit the original request headers,body etc
    String originalPath =  request.getRequestURI();
    // then fire another HTTP request using the same URI
    return "ProxiedResult"; // return back the result you get from
 }
}

the example above is a start point on how to implements HTTP proxy or how it is working there are many things to cover which is droped off like security for instance