All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.yawenok.apns.http2.impl.NettyHelper Maven / Gradle / Ivy

package io.github.yawenok.apns.http2.impl;

import io.github.yawenok.apns.http2.config.ProxyConfig;
import io.github.yawenok.apns.http2.config.proxy.HttpProxyConfig;
import io.github.yawenok.apns.http2.config.proxy.Socks4ProxyConfig;
import io.github.yawenok.apns.http2.config.proxy.Socks5ProxyConfig;
import io.github.yawenok.apns.http2.enums.proxy.ProxyMode;
import io.netty.handler.proxy.HttpProxyHandler;
import io.netty.handler.proxy.ProxyHandler;
import io.netty.handler.proxy.Socks4ProxyHandler;
import io.netty.handler.proxy.Socks5ProxyHandler;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

public class NettyHelper {
    public static ProxyHandler getProxyHandler(final ProxyConfig proxyConfig) {
        if (proxyConfig == null || proxyConfig.getProxyMode() == null || proxyConfig.getHost() == null) {
            throw new IllegalArgumentException("proxyConfig is error!");
        }

        SocketAddress address = new InetSocketAddress(proxyConfig.getHost(), proxyConfig.getPort());
        ProxyHandler handler = null;
        if (proxyConfig.getProxyMode() == ProxyMode.HTTP) {
            HttpProxyConfig httpProxyConfig = (HttpProxyConfig) proxyConfig;

            if (httpProxyConfig.getUsername() != null && httpProxyConfig.getPassword() != null) {
                handler = new HttpProxyHandler(address, httpProxyConfig.getUsername(), httpProxyConfig.getPassword());
            } else {
                handler = new HttpProxyHandler(address);
            }
        } else if (proxyConfig.getProxyMode() == ProxyMode.SOCKS4) {
            Socks4ProxyConfig socks4ProxyConfig = (Socks4ProxyConfig) proxyConfig;

            handler = new Socks4ProxyHandler(address, socks4ProxyConfig.getUsername());
        } else if (proxyConfig.getProxyMode() == ProxyMode.SOCKS5) {
            Socks5ProxyConfig socks5ProxyConfig = (Socks5ProxyConfig) proxyConfig;

            handler = new Socks5ProxyHandler(address, socks5ProxyConfig.getUsername(), socks5ProxyConfig.getPassword());
        }

        return handler;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy