Search in sources :

Example 6 with AgentWhitelist

use of io.jpom.model.data.AgentWhitelist in project Jpom by dromara.

the class ProjectFileControl method getFileList.

@RequestMapping(value = "getFileList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String getFileList(String id, String path) {
    // 查询项目路径
    NodeProjectInfoModel pim = projectInfoService.getItem(id);
    Assert.notNull(pim, "查询失败:项目不存在");
    String lib = pim.allLib();
    File fileDir = FileUtil.file(lib, StrUtil.emptyToDefault(path, FileUtil.FILE_SEPARATOR));
    boolean exist = FileUtil.exist(fileDir);
    if (!exist) {
        return JsonMessage.getString(200, "查询成功", new JSONArray());
    }
    // 
    File[] filesAll = fileDir.listFiles();
    if (ArrayUtil.isEmpty(filesAll)) {
        return JsonMessage.getString(200, "查询成功", new JSONArray());
    }
    JSONArray arrayFile = FileUtils.parseInfo(filesAll, false, lib);
    AgentWhitelist whitelist = whitelistDirectoryService.getWhitelist();
    for (Object o : arrayFile) {
        JSONObject jsonObject = (JSONObject) o;
        String filename = jsonObject.getString("filename");
        jsonObject.put("textFileEdit", AgentWhitelist.checkSilentFileSuffix(whitelist.getAllowEditSuffix(), filename));
    }
    return JsonMessage.getString(200, "查询成功", arrayFile);
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) AgentWhitelist(io.jpom.model.data.AgentWhitelist) JSONObject(com.alibaba.fastjson.JSONObject) File(java.io.File)

Example 7 with AgentWhitelist

use of io.jpom.model.data.AgentWhitelist in project Jpom by dromara.

the class WhitelistDirectoryController method whiteList.

/**
 * get whiteList data
 * 白名单数据接口
 *
 * @return json
 * @author Hotstrip
 */
