2015-04-09 21:22:47 +00:00
|
|
|
package wa.server.plugin.apache;
|
|
|
|
|
|
|
|
|
|
import wa.server.WAConstants;
|
2015-11-16 17:48:37 +01:00
|
|
|
import wa.server.plugin.WAConfigEntry;
|
2015-07-27 15:10:11 +00:00
|
|
|
import wa.server.plugin.WAServiceConfig;
|
2015-09-18 15:07:33 +00:00
|
|
|
import wa.server.util.ConfigFileUtil;
|
2015-08-07 14:46:01 +00:00
|
|
|
import zutil.db.DBConnection;
|
2015-09-26 21:19:24 +00:00
|
|
|
import zutil.db.bean.Configurator.Configurable;
|
2015-04-09 21:22:47 +00:00
|
|
|
import zutil.db.bean.DBBean;
|
2015-08-07 14:46:01 +00:00
|
|
|
import zutil.db.bean.DBBean.DBTable;
|
2015-09-18 15:07:33 +00:00
|
|
|
import zutil.io.IOUtil;
|
2015-08-07 14:46:01 +00:00
|
|
|
|
2015-09-18 15:07:33 +00:00
|
|
|
import java.io.*;
|
2015-08-07 14:46:01 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2015-04-09 21:22:47 +00:00
|
|
|
|
2015-07-27 15:10:11 +00:00
|
|
|
public class ApacheConfigVirtualHost implements WAServiceConfig{
|
2015-09-18 15:07:33 +00:00
|
|
|
private static final String CONFIG_NAME = "Apache Virtual Host";
|
2015-11-16 17:48:37 +01:00
|
|
|
private static final String APACHE_MAIN_CONFIG_FILE = "/apache2/apache2.conf";
|
|
|
|
|
private static final String APACHE_SITE_AVAILABLE_PATH = "/apache2/site-available/";
|
2015-09-18 15:07:33 +00:00
|
|
|
private static final String STATIC_PRE_CONF = "wa/server/plugin/apache/apache_default.config";
|
2015-07-27 15:10:11 +00:00
|
|
|
|
2015-11-16 17:48:37 +01:00
|
|
|
private List<ApacheVirtualHostEntry> vhosts = new ArrayList<>();
|
2015-04-09 21:22:47 +00:00
|
|
|
|
2015-07-27 15:10:11 +00:00
|
|
|
|
2015-11-16 17:48:37 +01:00
|
|
|
public ApacheConfigVirtualHost() throws Exception {
|
|
|
|
|
DBConnection db = WAConstants.getDB();
|
|
|
|
|
vhosts = DBBean.load(db, ApacheVirtualHostEntry.class);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-27 15:10:11 +00:00
|
|
|
@Override
|
|
|
|
|
public String getName() {
|
|
|
|
|
return CONFIG_NAME;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 17:48:37 +01:00
|
|
|
public WAConfigEntry createConfig(){
|
|
|
|
|
return new ApacheVirtualHostEntry();
|
|
|
|
|
}
|
|
|
|
|
public void deleteConfig(int id){
|
|
|
|
|
|
|
|
|
|
}
|
2015-08-07 14:46:01 +00:00
|
|
|
|
2015-07-27 15:10:11 +00:00
|
|
|
|
|
|
|
|
@Override
|
2015-08-07 14:46:01 +00:00
|
|
|
public void configure() throws Exception {
|
2015-09-18 15:07:33 +00:00
|
|
|
// Update main configuration file
|
|
|
|
|
ConfigFileUtil.writeBetweenBoundary(
|
|
|
|
|
WAConstants.getConfigFile(APACHE_MAIN_CONFIG_FILE),
|
|
|
|
|
"#",
|
|
|
|
|
IOUtil.getContentString(
|
|
|
|
|
new FileInputStream(STATIC_PRE_CONF)));
|
|
|
|
|
|
|
|
|
|
// Write Vhost configuration
|
2015-11-16 17:48:37 +01:00
|
|
|
for(ApacheVirtualHostEntry vhost : vhosts){
|
2015-09-18 15:07:33 +00:00
|
|
|
vhost.configure();
|
|
|
|
|
}
|
2015-07-27 15:10:11 +00:00
|
|
|
}
|
2015-08-07 14:46:01 +00:00
|
|
|
|
2015-09-26 21:19:24 +00:00
|
|
|
@Override
|
2015-11-16 17:48:37 +01:00
|
|
|
public List<? extends WAConfigEntry> getConfigData() {
|
2015-09-26 21:19:24 +00:00
|
|
|
return vhosts;
|
|
|
|
|
}
|
|
|
|
|
@Override
|
2015-11-16 17:48:37 +01:00
|
|
|
public Class<? extends WAConfigEntry> getConfigClass() {
|
|
|
|
|
return ApacheVirtualHostEntry.class;
|
2015-09-26 21:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-07 14:46:01 +00:00
|
|
|
|
|
|
|
|
@DBTable(WAConstants.DB_TABLE_PREFIX + "_apache_vhost")
|
2015-11-16 17:48:37 +01:00
|
|
|
public static class ApacheVirtualHostEntry extends WAConfigEntry {
|
2015-09-26 21:19:24 +00:00
|
|
|
@Configurable("Domain")
|
2015-08-07 14:46:01 +00:00
|
|
|
protected String domain;
|
2015-09-26 21:19:24 +00:00
|
|
|
protected transient String domain_old;
|
|
|
|
|
@Configurable("DocRoot")
|
2015-08-07 14:46:01 +00:00
|
|
|
protected String path;
|
2015-09-26 21:19:24 +00:00
|
|
|
@Configurable("SSL")
|
2015-08-07 14:46:01 +00:00
|
|
|
protected boolean ssl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getDomain() {
|
|
|
|
|
return domain;
|
|
|
|
|
}
|
|
|
|
|
public void setDomain(String domain) {
|
|
|
|
|
this.domain = domain;
|
|
|
|
|
}
|
|
|
|
|
public String getPath() {
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
public void setPath(String path) {
|
|
|
|
|
this.path = path;
|
|
|
|
|
}
|
|
|
|
|
public boolean isSSL() {
|
|
|
|
|
return ssl;
|
|
|
|
|
}
|
|
|
|
|
public void setSSL(boolean ssl) {
|
|
|
|
|
this.ssl = ssl;
|
|
|
|
|
}
|
2015-09-18 15:07:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public final void configure() throws IOException {
|
|
|
|
|
if(domain != domain_old){ // Configuration file has changed name
|
2015-11-16 17:48:37 +01:00
|
|
|
WAConstants.getConfigFile(APACHE_SITE_AVAILABLE_PATH+ domain_old).delete();
|
|
|
|
|
domain_old = domain;
|
2015-09-18 15:07:33 +00:00
|
|
|
}
|
|
|
|
|
PrintStream out = new PrintStream(
|
2015-11-16 17:48:37 +01:00
|
|
|
WAConstants.getConfigFile(APACHE_SITE_AVAILABLE_PATH+ domain));
|
2015-09-18 15:07:33 +00:00
|
|
|
|
|
|
|
|
writeConfiguration(out);
|
|
|
|
|
out.close();
|
|
|
|
|
}
|
2015-09-26 21:19:24 +00:00
|
|
|
protected void writeConfiguration(PrintStream out){
|
2015-09-18 15:07:33 +00:00
|
|
|
if(ssl){
|
|
|
|
|
out.println("<VirtualHost *:80>");
|
|
|
|
|
out.println(" ServerName "+ domain +":80");
|
|
|
|
|
out.println(" RewriteEngine On");
|
|
|
|
|
out.println(" RewriteCond %{SERVER_PORT} !^443$");
|
|
|
|
|
out.println(" RewriteRule ^(.*)$ https://server$1 [L,R]");
|
|
|
|
|
out.println("</VirtualHost>");
|
|
|
|
|
out.println("<VirtualHost *:443>");
|
|
|
|
|
out.println(" ServerName "+ domain +":443");
|
|
|
|
|
out.println(" DocumentRoot "+ path);
|
|
|
|
|
out.println();
|
|
|
|
|
out.println(" SSLEngine on");
|
|
|
|
|
out.println(" SSLCertificateFile "+ WAConstants.getConfigFile(WAConstants.WA_SSL_CERT));
|
|
|
|
|
out.println(" SSLCertificateKeyFile "+ WAConstants.getConfigFile(WAConstants.WA_SSL_KEY));
|
|
|
|
|
out.println("</VirtualHost>");
|
|
|
|
|
out.println("");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
out.println("<VirtualHost *:80>");
|
2015-11-16 17:48:37 +01:00
|
|
|
out.println(" ServerName "+ domain +":80");
|
|
|
|
|
out.println(" DocumentRoot "+ path);
|
2015-09-18 15:07:33 +00:00
|
|
|
out.println("</VirtualHost>");
|
|
|
|
|
out.println("");
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-07 14:46:01 +00:00
|
|
|
}
|
2015-04-09 21:22:47 +00:00
|
|
|
}
|