Skip to content

Commit 98e1476

Browse files
committed
fix: 覆盖hosts中的值
Signed-off-by: 何国健 <1664931260@qq.com>
1 parent 3c1db5d commit 98e1476

5 files changed

Lines changed: 39 additions & 51 deletions

File tree

src/main/java/com/sololn/fastergithub/Starter.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.springframework.boot.CommandLineRunner;
66
import org.springframework.stereotype.Component;
77

8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
810
import java.util.Map;
911

1012
/**
@@ -18,9 +20,11 @@
1820
public class Starter implements CommandLineRunner {
1921
@Override
2022
public void run(String... args) throws Exception {
21-
Map<String, String> stringStringMap = IpUtil.readIpMap("https://myssl.com/api/v1/tools/dns_query?qtype=1&host=", "qmode=-1");
22-
HostUtil.toFile(stringStringMap);
23-
/*Map<String, String> ips = Inet4Address.getIps();
24-
HostUtil.buildContent(ips);*/
23+
String url = "https://myssl.com/api/v1/tools/dns_query?qtype=1&host=";
24+
String params = "qmode=-1";
25+
Path outPath = Paths.get(System.getProperty("user.dir") ,"hosts");
26+
27+
Map<String, String> stringStringMap = IpUtil.readIpMap(url, params);
28+
HostUtil.toFile(stringStringMap, outPath);
2529
}
2630
}

src/main/java/com/sololn/fastergithub/pojo/HttpResponse.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/java/com/sololn/fastergithub/pojo/IPPojo.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
/**
44
* @ClassName pojo
5-
* @Description TODO
65
* @Author HeGuojian
76
* @Date 2021/9/8 15:53
87
* @Version 1.0

src/main/java/com/sololn/fastergithub/util/HostUtil.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@
1515

1616
/**
1717
* @ClassName HostUtil
18-
* @Description TODO
1918
* @Author HeGuojian
2019
* @Date 2021/8/30 17:14
2120
* @Version 1.0
2221
**/
2322
public class HostUtil {
24-
private static final Path outPath ;
25-
26-
static {
27-
outPath = Paths.get(System.getProperty("user.dir") ,"hosts");
23+
24+
public static void toFile(Map<String, String> stringStringMap, Path path) throws IOException {
25+
write(buildContent(stringStringMap), path);
2826
}
29-
private static List<String> buildContent(Map<String, String> ips) throws IOException {
27+
private static List<String> buildContent(Map<String, String> ips) {
3028
//C:\Windows\System32\drivers\etc
3129
Path inPath = Paths.get("C:", "Windows", "System32", "drivers", "etc", "hosts");
3230
List<String> content = new ArrayList<>();
@@ -61,12 +59,17 @@ private static List<String> buildContent(Map<String, String> ips) throws IOExcep
6159
}
6260
continue;
6361
}
64-
// todo 读取原github配置,并用新的值去覆盖
62+
/*
63+
* 用ips的值覆盖hosts的值
64+
* */
65+
ips.forEach((key, value) -> {
66+
contentGithub.put(key, value);
67+
});
6568
List<String> contentStr = new ArrayList<>();
6669
// 添加 github 的配置
6770
contentStr.add("#github-faster 配置 #");
6871
// map 转 string
69-
ips.forEach((key, value) -> {
72+
contentGithub.forEach((key, value) -> {
7073
StringBuffer tap = new StringBuffer();
7174
int num = 30 - value.length();
7275
while (num > 0){
@@ -82,22 +85,22 @@ private static List<String> buildContent(Map<String, String> ips) throws IOExcep
8285
return content;
8386
}
8487

85-
private static void openFolder() {
88+
private static void openFolder(Path path) {
8689
try {
8790
String[] cmd = new String[5];
8891
cmd[0] = "cmd";
8992
cmd[1] = "/c";
9093
cmd[2] = "start";
9194
cmd[3] = " ";
92-
cmd[4] = outPath.toString();
95+
cmd[4] = path.toString();
9396
Runtime.getRuntime().exec(cmd);
9497
} catch (IOException e) {
9598
e.printStackTrace();
9699
}
97100
}
98101

99-
public static void write(List<String> content) throws IOException {
100-
File outDir = outPath.toFile();
102+
public static void write(List<String> content, Path path) throws IOException {
103+
File outDir = path.toFile();
101104
if (!outDir.exists()){
102105
outDir.mkdirs();
103106
}
@@ -106,21 +109,18 @@ public static void write(List<String> content) throws IOException {
106109
hosts.createNewFile();
107110
}
108111
Files.write(hosts.toPath(),content);
109-
}
110-
111-
private static void unlockInWin(File file) {
112-
String cmd = "C:\\myProgram\\fast-github\\cmd\\hostWritable";
113-
try {
114-
Process p = Runtime.getRuntime().exec(cmd);
115-
p.waitFor();
116-
} catch (IOException | InterruptedException e) {
117-
throw new RuntimeException(e);
118-
}
112+
openFolder(path);
119113
}
120114

121-
public static void toFile(Map<String, String> stringStringMap) throws IOException {
122-
List<String> content = buildContent(stringStringMap);
123-
write(content);
124-
openFolder();
125-
}
115+
116+
117+
/* private static void unlockInWin(File file) {
118+
String cmd = "C:\\myProgram\\fast-github\\cmd\\hostWritable";
119+
try {
120+
Process p = Runtime.getRuntime().exec(cmd);
121+
p.waitFor();
122+
} catch (IOException | InterruptedException e) {
123+
throw new RuntimeException(e);
124+
}
125+
}*/
126126
}

src/main/java/com/sololn/fastergithub/util/Inet4Address.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@
1111
import java.net.URLConnection;
1212
import java.security.KeyManagementException;
1313
import java.security.NoSuchAlgorithmException;
14-
import java.util.HashMap;
15-
import java.util.Map;
16-
import java.util.regex.Matcher;
17-
import java.util.regex.Pattern;
1814

1915
/**
2016
* @ClassName getIP
21-
* @Description TODO
2217
* @Author HeGuojian
2318
* @Date 2021/8/30 16:57
2419
* @Version 1.0
@@ -87,7 +82,7 @@ public static String sendGet(String url) {
8782

8883

8984

90-
public static String getIpFromJson(String url){
85+
/*public static String getIpFromJson(String url){
9186
return sendGet(url);
9287
}
9388
public static Map<String, String> getIps(){
@@ -127,10 +122,10 @@ public static Map<String, String> getIps(){
127122
Map<String, String> ips = new HashMap<>();
128123
for (String s : urls){
129124
String response = Inet4Address.sendGet(s);
130-
/*String ip = Inet4Address.getIpFromStr(response);
125+
*//*String ip = Inet4Address.getIpFromStr(response);
131126
if (null == ip){
132127
continue;
133-
}*/
128+
}*//*
134129
if ("".equals(response)){
135130
continue;
136131
}
@@ -160,5 +155,5 @@ public static String getIpFromStr (String response){
160155
}
161156
return null;
162157
}
163-
158+
*/
164159
}

0 commit comments

Comments
 (0)