windows hosts file bug fi

This commit is contained in:
sirjonasxx 2018-06-29 10:39:03 +02:00
parent b1964c126e
commit 649fea0f29
3 changed files with 33 additions and 25 deletions

View File

@ -229,7 +229,7 @@ public class HConnection {
handler.act(buffer); handler.act(buffer);
if (!datastream[0] && handler.isDataStream()) { if (!datastream[0] && handler.isDataStream()) {
clientHostAndPort = client.getInetAddress().getHostAddress() + ":" + client.getPort(); clientHostAndPort = client.getInetAddress().getHostAddress() + ":" + client.getPort();
System.out.println(clientHostAndPort); if (DEBUG) System.out.println(clientHostAndPort);
datastream[0] = true; datastream[0] = true;
setState(State.CONNECTED); setState(State.CONNECTED);
onConnect(); onConnect();
@ -359,15 +359,19 @@ public class HConnection {
} }
private void addToHosts() { private void addToHosts() {
String[] lines = new String[input_domain.size()];
for (int i = 0; i < input_domain.size(); i++) { for (int i = 0; i < input_domain.size(); i++) {
hostsReplacer.addRedirect(input_domain.get(i), "127.0.0." + (i+1)); lines[i] = ("127.0.0." + (i+1)) + " " + input_domain.get(i);
} }
hostsReplacer.addRedirect(lines);
hostRedirected = true; hostRedirected = true;
} }
private void removeFromHosts(){ private void removeFromHosts(){
String[] lines = new String[input_domain.size()];
for (int i = 0; i < input_domain.size(); i++) { for (int i = 0; i < input_domain.size(); i++) {
hostsReplacer.removeRedirect(input_domain.get(i), "127.0.0." + (i+1)); lines[i] = ("127.0.0." + (i+1)) + " " + input_domain.get(i);
} }
hostsReplacer.removeRedirect(lines);
hostRedirected = false; hostRedirected = false;
} }

View File

@ -2,8 +2,8 @@ package main.protocol.hostreplacer;
public interface HostReplacer { public interface HostReplacer {
void addRedirect(String original, String redirect); void addRedirect(String[] lines);
void removeRedirect(String original, String redirect); void removeRedirect(String[] lines);
} }

View File

@ -2,6 +2,7 @@ package main.protocol.hostreplacer;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
/** /**
* Created by Jeunez on 04/04/18. * Created by Jeunez on 04/04/18.
@ -15,8 +16,11 @@ class UnixHostReplacer implements HostReplacer {
} }
@Override @Override
public void addRedirect(String original, String redirect) { public void addRedirect(String[] lines) {
String text = redirect + " " + original + "\t# G-Earth replacement"; List<String> adders = new ArrayList<>();
for (int i = 0; i < lines.length; i++) {
adders.add(lines[i] + "\t# G-Earth replacement");
}
FileReader fr = null; FileReader fr = null;
BufferedReader br = null; BufferedReader br = null;
@ -25,17 +29,15 @@ class UnixHostReplacer implements HostReplacer {
try try
{ {
ArrayList<String> lines = new ArrayList<>(); ArrayList<String> fileLines = new ArrayList<>();
File f1 = new File(hostsFileLocation); File f1 = new File(hostsFileLocation);
fr = new FileReader(f1); fr = new FileReader(f1);
br = new BufferedReader(fr); br = new BufferedReader(fr);
String line = null; String line = null;
boolean containmmm = false;
while ((line = br.readLine()) != null) while ((line = br.readLine()) != null)
{ {
if (line.equals(text)) adders.remove(line);
containmmm = true; fileLines.add(line);
lines.add(line);
} }
fr.close(); fr.close();
br.close(); br.close();
@ -43,12 +45,12 @@ class UnixHostReplacer implements HostReplacer {
fw = new FileWriter(f1); fw = new FileWriter(f1);
out = new BufferedWriter(fw); out = new BufferedWriter(fw);
if (!containmmm) { for (String li : adders) {
out.write(text); out.write(li + System.getProperty("line.separator"));
} }
for (int i = 0; i < lines.size(); i++) { for (int i = 0; i < fileLines.size(); i++) {
out.write(((containmmm && i == 0) ? "" : System.getProperty("line.separator"))+ lines.get(i)); out.write(((i == 0) ? "" : System.getProperty("line.separator"))+ fileLines.get(i));
} }
out.flush(); out.flush();
fw.close(); fw.close();
@ -61,8 +63,11 @@ class UnixHostReplacer implements HostReplacer {
} }
@Override @Override
public void removeRedirect(String original, String redirect) { public void removeRedirect(String[] lines) {
String text = redirect + " " + original + "\t# G-Earth replacement"; ArrayList<String> removers = new ArrayList<>();
for (int i = 0; i < lines.length; i++) {
removers.add(lines[i] + "\t# G-Earth replacement");
}
FileReader fr = null; FileReader fr = null;
BufferedReader br = null; BufferedReader br = null;
@ -71,16 +76,15 @@ class UnixHostReplacer implements HostReplacer {
try try
{ {
ArrayList<String> lines = new ArrayList<String>(); ArrayList<String> fileLines = new ArrayList<String>();
File f1 = new File(hostsFileLocation); File f1 = new File(hostsFileLocation);
fr = new FileReader(f1); fr = new FileReader(f1);
br = new BufferedReader(fr); br = new BufferedReader(fr);
String line = null; String line = null;
while ((line = br.readLine()) != null) while ((line = br.readLine()) != null)
{ {
if (!line.equals(text)) if (!removers.contains(line))
lines.add(line); fileLines.add(line);
} }
fr.close(); fr.close();
br.close(); br.close();
@ -88,9 +92,9 @@ class UnixHostReplacer implements HostReplacer {
fw = new FileWriter(f1); fw = new FileWriter(f1);
out = new BufferedWriter(fw); out = new BufferedWriter(fw);
for (int i = 0; i < lines.size(); i++) { for (int i = 0; i < fileLines.size(); i++) {
out.write(lines.get(i)); out.write(fileLines.get(i));
if (i != lines.size() - 1) out.write(System.getProperty("line.separator")); if (i != fileLines.size() - 1) out.write(System.getProperty("line.separator"));
} }
out.flush(); out.flush();
fw.close(); fw.close();