package squid;
import java.util.*;
import java.io.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DISPLAY {
private String fileop=System.getProperty("file.separator");
private String base;
private String file;
private Vector clist;
private String group;
private Process process;
public DISPLAY(){
base=fileop+"usr"+
fileop+"local"+
fileop+"squid"+
fileop+"etc"+
fileop;
file="";
clist=new Vector();
group="";
process=null;
}
/**
* get group where ip belong to
* @param ip
* @return
*/
public String getGIP(String ip){
String aLine="";
String[]sub;
String[] filetemp={"normal","advance","2normal","3normal"};
File fl;
try {
for(int i=0;i<filetemp.length;i++)
{
fl=new File(base+filetemp[i]);
FileReader fr = new FileReader(fl);
BufferedReader br = new BufferedReader(fr);
while ((aLine=br.readLine()) != null ) {
sub=aLine.split("/");
if(sub[0].equals(ip))
group=filetemp[i];
}
br.close();
fr.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace(System.out);
return null;
} catch (IOException e) {
e.printStackTrace(System.out);
return null;
}
return group;
}
/**
*
* get a Client list
* @param newfile
* @return
*/
public Vector getCList(String newfile){
String[] subString,sub;
String nf=newfile;
String aLine="";
try {
file=base+nf;
File fl=new File(file);
FileReader fr = new FileReader(fl);
BufferedReader br = new BufferedReader(fr);
while ((aLine=br.readLine()) != null) {
aLine=aLine.trim();
subString=aLine.split(" ",2);
sub=subString[0].split("/",2);
subString[1]=subString[1].trim();
CLIENT client = new CLIENT();
client.setUser(subString[1]);
client.setIP(sub[0]);
client.setGroup(getGIP(sub[0]));
clist.addElement(client);
}
br.close();
fr.close();
return clist;
} catch (FileNotFoundException e) {
e.printStackTrace(System.out);
return null;
} catch (IOException e) {
e.printStackTrace(System.out);
return null;
}
}
/**
* get some infomation for editing
* @param newip
* @return
*/
public CLIENT getEData(String newip){
String aLine="",user="";
String[] subString;
String[] sub;
String[] pswd;
String[] gpp={"normal","advance","2normal","3normal"};
CLIENT client =new CLIENT();
try {
file=base+"ip_user.conf";
File fl=new File(file);
FileReader fr = new FileReader(fl);
BufferedReader br = new BufferedReader(fr);
while ((aLine=br.readLine()) != null) {
aLine=aLine.trim();
subString=aLine.split(" ",2);
sub=subString[0].split("/",2);
subString[1]=subString[1].trim();
if(sub[0].equals(newip))
client.setUser(subString[1]);
user=subString[1];
client.setIP(newip);
}
br.close();
fr.close();
for(int i=0;i<gpp.length;i++)
{
File fll=new File(base+gpp[i]);
FileReader frr = new FileReader(fll);
BufferedReader brr = new BufferedReader(frr);
while ((aLine=brr.readLine()) != null ) {
sub=aLine.split("/");
if(sub[0].equals(newip))
client.setGroup(gpp[i]);
}
br.close();
fr.close();
}
client.setPasswd("123456");
/* file=base+"passwd";
fl=new File(file);
fr=new FileReader(fl);
br=new BufferedReader(fr);
while ((aLine=br.readLine()) != null) {
aLine=aLine.trim();
pswd=aLine.split(":",2);
if(pswd[0].equals(user))
{
pswd[1]=getFromBASE64(pswd[1]);
client.setPasswd(pswd[1]);
}
}
br.close();
fr.close();*/
} catch (FileNotFoundException e) {
e.printStackTrace(System.out);
return null;
} catch (IOException e) {
e.printStackTrace(System.out);
return null;
}
return client;
}
/**
* enconde and decode
*/
public String getBASE64(String s){
if (s == null) return null;
return (new BASE64Encoder()).encode(s.getBytes());
}
public String getFromBASE64(String s) {
if (s == null) return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
}
}
/**
* query data from file
* @param arg0 for user
* @param arg1 for ip
* @return
*/
public boolean query(String arg0,String arg1){
String aLine="";
String[] subString,sub;
boolean flag=false;
String[] newf={"ip_user.conf","advance","normal","2normal","3normal","passwd"};
try {
for(int i=0;i<newf.length;i++){
file=base+newf[i];
File fl=new File(file);
FileReader fr = new FileReader(fl);
BufferedReader br = new BufferedReader(fr);
while ((aLine=br.readLine()) != null) {
aLine=aLine.trim();
if(newf.equals("ip_user.conf"))
{
subString=aLine.split(" ",2);
sub=subString[0].split("/",2);
subString[1]=subString[1].trim();
if(subString[1].equals(arg0)&&sub[0].equals(arg1))
flag=true;
else
flag=false;
}
if(newf.equals("normal")|| newf.equals("advance")||newf.equals("2normal")||newf.equals("3normal"))
{
sub=aLine.split("/",2);
if(sub[0].equals(arg1))
flag=true;
else
flag=false;
}
if(newf.equals("passwd")){
subString=aLine.split(":",2);
if(subString[0].equals(arg0))
flag=true;
else
flag=false;
}
}
br.close();
fr.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace(System.out);
flag=false;
} catch (IOException e) {
e.printStackTrace(System.out);
flag=false;
}
return flag;
}
/**
* update data
* @param user
* @param pass
* @param ip
* @param group
* @return
*/
public boolean update(String user,String pass,String ip,String group,boolean change){
String[] files={"ip_user.conf","normal","advance","2normal","3normal","passwd"};
String[] sub,subString;
String aLine="";
String file1="";
//Process process;
int sw=0;
if(query(user,ip)==false){
if(group.equals("normal"))
sw=1;
if(group.equals("advance"))
sw=2;
if(group.e