private void addHeaders(StringBuilder builder) { // For each additional header. for (Map.Entry<String, List<String>> header : mSettings.getHeaders().entrySet()) { // Header name. String name = header.getKey(); // For each header value. for (String value : header.getValue()) { if (value == null) { value = ""; } builder.append(name).append(": ").append(value).append(RN); } } }
/** * Set credentials for authentication at the proxy server. * This method is an alias of {@link #setId(String) setId}{@code * (id).}{@link #setPassword(String) setPassword}{@code * (password)}. * * @param id * The ID. * * @param password * The password. * * @return * {@code this} object. */ public ProxySettings setCredentials(String id, String password) { return setId(id).setPassword(password); }
/** * Set the proxy server by a URI. See the description of * {@link #setServer(URI)} about how the parameters are updated. * * @param uri * The URI of the proxy server. If {@code null} is given, * none of the parameters are updated. * * @return * {@code this} object. * * @throws IllegalArgumentException * Failed to convert the given string to a {@link URI} instance. */ public ProxySettings setServer(String uri) { if (uri == null) { return this; } return setServer(URI.create(uri)); }
private void addProxyAuthorization(StringBuilder builder) { String id = mSettings.getId(); if (id == null || id.length() == 0) { return; } String password = mSettings.getPassword(); if (password == null) { password = ""; } // {id}:{password} String credentials = String.format("%s:%s", id, password); // The current implementation always uses Basic Authentication. builder .append("Proxy-Authorization: Basic ") .append(Base64.encode(credentials)) .append(RN); }
private SocketConnector createProxiedRawSocket( String host, int port, boolean secure, int timeout) throws IOException { // Determine the port number of the proxy server. // Especially, if getPort() returns -1, the value // is converted to 80 or 443. int proxyPort = determinePort(mProxySettings.getPort(), mProxySettings.isSecure()); // Select a socket factory. SocketFactory socketFactory = mProxySettings.selectSocketFactory(); // Let the socket factory create a socket. Socket socket = socketFactory.createSocket(); // Set up server names for SNI as necessary if possible. SNIHelper.setServerNames(socket, mProxySettings.getServerNames()); // The address to connect to. Address address = new Address(mProxySettings.getHost(), proxyPort); // The delegatee for the handshake with the proxy. ProxyHandshaker handshaker = new ProxyHandshaker(socket, host, port, mProxySettings); // SSLSocketFactory for SSL handshake with the WebSocket endpoint. SSLSocketFactory sslSocketFactory = secure ? (SSLSocketFactory)mSocketFactorySettings.selectSocketFactory(secure) : null; // Create an instance that will execute the task to connect to the server later. return new SocketConnector( socket, address, timeout, handshaker, sslSocketFactory, host, port) .setVerifyHostname(mVerifyHostname); }
int proxyPort = proxyInetAddress.getPort(); ProxySettings proxySettings = factory.getProxySettings(); proxySettings.setHost(proxyHost).setPort(proxyPort); requestHeaders.forEach((headerName, headerValues) -> { if (headerValues == null) { proxySettings.getHeaders().remove(headerName); return; proxySettings.getHeaders().remove(headerName); } else { proxySettings.addHeader(headerName, firstHeaderValue); headerValue -> proxySettings.addHeader(headerName, headerValue)); }); if (credentials != null) { proxySettings .setId(credentials.getUserName()) .setPassword(String.valueOf(credentials.getPassword()));
settings.setServer("http://proxy.example.com"); settings.setCredentials("id", "password");
public WebSocketFactory() { mSocketFactorySettings = new SocketFactorySettings(); mProxySettings = new ProxySettings(this); }
private SocketConnector createRawSocket( String host, int port, boolean secure, int timeout) throws IOException { // Determine the port number. Especially, if 'port' is -1, // it is converted to 80 or 443. port = determinePort(port, secure); // True if a proxy server should be used. boolean proxied = (mProxySettings.getHost() != null); // See "Figure 2 -- Proxy server traversal decision tree" at // http://www.infoq.com/articles/Web-Sockets-Proxy-Servers if (proxied) { // Create a connector to connect to the proxy server. return createProxiedRawSocket(host, port, secure, timeout); } else { // Create a connector to connect to the WebSocket endpoint directly. return createDirectRawSocket(host, port, secure, timeout); } }
private SocketConnector createProxiedRawSocket( String host, int port, boolean secure, int timeout) throws IOException { // Determine the port number of the proxy server. // Especially, if getPort() returns -1, the value // is converted to 80 or 443. int proxyPort = determinePort(mProxySettings.getPort(), mProxySettings.isSecure()); // Select a socket factory. SocketFactory socketFactory = mProxySettings.selectSocketFactory(); // Let the socket factory create a socket. Socket socket = socketFactory.createSocket(); // Set up server names for SNI as necessary if possible. SNIHelper.setServerNames(socket, mProxySettings.getServerNames()); // The address to connect to. Address address = new Address(mProxySettings.getHost(), proxyPort); // The delegatee for the handshake with the proxy. ProxyHandshaker handshaker = new ProxyHandshaker(socket, host, port, mProxySettings); // SSLSocketFactory for SSL handshake with the WebSocket endpoint. SSLSocketFactory sslSocketFactory = secure ? (SSLSocketFactory)mSocketFactorySettings.selectSocketFactory(secure) : null; // Create an instance that will execute the task to connect to the server later. return new SocketConnector( socket, address, timeout, handshaker, sslSocketFactory, host, port) .setVerifyHostname(mVerifyHostname); }
private void connect(String url) { WebSocketFactory factory = new WebSocketFactory(); if (proxy != null) { ProxySettings settings = factory.getProxySettings(); settings.setHost(proxy.getHostName()); settings.setPort(proxy.getPort()); } try { socket = factory.createSocket(url) .addHeader("Accept-Encoding", "gzip") .addListener(this); socket.connect(); } catch (IOException | WebSocketException e) { //Completely fail here. We couldn't make the connection. throw new RuntimeException(e); } }
private void addProxyAuthorization(StringBuilder builder) { String id = mSettings.getId(); if (id == null || id.length() == 0) { return; } String password = mSettings.getPassword(); if (password == null) { password = ""; } // {id}:{password} String credentials = String.format("%s:%s", id, password); // The current implementation always uses Basic Authentication. builder .append("Proxy-Authorization: Basic ") .append(Base64.encode(credentials)) .append(RN); }
public WebSocketFactory() { mSocketFactorySettings = new SocketFactorySettings(); mProxySettings = new ProxySettings(this); }
private SocketConnector createRawSocket( String host, int port, boolean secure, int timeout) throws IOException { // Determine the port number. Especially, if 'port' is -1, // it is converted to 80 or 443. port = determinePort(port, secure); // True if a proxy server should be used. boolean proxied = (mProxySettings.getHost() != null); // See "Figure 2 -- Proxy server traversal decision tree" at // http://www.infoq.com/articles/Web-Sockets-Proxy-Servers if (proxied) { // Create a connector to connect to the proxy server. return createProxiedRawSocket(host, port, secure, timeout); } else { // Create a connector to connect to the WebSocket endpoint directly. return createDirectRawSocket(host, port, secure, timeout); } }
/** * Set credentials for authentication at the proxy server. * This method is an alias of {@link #setId(String) setId}{@code * (id).}{@link #setPassword(String) setPassword}{@code * (password)}. * * @param id * The ID. * * @param password * The password. * * @return * {@code this} object. */ public ProxySettings setCredentials(String id, String password) { return setId(id).setPassword(password); }
/** * Set the proxy server by a URL. See the description of * {@link #setServer(URI)} about how the parameters are updated. * * @param url * The URL of the proxy server. If {@code null} is given, * none of the parameters are updated. * * @return * {@code this} object. * * @throws IllegalArgumentException * Failed to convert the given URL to a {@link URI} instance. */ public ProxySettings setServer(URL url) { if (url == null) { return this; } try { return setServer(url.toURI()); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } }
private void addHeaders(StringBuilder builder) { // For each additional header. for (Map.Entry<String, List<String>> header : mSettings.getHeaders().entrySet()) { // Header name. String name = header.getKey(); // For each header value. for (String value : header.getValue()) { if (value == null) { value = ""; } builder.append(name).append(": ").append(value).append(RN); } } }
int port = uri.getPort(); return setServer(scheme, userInfo, host, port);
/** * Set the proxy server by a URI. See the description of * {@link #setServer(URI)} about how the parameters are updated. * * @param uri * The URI of the proxy server. If {@code null} is given, * none of the parameters are updated. * * @return * {@code this} object. * * @throws IllegalArgumentException * Failed to convert the given string to a {@link URI} instance. */ public ProxySettings setServer(String uri) { if (uri == null) { return this; } return setServer(URI.create(uri)); }