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

com.aoindustries.aoserv.client.SSLConnector Maven / Gradle / Ivy

There is a newer version: 1.92.0
Show newest version
/*
 * aoserv-client - Java client for the AOServ Platform.
 * Copyright (C) 2001-2012, 2016, 2017, 2018, 2019, 2020  AO Industries, Inc.
 *     [email protected]
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of aoserv-client.
 *
 * aoserv-client is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aoserv-client 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with aoserv-client.  If not, see .
 */
package com.aoindustries.aoserv.client;

import com.aoindustries.aoserv.client.account.User;
import com.aoindustries.io.AOPool;
import com.aoindustries.net.DomainName;
import com.aoindustries.net.HostAddress;
import com.aoindustries.net.InetAddress;
import com.aoindustries.net.Port;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.net.ssl.SSLSocketFactory;

/**
 * A SSLConnector provides the connection between
 * the client and server over secured SSL sockets.
 *
 * @see  AOServConnector
 *
 * @author  AO Industries, Inc.
 */
public class SSLConnector extends TCPConnector {

	/**
	 * The trust store used for this connector.
	 */
	private static String trustStorePath;

	/**
	 * The password for the trust store.
	 */
	private static String trustStorePassword;

	/**
	 * The protocol for this connector.
	 */
	public static final String SSL_PROTOCOL = "ssl";

	/**
	 * Instances of connectors are created once and then reused.
	 */
	private static final List connectors=new ArrayList<>();

	protected SSLConnector(
		HostAddress hostname,
		InetAddress local_ip,
		Port port,
		User.Name connectAs,
		User.Name authenticateAs,
		String password,
		DomainName daemonServer,
		int poolSize,
		long maxConnectionAge,
		String trustStorePath,
		String trustStorePassword
	) {
		super(hostname, local_ip, port, connectAs, authenticateAs, password, daemonServer, poolSize, maxConnectionAge);
		if(
			(
				SSLConnector.trustStorePath!=null
				&& !SSLConnector.trustStorePath.equals(trustStorePath)
			) || (
				SSLConnector.trustStorePassword!=null
				&& !SSLConnector.trustStorePassword.equals(trustStorePassword)
			)
		) throw new IllegalArgumentException(
			"Trust store path and password may only be set once, currently '"
			+ SSLConnector.trustStorePath
			+ "', trying to set to '"
			+ trustStorePath
			+ "'"
		);
		if(SSLConnector.trustStorePath==null) {
			SSLConnector.trustStorePath=trustStorePath;
			SSLConnector.trustStorePassword=trustStorePassword;
		}
	}

	@Override
	public String getProtocol() {
		return SSL_PROTOCOL;
	}

	@Override
	Socket getSocket() throws IOException {
		if(trustStorePath!=null && trustStorePath.length()>0) {
			System.setProperty("javax.net.ssl.trustStore", trustStorePath);
		}
		if(trustStorePassword!=null && trustStorePassword.length()>0) {
			System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
		}

		SSLSocketFactory sslFact=(SSLSocketFactory)SSLSocketFactory.getDefault();
		Socket regSocket = new Socket();
		regSocket.setKeepAlive(true);
		regSocket.setSoLinger(true, AOPool.DEFAULT_SOCKET_SO_LINGER);
		regSocket.setTcpNoDelay(true);
		if(local_ip != null && !local_ip.isUnspecified()) regSocket.bind(new InetSocketAddress(local_ip.toString(), 0));
		regSocket.connect(new InetSocketAddress(hostname.toString(), port.getPort()), AOPool.DEFAULT_CONNECT_TIMEOUT);
		return sslFact.createSocket(regSocket, hostname.toString(), port.getPort(), true);
	}

	public static synchronized SSLConnector getSSLConnector(
		HostAddress hostname,
		InetAddress local_ip,
		Port port,
		User.Name connectAs,
		User.Name authenticateAs,
		String password,
		DomainName daemonServer,
		int poolSize,
		long maxConnectionAge,
		String trustStorePath,
		String trustStorePassword
	) {
		if(connectAs==null) throw new IllegalArgumentException("connectAs is null");
		if(authenticateAs==null) throw new IllegalArgumentException("authenticateAs is null");
		if(password==null) throw new IllegalArgumentException("password is null");
		int size=connectors.size();
		for(int c=0;c




© 2015 - 2025 Weber Informatics LLC | Privacy Policy