2016-07-28 18:05:20 +02:00
|
|
|
package wa.server.plugin.apache;
|
|
|
|
|
|
|
|
|
|
import wa.server.WAConstants;
|
2016-08-02 17:52:47 +02:00
|
|
|
import wa.server.plugin.WAConfigObject;
|
2016-07-28 18:05:20 +02:00
|
|
|
import zutil.ui.Configurator;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.PrintStream;
|
|
|
|
|
|
|
|
|
|
|
2016-08-02 17:52:47 +02:00
|
|
|
public abstract class ApacheAbstractConfig extends WAConfigObject {
|
2016-07-28 18:05:20 +02:00
|
|
|
private static final String CONFIG_NAME = "vhost";
|
|
|
|
|
private static final String NAVIGATION_NAME = "Apache Virtual Host";
|
|
|
|
|
|
|
|
|
|
private static final String SITE_AVAILABLE_DIR = "/apache2/site-available/";
|
|
|
|
|
private static final String SITE_ENABLED_DIR = "/apache2/site-enabled/";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configurator.Configurable("Domain")
|
|
|
|
|
protected String domain;
|
|
|
|
|
protected transient String domain_old;
|
|
|
|
|
@Configurator.Configurable("WWW Redirect")
|
|
|
|
|
protected boolean wwwRedirect;
|
|
|
|
|
@Configurator.Configurable("SSL")
|
|
|
|
|
protected boolean ssl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getDomain() {
|
|
|
|
|
return domain;
|
|
|
|
|
}
|
|
|
|
|
public void setDomain(String domain) {
|
|
|
|
|
this.domain = domain;
|
|
|
|
|
}
|
|
|
|
|
public boolean wwwRedirectEnabled() {
|
|
|
|
|
return wwwRedirect;
|
|
|
|
|
}
|
|
|
|
|
public void setWWWRedirect(boolean redirect) {
|
|
|
|
|
this.wwwRedirect = redirect;
|
|
|
|
|
}
|
|
|
|
|
public boolean sslEnabk() {
|
|
|
|
|
return ssl;
|
|
|
|
|
}
|
|
|
|
|
public void setSSL(boolean ssl) {
|
|
|
|
|
this.ssl = ssl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void saveConfiguration() throws IOException {
|
|
|
|
|
if(domain != domain_old){ // Configuration file has changed name
|
|
|
|
|
WAConstants.getConfigFile(SITE_AVAILABLE_DIR+ domain_old).delete();
|
|
|
|
|
domain_old = domain;
|
|
|
|
|
}
|
|
|
|
|
PrintStream out = new PrintStream(
|
|
|
|
|
WAConstants.getConfigFile(SITE_AVAILABLE_DIR+ domain));
|
|
|
|
|
|
|
|
|
|
saveConfiguration(out);
|
|
|
|
|
out.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveConfiguration(PrintStream out){
|
|
|
|
|
if (wwwRedirect){
|
|
|
|
|
out.println("<VirtualHost *:80>");
|
|
|
|
|
out.println(" ServerName www."+ domain);
|
|
|
|
|
out.println();
|
|
|
|
|
out.println(" RewriteEngine on");
|
|
|
|
|
out.println(" RewriteCond %{HTTP_HOST} ^www\\..*$ [NC]");
|
|
|
|
|
out.println(" RewriteRule ^ http://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]"); // 301: permanent, 302: temporary
|
|
|
|
|
out.println("</VirtualHost>");
|
|
|
|
|
out.println();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ssl){
|
|
|
|
|
out.println("<VirtualHost *:80>");
|
|
|
|
|
out.println(" ServerName "+ domain);
|
|
|
|
|
out.println();
|
|
|
|
|
out.println(" RewriteEngine On");
|
|
|
|
|
out.println(" RewriteCond %{SERVER_PORT} !^443$");
|
|
|
|
|
out.println(" RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]");
|
|
|
|
|
out.println("</VirtualHost>");
|
|
|
|
|
out.println("<VirtualHost *:443>");
|
|
|
|
|
out.println(" ServerName "+ domain +":443");
|
|
|
|
|
out.println();
|
|
|
|
|
apacheConfiguration(out);
|
|
|
|
|
out.println();
|
|
|
|
|
out.println(" ServerAlias "+domain);
|
|
|
|
|
out.println(" Include /etc/letsencrypt/options-ssl-apache.conf");
|
|
|
|
|
out.println(" SSLCertificateFile /etc/letsencrypt/live/"+domain+"/cert.pem");
|
|
|
|
|
out.println(" SSLCertificateKeyFile /etc/letsencrypt/live/"+domain+"/privkey.pem");
|
|
|
|
|
out.println(" SSLCertificateChainFile /etc/letsencrypt/live/"+domain+"/chain.pem");
|
|
|
|
|
out.println("</VirtualHost>");
|
|
|
|
|
out.println("");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
out.println("<VirtualHost *:80>");
|
|
|
|
|
out.println(" ServerName "+ domain);
|
|
|
|
|
out.println();
|
|
|
|
|
apacheConfiguration(out);
|
|
|
|
|
out.println("</VirtualHost>");
|
|
|
|
|
out.println("");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void apacheConfiguration(PrintStream out);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void deleteConfiguration() throws Exception {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|