@RequestMapping(value = "white-list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@SystemPermission
public String whiteList() {
    AgentWhitelist agentWhitelist = whitelistDirectoryService.getData(getNode());
    Map<String, String> map = new HashMap<>(8);
    if (agentWhitelist != null) {
        /**
         * put key and value into map
         * 赋值给 map 对象返回
         */
        Field[] fields = ReflectUtil.getFields(AgentWhitelist.class);
        for (Field field : fields) {
            Collection<String> fieldValue = (Collection<String>) ReflectUtil.getFieldValue(agentWhitelist, field);
            map.put(field.getName(), AgentWhitelist.convertToLine(fieldValue));
        }
    }
    return JsonMessage.getString(200, "ok", map);
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) Collection(java.util.Collection) AgentWhitelist(io.jpom.model.data.AgentWhitelist) SystemPermission(io.jpom.permission.SystemPermission) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with AgentWhitelist

use of io.jpom.model.data.AgentWhitelist in project Jpom by dromara.

the class ProjectFileControl method remoteDownload.

/**
 * 下载远程文件
 *
 * @param id        项目id
 * @param url       远程 url 地址
 * @param levelName 保存的文件夹
 * @param unzip     是否为压缩包、true 将自动解压
 * @return json
 */
@PostMapping(value = "remote_download", produces = MediaType.APPLICATION_JSON_VALUE)
public String remoteDownload(String id, String url, String levelName, String unzip) {
    Assert.hasText(url, "请输入正确的远程地址");
    AgentWhitelist whitelist = whitelistDirectoryService.getWhitelist();
    Set<String> allowRemoteDownloadHost = whitelist.getAllowRemoteDownloadHost();
    Assert.state(CollUtil.isNotEmpty(allowRemoteDownloadHost), "还没有配置运行的远程地址");
    List<String> collect = allowRemoteDownloadHost.stream().filter(s -> StrUtil.startWith(url, s)).collect(Collectors.toList());
    Assert.state(CollUtil.isNotEmpty(collect), "不允许下载当前地址的文件");
    try {
        NodeProjectInfoModel pim = projectInfoService.getItem(id);
        File file = FileUtil.file(pim.allLib(), StrUtil.emptyToDefault(levelName, FileUtil.FILE_SEPARATOR));
        File downloadFile = HttpUtil.downloadFileFromUrl(url, file);
        if (BooleanUtil.toBoolean(unzip)) {
            // 需要解压文件
            try {
                CompressionFileUtil.unCompress(file, downloadFile);
            } finally {
                if (!FileUtil.del(downloadFile)) {
                    DefaultSystemLog.getLog().error("删除文件失败:" + file.getPath());
                }
            }
        }
        return JsonMessage.getString(200, "下载成功文件大小:" + FileUtil.readableFileSize(downloadFile));
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("下载远程文件异常", e);
        return JsonMessage.getString(500, "下载远程文件失败:" + e.getMessage());
    }
}
Also used : SecureUtil(cn.hutool.crypto.SecureUtil) MultipartFileBuilder(cn.jiangzeyin.controller.multipart.MultipartFileBuilder) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) ConsoleService(io.jpom.service.manage.ConsoleService) CollStreamUtil(cn.hutool.core.collection.CollStreamUtil) FileUtils(io.jpom.util.FileUtils) ServletUtil(cn.hutool.extra.servlet.ServletUtil) JsonMessage(cn.jiangzeyin.common.JsonMessage) JSONArray(com.alibaba.fastjson.JSONArray) HttpUtil(cn.hutool.http.HttpUtil) Charset(java.nio.charset.Charset) BaseEnum(io.jpom.model.BaseEnum) Map(java.util.Map) AbstractProjectCommander(io.jpom.common.commander.AbstractProjectCommander) AgentConfigBean(io.jpom.system.AgentConfigBean) DiffFileVo(io.jpom.controller.manage.vo.DiffFileVo) BaseAgentController(io.jpom.common.BaseAgentController) MediaType(org.springframework.http.MediaType) Set(java.util.Set) ConsoleCommandOp(io.jpom.socket.ConsoleCommandOp) CompressionFileUtil(io.jpom.util.CompressionFileUtil) AfterOpt(io.jpom.model.AfterOpt) Collectors(java.util.stream.Collectors) File(java.io.File) BooleanUtil(cn.hutool.core.util.BooleanUtil) WhitelistDirectoryService(io.jpom.service.WhitelistDirectoryService) TimeUnit(java.util.concurrent.TimeUnit) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) ArrayUtil(cn.hutool.core.util.ArrayUtil) StringUtil(io.jpom.util.StringUtil) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Convert(cn.hutool.core.convert.Convert) AgentWhitelist(io.jpom.model.data.AgentWhitelist) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) ThreadUtil(cn.hutool.core.thread.ThreadUtil) NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) ProjectCommanderUtil(io.jpom.util.ProjectCommanderUtil) Assert(org.springframework.util.Assert) NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) AgentWhitelist(io.jpom.model.data.AgentWhitelist) File(java.io.File)

Example 9 with AgentWhitelist

use of io.jpom.model.data.AgentWhitelist in project Jpom by dromara.

the class ProjectFileControl method updateConfigFile.

/**
 * 保存文件内容 (只能处理文本文件)
 *
 * @param filePath 相对项目文件的文件夹
 * @param filename 读取的文件名
 * @param fileText 文件内容
 * @return json
 */
