package com.cym.service;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.noear.solon.annotation.Inject;
import org.noear.solon.aspect.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cym.config.HomeConfig;
import com.cym.ext.AsycPack;
import com.cym.ext.ConfExt;
import com.cym.ext.ConfFile;
import com.cym.model.Bak;
import com.cym.model.BakSub;
import com.cym.model.Basic;
import com.cym.model.Cert;
import com.cym.model.Http;
import com.cym.model.Location;
import com.cym.model.Param;
import com.cym.model.Password;
import com.cym.model.Server;
import com.cym.model.Stream;
import com.cym.model.Template;
import com.cym.model.Upstream;
import com.cym.model.UpstreamServer;
import com.cym.sqlhelper.bean.Sort;
import com.cym.sqlhelper.bean.Sort.Direction;
import com.cym.sqlhelper.utils.ConditionAndWrapper;
import com.cym.sqlhelper.utils.SqlHelper;
import com.cym.utils.ToolUtils;
import com.github.odiszapc.nginxparser.NgxBlock;
import com.github.odiszapc.nginxparser.NgxConfig;
import com.github.odiszapc.nginxparser.NgxDumper;
import com.github.odiszapc.nginxparser.NgxEntry;
import com.github.odiszapc.nginxparser.NgxParam;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
@Service
public class ConfService {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Inject
UpstreamService upstreamService;
@Inject
SettingService settingService;
@Inject
ServerService serverService;
@Inject
LocationService locationService;
@Inject
ParamService paramService;
@Inject
SqlHelper sqlHelper;
@Inject
TemplateService templateService;
@Inject
OperateLogService operateLogService;
@Inject
HomeConfig homeConfig;
public synchronized ConfExt buildConf(Boolean decompose, Boolean check) {
ConfExt confExt = new ConfExt();
confExt.setFileList(new ArrayList<>());
String nginxPath = settingService.get("nginxPath");
if (check) {
nginxPath = homeConfig.home + "temp/nginx.conf";
}
try {
NgxConfig ngxConfig = new NgxConfig();
// 获取基本参数
List<Basic> basicList = sqlHelper.findAll(new Sort("seq", Direction.ASC), Basic.class);
for (Basic basic : basicList) {
NgxParam ngxParam = new NgxParam();
ngxParam.addValue(basic.getName().trim() + " " + basic.getValue().trim());
ngxConfig.addEntry(ngxParam);
}
// 获取http
List<Http> httpList = sqlHelper.findAll(new Sort("seq", Direction.ASC), Http.class);
boolean hasHttp = false;
NgxBlock ngxBlockHttp = new NgxBlock();
ngxBlockHttp.addValue("http");
for (Http http : httpList) {
NgxParam ngxParam = new NgxParam();
ngxParam.addValue(http.getName().trim() + " " + http.getValue().trim());
ngxBlockHttp.addEntry(ngxParam);
hasHttp = true;
}
// 添加upstream
NgxParam ngxParam;
List<Upstream> upstreams = upstreamService.getListByProxyType(0);
for (Upstream upstream : upstreams) {
NgxBlock ngxBlockServer = new NgxBlock();
ngxBlockServer.addValue("upstream " + upstream.getName().trim());
if (StrUtil.isNotEmpty(upstream.getDescr())) {
String[] descrs = upstream.getDescr().split("\n");
for (String d : descrs) {
ngxParam = new NgxParam();
ngxParam.addValue("# " + d);
ngxBlockServer.addEntry(ngxParam);
}
}
if (StrUtil.isNotEmpty(upstream.getTactics())) {
ngxParam = new NgxParam();
ngxParam.addValue(upstream.getTactics());
ngxBlockServer.addEntry(ngxParam);
}
List<UpstreamServer> upstreamServers = upstreamService.getUpstreamServers(upstream.getId());
for (UpstreamServer upstreamServer : upstreamServers) {
ngxParam = new NgxParam();
ngxParam.addValue("server " + buildNodeStr(upstreamServer));
ngxBlockServer.addEntry(ngxParam);
}
// 自定义参数
List<Param> paramList = paramService.getListByTypeId(upstream.getId(), "upstream");
for (Param param : paramList) {
setSameParam(param, ngxBlockServer);
}
hasHttp = true;
if (decompose) {
addConfFile(confExt, "upstreams." + upstream.getName() + ".conf", ngxBlockServer);
ngxParam = new NgxParam();
ngxParam.addValue("include " + new File(nginxPath).getParent().replace("\\", "/") + "/conf.d/upstreams." + upstream.getName().replace(" ", "_").replace("*", "-") + ".conf");
ngxBlockHttp.addEntry(ngxParam);
} else {
ngxBlockHttp.addEntry(ngxBlockServer);
}
}
// 添加server
List<Server> servers = serverService.getListByProxyType(new String[] { "0" });
for (Server server : servers) {
if (server.getEnable() == null || !server.getEnable()) {
continue;
}
NgxBlock ngxBlockServer = bulidBlockServer(server);
hasHttp = true;
// 是否需要分解
if (decompose) {
String name = "all";
if (StrUtil.isNotEmpty(server.getServerName())) {
name = server.getServerName();
}
addConfFile(confExt, name + ".conf", ngxBlockServer);
ngxParam = new NgxParam();
ngxParam.addValue("include " + new File(nginxPath).getParent().replace("\\", "/") + "/conf.d/" + name.replace(" ", "_").replace("*", "-") + ".conf");
if (noContain(ngxBlockHttp, ngxParam)) {
ngxBlockHttp.addEntry(ngxParam);
}
} else {
ngxBlockHttp.addEntry(ngxBlockServer);
}
}
if (hasHttp) {
ngxConfig.addEntry(ngxBlockHttp);
}
// TCP/UDP转发
// 创建stream
List<Stream> streamList = sqlHelper.findAll(new Sort("seq", Direction.ASC), Stream.class);
boolean hasStream = false;
NgxBlock ngxBlockStream = new NgxBlock();
ngxBlockStream.addValue("stream");
for (Stream stream : streamList) {
ngxParam = new NgxParam();
ngxParam.addValue(stream.getName() + " " + stream.getValue());
ngxBlockStream.addEntry(ngxParam);
hasStream = true;
}
// 添加upstream
upstreams = upstreamService.getListByProxyType(1);
for (Upstream upstream : upstreams) {
NgxBlock ngxBlockServer = buildBlockUpstream(upstream);
if (decompose) {
addConfFile(confExt, "upstreams." + upstream.getName() + ".conf", ngxBlockServer);
ngxParam = new NgxParam();
ngxParam.addValue("include " + new File(nginxPath).getParent().replace("\\", "/") + "/conf.d/upstreams." + upstream.getName().replace(" ", "_").replace("*", "-") + ".conf");
ngxBlockStream.addEntry(ngxParam);
} else {
ngxBlockStream.addEntry(ngxBlockServer);
}
hasStream = true;
}
// 添加server
servers = serverService.getListByProxyType(new String[] { "1", "2" });
for (Server server : servers) {
if (server.getEnable() == null || !server.getEnable()) {
continue;
}
NgxBlock ngxBlockServer = bulidBlockServer(server);
if (decompose) {
String type = "";
if (server.getProxyType() == 0) {
type = "http";
} else if (server.getProxyType() == 1) {
type = "tcp";
} else if (server.getProxyType() == 2) {
type = "udp";
}
addConfFile(confExt, type + "." + server.getListen() + ".conf", ngxBlockServer);
ngxParam = new NgxParam();
ngxParam.addValue("include " + new File(nginxPath).getParent().replace("\\", "/") + "/conf.d/" + type + "." + server.getListen() + ".conf");
ngxBlockStream.addEntry(ngxParam);
} else {
ngxBlockStream.addEntry(ngxBlockServer);
}
hasStream = true;
}
if (hasStream) {
ngxConfig.addEntry(ngxBlockStream);
}
String conf = ToolUtils.handleConf(new NgxDumper(ngxConfig).dump());
confExt.setConf(conf);
return confExt;
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return null;
}
public NgxBlock buildBlockUpstream(Upstream upstream) {
NgxParam ngxParam = null;
NgxBlock ngxBlockServer = new NgxBlock();
ngxBlockServer.addValue("upstream " + upstream.getName());
if (StrUtil.isNo
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
方便快捷易用的Nginx可视化UI网页管理部署配置工具系统,使用Web网页系统来快速配置部署,支持在线配置你的Nginx/Openresty的反向代理、负载均衡、SSL/HTTPS证书免费签发,自动续签、TCP/UDP 3-4层转发、可以自定义参数字段、均衡负载节点、Nginx服务状态监控故障报警、集群配置同步等,快速部署多个或N个Nginx集群节点,支持导入现有Nginx配置文件让你无忧使用.
资源推荐
资源详情
资源评论
收起资源包目录
nginxWebUI-master.zip (692个子文件)
README.asciidoc 9KB
nginx.conf 65B
layui.css 80KB
AllInOne.css 61KB
xterm.css 34KB
font.css 18KB
layer.css 14KB
common.css 13KB
awesome.css 8KB
laydate.css 7KB
metroStyle.css 6KB
zTreeStyle.css 6KB
zTreeStyleForApi.css 3KB
diffview.css 3KB
demo.css 2KB
code.css 1KB
common_ie6.css 1KB
base.css 783B
xt256.min.css 745B
Dockerfile 811B
.dockerignore 54B
iconfont.eot 46KB
zTreeStandard.gif 7KB
loading-0.gif 6KB
zTreeStandard.gif 5KB
metro.gif 5KB
loading-2.gif 2KB
apiMenu.gif 2KB
loading-1.gif 701B
overlay_arrow.gif 625B
overlay_close_IE6.gif 441B
loading.gif 381B
loading.gif 381B
loading.gif 381B
line_conn.gif 45B
.gitignore 358B
ServerApiController.html 84KB
CertApiController.html 80KB
api.html 74KB
UpstreamApiController.html 59KB
NginxApiController.html 43KB
ParamApiController.html 36KB
PasswordApiController.html 31KB
index.html 19KB
TokenController.html 17KB
index.html 12KB
index.html 11KB
index.html 11KB
error.html 11KB
dict.html 11KB
index.html 9KB
index.html 7KB
index.html 7KB
API_cn.html 5KB
API_en.html 5KB
index.html 5KB
index.html 4KB
index.html 4KB
index.html 4KB
fn.zTree.init.html 4KB
fn.zTree.init.html 4KB
setting.check.chkStyle.html 3KB
setting.check.chkStyle.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
zTreeObj.expandNode.html 3KB
zTreeObj.addNodes.html 3KB
treeNode.getCheckStatus.html 3KB
zTreeObj.addNodes.html 3KB
treeNode.getCheckStatus.html 3KB
zTreeObj.expandNode.html 3KB
zTreeObj.setChkDisabled.html 3KB
zTreeObj.checkNode.html 3KB
zTreeObj.reAsyncChildNodes.html 3KB
setting.callback.onClick.html 3KB
zTreeObj.copyNode.html 3KB
zTreeObj.moveNode.html 3KB
setting.callback.beforeClick.html 2KB
setting.callback.onDrop.html 2KB
setting.callback.beforeDrop.html 2KB
zTreeObj.reAsyncChildNodesPromise.html 2KB
zTreeObj.reAsyncChildNodesPromise.html 2KB
zTreeObj.reAsyncChildNodes.html 2KB
zTreeObj.checkNode.html 2KB
setting.callback.onClick.html 2KB
setting.callback.beforeClick.html 2KB
setting.callback.beforeRename.html 2KB
setting.callback.onDrop.html 2KB
zTreeObj.copyNode.html 2KB
setting.callback.beforeRename.html 2KB
zTreeObj.moveNode.html 2KB
zTreeObj.setChkDisabled.html 2KB
setting.async.autoParam.html 2KB
setting.callback.beforeDrop.html 2KB
zTreeObj.getNodesByFilter.html 2KB
setting.edit.drag.inner.html 2KB
index.html 2KB
select_mail.html 2KB
共 692 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
m0_72731342
- 粉丝: 4
- 资源: 1829
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 快速定制中国传统节日头像(源码)
- hcia 复习内容的实验
- 准Z源光伏并网系统MATLAB仿真模型,采用了三次谐波注入法SPWM调制,具有更高的电压利用效率 并网部分采用了电压外环电流内环 电池部分采用了扰动观察法,PO Z源并网和逆变器研究方向的同学可
- 海面目标检测跟踪数据集.zip
- 欧美风格, 节日主题模板
- 西门子1200和三菱FXU通讯程序
- 11种概率分布的拟合与ks检验,可用于概率分析,可靠度计算等领域 案例中提供11种概率分布,具体包括:gev、logistic、gaussian、tLocationScale、Rayleigh、Log
- 机械手自动排列控制PLC与触摸屏程序设计
- uDDS源程序publisher
- 中国风格, 节日 主题, PPT模板
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功