Permalink
Browse files

Initial coding for the master server.

  • Loading branch information...
1 parent 2c3f303 commit 185b01d09181dd20d5f585fe4f3a03a67321e37c @jwinnenb committed Jun 23, 2011
View
@@ -47,3 +47,7 @@
*** TODO 0.1.9 Clean up server computer page.
*** TODO 0.1.10 Clean up server user page.
+
+* Issues
+
+* Bugs
View
@@ -1,6 +1,8 @@
(defproject kh "0.0.1-SNAPSHOT"
:description "KH is a POC Remote Desktop System."
- :dependencies [[org.clojure/clojure "1.2.0"]
+ :dependencies [[org.clojars.kjw/commons-codec "1.3"]
+ [com.lispcast/org.apache.commons.lang "2.5.0"]
+ [org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]
[compojure "0.5.2"]
[hiccup "0.3.1"]
@@ -1,5 +1,6 @@
(ns kh.client.checksum
- (:import commons.apache.codec.digest.DigestUtils))
+ (:use clojure.contrib.io)
+ (:import org.apache.commons.codec.digest.DigestUtils))
(defn checksum [cookie path]
- (DigestUtils/md5hex (input-stream (file path))))
+ (DigestUtils/md5Hex (input-stream (file path))))
View
@@ -12,7 +12,7 @@
(POST "/upload" [cookie file dst name] (upload cookie file dst name)))
(GET "/download" [cookie file-path] (download cookie file-path))
(GET "/checksum" [cookie file-path] (checksum cookie file-path))
- (GET "/command" [cookie cmd] (command cookie cmd))
+ (GET "/command" [cookie cmd] (command cookie cmd)))
(defn launch-client [client-port client-ssl-cert client-ssl-pass]
(.start
@@ -6,7 +6,7 @@
(defn keyup [key]
(.keyRelease (Robot.) (Integer/parseInt key)))
-(defn keyboard-cmd [cookie cmd key]
+(defn keyboard [cookie cmd key]
(condp = cmd
"dn" (keydown key)
"up" (keyup key)))
View
@@ -9,7 +9,7 @@
(defn mouse-move [x y]
(.mouseMove (Robot.) x y))
-(defn mouse-cmd [cookie cmd x y nb]
+(defn mouse [cookie cmd x y nb]
(let [b (cond
(= nb 0) InputEvent/BUTTON1_MASK
(= nb 1) InputEvent/BUTTON1_MASK
View
@@ -0,0 +1,58 @@
+(ns kh.common.sql
+ (:use [clojure.contrib sql str-utils]
+ clojure.test)
+ (:import org.apache.commons.lang.StringEscapeUtils))
+
+(def *sql-debug-queries* (ref true))
+(def *sql-debug-output* (ref true))
+(def *sql-test-queries* (ref false))
+;; A global ref to store the DB information so you don't have to keep passing it around.
+
+(def db-ref (ref {}))
+
+(defn con-db
+ "Pushes the specified user information in to the db ref variable, simply provides the connection information, but doesn't really connect to the DB."
+ [user pass host db]
+ (dosync
+ (ref-set db-ref
+ {:classname "com.mysql.jdbc.Driver"
+ :subprotocol "mysql"
+ :subname (format "//%s:3306/%s" host db)
+ :user user
+ :password pass})))
+
+(defn escape-sql [s] (StringEscapeUtils/escapeSql s))
+
+(defn q
+ "Sends query string s to the database specified by db and returns the result."
+ ([#^String q-str & rest]
+ (let [s (apply format q-str (map escape-sql (flatten rest)))]
+ (when @*sql-debug-queries*
+ (println s))
+ (if @*sql-test-queries*
+ s
+ (let [r
+ (with-connection @db-ref
+ (with-query-results r [s]
+ (doall r)))]
+ (when @*sql-debug-queries*
+ (println (pr-str r)))
+ r)))))
+
+
+(defn mapq [fn-def q-str & params]
+ (map
+ fn-def
+ (apply q q-str params)))
+
+(defn c
+ ([#^String q-str & rest]
+ (let [s (apply format q-str (map escape-sql (flatten rest)))]
+ (when @*sql-debug-queries*
+ (println s))
+ (if @*sql-test-queries*
+ s
+ (with-connection @db-ref
+ (do-commands s))))))
+
+
View
@@ -0,0 +1,37 @@
+(ns kh.common.user
+ (:use kh.common.sql)
+ (:import org.apache.commons.codec.digest.DigestUtils))
+
+(defn add-user [user pass]
+ (c "insert khuser set khuname='%s' and khpass='%s'" user pass))
+(defn delete-user [user]
+ (c "delete from khuser where khuname='%s'" user))
+
+(defn change-pass-0 [user pass]
+ (c "update khuser set khpass='%s' where khuname='%s'" pass user))
+
+(defn change-pass [user pass pass0 pass1]
+ (let [rpass (first (q "select khpass from khuser where khuname='%s'" user))]
+ (when (and (not (nil? rpass))
+ (= rpass pass)
+ (= pass0 pass1))
+ (change-pass-0 user pass0))))
+
+(defn change-uname [user new-user]
+ (c "update khuser set khuname='%s' where khuname='%s'" new-user user))
+
+(defn create-cookie [user]
+ (DigestUtils/md5Hex (str user (System/currentTimeMillis))))
+
+(defn set-cookie [user & cookie0]
+ (let [cookie (or (first cookie0)
+ (create-cookie user))]
+ (c "update khuser set khcookie='%s' where khuname='%s'" cookie user)))
+
+(defn get-cookie [user]
+ (first
+ (q "select khcookie from khuser where khuname='%s'" user)))
+
+(defn good-cookie? [user cookie]
+ (not (empty? (q "select * from khuser where khuname='%s' and khcookie='%s'" user cookie))))
+
View
@@ -1,4 +1,22 @@
-(ns kh.server.core)
+(ns kh.server.core
+ (:use [compojure core route]
+ ring.adapter.jetty))
+
+(defroutes server-core
+ (GET "/" [] server-index-page)
+ (GET "/login" [user pass] (login-user-page user pass))
+ (GET "/user/new" [user pass] (new-user-page user pass))
+ (GET "/user/list" [] nil)
+ (GET "/user/edit" [] nil)
+ (GET "/user/del:uid" [] nil)
+ (GET "/user:uid" [] nil)
+
+ (GET "/comp/new" [] nil)
+ (GET "/comp/list" [] nil)
+ (GET "/comp/edit" [] nil)
+ (GET "/comp/del:cid" [] nil)
+ (GET "/comp:cid" [] nil)
+ (GET "/comp/control:cid" [] nil))
(defn launch-server [& args]
(println "launch-server stub"))
View
@@ -1,5 +1,6 @@
+drop table if exists khuser;
create table khuser (
khuname varchar (32) primary key,
khpass varchar (40),
khcookie varchar (40)
-);
+);
View
@@ -0,0 +1,6 @@
+(ns kh.test
+ (:use clojure.test)
+ (:gen-class))
+
+(deftest base-test
+ (is (= 1 1)))
View
@@ -0,0 +1,6 @@
+(ns kh.common.test
+ (:use clojure.test)
+ (:gen-class))
+
+(deftest base-test
+ (is (= 1 1)))

0 comments on commit 185b01d

Please sign in to comment.