Created
June 13, 2017 22:14
-
-
Save orarbel/5836f506faa73db46c773c4c51a34175 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.*; | |
| import java.net.*; | |
| import java.util.*; | |
| import java.util.Map.Entry; | |
| public class CourseRegister implements Runnable { | |
| boolean debug = false; | |
| boolean running = true; | |
| boolean adding; | |
| Vector<String> requestedCourses = new Vector<String>(); | |
| HashMap<String,String> hiddenInputs = new HashMap<String,String>(); | |
| URL bguURL; | |
| int checkInterval = 2; // in seconds | |
| URLConnection client; | |
| Socket socket; | |
| String currentCourse; | |
| // bgu login information | |
| String user; | |
| String pass; | |
| String id; | |
| // notification information | |
| String icqUIN; | |
| String icqPassword; | |
| String phone; | |
| String mail; | |
| String cookie; | |
| String rowid; | |
| PrintWriter out = null; | |
| BufferedReader in = null; | |
| OutputStreamWriter wr = null; | |
| BufferedReader rd = null; | |
| InetAddress address; | |
| public void run() { | |
| try { | |
| this.loadFromFile(); | |
| address = InetAddress.getByName("bgu4u.bgu.ac.il"); | |
| //this.signup(); | |
| //this.sendICQNotification("hi"); | |
| // logging in.. | |
| this.sendData(this.login()); // send login data | |
| this.readData(); // read response | |
| this.sendData(this.rowid()); // send row id data | |
| this.readData(); // read response | |
| this.sendData(this.addSemester()); | |
| this.readData(); // read response | |
| this.sendData(this.searchCourse()); | |
| this.readData(); // read response | |
| // start trying to sign up | |
| HashMap<String,String> hiddenInputsBackup = (HashMap<String,String>)this.hiddenInputs.clone(); | |
| while (this.requestedCourses.size() > 0 && running) { | |
| System.out.println("trying: " + this.requestedCourses); | |
| for (int i = 0 ; i < this.requestedCourses.size() ; i++ ) { | |
| this.hiddenInputs = (HashMap<String,String>)hiddenInputsBackup.clone(); | |
| currentCourse = this.requestedCourses.elementAt(i); | |
| System.out.println("current course: " + this.currentCourse); | |
| this.sendData(this.searchResults(currentCourse)); | |
| System.out.println("sent search"); | |
| this.readData(); // read response | |
| this.sendData(this.courseConsult(currentCourse)); | |
| System.out.println("sent consult"); | |
| this.readData(); // read response | |
| adding = true; | |
| this.sendData(this.addCourse(currentCourse)); | |
| System.out.println("sent add"); | |
| this.readData(); // read response | |
| adding = false; | |
| synchronized (this) { // pause for a bit, let the server rest | |
| this.wait(this.checkInterval * 1000); | |
| } | |
| } | |
| } | |
| // done signing up to all requested courses | |
| this.sendData(this.bye()); // send bye data | |
| } catch (Exception e) { | |
| System.out.println("expecoptin"); | |
| e.printStackTrace(); | |
| } | |
| } | |
| void sendData(String data) { | |
| try { | |
| socket = new Socket(address,80); | |
| wr = new OutputStreamWriter(socket.getOutputStream()); | |
| rd = new BufferedReader( | |
| new InputStreamReader( | |
| socket.getInputStream())); | |
| wr.write(data); | |
| wr.flush(); | |
| if (debug) { | |
| System.out.println("---------------------------------- out ------------------------------------"); | |
| System.out.println(data); | |
| System.out.println("---------------------------------- end out ------------------------------------"); | |
| } | |
| } catch (IOException e) { | |
| System.out.println("expecoptin"); | |
| e.printStackTrace(); | |
| } | |
| } | |
| void readData() throws Exception { | |
| try { | |
| boolean errorInRegistration = false; | |
| boolean error404 = false; | |
| hiddenInputs.clear(); | |
| String line; | |
| Vector<String> tmpv = new Vector<String>(); | |
| if (debug) System.out.println("---------------------------------- in ------------------------------------"); | |
| while ((line = rd.readLine()) != null) { | |
| if (line.contains("ShowDbErr")) { | |
| errorInRegistration = true; | |
| } | |
| if (line.contains("404")) { | |
| error404 = true; | |
| } | |
| if (line.contains("2021")) { | |
| System.out.println("try in 15 minutes"); | |
| System.exit(0); | |
| } | |
| if (line.startsWith("Set-Cookie:")) { | |
| StringTokenizer t = new StringTokenizer(line,":"); | |
| t.nextToken(); | |
| cookie = t.nextToken().trim(); | |
| } | |
| if (line.startsWith("<input")) { | |
| String name = this.getFieldName(line); | |
| tmpv.add(name); | |
| // System.out.println("inserted: " + name); | |
| //System.out.println(line); | |
| String value = this.getFieldValue(line).replaceAll("\"", ""); | |
| hiddenInputs.put(name,value); | |
| //System.out.println(+ " " + ); | |
| } | |
| if (line.contains("javascript:setFormActionAndSubmitAcLogInNew")) { | |
| StringTokenizer t = new StringTokenizer(line,"("); | |
| t.nextToken(); | |
| String values = t.nextToken(); | |
| StringTokenizer t1 = new StringTokenizer(values,","); | |
| String currentValue; | |
| int i = 0; | |
| while (t1.hasMoreTokens()) { | |
| currentValue = t1.nextToken().replaceAll("'", "").replace(")\">",""); | |
| hiddenInputs.put(tmpv.elementAt(i),currentValue ); | |
| i++; | |
| } | |
| } | |
| if (debug) System.out.println(line); | |
| } | |
| if (debug) System.out.println("---------------------------------- end in ------------------------------------"); | |
| if (adding) { | |
| if (!errorInRegistration && !error404) { | |
| // SUCCESS!! | |
| this.requestedCourses.remove(this.currentCourse); // remove from list | |
| this.notify("registered: " + this.currentCourse); // send notification mail | |
| System.out.println("SUCCESS! " + this.currentCourse); | |
| } | |
| else { | |
| System.out.println("couldn't register " + this.currentCourse); | |
| // FAILURE | |
| // do nothing , keep it in list | |
| } | |
| } | |
| wr.close(); | |
| rd.close(); | |
| } catch (IOException e) { | |
| System.out.println("expecoptin"); | |
| e.printStackTrace(); | |
| this.sendData(this.bye()); // send bye data | |
| } | |
| } | |
| String getFieldName(String input) { | |
| String ans; | |
| StringTokenizer st = new StringTokenizer(input,"="); | |
| st.nextToken(); | |
| st.nextToken(); | |
| ans = st.nextToken(); | |
| StringTokenizer st1 = new StringTokenizer(ans," "); | |
| ans = st1.nextToken(); | |
| return ans.replace("\"", "").replace(">", ""); | |
| } | |
| String getFieldValue(String input) { | |
| if (input.contains("value")) { | |
| String ans; | |
| StringTokenizer st = new StringTokenizer(input,"="); | |
| st.nextToken(); | |
| st.nextToken(); | |
| st.nextToken(); | |
| ans = st.nextToken(); | |
| StringTokenizer st1 = new StringTokenizer(ans,">"); | |
| ans = st1.nextToken(); | |
| return ans; | |
| } | |
| return ""; | |
| } | |
| String login() { | |
| String temp = null; | |
| String postData; | |
| try { | |
| // set login string | |
| postData = URLEncoder.encode("rc_system", "UTF-8") + "=" + URLEncoder.encode("sc", "UTF-8"); | |
| postData += "&" + URLEncoder.encode("oc_username", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8"); | |
| postData += "&" + URLEncoder.encode("oc_password", "UTF-8") + "=" + URLEncoder.encode(pass, "UTF-8"); | |
| postData += "&" + URLEncoder.encode("rc_id", "UTF-8") + "=" + URLEncoder.encode(id, "UTF-8"); | |
| temp = "POST /pls/scwp/!fw.CheckId HTTP/1.1\n"; | |
| temp += "Host: bgu4u.bgu.ac.il\n"; | |
| temp += "User-Agent: Mozilla/5.0 (Windows; N; Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)\n"; | |
| temp += "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n"; | |
| temp += "Accept-Language: en-us,en;q=0.5\n"; | |
| temp += "Accept-Encoding: gzip,deflate\n"; | |
| temp += "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n"; | |
| temp += "Keep-Alive: 300\n"; | |
| temp += "Proxy-Connection: keep-alive\n"; | |
| temp += "Referer: http://bgu4u.bgu.ac.il/pls/scwp/!fw.CheckId\n"; | |
| temp += "Content-Type: application/x-www-form-urlencoded\n"; | |
| temp += "Content-Length: " + Integer.toString(postData.length()) + "\n"; | |
| temp += "\n"; | |
| temp += postData + "\n" ; | |
| } | |
| catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| return temp; | |
| } | |
| String rowid() { | |
| return this.createHeader("POST http://bgu4u.bgu.ac.il/pls/scwp/!sc.academiclogin HTTP/1.1\n"); | |
| } | |
| String addSemester() { | |
| return this.createHeader("POST http://bgu4u.bgu.ac.il/pls/scwp/!sc.AddSemester HTTP/1.1\n"); | |
| } | |
| String searchCourse() { | |
| return this.createHeader("POST /pls/scwp/!sc.SearchCourse HTTP/1.1\n"); | |
| } | |
| String courseConsult(String currentCourse) { | |
| String dept,level,course; | |
| StringTokenizer st; | |
| st = new StringTokenizer(currentCourse,"-"); | |
| dept = st.nextToken().trim(); | |
| level = st.nextToken().trim(); | |
| course = st.nextToken().trim(); | |
| this.hiddenInputs.put("rn_course", course); | |
| this.hiddenInputs.put("rn_course_institution", "0"); | |
| this.hiddenInputs.put("rn_course_department", dept); | |
| this.hiddenInputs.put("rn_course_degree_level", level); | |
| return this.createHeader("POST /pls/scwp/!sc.CourseConsult HTTP/1.1\n"); | |
| } | |
| String searchResults(String currentCourse) { | |
| String dept,level,course; | |
| StringTokenizer st; | |
| st = new StringTokenizer(currentCourse,"-"); | |
| dept = st.nextToken().trim(); | |
| level = st.nextToken().trim(); | |
| course = st.nextToken().trim(); | |
| this.hiddenInputs.put("oc_course_name", ""); | |
| this.hiddenInputs.put("on_course_department", dept); | |
| this.hiddenInputs.put("on_course_degree_level", level); | |
| this.hiddenInputs.put("on_course", course); | |
| return this.createHeader("POST /pls/scwp/!sc.SearchResults HTTP/1.1\n"); | |
| } | |
| String addCourse(String currentCourse) { | |
| this.hiddenInputs.put("on_group_number_1","1"); | |
| return this.createHeader("POST /pls/scwp/!sc.AddCourse HTTP/1.1\n"); | |
| } | |
| String createHeader(String post) { | |
| String temp = null; | |
| String postData; | |
| try { | |
| // set post data string | |
| Set <Entry<String,String>> allEntries = hiddenInputs.entrySet(); | |
| Iterator<Entry<String,String>> it = allEntries.iterator(); | |
| Entry<String,String > entry = it.next(); | |
| // first entry in post data has no & prefix | |
| postData = URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); // | |
| while (it.hasNext()) { | |
| entry = it.next(); | |
| postData += "&" + URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); | |
| } | |
| // System.out.println(postData); | |
| temp = post; | |
| temp += "Host: bgu4u.bgu.ac.il\n"; | |
| temp += "User-Agent: Mozilla/5.0 (Windows; N; Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)\n"; | |
| temp += "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n"; | |
| temp += "Accept-Language: en-us,en;q=0.5\n"; | |
| temp += "Accept-Encoding: gzip,deflate\n"; | |
| temp += "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n"; | |
| temp += "Keep-Alive: 300\n"; | |
| temp += "Proxy-Connection: keep-alive\n"; | |
| temp += "Referer: http://bgu4u.bgu.ac.il/pls/scwp/!fw.CheckId\n"; | |
| temp += "Cookie: " + this.cookie + "\n"; | |
| temp += "Content-Type: application/x-www-form-urlencoded\n"; | |
| temp += "Content-Length: " + Integer.toString(postData.length()) + "\n"; | |
| temp += "\n"; | |
| temp += postData + "\n" ; | |
| } | |
| catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| return temp; | |
| } | |
| String bye() { | |
| String temp = null; | |
| String postData; | |
| try { | |
| // set post data string | |
| Set <Entry<String,String>> allEntries = hiddenInputs.entrySet(); | |
| Iterator<Entry<String,String>> it = allEntries.iterator(); | |
| Entry<String,String > entry = it.next(); | |
| // first entry in post data has no & prefix | |
| postData = URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); // | |
| while (it.hasNext()) { | |
| entry = it.next(); | |
| postData += "&" + URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); | |
| } | |
| // System.out.println(postData); | |
| temp = "POST /pls/scwp/!SC.BYEBYEHD HTTP/1.1\n"; | |
| temp += "Host: bgu4u.bgu.ac.il\n"; | |
| temp += "User-Agent: Mozilla/5.0 (Windows; N; Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)\n"; | |
| temp += "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n"; | |
| temp += "Accept-Language: en-us,en;q=0.5\n"; | |
| temp += "Accept-Encoding: gzip,deflate\n"; | |
| temp += "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n"; | |
| temp += "Keep-Alive: 300\n"; | |
| temp += "Proxy-Connection: keep-alive\n"; | |
| temp += "Referer: http://bgu4u.bgu.ac.il/pls/scwp/!fw.CheckId\n"; | |
| temp += "Cookie: " + this.cookie + "\n"; | |
| temp += "Content-Type: application/x-www-form-urlencoded\n"; | |
| temp += "Content-Length: " + Integer.toString(postData.length()) + "\n"; | |
| temp += "\n"; | |
| temp += postData + "\n" ; | |
| } | |
| catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| return temp; | |
| } | |
| void stop() { | |
| this.sendData(this.bye()); // send bye data | |
| running = false; | |
| } | |
| void loadFromFile() { | |
| File file = new File("courses.txt"); | |
| FileInputStream fis = null; | |
| BufferedInputStream bis = null; | |
| DataInputStream dis = null; | |
| try { | |
| fis = new FileInputStream(file); | |
| BufferedReader br | |
| = new BufferedReader(new InputStreamReader(fis)); | |
| // Here BufferedInputStream is added for fast reading. | |
| bis = new BufferedInputStream(fis); | |
| dis = new DataInputStream(bis); | |
| String line; | |
| // dis.available() returns 0 if the file does not have more lines. | |
| while ((line = br.readLine()) != null) { | |
| if (line.length() > 0 && line.charAt(0) != '#') { | |
| // this statement reads the line from the file and print it to | |
| // the console. | |
| StringTokenizer st = new StringTokenizer(line,":"); | |
| String key = st.nextToken(); | |
| String value = st.nextToken().trim(); | |
| if (key.compareTo("user") == 0) this.user = value; | |
| if (key.compareTo("pass") == 0) this.pass = value; | |
| if (key.compareTo("id") == 0) this.id = value; | |
| if (key.compareTo("icq number") == 0) this.icqUIN = value; | |
| if (key.compareTo("icq pass") == 0) this.icqPassword = value; | |
| if (key.compareTo("phone number") == 0) this.phone = value; | |
| if (key.compareTo("mail") == 0) this.mail = value; | |
| if (key.compareTo("course") == 0) this.requestedCourses.add(value); | |
| } | |
| } | |
| // dispose all the resources after using them. | |
| fis.close(); | |
| bis.close(); | |
| dis.close(); | |
| } catch (FileNotFoundException e) { | |
| e.printStackTrace(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| String getCoursesFromFile() { | |
| String str = null; | |
| try { | |
| BufferedReader in = new BufferedReader(new FileReader("infilename")); | |
| while ((str = in.readLine()) != null) { | |
| //process(str); | |
| } | |
| in.close(); | |
| } catch (IOException e) { } | |
| return str; | |
| } | |
| void sendICQNotification(String notification) { | |
| try { | |
| InetAddress address = InetAddress.getByName("misron.net"); | |
| //InetAddress address = InetAddress.getByName("localhost"); | |
| Socket socket = new Socket(address,80); | |
| OutputStreamWriter wr = new OutputStreamWriter(socket.getOutputStream()); | |
| String msg = "";//"uin=" + this.icqUIN + "&uin_password=" + this.icqPassword + "&number=" + this.phone + "&smstext=" + notification + "&posted=%D7%A9%D7%9C%D7%97 | |
| msg = ""; //URLEncoder.encode("rc_system", "UTF-8") + "=" + URLEncoder.encode("sc", "UTF-8"); | |
| msg += "" + URLEncoder.encode("uin", "UTF-8") + "=" + URLEncoder.encode(this.icqUIN, "UTF-8"); | |
| msg += "&" + URLEncoder.encode("uin_password", "UTF-8") + "=" + URLEncoder.encode(this.icqPassword, "UTF-8"); | |
| msg += "&" + URLEncoder.encode("number", "UTF-8") + "=" + URLEncoder.encode(this.phone , "UTF-8"); | |
| msg += "" + URLEncoder.encode("smstext", "UTF-8") + "=" + URLEncoder.encode(notification, "UTF-8"); | |
| msg += "&" + URLEncoder.encode("uin_password", "UTF-8") + "=" + URLEncoder.encode(this.icqPassword, "UTF-8"); | |
| msg += "&" + URLEncoder.encode("number", "UTF-8") + "=" + URLEncoder.encode(this.phone , "UTF-8"); | |
| String temp = "POST http://misron.net/send/index.php HTTP/1.1\n"; | |
| temp += "Host: misron.net\n"; | |
| temp += "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1\n"; | |
| temp += "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n"; | |
| temp += "Accept-Language: en-us,en;q=0.5\n"; | |
| temp += "Accept-Encoding: gzip,deflate\n"; | |
| temp += "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n"; | |
| temp += "Keep-Alive: 300\n"; | |
| temp += "Proxy-Connection: keep-alive\n"; | |
| temp += "Referer: http://misron.net/send/index.php\n"; | |
| temp += "Content-Type: application/x-www-form-urlencoded\n"; | |
| temp += "Content-Length: " + Integer.toString(msg.length()) + "\n"; | |
| temp += "\n"; | |
| temp += msg + "\n" ; | |
| System.out.println(msg); | |
| BufferedReader rd = new BufferedReader( | |
| new InputStreamReader( | |
| socket.getInputStream())); | |
| String line; | |
| while ((line = rd.readLine()) != null) { | |
| System.out.println(line); | |
| } | |
| wr.close(); | |
| rd.close(); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| void notify(String notification) { | |
| try { | |
| SendMail sender = new SendMail(this.mail,"CourseRegister notification",notification); | |
| sender.send(); | |
| } | |
| catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment