ch.bfh.abcvote.util.helpers.SocksConnectionSocketFactory.java Source code

Java tutorial

Introduction

Here is the source code for ch.bfh.abcvote.util.helpers.SocksConnectionSocketFactory.java

Source

/*
 * abcVote
 *
 *  abcVote - an e-voting prototype with everlasting privacy
 *  Copyright (c) 2017 Timo Buerk and Sebastian Nellen
 *
 *  Licensed under Dual License consisting of:
 *  1. GNU Affero General Public License (AGPL) v3
 *  and
 *  2. Commercial license
 *
 *
 *  1. This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *  2. Licensees holding valid commercial licenses for abcVote may use this file in
 *   accordance with the commercial license agreement provided with the
 *   Software or, alternatively, in accordance with the terms contained in
 *   a written agreement between you and us.
 *
 *
 *   For further information contact <e-mail: burkt4@gmail.com> or <e-mail: sebastian@nellen.it>
 *
 *
 * Redistributions of files must retain the above copyright notice.
 */
package ch.bfh.abcvote.util.helpers;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import javax.net.ssl.SSLContext;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.protocol.HttpContext;

/**
 *
 * ConnectionSocketFactory needed for Socks Proxy
 *  based on answer http://stackoverflow.com/a/22960881 
 *  from http://stackoverflow.com/questions/22937983/how-to-use-socks-5-proxy-with-apache-http-client-4 
 * 
 * @author Sebastian Nellen <sebastian at nellen.it>
 */
class SocksConnectionSocketFactory extends SSLConnectionSocketFactory {

    public SocksConnectionSocketFactory(final SSLContext sslContext) {
        super(sslContext);
    }

    /**
     * ConnectionSocketFactory needed for Socks Proxy
     * @param context the http context
     * @return returns a proxy socket
     */
    public Socket createSocket(final HttpContext context) {
        InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socks.address");
        Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
        return new Socket(proxy);
    }

}