Java tutorial
/* LICENSE : MIT * * PROJET : lanceur.jar * NOM : Auth.java * MAJ : 13/07/2014 * * AUTEUR : Tutul */ package be.tutul.naheulcraft.launcher.auth; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.util.Random; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; import org.apache.commons.codec.binary.Base64; import be.tutul.naheulcraft.launcher.Launcher; import be.tutul.naheulcraft.launcher.Variables; import be.tutul.naheulcraft.launcher.util.Logger; public class Auth { private final Launcher launcher; private final Logger logger; private final LastLogin lastLogin; private String[] knowUser; private int knowUserNbr; public Auth(Launcher launcher) { this.launcher = launcher; this.logger = this.launcher.getLogger(); this.lastLogin = new LastLogin(launcher); } public Launcher getLauncher() { return this.launcher; } public LastLogin getLastLoginManager() { return this.lastLogin; } public int getKnowUserNbr() { return this.knowUserNbr; } public String[] getKnowUser() { if ((this.knowUser != null) && (this.knowUser.length > 0)) { return this.knowUser; } File save = new File(this.launcher.getWorkingDirectory(), Variables.save); if (!save.exists()) { save.mkdir(); this.knowUserNbr = 0; } else { String[] listefichiers; String a = ""; // Permet de construire la liste listefichiers = save.list(); int j = 0; for (int i = 0; i < listefichiers.length; i++) { if (listefichiers[i].startsWith("nc-") == true) { String[] c = listefichiers[i].split("nc-"); a += c[1] + "<>"; j++; } } this.knowUser = a.split("<>"); this.knowUserNbr = j; } return this.knowUser; } // Systme de chiffrage du mot de passe private Cipher getCipher(int paramInt, String key) throws Exception { Random random = new Random(43287234L); byte[] salt = new byte[8]; random.nextBytes(salt); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 5); SecretKey pbeKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES") .generateSecret(new PBEKeySpec(key.toCharArray())); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(paramInt, pbeKey, pbeParamSpec); return cipher; } /** Ecriture en base64 * @param msg : le string traiter (en clair) * @param key : la cl * @return String : la rponse en base64 */ private String crypt(String msg, String key) throws Exception { Cipher localCipher = getCipher(1, key); return new String(Base64.encodeBase64(localCipher.doFinal(msg.getBytes()))); } /** Lecture en base64 * @param msg : le string traiter (en base64) * @param key : la cl * @return String : la rponse en clair */ private String decrypt(String msg, String key) throws Exception { Cipher localCipher = getCipher(2, key); return new String(localCipher.doFinal(Base64.decodeBase64(msg)), "UTF-8"); } // Porgramme qui gre la tentative de login public void logIn() { new Thread() { private Socket ss; private BufferedReader in; private PrintWriter out; public void run() { try { InetAddress adresse = InetAddress.getByName("naheulcraft.be"); // Rcupre l'adresse IP String IP = adresse.toString().split("/")[1]; Auth.this.logger.info("Connexion " + IP + ":" + Variables.PORT); this.ss = new Socket(IP, Variables.PORT); this.in = new BufferedReader(new InputStreamReader(this.ss.getInputStream())); this.out = new PrintWriter(this.ss.getOutputStream()); // On signale au serveur qu'on demande une authentification this.out.println("<start>"); this.out.flush(); String ok = this.in.readLine(); if (!ok.contains("<ok>")) { Auth.this.launcher.getPanel().getLogInPopup().setText("Rponse innatendu du serveur"); Auth.this.launcher.getPanel().getLogInPopup().setCanLogIn(true); Auth.this.launcher.getPanel().showLoginPrompt(); return; } Auth.this.logger.info("Connexion au service de login tablie"); String key = ok.split("<ok>")[1]; // On rcure la cl String msg = Auth.this.launcher.getUser().getPseudo() + ":" + Auth.this.launcher.getUser().getPass() + ":" + Variables.version + "<end>"; System.err.println("A : " + msg);//TODO msg = crypt(msg, key); // On crypt en base 64 // On lui envoit la requte Auth.this.logger.info("Vrification du login en cours..."); System.err.println("B : " + msg);//TODO this.out.println(msg); this.out.flush(); String login = this.in.readLine(); login = decrypt(login, key); // On dcrypt en string System.err.println("C : " + login);//TODO // On ferme le socket this.in.close(); this.out.close(); this.ss.close(); String ret; if (login != null) { if (login.contains("pass")) { ret = "Mauvais mot de passe"; Auth.this.launcher.getUser().setLogin(false); Auth.this.logger.warning(ret); Auth.this.launcher.getPanel().getLogInPopup().setText(ret); Auth.this.launcher.getPanel().showLoginPrompt(); return; } else if (login.contains("user")) { ret = "Mauvais pseudo"; Auth.this.launcher.getUser().setLogin(false); Auth.this.logger.warning(ret); Auth.this.launcher.getPanel().getLogInPopup().setText(ret); Auth.this.launcher.getPanel().showLoginPrompt(); return; } else if (login.contains("statut")) { ret = "Compte n'ont autoris"; Auth.this.launcher.getUser().setLogin(false); Auth.this.logger.warning(ret); Auth.this.launcher.getPanel().getLogInPopup().setText(ret); Auth.this.launcher.getPanel().showLoginPrompt(); return; } else if (login.contains("encodage")) { ret = "Problme dans la demande de login"; Auth.this.launcher.getUser().setLogin(false); Auth.this.logger.error(ret); Auth.this.launcher.getPanel().getLogInPopup().setText(ret); Auth.this.launcher.getPanel().showLoginPrompt(); return; } else if (login.contains("serveur")) { ret = "Erreur du service de login"; Auth.this.launcher.getUser().setLogin(false); Auth.this.logger.error(ret); Auth.this.launcher.getPanel().getLogInPopup().setText(ret); Auth.this.launcher.getPanel().showLoginPrompt(); return; } else if (login.contains("<ok>")) { String id = login.split("<end>")[0].split("<ok>")[1]; Auth.this.launcher.getUser().setID(id); Auth.this.launcher.getUser().setLogin(true); Auth.this.logger.info("Login vrifier avec succs"); Auth.this.logger.info("ID de connexion : " + id); Auth.this.lastLogin.writeUsername(); Auth.this.launcher.getPanel().showPanel(); return; } else { Auth.this.logger.error(login); Auth.this.launcher.getUser().setLogin(false); Auth.this.launcher.getPanel().getLogInPopup().setText("Erreur inconnue"); Auth.this.launcher.getPanel().showLoginPrompt(); return; } } Auth.this.logger.error("Pas de rponse du serveur"); Auth.this.launcher.getUser().setLogin(false); Auth.this.launcher.getPanel().getLogInPopup().setText("Pas de rponse"); Auth.this.launcher.getPanel().showLoginPrompt(); } catch (Exception e) { Auth.this.logger.fatal("Impossible de communiquer avec le service de login", e); Auth.this.launcher.getUser().setLogin(false); Auth.this.launcher.getPanel().getLogInPopup().setText("Problme de communication"); Auth.this.launcher.getPanel().showLoginPrompt(); try { // On tente quand mme de couper le socket this.in.close(); this.out.close(); this.ss.close(); } catch (Exception ingnored) { } } } }.start(); } }