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);
if (!datastream[0] && handler.isDataStream()) {
clientHostAndPort = client.getInetAddress().getHostAddress() + ":" + client.getPort();
System.out.println(clientHostAndPort);
if (DEBUG) System.out.println(clientHostAndPort);
datastream[0] = true;
setState(State.CONNECTED);
onConnect();
@ -359,15 +359,19 @@ public class HConnection {
}
private void addToHosts() {
String[] lines = new String[input_domain.size()];
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;
}
private void removeFromHosts(){
String[] lines = new String[input_domain.size()];
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;
}

View File

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