@PostMapping(value = "update_config_file", produces = MediaType.APPLICATION_JSON_VALUE)
public String updateConfigFile(String filePath, String filename, String fileText) {
    NodeProjectInfoModel pim = getProjectInfoModel();
    filePath = StrUtil.emptyToDefault(filePath, File.separator);
    // 判断文件后缀
    AgentWhitelist whitelist = whitelistDirectoryService.getWhitelist();
    Charset charset = AgentWhitelist.checkFileSuffix(whitelist.getAllowEditSuffix(), filename);
    FileUtil.writeString(fileText, FileUtil.file(pim.allLib(), filePath, filename), charset);
    return JsonMessage.getString(200, "文件写入成功");
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) Charset(java.nio.charset.Charset) AgentWhitelist(io.jpom.model.data.AgentWhitelist)

Example 10 with AgentWhitelist

use of io.jpom.model.data.AgentWhitelist in project Jpom by dromara.

the class NginxService method list.

public List<JSONObject> list(String whitePath, String fileName, String showAll) {
    AgentWhitelist agentWhitelist = whitelistDirectoryService.getWhitelist();
    if (agentWhitelist == null) {
        return null;
    }
    List<String> ngxDirectory = agentWhitelist.getNginx();
    if (ngxDirectory == null) {
        return null;
    }
    File normalize = FileUtil.file(whitePath, fileName);
    File[] files = FileUtil.ls(normalize.getAbsolutePath());
    if (files == null || files.length <= 0) {
        return null;
    }
    String[] suffixes = Convert.toBool(showAll, false) ? new String[] { ".conf", ".conf_back" } : new String[] { ".conf" };
    return Arrays.stream(files).map(file -> {
        String name = file.getName();
        if (file.isDirectory()) {
            return null;
        }
        if (!StrUtil.endWithAnyIgnoreCase(name, suffixes)) {
            return null;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("path", whitePath);
        long time = file.lastModified();
        jsonObject.put("time", DateUtil.date(time).toString());
        jsonObject.put("name", name);
        jsonObject.put("relativePath", FileUtil.normalize(fileName + StrUtil.SLASH + name));
        try {
            NgxConfig config = NgxConfig.read(file.getPath());
            List<NgxEntry> server = config.findAll(NgxBlock.class, "server");
            JSONObject data = findSeverName(server);
            if (data != null) {
                jsonObject.putAll(data);
            }
        } catch (IOException e) {
            DefaultSystemLog.getLog().error(e.getMessage(), e);
        }
        return jsonObject;
    }).filter(Objects::nonNull).collect(Collectors.toList());
}
Also used : NgxEntry(com.github.odiszapc.nginxparser.NgxEntry) NgxConfig(com.github.odiszapc.nginxparser.NgxConfig) JSONObject(com.alibaba.fastjson.JSONObject) AgentWhitelist(io.jpom.model.data.AgentWhitelist) IOException(java.io.IOException) File(java.io.File)

Aggregations

AgentWhitelist (io.jpom.model.data.AgentWhitelist)11 JSONObject (com.alibaba.fastjson.JSONObject)4 NodeProjectInfoModel (io.jpom.model.data.NodeProjectInfoModel)4 File (java.io.File)4 JSONArray (com.alibaba.fastjson.JSONArray)3 Charset (java.nio.charset.Charset)3 JsonMessage (cn.jiangzeyin.common.JsonMessage)2 CollStreamUtil (cn.hutool.core.collection.CollStreamUtil)1 CollUtil (cn.hutool.core.collection.CollUtil)1 Convert (cn.hutool.core.convert.Convert)1 FileUtil (cn.hutool.core.io.FileUtil)1 ThreadUtil (cn.hutool.core.thread.ThreadUtil)1 ArrayUtil (cn.hutool.core.util.ArrayUtil)1 BooleanUtil (cn.hutool.core.util.BooleanUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 SecureUtil (cn.hutool.crypto.SecureUtil)1 ServletUtil (cn.hutool.extra.servlet.ServletUtil)1 HttpUtil (cn.hutool.http.HttpUtil)1 DefaultSystemLog (cn.jiangzeyin.common.DefaultSystemLog)1 MultipartFileBuilder (cn.jiangzeyin.controller.multipart.MultipartFileBuilder)1