lightbody/browsermob-proxy — pull request #111 by nite23 (2014-10-17 15:03:56+00:00)
Title: Proxy ports to be assigned only within a given range

This change is meant to prevent the following:
* running out of system resources by creating too many new proxies.
* accidentally creating proxies on ports that are reserved for other applications.

Previously, when proxy creation was requested by the client without specifying the port, bmproxy got the port number for the new proxy by incrementing the API port by one. (8081 by default). Each subsequent request incremented this number further.

This commit changes this behavior, so that the port can be assigned only within a certain range. Port numbers start at the configured minimum value and are incremented until the maximum value is reached, then the counter is wrapped around. If the next port in sequence is already occupied by a proxy, it is skipped. If all the ports within the range are already assigned to a proxy, error is returned.

New command line parameter:
* --proxyPortRange minPort-maxPort (default is 8081-8581)

API update:
* POST /
* if *port* parameter is specified, and a proxy already exists on this port, code 455 is returned.
* if *port* parameter is not specified, and all the ports withing the range are already assigned to a proxy instance, code 456 is returned
* if any other exception occurs, code 550 is returned, with the full stack trace in plain text.

Note: If the client opens a proxy but fails to close it by the DELETE command for any reason, the port range will eventually become filled with orphaned proxies. My next pull request will address this.