Skip to content

Adding support for specifying a Proxy to use for the connection#38

Open
davydotcom wants to merge 1 commit into
oVirt:masterfrom
davydotcom:patch-proxy
Open

Adding support for specifying a Proxy to use for the connection#38
davydotcom wants to merge 1 commit into
oVirt:masterfrom
davydotcom:patch-proxy

Conversation

@davydotcom

Copy link
Copy Markdown

This library does not allow you to specify a Proxy for the connection. This adds support for passing Proxy Information for both SOCKS or HTTP proxies

Signed-off-by: David Estes <davydotcom@gmail.com>

@peter-boden peter-boden left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review, but it would be great if you could look into my remarks 🙈

SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[]{noCaTrustManager}, null);
sslsf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE);
sslsf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why no socks handling for secure ssl, i.e. ssl with a valid certificate?

Also, this seems like duplicate code?

Why not do something like:

  static class ProxyConnectionSocketFactory implements ConnectionSocketFactory {                                                          
      private final ConnectionSocketFactory delegate;                                                                                     
      private final ProxyInfo proxyInfo;                                                                                                  
                                                                                                                                          
      ProxyConnectionSocketFactory(ConnectionSocketFactory delegate, ProxyInfo proxyInfo) {                                               
          this.delegate = delegate;                                                                                                       
          this.proxyInfo = proxyInfo;                                                                                                     
      }                                                                                                                                   
                                                                                                                                          
      /**                                                                                                                                 
       * Wraps the given factory with SOCKS proxy support if needed.                                                                      
       * Returns the original factory unchanged if proxy is null or not SOCKS.                                                            
       */                                                                                                                                 
      static ConnectionSocketFactory wrapIfNeeded(ConnectionSocketFactory factory, ProxyInfo proxyInfo) {                                 
          if (proxyInfo != null                                                                                                           
                  && proxyInfo.getProxyType() == ProxyInfo.ProxyType.SOCKS                                                                
                  && proxyInfo.getHost() != null                                                                                          
                  && proxyInfo.getProxyPort() != null) {                                                                                  
              return new ProxyConnectionSocketFactory(factory, proxyInfo);                                                                
          }                                                                                                                               
          return factory;                                                                                                                 
      }                                                                                                                                   
                                                                                                                                          
      // ... delegate methods ...                                                                                                         
  }                                                                                                                                       

Use it like this:

  private Registry createConnectionSocketFactoryRegistry() {                                                                              
      ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();                                                  
      ConnectionSocketFactory sslsf = createSslSocketFactory();                                                                           
                                                                                                                                          
      return RegistryBuilder.<ConnectionSocketFactory>create()                                                                            
          .register(HTTP_PROTOCOL, ProxyConnectionSocketFactory.wrapIfNeeded(plainsf, proxyInfo))                                         
          .register(HTTPS_PROTOCOL, ProxyConnectionSocketFactory.wrapIfNeeded(sslsf, proxyInfo))                                          
          .build();                                                                                                                       
  }                                                                                                                                       

package org.ovirt.engine.sdk4;

public class ProxyInfo {
protected String host;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are some fields protected?

protected String user;
protected String password;
private String proxyDomain;
private String proxyWorkstation;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proxyWorkstation isn't set in the constructor? Any reason for that?

}


//create an enum for proxy type

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless comment.

protected String user;
protected String password;
protected String token;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the newline?

private String ssoTokenName = null;
private String ssoUrl = null;
private String ssoRevokeUrl = null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the newline?

if(proxyInfo.getHost() != null && proxyInfo.getProxyPort() != null) {
clientBuilder.setProxy(new HttpHost(proxyInfo.getHost(), proxyInfo.getProxyPort()));
if(proxyInfo.getUser() != null) { //authenticated proxy
NTCredentials ntCreds = new NTCredentials(proxyInfo.getUser(), proxyInfo.getPassword(), proxyInfo.getProxyWorkstation(), proxyInfo.getProxyDomain());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NTCredentials feels oddly specific? I would say the majority of proxies doesn't use windows domain auth?

private ProxyType proxyType;


public ProxyInfo(String host, Integer proxyPort, String user, String password, String proxyDomain, ProxyType proxyType) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add some validation to the constructor to check for mandatory fields, this would make it more user friendly.

clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
}
}
} else if(proxyInfo.getProxyType() == ProxyInfo.ProxyType.SOCKS) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remove this if, just having this branch for that comment is kind of useless.

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

Successfully merging this pull request may close these issues.

2 participants