-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNS.java
More file actions
141 lines (129 loc) · 3.92 KB
/
DNS.java
File metadata and controls
141 lines (129 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package WindowsSettings;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//"runas /profile /user:Administrator \"cmd.exe /c Powrprof.dll,SetSuspendState\""
public class DNS implements Settings {
private String myServer = "";
private Runtime rt = Runtime.getRuntime();
private Map<String, List<String>> routers = new HashMap<>();
private List<String> Allservers = new ArrayList<>();
public DNS() {
initServers(Arrays.asList(new Cloudflare(), new Google(), new OpenDNS(), new Quad9()));
}
/**
* Changes the myServer variable to the server of choosing
* @param path
* @return
*/
public boolean DNSpath(String path) {
path.toLowerCase();
if (routers.containsKey(path)) {
myServer = path;
return true;
}
return false;
}
public List<String> routers(){
return Allservers;
}
/**
* Changes the current DNS to server to a server of choosing
*/
public boolean change() throws IOException {
try {
rt.exec("runas /profile /user:Administrator \"cmd.exe /c Powrprof.dll,SetSuspendState\"");
Process pross = rt.exec("ipconfig");
BufferedReader cmdlines = new BufferedReader(new InputStreamReader(pross.getInputStream()));
String cnName = findMyConnection(cmdlines);
if(!run(cnName)) {
return false;
}
return true;
}
catch(IOException e){
throw new IOException("Cannot find directory");
}
}
/**
* Finds the current internet connection
* @param lines(from ipcondfiguration)
* @return
* @throws IOException
*/
public String findMyConnection(BufferedReader lines) throws IOException{
List<List<String>> cnCentral = new ArrayList<>();
List<String> cnTypes = new ArrayList<>();
cnTypes.add("Ethernet");
cnTypes.add("Wireless");
String connectionname = "";
String s = null;
List<String> cnInfo = new ArrayList<>();
while ((s = lines.readLine()) != null) {
if(cnTypes.contains(s.split(" ")[0])) {
cnCentral.add(cnInfo);
cnInfo= new ArrayList<>();
}
for(String word : s.split(" ")) {
cnInfo.add(word);
}
}
cnCentral.add(cnInfo);
for(List<String> networkInfo: cnCentral) {
if(networkInfo.contains("disconnected") || networkInfo.contains("Local") || networkInfo.contains("Configuration")) {
continue;
}
for(String word : networkInfo) {
if(word.equals("adapter")) {
connectionname = networkInfo.get(networkInfo.indexOf(word)+1);
if(connectionname.contains(":")) {
connectionname = connectionname.substring(0, connectionname.length()-1);
}
else {
connectionname = connectionname + " " + networkInfo.get(networkInfo.indexOf(word)+2).substring(0,
networkInfo.get(networkInfo.indexOf(word)+2).length()-1);
}
break;
}
}
}
return "\""+connectionname + "\"";
}
/**
* Initiate servers that your DNS can be changed to
* @param servers
*/
public void initServers(List<IdnsServers> servers) {
for(IdnsServers server: servers) {
Allservers.add(server.getName());
routers.put(server.getName(), server.getServers());
}
}
/**
* Changes the current DNS server to the server of choosing
* @param connection
* @return
* @throws IOException
*/
public boolean run(String connection) throws IOException {
String Command = "netsh interface ip set dns" + " " + connection + " " + "static " + (routers.get(myServer)).get(0);
Process pross = rt.exec(Command);
BufferedReader lines = new BufferedReader(new InputStreamReader(pross.getInputStream()));
String s = lines.readLine();
if(s.length()>0){
Command = "netsh interface ip set dns" + " " + connection + " " + "static" + " " + (routers.get(myServer)).get(1);
pross = rt.exec(Command);
lines = new BufferedReader(new InputStreamReader(pross.getInputStream()));
s = lines.readLine();
if(s.length()>0) {
return false;
}
}
return true;
}
}