netty / netty

Netty project - an event-driven asynchronous network application framework
http://netty.io
Apache License 2.0
33.4k stars 15.91k forks source link

Netty Handler-proxy could use a factory #4125

Open CodingFabian opened 9 years ago

CodingFabian commented 9 years ago

Dear Netty people,

adding Proxy support to a netty client is really easy. However I needed this piece of code. Do you think that this is common and should be part of netty? Feel free to take it :-)

PS: the use case is clear, right? Many tools will expose proxy settings as configuration, so everybody needs to parse it then somehow

public class ProxyFactory {

  public static ProxyHandler make(String proxyType, String proxyHost, int proxyPort, String proxyUser,
      String proxyPassword) {
    InetSocketAddress proxyAddress = new InetSocketAddress(proxyHost, proxyPort);
    String type = proxyType.trim();
    if ("http".equalsIgnoreCase(type)) {
      if (StringUtils.isBlank(proxyUser)) {
        return new HttpProxyHandler(proxyAddress);
      } else {
        return new HttpProxyHandler(proxyAddress, proxyUser, proxyPassword);
      }
    } else if ("socks4".equalsIgnoreCase(type)) {
      if (StringUtils.isBlank(proxyUser)) {
        return new Socks4ProxyHandler(proxyAddress);
      } else {
        return new Socks4ProxyHandler(proxyAddress, proxyUser);
      }
    } else if ("socks5".equalsIgnoreCase(type)) {
      if (StringUtils.isBlank(proxyUser)) {
        return new Socks5ProxyHandler(proxyAddress);
      } else {
        return new Socks5ProxyHandler(proxyAddress, proxyUser, proxyPassword);
      }
    }
    throw new RuntimeException("Invalid proxy type " + type);
  }

}
trustin commented 9 years ago

Yeah, such a factory method would be useful for sure. Care to send a pull request?

normanmaurer commented 9 years ago

+1

Am 22.08.2015 um 03:23 schrieb Trustin Lee notifications@github.com:

Yeah, such a factory method would be useful for sure. Care to send a pull request?

— Reply to this email directly or view it on GitHub.

CodingFabian commented 9 years ago

ok, cool I will beautify and document this and send a PR

normanmaurer commented 9 years ago

<3

Am 22.08.2015 um 11:08 schrieb Fabian Lange notifications@github.com:

ok, cool I will beautify and document this and send a PR

— Reply to this email directly or view it on GitHub.