Class DefaultHttpProxyServer
- java.lang.Object
-
- org.littleshoot.proxy.impl.DefaultHttpProxyServer
-
- All Implemented Interfaces:
HttpProxyServer
public class DefaultHttpProxyServer extends Object implements HttpProxyServer
Primary implementation of an
HttpProxyServer.DefaultHttpProxyServeris bootstrapped by callingbootstrap()orbootstrapFromFile(String), and then callingDefaultHttpProxyServer.DefaultHttpProxyServerBootstrap.start(). For example:DefaultHttpProxyServer server = DefaultHttpProxyServer .bootstrap() .withPort(8090) .start();
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classDefaultHttpProxyServer.DefaultHttpProxyServerBootstrap
-
Field Summary
Fields Modifier and Type Field Description private booleanacceptProxyProtocolprivate Collection<ActivityTracker>activityTrackersTrack all ActivityTrackers for tracking proxying activity.private ChannelGroupallChannelsKeep track of all channels created by this proxy server for later shutdown when the proxy is stopped.private booleanallowRequestsToOriginServerprivate booleanauthenticateSslClientsprivate InetSocketAddressboundAddressprivate ChainedProxyManagerchainProxyManagerprivate intconnectTimeoutprivate static StringFALLBACK_PROXY_ALIASThe proxy alias to use in the Via header if no explicit proxy alias is specified and the hostname of the local machine cannot be resolved.private HttpFiltersSourcefiltersSourceprivate GlobalTrafficShapingHandlerglobalTrafficShapingHandlerprivate intidleConnectionTimeoutprivate ThreadjvmShutdownHookJVM shutdown hook to shutdown this proxy server.private InetSocketAddresslocalAddressprivate static org.slf4j.LoggerLOGprivate static intMAX_CHUNK_SIZE_DEFAULTprivate static intMAX_HEADER_SIZE_DEFAULTprivate static intMAX_INITIAL_LINE_LENGTH_DEFAULTprivate intmaxChunkSizeprivate intmaxHeaderSizeprivate intmaxInitialLineLengthprivate MitmManagermitmManagerprivate StringproxyAliasThe alias or pseudonym for this proxy, used when adding the Via header.private ProxyAuthenticatorproxyAuthenticatorprivate InetSocketAddressrequestedAddressprivate booleansendProxyProtocolprivate ServerGroupserverGroupOurServerGroup.private HostResolverserverResolverprivate SslEngineSourcesslEngineSourceprivate AtomicBooleanstoppedprivate static longTRAFFIC_SHAPING_CHECK_INTERVAL_MSThe interval in ms at which the GlobalTrafficShapingHandler will run to compute and throttle the proxy-to-server bandwidth.private booleantransparentprivate TransportProtocoltransportProtocol
-
Constructor Summary
Constructors Modifier Constructor Description privateDefaultHttpProxyServer(ServerGroup serverGroup, TransportProtocol transportProtocol, InetSocketAddress requestedAddress, SslEngineSource sslEngineSource, boolean authenticateSslClients, ProxyAuthenticator proxyAuthenticator, ChainedProxyManager chainProxyManager, MitmManager mitmManager, HttpFiltersSource filtersSource, boolean transparent, int idleConnectionTimeout, Collection<ActivityTracker> activityTrackers, int connectTimeout, HostResolver serverResolver, long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond, InetSocketAddress localAddress, String proxyAlias, int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean allowRequestsToOriginServer, boolean acceptProxyProtocol, boolean sendProxyProtocol)Creates a new proxy server.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabort()Stops the server and all related clones immediately, without waiting for traffic to stop.static HttpProxyServerBootstrapbootstrap()Bootstrap a newDefaultHttpProxyServerstarting from scratch.static HttpProxyServerBootstrapbootstrapFromFile(String path)Bootstrap a newDefaultHttpProxyServerusing defaults from the given file.HttpProxyServerBootstrapclone()Clone the existing server, with a port 1 higher and everything else the same.protected voidcloseAllChannels(boolean graceful)Closes all channels opened by this proxy server.private GlobalTrafficShapingHandlercreateGlobalTrafficShapingHandler(TransportProtocol transportProtocol, long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond)Creates a new GlobalTrafficShapingHandler for this HttpProxyServer, using this proxy's proxyToServerEventLoop.private voiddoStart()protected voiddoStop(boolean graceful)Performs cleanup necessary to stop the server.protected Collection<ActivityTracker>getActivityTrackers()protected ChainedProxyManagergetChainProxyManager()intgetConnectTimeout()Returns the maximum time to wait, in milliseconds, to connect to a server.HttpFiltersSourcegetFiltersSource()intgetIdleConnectionTimeout()InetSocketAddressgetListenAddress()Return the address on which this proxy is listening.InetSocketAddressgetLocalAddress()intgetMaxChunkSize()intgetMaxHeaderSize()intgetMaxInitialLineLength()protected MitmManagergetMitmManager()StringgetProxyAlias()protected ProxyAuthenticatorgetProxyAuthenticator()protected EventLoopGroupgetProxyToServerWorkerFor(TransportProtocol transportProtocol)longgetReadThrottle()HostResolvergetServerResolver()protected SslEngineSourcegetSslEngineSource()longgetWriteThrottle()booleanisAcceptProxyProtocol()booleanisAllowRequestsToOriginServer()booleanisSendProxyProtocol()(package private) booleanisTransparent()protected voidregisterChannel(Channel channel)Register a newChannelwith this server, for later closing.voidsetConnectTimeout(int connectTimeoutMs)Sets the maximum time to wait, in milliseconds, to connect to a server.voidsetIdleConnectionTimeout(int idleConnectionTimeout)voidsetThrottle(long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond)Set the read/write throttle bandwidths (in bytes/second) for this proxy.private HttpProxyServerstart()voidstop()Stops the server and all related clones.protected voidunregisterChannel(Channel channel)
-
-
-
Field Detail
-
LOG
private static final org.slf4j.Logger LOG
-
TRAFFIC_SHAPING_CHECK_INTERVAL_MS
private static final long TRAFFIC_SHAPING_CHECK_INTERVAL_MS
The interval in ms at which the GlobalTrafficShapingHandler will run to compute and throttle the proxy-to-server bandwidth.- See Also:
- Constant Field Values
-
MAX_INITIAL_LINE_LENGTH_DEFAULT
private static final int MAX_INITIAL_LINE_LENGTH_DEFAULT
- See Also:
- Constant Field Values
-
MAX_HEADER_SIZE_DEFAULT
private static final int MAX_HEADER_SIZE_DEFAULT
- See Also:
- Constant Field Values
-
MAX_CHUNK_SIZE_DEFAULT
private static final int MAX_CHUNK_SIZE_DEFAULT
- See Also:
- Constant Field Values
-
FALLBACK_PROXY_ALIAS
private static final String FALLBACK_PROXY_ALIAS
The proxy alias to use in the Via header if no explicit proxy alias is specified and the hostname of the local machine cannot be resolved.- See Also:
- Constant Field Values
-
serverGroup
private final ServerGroup serverGroup
OurServerGroup. Multiple proxy servers can share the same ServerGroup in order to reuse threads and other such resources.
-
transportProtocol
private final TransportProtocol transportProtocol
-
requestedAddress
private final InetSocketAddress requestedAddress
-
localAddress
private final InetSocketAddress localAddress
-
boundAddress
private volatile InetSocketAddress boundAddress
-
sslEngineSource
private final SslEngineSource sslEngineSource
-
authenticateSslClients
private final boolean authenticateSslClients
-
proxyAuthenticator
private final ProxyAuthenticator proxyAuthenticator
-
chainProxyManager
private final ChainedProxyManager chainProxyManager
-
mitmManager
private final MitmManager mitmManager
-
filtersSource
private final HttpFiltersSource filtersSource
-
transparent
private final boolean transparent
-
connectTimeout
private volatile int connectTimeout
-
idleConnectionTimeout
private volatile int idleConnectionTimeout
-
serverResolver
private final HostResolver serverResolver
-
globalTrafficShapingHandler
private volatile GlobalTrafficShapingHandler globalTrafficShapingHandler
-
maxInitialLineLength
private final int maxInitialLineLength
-
maxHeaderSize
private final int maxHeaderSize
-
maxChunkSize
private final int maxChunkSize
-
allowRequestsToOriginServer
private final boolean allowRequestsToOriginServer
-
acceptProxyProtocol
private final boolean acceptProxyProtocol
-
sendProxyProtocol
private final boolean sendProxyProtocol
-
proxyAlias
private final String proxyAlias
The alias or pseudonym for this proxy, used when adding the Via header.
-
stopped
private final AtomicBoolean stopped
-
activityTrackers
private final Collection<ActivityTracker> activityTrackers
Track all ActivityTrackers for tracking proxying activity.
-
allChannels
private final ChannelGroup allChannels
Keep track of all channels created by this proxy server for later shutdown when the proxy is stopped.
-
jvmShutdownHook
private final Thread jvmShutdownHook
JVM shutdown hook to shutdown this proxy server. Declared as a class-level variable to allow removing the shutdown hook when the proxy server is stopped normally.
-
-
Constructor Detail
-
DefaultHttpProxyServer
private DefaultHttpProxyServer(ServerGroup serverGroup, TransportProtocol transportProtocol, InetSocketAddress requestedAddress, SslEngineSource sslEngineSource, boolean authenticateSslClients, ProxyAuthenticator proxyAuthenticator, ChainedProxyManager chainProxyManager, MitmManager mitmManager, HttpFiltersSource filtersSource, boolean transparent, int idleConnectionTimeout, Collection<ActivityTracker> activityTrackers, int connectTimeout, HostResolver serverResolver, long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond, InetSocketAddress localAddress, String proxyAlias, int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean allowRequestsToOriginServer, boolean acceptProxyProtocol, boolean sendProxyProtocol)
Creates a new proxy server.- Parameters:
serverGroup- our ServerGroup for shared thread pools and suchtransportProtocol- The protocol to use for data transportrequestedAddress- The address on which this server will listensslEngineSource- (optional) if specified, this Proxy will encrypt inbound connections from clients using anSSLEngineobtained from thisSslEngineSource.authenticateSslClients- Indicate whether or not to authenticate clients when using SSLproxyAuthenticator- (optional) If specified, requests to the proxy will be authenticated using HTTP BASIC authentication per the providedProxyAuthenticatorchainProxyManager- The proxy to send requests to if chaining proxies. Typicallynull.mitmManager- TheMitmManagerto use for man in the middle'ing CONNECT requestsfiltersSource- Source forHttpFilterstransparent- If true, this proxy will run as a transparent proxy. This will not modify the response, and will only modify the request to amend the URI if the target is the origin server (to comply with RFC 7230 section 5.3.1).idleConnectionTimeout- The timeout (in seconds) for auto-closing idle connections.activityTrackers- for tracking activity on this proxyconnectTimeout- number of milliseconds to wait to connect to the upstream serverserverResolver- theHostResolverto use for resolving server addressesreadThrottleBytesPerSecond- read throttle bandwidthwriteThrottleBytesPerSecond- write throttle bandwidthmaxInitialLineLength-maxHeaderSize-maxChunkSize-allowRequestsToOriginServer- when true, allow the proxy to handle requests that contain an origin-form URI, as defined in RFC 7230 5.3.1acceptProxyProtocol- when true, the proxy will accept a proxy protocol header from clientsendProxyProtocol- when true, the proxy will send a proxy protocol header to the server
-
-
Method Detail
-
bootstrap
public static HttpProxyServerBootstrap bootstrap()
Bootstrap a newDefaultHttpProxyServerstarting from scratch.
-
bootstrapFromFile
public static HttpProxyServerBootstrap bootstrapFromFile(String path)
Bootstrap a newDefaultHttpProxyServerusing defaults from the given file.
-
createGlobalTrafficShapingHandler
private GlobalTrafficShapingHandler createGlobalTrafficShapingHandler(TransportProtocol transportProtocol, long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond)
Creates a new GlobalTrafficShapingHandler for this HttpProxyServer, using this proxy's proxyToServerEventLoop.
-
isTransparent
boolean isTransparent()
-
getIdleConnectionTimeout
public int getIdleConnectionTimeout()
- Specified by:
getIdleConnectionTimeoutin interfaceHttpProxyServer
-
setIdleConnectionTimeout
public void setIdleConnectionTimeout(int idleConnectionTimeout)
- Specified by:
setIdleConnectionTimeoutin interfaceHttpProxyServer
-
getConnectTimeout
public int getConnectTimeout()
Description copied from interface:HttpProxyServerReturns the maximum time to wait, in milliseconds, to connect to a server.- Specified by:
getConnectTimeoutin interfaceHttpProxyServer
-
setConnectTimeout
public void setConnectTimeout(int connectTimeoutMs)
Description copied from interface:HttpProxyServerSets the maximum time to wait, in milliseconds, to connect to a server.- Specified by:
setConnectTimeoutin interfaceHttpProxyServer
-
getServerResolver
public HostResolver getServerResolver()
-
getLocalAddress
public InetSocketAddress getLocalAddress()
-
getListenAddress
public InetSocketAddress getListenAddress()
Description copied from interface:HttpProxyServerReturn the address on which this proxy is listening.- Specified by:
getListenAddressin interfaceHttpProxyServer
-
setThrottle
public void setThrottle(long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond)Description copied from interface:HttpProxyServerSet the read/write throttle bandwidths (in bytes/second) for this proxy.
- Specified by:
setThrottlein interfaceHttpProxyServer
-
getReadThrottle
public long getReadThrottle()
-
getWriteThrottle
public long getWriteThrottle()
-
getMaxInitialLineLength
public int getMaxInitialLineLength()
-
getMaxHeaderSize
public int getMaxHeaderSize()
-
getMaxChunkSize
public int getMaxChunkSize()
-
isAllowRequestsToOriginServer
public boolean isAllowRequestsToOriginServer()
-
isAcceptProxyProtocol
public boolean isAcceptProxyProtocol()
-
isSendProxyProtocol
public boolean isSendProxyProtocol()
-
clone
public HttpProxyServerBootstrap clone()
Description copied from interface:HttpProxyServerClone the existing server, with a port 1 higher and everything else the same. If the proxy was started with port 0 (JVM-assigned port), the cloned proxy will also use a JVM-assigned port.
The new server will share event loops with the original server. The event loops will use whatever name was given to the first server in the clone group. The server group will not terminate until the original server and all clones terminate.
- Specified by:
clonein interfaceHttpProxyServer- Overrides:
clonein classObject- Returns:
- a bootstrap that allows customizing and starting the cloned server
-
stop
public void stop()
Description copied from interface:HttpProxyServerStops the server and all related clones. Waits for traffic to stop before shutting down.- Specified by:
stopin interfaceHttpProxyServer
-
abort
public void abort()
Description copied from interface:HttpProxyServerStops the server and all related clones immediately, without waiting for traffic to stop.- Specified by:
abortin interfaceHttpProxyServer
-
doStop
protected void doStop(boolean graceful)
Performs cleanup necessary to stop the server. Closes all channels opened by the server and unregisters this server from the server group.- Parameters:
graceful- when true, waits for requests to terminate before stopping the server
-
registerChannel
protected void registerChannel(Channel channel)
Register a newChannelwith this server, for later closing.
-
unregisterChannel
protected void unregisterChannel(Channel channel)
-
closeAllChannels
protected void closeAllChannels(boolean graceful)
Closes all channels opened by this proxy server.- Parameters:
graceful- when false, attempts to shutdown all channels immediately and ignores any channel-closing exceptions
-
start
private HttpProxyServer start()
-
doStart
private void doStart()
-
getChainProxyManager
protected ChainedProxyManager getChainProxyManager()
-
getMitmManager
protected MitmManager getMitmManager()
-
getSslEngineSource
protected SslEngineSource getSslEngineSource()
-
getProxyAuthenticator
protected ProxyAuthenticator getProxyAuthenticator()
-
getFiltersSource
public HttpFiltersSource getFiltersSource()
-
getActivityTrackers
protected Collection<ActivityTracker> getActivityTrackers()
-
getProxyAlias
public String getProxyAlias()
-
getProxyToServerWorkerFor
protected EventLoopGroup getProxyToServerWorkerFor(TransportProtocol transportProtocol)
-
-