2828import org .apache .commons .lang .StringUtils ;
2929import org .apache .commons .logging .Log ;
3030import org .apache .commons .logging .LogFactory ;
31+ import org .apache .http .HttpException ;
3132import org .apache .http .HttpHost ;
33+ import org .apache .http .HttpRequest ;
3234import org .apache .http .auth .AuthScope ;
3335import org .apache .http .auth .UsernamePasswordCredentials ;
3436import org .apache .http .client .CredentialsProvider ;
4648import org .apache .http .impl .client .HttpClients ;
4749import org .apache .http .impl .conn .BasicHttpClientConnectionManager ;
4850import org .apache .http .impl .conn .DefaultProxyRoutePlanner ;
51+ import org .apache .http .protocol .HttpContext ;
4952import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
5053import org .apache .synapse .MessageContext ;
5154import org .apache .synapse .SynapseConstants ;
@@ -343,6 +346,8 @@ private static ProxyConfigs getProxyConfigs(OMElement grantTypeOMElement) {
343346 proxyConfigs .setProxyProtocol (getChildValue (proxyConfigsOM , AuthConstants .OAUTH_PROXY_PROTOCOL ));
344347 proxyConfigs .setProxyUsername (getChildValue (proxyConfigsOM , AuthConstants .PROXY_USERNAME ));
345348 proxyConfigs .setProxyPassword (getChildValue (proxyConfigsOM , AuthConstants .PROXY_PASSWORD ));
349+ proxyConfigs .setNonProxyHosts (getChildValue (proxyConfigsOM , AuthConstants .PROXY_NON_PROXY_HOSTS ));
350+ proxyConfigs .setTargetProxyHosts (getChildValue (proxyConfigsOM , AuthConstants .PROXY_TARGET_PROXY_HOSTS ));
346351 } else {
347352 Properties synapseProperties = SynapsePropertiesLoader .loadSynapseProperties ();
348353 if (Boolean .parseBoolean (getChildValue (grantTypeOMElement , AuthConstants .USE_GLOBAL_PROXY_CONFIGS ))
@@ -352,6 +357,8 @@ private static ProxyConfigs getProxyConfigs(OMElement grantTypeOMElement) {
352357 proxyConfigs .setProxyHost (synapseProperties .getProperty (AuthConstants .OAUTH_GLOBAL_PROXY_HOST ));
353358 proxyConfigs .setProxyPort (synapseProperties .getProperty (AuthConstants .OAUTH_GLOBAL_PROXY_PORT ));
354359 proxyConfigs .setProxyProtocol (synapseProperties .getProperty (AuthConstants .OAUTH_GLOBAL_PROXY_PROTOCOL ));
360+ proxyConfigs .setNonProxyHosts (synapseProperties .getProperty (AuthConstants .OAUTH_GLOBAL_PROXY_NON_PROXY_HOSTS ));
361+ proxyConfigs .setTargetProxyHosts (synapseProperties .getProperty (AuthConstants .OAUTH_GLOBAL_PROXY_TARGET_PROXY_HOSTS ));
355362 if (StringUtils .isNotBlank (synapseProperties .getProperty (AuthConstants .OAUTH_GLOBAL_PROXY_USERNAME )) &&
356363 StringUtils .isNotBlank (synapseProperties .getProperty (AuthConstants .OAUTH_GLOBAL_PROXY_PASSWORD ))) {
357364 proxyConfigs .setProxyUsername (synapseProperties .getProperty (AuthConstants .OAUTH_GLOBAL_PROXY_USERNAME ));
@@ -812,6 +819,18 @@ public static OMElement createOMProxyConfigs(OMFactory omFactory, ProxyConfigs p
812819 proxyConfigs .getProxyProtocol ());
813820 proxyConfigsOM .addChild (proxyProtocolOM );
814821
822+ if (StringUtils .isNotBlank (proxyConfigs .getNonProxyHosts ())) {
823+ OMElement nonProxyHostsOM = OAuthUtils .createOMElementWithValue (omFactory ,
824+ AuthConstants .PROXY_NON_PROXY_HOSTS , proxyConfigs .getNonProxyHosts ());
825+ proxyConfigsOM .addChild (nonProxyHostsOM );
826+ }
827+
828+ if (StringUtils .isNotBlank (proxyConfigs .getTargetProxyHosts ())) {
829+ OMElement targetProxyHostsOM = OAuthUtils .createOMElementWithValue (omFactory ,
830+ AuthConstants .PROXY_TARGET_PROXY_HOSTS , proxyConfigs .getTargetProxyHosts ());
831+ proxyConfigsOM .addChild (targetProxyHostsOM );
832+ }
833+
815834 return proxyConfigsOM ;
816835 }
817836
@@ -888,7 +907,8 @@ public boolean verify(String urlHostName, SSLSession session) {
888907 HttpHost host = new HttpHost (proxyConfigs .getProxyHost (), Integer .parseInt (proxyConfigs .getProxyPort ()),
889908 proxyConfigs .getProxyProtocol ());
890909
891- DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner (host );
910+ NonProxyAwareProxyRoutePlanner routePlanner = new NonProxyAwareProxyRoutePlanner (host ,
911+ proxyConfigs .getNonProxyHosts (), proxyConfigs .getTargetProxyHosts ());
892912
893913 clientBuilder = HttpClients .custom ().setConnectionManager (pool ).setRoutePlanner (routePlanner )
894914 .setDefaultRequestConfig (config ).setSSLSocketFactory (sslConnectionFactory );
@@ -948,4 +968,84 @@ public static String resolveProxyPassword(ProxyConfigs proxyConfigs, MessageCont
948968 return OAuthUtils .resolveExpression (proxyConfigs .getProxyPassword (), messageContext );
949969 }
950970 }
971+
972+ private static class NonProxyAwareProxyRoutePlanner extends DefaultProxyRoutePlanner {
973+
974+ private final String [] nonProxyPatterns ;
975+ private final String [] targetProxyPatterns ;
976+
977+ NonProxyAwareProxyRoutePlanner (HttpHost proxy , String nonProxyHosts , String targetProxyHosts ) {
978+ super (proxy );
979+ if (org .apache .commons .lang3 .StringUtils .isNotBlank (nonProxyHosts )) {
980+ this .nonProxyPatterns = nonProxyHosts .split ("\\ |" );
981+ } else {
982+ this .nonProxyPatterns = new String [0 ];
983+ }
984+ if (org .apache .commons .lang3 .StringUtils .isNotBlank (targetProxyHosts )) {
985+ this .targetProxyPatterns = targetProxyHosts .split ("\\ |" );
986+ } else {
987+ this .targetProxyPatterns = new String [0 ];
988+ }
989+ }
990+
991+ @ Override
992+ protected HttpHost determineProxy (HttpHost target , HttpRequest request , HttpContext context )
993+ throws HttpException {
994+
995+ String targetHost = target .getHostName ();
996+
997+ for (String pattern : nonProxyPatterns ) {
998+ String trimmedPattern = pattern .trim ();
999+ if (!trimmedPattern .isEmpty ()) {
1000+ if ("*" .equals (trimmedPattern )) {
1001+ if (log .isDebugEnabled ()) {
1002+ log .debug ("Host [" + targetHost + "] matches wildcard non-proxy pattern [*]. "
1003+ + "Bypassing proxy." );
1004+ }
1005+ return null ;
1006+ }
1007+ if (matchesPattern (targetHost , trimmedPattern )) {
1008+ if (log .isDebugEnabled ()) {
1009+ log .debug ("Host [" + targetHost + "] matches non-proxy pattern [" + trimmedPattern
1010+ + "]. Bypassing proxy." );
1011+ }
1012+ return null ;
1013+ }
1014+ }
1015+ }
1016+
1017+ if (targetProxyPatterns .length > 0 ) {
1018+ for (String pattern : targetProxyPatterns ) {
1019+ String trimmedPattern = pattern .trim ();
1020+ if (!trimmedPattern .isEmpty ()) {
1021+ if ("*" .equals (trimmedPattern )) {
1022+ if (log .isDebugEnabled ()) {
1023+ log .debug ("Host [" + targetHost + "] matches wildcard target-proxy pattern [*]. "
1024+ + "Using proxy." );
1025+ }
1026+ return super .determineProxy (target , request , context );
1027+ }
1028+ if (matchesPattern (targetHost , trimmedPattern )) {
1029+ if (log .isDebugEnabled ()) {
1030+ log .debug ("Host [" + targetHost + "] matches target-proxy pattern [" + trimmedPattern
1031+ + "]. Using proxy." );
1032+ }
1033+ return super .determineProxy (target , request , context );
1034+ }
1035+ }
1036+ }
1037+ if (log .isDebugEnabled ()) {
1038+ log .debug ("Host [" + targetHost + "] does not match any target-proxy pattern. Bypassing proxy." );
1039+ }
1040+ return null ;
1041+ }
1042+
1043+ return super .determineProxy (target , request , context );
1044+ }
1045+
1046+ private boolean matchesPattern (String hostname , String pattern ) {
1047+ String regex = pattern .replace ("." , "\\ ." ).replace ("*" , ".*" );
1048+ return hostname .matches (regex );
1049+ }
1050+ }
9511051}
0 commit comments