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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
| public class Controleur {
private Connexion telnet;
private Connexion telnet2;
private String cheminLog;
private String cheminConf;
private List<Acces> listAcces;
private boolean mode_default;
public Controleur() {
telnet = new Connexion( "IP_ACCES", "LOGIN", "MDP",'PROMPT');
telnet2 = new Connexion( "IP_FIREWALL", "LOGIN", "MDP", 'PROMPT1', 'PROMPT2');
cheminLog = "CHEMIN_FICHIER_LOG";
cheminConf = "CHEMIN_FICHIER_CONF";
mode_default = false;
try {
listAcces = getConfAcces();
} catch (IOException e) {
listAcces = getDefaultAcces();
mode_default = true;
e.printStackTrace();
}
}
public boolean getModeDefault(){
return this.mode_default;
}
public String getCheminLog(){
return this.cheminLog;
}
public List<Acces> getListAcces(){
return this.listAcces;
}
public class Acces{
private String label ="";
private String connexion ="";
private EtatVPN etat;
private String[] ports;
private JRadioButton[] boutons;
private ButtonGroup groupeBoutons;
public Acces(String l, String type_connexion, String... num_ports){
this.label = l;
this.etat = new EtatVPN();
this.connexion = type_connexion;
this.ports = num_ports;
this.boutons = new JRadioButton[2];
this.boutons[0]= new JRadioButton("Ouvert");
this.boutons[1]= new JRadioButton("Fermé");
this.boutons[0].setBackground(new Color(255,228,202));
this.boutons[1].setBackground(new Color(255,228,202));
this.groupeBoutons =new ButtonGroup();
this.groupeBoutons.add(this.boutons[0]);
this.groupeBoutons.add(this.boutons[1]);
}
public EtatVPN getEtat(){
return this.etat;
}
public String getLabel(){
return label;
}
public String getConnexion(){
return connexion;
}
public String[] getPorts(){
return ports;
}
public JRadioButton[] getBoutons(){
return boutons;
}
}
private List<Acces> getDefaultAcces(){
List<Acces> listeAcc = new ArrayList<Acces>();
listeAcc.add(new Acces("Acces1" , "ACCES" , "14","17"));
listeAcc.add(new Acces("Acces2" , "ACCES" , "16","19"));
listeAcc.add(new Acces("Acces3" , "ACCES" , "23"));
listeAcc.add(new Acces("Acces4" , "ACCES" , "24"));
listeAcc.add(new Acces("FirewallA" , "FIREWALL" , "26"));
listeAcc.add(new Acces("FirewallB" , "FIREWALL" , "33"));
listeAcc.add(new Acces("FirewallC" , "FIREWALL" , "25"));
return listeAcc;
}
private List<Acces> getConfAcces() throws IOException{
List<Acces> listeAcc = new ArrayList<Acces>();
ArrayList<String> tmp = null;
String line = "";
BufferedReader bis = new BufferedReader(new FileReader(this.cheminConf));
while((line = bis.readLine()) != null){
tmp = new ArrayList<String>();
for(String s : line.split(",")){
tmp.add(s);
}
String[] lineTab = new String[tmp.size()];
lineTab = tmp.toArray(lineTab);
String[] portsTab = new String[lineTab.length-2];
for(int i=2 ; i<lineTab.length ; i++){
portsTab[i-2]=lineTab[i];
}
Acces acc = new Acces(lineTab[0],lineTab[1],portsTab);
listeAcc.add(acc);
}
bis.close();
return listeAcc;
}
private void envoiTelnetCommande(Acces acc, String statut){
switch(acc.getConnexion()){
case "ACCES":
for(int i=0 ; i<acc.getPorts().length ; i++){
telnet.sendCommand( "config firewall policy");
telnet.sendCommand( "edit "+acc.getPorts()[i]);
telnet.sendCommand( "set status "+statut);
telnet.sendCommand( "end");
}
break;
case "FIREWALL":
for(int i=0 ; i<acc.getPorts().length ; i++){
telnet2.sendCommand2( "configure terminal");
telnet2.sendCommand2( "interface "+acc.getPorts()[i]);
telnet2.sendCommand2( statut);
telnet2.sendCommand2( "exit");
telnet2.sendCommand2( "exit");
}
break;
}
}
public boolean getEtatAcces(Acces acc){
switch(acc.getConnexion()){
case "ACCES":
for(int i=0 ; i<acc.getPorts().length ; i++){
String s = telnet.sendCommand( "show firewall policy "+ acc.getPorts()[i]);
if(s.indexOf("disable")!= -1){
return false;
}
}
return true;
case "FIREWALL":
for(int i=0 ; i<acc.getPorts().length ; i++){
String s = telnet.sendCommand( "show interface "+ acc.getPorts()[i] + " status");
if(s.indexOf("disable")!= -1){
return false;
}
}
return true;
}
return false;
}
public void disconnect(){
telnet.disconnect();
telnet2.disconnect();
}
public void ouvrirAcces(Acces acc){
String statut = "";
String connexion = acc.getConnexion();;
switch(connexion){
case "ACCES":
statut = "enable";
envoiTelnetCommande(acc,statut);break;
case "FIREWALL":
statut = "no shutdown";
envoiTelnetCommande(acc,statut);break;
}
}
public void fermerAcces(Acces acc){
String statut = "";
String connexion = acc.getConnexion();
switch(connexion){
case "ACCES":
statut = "disable";
envoiTelnetCommande(acc,statut);break;
case "FIREWALL":
statut = "shutdown";
envoiTelnetCommande(acc,statut);break;
}
}
public void actualiserEtat(Acces acc, boolean etatConnexion){
acc.etat.setEtat(etatConnexion);acc.etat.repaint();
}
public void actualiserBoutons(Acces acc, boolean etatConnexion){
acc.boutons[0].setSelected(etatConnexion == acc.boutons[0].getText().equalsIgnoreCase("Ouvert"));
acc.boutons[1].setSelected(etatConnexion == acc.boutons[1].getText().equalsIgnoreCase("Ouvert"));
}
}
class GestionActionListener implements ActionListener{
private Controleur firewall;
private Controleur.Acces acces;
private String textBouton;
public GestionActionListener(Controleur gaf, Controleur.Acces gafa, String s){
firewall = gaf;
acces = gafa;
textBouton = s;
}
public void actionPerformed(ActionEvent e) {
switch(textBouton){
case "Ouvert" : firewall.ouvrirAcces(acces);
firewall.actualiserEtat(acces,true);
try {
ecrireLog();
} catch (IOException e1) {
e1.printStackTrace();
}
break;
case "Fermé" : firewall.fermerAcces(acces);
firewall.actualiserEtat(acces,false);
try {
ecrireLog(
);
} catch (IOException e1) {
e1.printStackTrace();
}
break;
}
}
public void ecrireLog() throws IOException{
Calendar c = Calendar.getInstance();
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String ip;
try {
ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
ip = "IP INCONNUE";
}
String acc = acces.getLabel();
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(firewall.getCheminLog() , true)));
pw.println(ip+" - "+df.format(c.getTime())+" - "+acc+" "+textBouton);
pw.close();
}
}
class GestionActionRefreshListener implements ActionListener{
Controleur firewall;
public GestionActionRefreshListener(Controleur gaf){
firewall = gaf;
}
public void actionPerformed(ActionEvent e) {
for(int i=0 ; i<firewall.getListAcces().size() ; i++){
boolean etatConnexion = firewall.getEtatAcces(firewall.getListAcces().get(i));
firewall.actualiserEtat(firewall.getListAcces().get(i),etatConnexion);
firewall.actualiserBoutons(firewall.getListAcces().get(i), etatConnexion);
}
}
}
class GestionWindowListener implements WindowListener {
Controleur firewall;
public GestionWindowListener(Controleur gaf){
firewall = gaf;
}
public void windowActivated(WindowEvent arg0) {}
public void windowClosed(WindowEvent arg0) {}
public void windowClosing(WindowEvent arg0) {
firewall.disconnect();
}
public void windowDeactivated(WindowEvent arg0){}
public void windowDeiconified(WindowEvent arg0){}
public void windowIconified(WindowEvent arg0){}
public void windowOpened(WindowEvent arg0){}
}
class EtatVPN extends JPanel{
private static final long serialVersionUID = 1L;
private boolean etatAcces;
public void paintComponent(Graphics g){
g.setColor(Color.white);
g.fillRect(5, 5, this.getWidth()/2, this.getHeight()/2);
if(etatAcces){
g.setColor(Color.GREEN);
}else{
g.setColor(Color.RED);
}
g.fillRect(5, 5, this.getWidth()/2, this.getHeight()/2);;
}
public Dimension getPreferredSize(){
return new Dimension(30,30);
}
public void setEtat(boolean b){
etatAcces = b;
}
} |
Partager