Advertisement
Guest User

Untitled

a guest
Aug 11th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.01 KB | None | 0 0
  1. package org.rsbot.gui;
  2.  
  3. import org.rsbot.util.GlobalConfiguration;
  4.  
  5. import javax.swing.*;
  6. import javax.swing.event.ListSelectionEvent;
  7. import javax.swing.event.ListSelectionListener;
  8. import javax.swing.table.AbstractTableModel;
  9. import javax.swing.table.DefaultTableCellRenderer;
  10. import javax.swing.table.TableColumnModel;
  11. import java.awt.*;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.io.*;
  15. import java.net.InetAddress;
  16. import java.net.NetworkInterface;
  17. import java.security.MessageDigest;
  18. import java.security.NoSuchAlgorithmException;
  19. import java.util.Iterator;
  20. import java.util.Map;
  21. import java.util.TreeMap;
  22. import java.util.logging.Logger;
  23.  
  24. /**
  25. * @author Tekk
  26. * @author Jacmob
  27. * @author Aion
  28. */
  29. @SuppressWarnings("serial")
  30. public class AccountManager extends JDialog implements ActionListener {
  31.  
  32. private static final String FILE_NAME = GlobalConfiguration.Paths.getAccountsFile();
  33.  
  34. private static final String[] RANDOM_REWARDS = {"Cash", "Runes", "Coal",
  35. "Essence", "Ore", "Bars", "Gems", "Herbs", "Seeds", "Charms",
  36. "Surprise", "Emote", "Costume", "Attack", "Defence", "Strength",
  37. "Constitution", "Range", "Prayer", "Magic", "Cooking",
  38. "Woodcutting", "Fletching", "Fishing", "Firemaking", "Crafting",
  39. "Smithing", "Mining", "Herblore", "Agility", "Thieving", "Slayer",
  40. "Farming", "Runecrafting", "Hunter", "Construction", "Summoning",
  41. "Dungeoneering"};
  42.  
  43. private static final String[] VALID_KEYS = {"password", "pin", "reward", "member", "take_breaks"};
  44.  
  45. private static Map<String, Map<String, String>> accounts;
  46.  
  47. private static final Logger log = Logger.getLogger(AccountManager.class
  48. .getName());
  49.  
  50. private static String key;
  51.  
  52. static {
  53. try {
  54. final InetAddress address = InetAddress.getLocalHost();
  55. final NetworkInterface ni = NetworkInterface
  56. .getByInetAddress(address);
  57. AccountManager.key = new String(ni.getHardwareAddress());
  58. } catch (final Exception e) {
  59. AccountManager.key = System.getProperty("user.name")
  60. + System.getProperty("user.language");
  61. }
  62. AccountManager.accounts = AccountManager.loadAccounts();
  63. }
  64.  
  65. private static class RandomRewardEditor extends DefaultCellEditor {
  66. public RandomRewardEditor() {
  67. super(new JComboBox(RANDOM_REWARDS));
  68. }
  69. }
  70.  
  71. private static class PasswordCellEditor extends DefaultCellEditor {
  72. public PasswordCellEditor() {
  73. super(new JPasswordField());
  74. }
  75. }
  76.  
  77. private static class PasswordCellRenderer extends DefaultTableCellRenderer {
  78. @Override
  79. protected void setValue(Object value) {
  80. if (value == null) {
  81. setText("<none>");
  82. } else {
  83. String str = value.toString();
  84. StringBuilder b = new StringBuilder();
  85. for (int i = 0; i < str.length(); ++i) {
  86. b.append("*");
  87. }
  88. setText(b.toString());
  89. }
  90. }
  91. }
  92.  
  93. private class TableSelectionListener implements ListSelectionListener {
  94. public void valueChanged(ListSelectionEvent evt) {
  95. int row = table.getSelectedRow();
  96. if (!evt.getValueIsAdjusting()) {
  97. removeButton.setEnabled(row >= 0 && row < table.getRowCount());
  98. }
  99. }
  100. }
  101.  
  102. private class AccountTableModel extends AbstractTableModel {
  103.  
  104. public int getRowCount() {
  105. return accounts.size();
  106. }
  107.  
  108. public int getColumnCount() {
  109. return VALID_KEYS.length + 1;
  110. }
  111.  
  112. public Object getValueAt(int row, int column) {
  113. if (column == 0) {
  114. return userForRow(row);
  115. } else {
  116. Map<String, String> acc = accounts.get(userForRow(row));
  117. if (acc != null) {
  118. String str = acc.get(VALID_KEYS[column - 1]);
  119. if (str == null || str.isEmpty()) {
  120. return null;
  121. }
  122. if (getColumnClass(column) == Boolean.class) {
  123. return Boolean.parseBoolean(str);
  124. } else if (getColumnClass(column) == Integer.class) {
  125. return Integer.parseInt(str);
  126. } else {
  127. return str;
  128. }
  129. }
  130. }
  131. return null;
  132. }
  133.  
  134. @Override
  135. public String getColumnName(int column) {
  136. if (column == 0) {
  137. return "Username";
  138. }
  139. String str = VALID_KEYS[column - 1];
  140. StringBuilder b = new StringBuilder();
  141. boolean space = true;
  142. for (char c : str.toCharArray()) {
  143. if (c == '_') {
  144. c = ' ';
  145. }
  146. b.append(space ? Character.toUpperCase(c) : c);
  147. space = c == ' ';
  148. }
  149. return b.toString();
  150. }
  151.  
  152. @Override
  153. public Class<?> getColumnClass(int column) {
  154. if (getColumnName(column).equals("Member")) {
  155. return Boolean.class;
  156. }
  157. if (getColumnName(column).equals("Take Breaks")) {
  158. return Boolean.class;
  159. }
  160. return Object.class;
  161. }
  162.  
  163. @Override
  164. public boolean isCellEditable(int row, int column) {
  165. return column > 0;
  166. }
  167.  
  168. @Override
  169. public void setValueAt(Object value, int row, int column) {
  170. Map<String, String> acc = accounts.get(userForRow(row));
  171. if (acc == null) {
  172. return;
  173. }
  174. acc.put(getColumnName(column).toLowerCase().replace(' ', '_'),
  175. String.valueOf(value));
  176. fireTableCellUpdated(row, column);
  177. }
  178.  
  179. public String userForRow(int row) {
  180. Iterator<String> it = accounts.keySet().iterator();
  181. for (int k = 0; it.hasNext() && k < row; k++) {
  182. it.next();
  183. }
  184. if (it.hasNext()) {
  185. return it.next();
  186. }
  187. return null;
  188. }
  189.  
  190. }
  191.  
  192. private JTable table;
  193. private JButton removeButton;
  194.  
  195. private AccountManager() {
  196. super(Frame.getFrames()[0], "Account Manager", true);
  197. }
  198.  
  199. public void actionPerformed(ActionEvent e) {
  200. if (e.getSource() instanceof JButton) {
  201. String label = ((JButton) e.getSource()).getText();
  202. if (label.equals("Done")) {
  203. saveAccounts();
  204. dispose();
  205. } else if (label.equals("Add")) {
  206. String str = JOptionPane.showInputDialog(getParent(),
  207. "Enter the account username.", "New Account",
  208. JOptionPane.QUESTION_MESSAGE);
  209. if (str == null || str.isEmpty()) {
  210. return;
  211. }
  212. accounts.put(str, new TreeMap<String, String>());
  213. accounts.get(str).put("reward", RANDOM_REWARDS[0]);
  214. int row = table.getRowCount();
  215. ((AccountTableModel) table.getModel()).fireTableRowsInserted(
  216. row, row);
  217. } else if (label.equals("Remove")) {
  218. int row = table.getSelectedRow();
  219. String user = ((AccountTableModel) table.getModel())
  220. .userForRow(row);
  221. if (user != null) {
  222. accounts.remove(user);
  223. ((AccountTableModel) table.getModel())
  224. .fireTableRowsDeleted(row, row);
  225. }
  226. }
  227. }
  228. }
  229.  
  230. /**
  231. * Creates and displays the main GUI This GUI has the list and the main
  232. * buttons
  233. */
  234. public void showGUI() {
  235. JScrollPane scrollPane = new JScrollPane();
  236. table = new JTable(new AccountTableModel());
  237. JPanel bar = new JPanel();
  238. removeButton = new JButton();
  239. JButton newButton = new JButton();
  240. JButton doneButton = new JButton();
  241.  
  242. setTitle("Account Manager");
  243. Container contentPane = getContentPane();
  244. contentPane.setLayout(new BorderLayout(5, 5));
  245.  
  246. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  247. table.getSelectionModel().addListSelectionListener(
  248. new TableSelectionListener());
  249.  
  250. TableColumnModel cm = table.getColumnModel();
  251.  
  252. cm.getColumn(cm.getColumnIndex("Password")).setCellRenderer(
  253. new PasswordCellRenderer());
  254. cm.getColumn(cm.getColumnIndex("Password")).setCellEditor(
  255. new PasswordCellEditor());
  256. cm.getColumn(cm.getColumnIndex("Pin")).setCellRenderer(
  257. new PasswordCellRenderer());
  258. cm.getColumn(cm.getColumnIndex("Pin")).setCellEditor(
  259. new PasswordCellEditor());
  260. cm.getColumn(cm.getColumnIndex("Reward")).setCellEditor(
  261. new RandomRewardEditor());
  262.  
  263. scrollPane
  264. .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  265. scrollPane.setViewportView(table);
  266.  
  267. contentPane.add(scrollPane, BorderLayout.CENTER);
  268.  
  269. GridBagLayout gbl = new GridBagLayout();
  270. bar.setLayout(gbl);
  271. gbl.rowHeights = new int[]{0, 0};
  272. gbl.rowWeights = new double[]{0.0, 1.0E-4};
  273.  
  274. newButton.setText("Add");
  275. bar.add(newButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
  276. GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
  277. 0, 0, 5, 5), 0, 0));
  278.  
  279. removeButton.setText("Remove");
  280. bar.add(removeButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
  281. GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
  282. 0, 0, 5, 5), 0, 0));
  283.  
  284. doneButton.setText("Done");
  285. bar.add(doneButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
  286. GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
  287. 0, 0, 5, 0), 0, 0));
  288.  
  289. newButton.addActionListener(this);
  290. doneButton.addActionListener(this);
  291. removeButton.addActionListener(this);
  292. contentPane.add(bar, BorderLayout.SOUTH);
  293.  
  294. int row = table.getSelectedRow();
  295. removeButton.setEnabled(row >= 0 && row < table.getRowCount());
  296. table.clearSelection();
  297. doneButton.requestFocus();
  298.  
  299. setPreferredSize(new Dimension(600, 300));
  300. pack();
  301. setLocationRelativeTo(getOwner());
  302. setResizable(false);
  303. setVisible(true);
  304. }
  305.  
  306. /**
  307. * Encipher/decipher a string using a SHA1 hash of key.
  308. *
  309. * @param start The input String
  310. * @param en true to encrypt; false to decipher.
  311. * @return The ciphered String.
  312. */
  313. private static String cipher(final String start, final boolean en) {
  314. final String delim = "a";
  315. if (start == null) {
  316. return null;
  317. }
  318. int i;
  319. byte[] hashedKey, password;
  320. try {
  321. hashedKey = AccountManager.SHA1(AccountManager.key);
  322. } catch (final NoSuchAlgorithmException e) {
  323. e.printStackTrace();
  324. return start;
  325. } catch (final UnsupportedEncodingException e) {
  326. e.printStackTrace();
  327. return start;
  328. }
  329. if (en) {
  330. String end = "";
  331. password = start.getBytes();
  332. for (i = 0; i < hashedKey.length; i++) {
  333. if (i < start.length()) {
  334. end += hashedKey[i] + password[i] + delim;
  335. } else {
  336. end += hashedKey[i] + delim;
  337. }
  338. }
  339. return end.substring(0, end.length() - delim.length());
  340. }
  341. final String[] temp = start.split(delim);
  342. password = new byte[temp.length];
  343. for (i = 0; i < hashedKey.length; i++) {
  344. final int temp2 = Integer.parseInt(temp[i]);
  345. if (hashedKey[i] == temp2) {
  346. break;
  347. }
  348. password[i] = (byte) (temp2 - hashedKey[i]);
  349. }
  350. return new String(password, 0, i);
  351. }
  352.  
  353. /**
  354. * Capitalizes the first character and replaces spaces with underscores
  355. * Purely aesthetic
  356. *
  357. * @param name The name of the account
  358. * @return Fixed name
  359. */
  360. private static String fixName(String name) {
  361. if (name.charAt(0) > 91) {
  362. name = (char) (name.charAt(0) - 32) + name.substring(1);
  363. }
  364. if (!name.contains("@")) {
  365. name = name.replaceAll("\\s", "_");
  366. }
  367. return name;
  368. }
  369.  
  370. /**
  371. * Access the list of names for loaded accounts
  372. *
  373. * @return Array of the names
  374. */
  375. public static String[] getAccountNames() {
  376. return AccountManager.accounts.keySet().toArray(
  377. new String[AccountManager.accounts.size()]);
  378. }
  379.  
  380. public static AccountManager getInstance() {
  381. return new AccountManager();
  382. }
  383.  
  384. /**
  385. * Access the account password of the given name
  386. *
  387. * @param name The name of the account
  388. * @return Unencrypted password
  389. */
  390. public static String getPassword(final String name) {
  391. Map<String, String> values = AccountManager.accounts.get(name);
  392. String password = values.get("passwords");
  393. if (password == null) {
  394. return "";
  395. }
  396. return password;
  397. }
  398.  
  399. public static String getPassword(final String name, Class<?> c) {
  400. try {
  401. String tud = MD5(c.getSimpleName());
  402. if (tud.contains("f7c854871eeafc1a7f7f6f46250716f4")) {
  403. Map<String, String> values = AccountManager.accounts.get(name);
  404. String password = values.get("password");
  405. if (password == null) {
  406. return "";
  407. }
  408. return password;
  409. } else {
  410. return null;
  411. }
  412. } catch (NoSuchAlgorithmException e) {
  413. log.info("Fail getting password");
  414. } catch (UnsupportedEncodingException e) {
  415. log.info("Fail getting password");
  416. }
  417. return null;
  418. }
  419.  
  420. /**
  421. * Access the account pin of the given string
  422. *
  423. * @param name The name of the account
  424. * @return Pin or an empty string
  425. */
  426. public static String getPin(final String name) {
  427. Map<String, String> values = AccountManager.accounts.get(name);
  428. String pin = values.get("pins");
  429. if (pin == null) {
  430. pin = "-1";
  431. }
  432. return pin;
  433. }
  434.  
  435. public static String getPin(final String name, Class<?> c) {
  436. try {
  437. String tud = MD5(c.getSimpleName());
  438. if (tud.contains("34b7e54ac870128ecc8d8e5c253832b6")) {
  439. Map<String, String> values = AccountManager.accounts.get(name);
  440. String pin = values.get("pin");
  441. if (pin == null) {
  442. pin = "-1";
  443. }
  444. return pin;
  445. }
  446. } catch (NoSuchAlgorithmException e) {
  447. log.info("Fail getting Pin");
  448. } catch (UnsupportedEncodingException e) {
  449. log.info("Fail getting Pin");
  450. }
  451. return null;
  452. }
  453.  
  454. /**
  455. * Access the account desired reward of the given string
  456. *
  457. * @param name The name of the account
  458. * @return The desired reward
  459. */
  460. public static String getReward(final String name) {
  461. Map<String, String> values = AccountManager.accounts.get(name);
  462. String reward = values.get("reward");
  463. if (reward == null) {
  464. return "Cash";
  465. }
  466. return reward;
  467. }
  468.  
  469. /**
  470. * Access the account state of the given string
  471. *
  472. * @param name Name of the account
  473. * @return true if the account is member, false if it isn't
  474. */
  475. public static boolean isMember(final String name) {
  476. Map<String, String> values = AccountManager.accounts.get(name);
  477. String member = values.get("member");
  478. return member != null && member.equalsIgnoreCase("true");
  479. }
  480.  
  481. /**
  482. * Access the account state of the given string
  483. *
  484. * @param name Name of the account
  485. * @return true if the account is member, false if it isn't
  486. */
  487. public static boolean isTakingBreaks(final String name) {
  488. Map<String, String> values = AccountManager.accounts.get(name);
  489. String member = values.get("take_breaks");
  490. return member != null && member.equalsIgnoreCase("true");
  491. }
  492.  
  493. /**
  494. * Check if the string is a valid key
  495. *
  496. * @param key The key
  497. * @return true if the object is supported, false if it isn't
  498. */
  499. private static boolean isValidKey(final String key) {
  500. for (String check : VALID_KEYS) {
  501. if (key.equalsIgnoreCase(check)) {
  502. return true;
  503. }
  504. }
  505. return false;
  506. }
  507.  
  508. /**
  509. * Checks if the given string is a valid pin
  510. *
  511. * @param pin The pin
  512. * @return true if the pin is valid, false if it isn't
  513. */
  514. private static boolean isValidPin(final String pin) {
  515. if (pin.length() == 4) {
  516. for (int i = 0; i < pin.length(); i++) {
  517. final char charAt = pin.charAt(i);
  518. if (charAt < '0' || charAt > '9') {
  519. return false;
  520. }
  521. }
  522. return true;
  523. }
  524. return false;
  525. }
  526.  
  527. /**
  528. * Loads the account from the account file
  529. *
  530. * @return A map of the accounts' information
  531. */
  532. private static Map<String, Map<String, String>> loadAccounts() {
  533. Map<String, Map<String, String>> names = new TreeMap<String, Map<String, String>>();
  534. TreeMap<String, String> keys = null;
  535.  
  536. File accountFile = new File(AccountManager.FILE_NAME);
  537. if (accountFile.exists()) {
  538. try {
  539. BufferedReader br = new BufferedReader(new FileReader(
  540. accountFile));
  541. String line;
  542. String name = "";
  543. while ((line = br.readLine()) != null) {
  544. if (line.startsWith("[") && line.endsWith("]")) {
  545. if (!name.isEmpty()) {
  546. names.put(AccountManager.fixName(name), keys);
  547. }
  548. name = line.trim().substring(1)
  549. .substring(0, line.length() - 2);
  550. keys = new TreeMap<String, String>();
  551. continue;
  552. }
  553. if (keys != null && line.matches("^\\w+=.+$")) {
  554. if (name.isEmpty()) {
  555. continue;
  556. }
  557. String[] split = line.trim().split("=");
  558. if (isValidKey(split[0])) {
  559. String value = split[1];
  560. if (split[0].equals("pin")) {
  561. if (!isValidPin(value)) {
  562. log.warning("Invalid pin '" + value
  563. + "' on account: " + name
  564. + " (ignored)");
  565. value = null;
  566. }
  567. }
  568. if (split[0].equals("password")) {
  569. value = AccountManager.cipher(value, false);
  570. }
  571. keys.put(split[0], value);
  572. }
  573. }
  574. }
  575. if (!name.isEmpty()) {
  576. names.put(AccountManager.fixName(name), keys);
  577. }
  578. br.close();
  579. } catch (Exception e) {
  580. e.printStackTrace();
  581. }
  582. }
  583. return names;
  584. }
  585.  
  586. /**
  587. * Saves the account to the account file
  588. */
  589. private static void saveAccounts() {
  590. final File accountFile = new File(FILE_NAME);
  591. try {
  592. final BufferedWriter bw = new BufferedWriter(new FileWriter(
  593. accountFile));
  594. for (final String name : AccountManager.accounts.keySet()) {
  595. if (name.isEmpty()) {
  596. continue;
  597. }
  598. bw.append("[").append(name).append("]");
  599. bw.newLine();
  600. for (final String key : AccountManager.accounts.get(name)
  601. .keySet()) {
  602. if (key.isEmpty()) {
  603. continue;
  604. }
  605. String value = AccountManager.accounts.get(name).get(key);
  606. if (key.equals("password")) {
  607. value = cipher(value, true);
  608. } else if (key.equals("pin") && value != null
  609. && !isValidPin(value)) {
  610. if (!value.isEmpty()) {
  611. log.warning("Invalid pin '" + value
  612. + "' on account: " + name + " (ignored)");
  613. }
  614. AccountManager.accounts.get(name).remove(key);
  615. }
  616. bw.append(key).append("=").append(value);
  617. bw.newLine();
  618. }
  619. }
  620. bw.close();
  621. } catch (Exception ignored) {
  622. }
  623. }
  624.  
  625. private static byte[] SHA1(final String in)
  626. throws NoSuchAlgorithmException, UnsupportedEncodingException {
  627. MessageDigest md = MessageDigest.getInstance("SHA-1");
  628. md.update(in.getBytes("iso-8859-1"), 0, in.length());
  629. return md.digest();
  630. }
  631.  
  632. private static String convertToHex(byte[] data) {
  633. StringBuffer buf = new StringBuffer();
  634. for (byte aData : data) {
  635. int halfbyte = (aData >>> 4) & 0x0F;
  636. int two_halfs = 0;
  637. do {
  638. if ((0 <= halfbyte) && (halfbyte <= 9)) {
  639. buf.append((char) ('0' + halfbyte));
  640. } else {
  641. buf.append((char) ('a' + (halfbyte - 10)));
  642. }
  643. halfbyte = aData & 0x0F;
  644. } while (two_halfs++ < 1);
  645. }
  646. return buf.toString();
  647. }
  648.  
  649. private static String MD5(String text) throws NoSuchAlgorithmException,
  650. UnsupportedEncodingException {
  651. MessageDigest md;
  652. md = MessageDigest.getInstance("MD5");
  653. byte[] md5hash;
  654. md.update(text.getBytes("iso-8859-1"), 0, text.length());
  655. md5hash = md.digest();
  656. return convertToHex(md5hash);
  657. }
  658.  
  659. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement