|
| 1 | +package max.plus.frp; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.io.FileOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +public class ConfigFileStore { |
| 14 | + private static final Pattern TOML_PATH_QUOTED = Pattern.compile("(?m)^(\\s*path\\s*=\\s*)\"\\./([^\"]+)\"(\\s*(?:#.*)?)$"); |
| 15 | + private static final Pattern TOML_PATH_RAW = Pattern.compile("(?m)^(\\s*path\\s*=\\s*)\\./([^\\s#;]+)(\\s*(?:#.*)?)$"); |
| 16 | + private static final Pattern JSON_YAML_PATH = Pattern.compile("(?m)^(\\s*\"?path\"?\\s*:\\s*)\"\\./([^\"]+)\"(\\s*,?\\s*)$"); |
| 17 | + |
| 18 | + private static final String DIR_FRPC = "frpc"; |
| 19 | + private static final String DIR_FRPS = "frps"; |
| 20 | + |
| 21 | + private ConfigFileStore() { |
| 22 | + } |
| 23 | + |
| 24 | + public static File getConfigFile(Context context, String type, String uid, String name, String format) { |
| 25 | + String safeType = "frps".equalsIgnoreCase(type) ? DIR_FRPS : DIR_FRPC; |
| 26 | + String safeFormat = ConfigFormatUtils.normalizeFormat(format); |
| 27 | + File typeRoot = new File(context.getFilesDir(), safeType); |
| 28 | + if (!typeRoot.exists()) { |
| 29 | + typeRoot.mkdirs(); |
| 30 | + } |
| 31 | + |
| 32 | + String safeUid = sanitizeFileName(uid); |
| 33 | + if (safeUid.isEmpty()) { |
| 34 | + safeUid = "unknown"; |
| 35 | + } |
| 36 | + File uidDir = new File(typeRoot, safeUid); |
| 37 | + if (!uidDir.exists()) { |
| 38 | + uidDir.mkdirs(); |
| 39 | + } |
| 40 | + |
| 41 | + String baseName = sanitizeFileName(name); |
| 42 | + if (baseName.isEmpty()) { |
| 43 | + baseName = DIR_FRPS.equals(safeType) ? "frps" : "frpc"; |
| 44 | + } |
| 45 | + // DB 中 name 不带格式;落盘文件名统一拼接 format 后缀 |
| 46 | + return new File(uidDir, baseName + "." + safeFormat); |
| 47 | + } |
| 48 | + |
| 49 | + public static File writeConfigAtomic(Context context, String type, String uid, String name, String format, String content) throws IOException { |
| 50 | + File target = getConfigFile(context, type, uid, name, format); |
| 51 | + File parent = target.getParentFile(); |
| 52 | + if (parent != null && !parent.exists()) { |
| 53 | + parent.mkdirs(); |
| 54 | + } |
| 55 | + String normalized = normalizeStorePath(content == null ? "" : content, parent); |
| 56 | + // 内容一致时直接复用,避免不必要的写盘与 FileObserver 触发 |
| 57 | + if (target.exists()) { |
| 58 | + String oldContent = readUtf8(target); |
| 59 | + if (oldContent.equals(normalized)) { |
| 60 | + return target; |
| 61 | + } |
| 62 | + } |
| 63 | + writeExistingFileAtomic(target, normalized); |
| 64 | + return target; |
| 65 | + } |
| 66 | + |
| 67 | + public static String readUtf8(File file) throws IOException { |
| 68 | + byte[] buf = new byte[(int) file.length()]; |
| 69 | + try (FileInputStream fis = new FileInputStream(file)) { |
| 70 | + int read = fis.read(buf); |
| 71 | + if (read <= 0) { |
| 72 | + return ""; |
| 73 | + } |
| 74 | + return new String(buf, 0, read, StandardCharsets.UTF_8); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static void writeExistingFileAtomic(File target, String content) throws IOException { |
| 79 | + File parent = target.getParentFile(); |
| 80 | + if (parent != null && !parent.exists()) { |
| 81 | + parent.mkdirs(); |
| 82 | + } |
| 83 | + File temp = new File(parent, target.getName() + ".tmp"); |
| 84 | + try (FileOutputStream fos = new FileOutputStream(temp, false)) { |
| 85 | + byte[] data = (content == null ? "" : content).getBytes(StandardCharsets.UTF_8); |
| 86 | + fos.write(data); |
| 87 | + fos.flush(); |
| 88 | + } |
| 89 | + if (target.exists() && !target.delete()) { |
| 90 | + throw new IOException("Failed to replace old config file: " + target.getAbsolutePath()); |
| 91 | + } |
| 92 | + if (!temp.renameTo(target)) { |
| 93 | + throw new IOException("Failed to rename temp config file: " + temp.getAbsolutePath()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static String normalizeStorePath(String content, File configDir) { |
| 98 | + if (content == null || content.isEmpty() || configDir == null) { |
| 99 | + return content == null ? "" : content; |
| 100 | + } |
| 101 | + String normalized = content; |
| 102 | + normalized = replaceWithAbsolute(TOML_PATH_QUOTED, normalized, configDir); |
| 103 | + normalized = replaceWithAbsolute(TOML_PATH_RAW, normalized, configDir); |
| 104 | + normalized = replaceWithAbsolute(JSON_YAML_PATH, normalized, configDir); |
| 105 | + return normalized; |
| 106 | + } |
| 107 | + |
| 108 | + private static String replaceWithAbsolute(Pattern pattern, String source, File configDir) { |
| 109 | + Matcher matcher = pattern.matcher(source); |
| 110 | + StringBuffer sb = new StringBuffer(); |
| 111 | + while (matcher.find()) { |
| 112 | + String prefix = matcher.group(1); |
| 113 | + String relative = matcher.group(2); |
| 114 | + String suffix = matcher.group(3); |
| 115 | + File abs = new File(configDir, relative); |
| 116 | + String absPath = abs.getAbsolutePath().replace("\\", "/"); |
| 117 | + String replacement = prefix + "\"" + absPath + "\"" + suffix; |
| 118 | + matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement)); |
| 119 | + } |
| 120 | + matcher.appendTail(sb); |
| 121 | + return sb.toString(); |
| 122 | + } |
| 123 | + |
| 124 | + private static String sanitizeFileName(String name) { |
| 125 | + if (name == null) { |
| 126 | + return ""; |
| 127 | + } |
| 128 | + String n = name.trim(); |
| 129 | + n = n.replaceAll("[\\\\/:*?\"<>|]", "_"); |
| 130 | + n = n.replaceAll("\\s+", "_"); |
| 131 | + return n; |
| 132 | + } |
| 133 | +} |
0 commit comments