Skip to content

Commit

Permalink
Do not depend from com.msopentech.thali
Browse files Browse the repository at this point in the history
  • Loading branch information
edne committed Sep 13, 2016
1 parent f8208bd commit 23b44bf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 100 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Static hidden services with no effort
## TODO
- Fix dependencies:
- Get [jtorctl](https://github.com/guardianproject/jtorctl) from Maven
- Try to avoid [Tor_Onion_Proxy_Library](https://github.com/thaliproject/Tor_Onion_Proxy_Library)
- <del>Try to avoid [Tor_Onion_Proxy_Library](https://github.com/thaliproject/Tor_Onion_Proxy_Library)</del>
- Directory indexing
- GUI
- Exhaustive readme
3 changes: 1 addition & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
:url "https://opensource.org/licenses/MIT"}
:resource-paths ["resources/jtorctl-briar.jar"]
:dependencies [[org.clojure/clojure "1.7.0"]
[com.msopentech.thali/ThaliOnionProxyUniversal "0.0.2"]
[com.msopentech.thali/ThaliOnionProxyJava "0.0.2"]
[juxt/dirwatch "0.2.3"]
[ring "1.5.0"]]

:main ^:skip-aot hidden-pod.core
Expand Down
1 change: 0 additions & 1 deletion src/hidden_pod/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
(let [path (first args)
local-port 3000
onion-addr (tor/publish-hidden-service local-port 80)]
(println "Serving at:" onion-addr)
(server/serve-folder path local-port))))
131 changes: 35 additions & 96 deletions src/hidden_pod/tor.clj
Original file line number Diff line number Diff line change
@@ -1,71 +1,15 @@
(ns hidden-pod.tor
(:require [clojure.string :as string]
[clojure.java.io :as io])
[clojure.java.io :as io]
[juxt.dirwatch :refer (watch-dir)])
(:import (java.io File
FileWriter
BufferedWriter
PrintWriter)
(java.nio.file Files)
(java.net Socket)
(java.util Scanner)
(java.util.concurrent TimeUnit)
(net.freehaven.tor.control TorControlConnection)
(com.msopentech.thali.toronionproxy OnionProxyManagerEventHandler
FileUtilities)
(com.msopentech.thali.java.toronionproxy JavaOnionProxyContext
JavaWatchObserver)))


(defn- can-create-parent-dir [file]
(or (-> file
.getParentFile
.exists)
(-> file
.getParentFile
.mkdirs)))


(defn- can-create-file [file]
(or (.exists file)
(.createNewFile file)))


(defn- get-os-name []
(-> "os.name"
System/getProperty
.toLowerCase))


(defn- linux? []
(.contains (get-os-name) "linux"))


(defn- windows? []
(.contains (get-os-name) "win"))


(defn- mac? []
(.contains (get-os-name) "mac"))


(defn- bootstrapped? [control-connection]
(-> control-connection
(.getinfo "status/bootstrap-phase")
(.contains "progress=100")))


(defn- new-observer [file]
(if-not (can-create-parent-dir file)
(throw (Exception. (str "Could not create " file " parent directory"))))
(if-not (can-create-file file)
(throw (Exception. (str "Could not create " file))))
(new JavaWatchObserver file))


(defn- wait-observer [observer timeout]
(if-not (.poll observer (* timeout 1000)
TimeUnit/MILLISECONDS)
(throw (Exception. "Wait time for file to be created expired"))))
(net.freehaven.tor.control TorControlConnection)))


(defn- set-conf [control-connection
Expand All @@ -79,55 +23,50 @@
(.saveConf control-connection))


(defn- start-with-timeout [ctx timeout-secs]
{:pre [(> timeout-secs 0)]}
(let [control-connection (:control-connection ctx)
control-socket (:control-socket ctx)]
(if-not (->> #(or (bootstrapped? control-connection)
(Thread/sleep 1000))
(take timeout-secs)
(filter identity)
#(if % (first %)))
(do (.close control-socket)
(throw (Exception. "Failed to run Tor")))
ctx)))
(defn- wait-bootstrap [control-connection control-socket timeout-secs]
(if-not (->> #(or (-> control-connection
(.getinfo "status/bootstrap-phase")
(.contains "progress=100"))
(Thread/sleep 100))
repeatedly
(take (* 10 timeout-secs))
(filter identity)
#(if % (first %)))
(do (.close control-socket)
(throw (Exception. "Wait time to bootstrapping Tor expired")))))


(defn- connect-to-tor [ctx]
(let [control-port 9051
control-socket (new Socket "127.0.0.1" control-port)
(defn- connect []
(let [control-socket (new Socket "127.0.0.1" 9051)
control-connection (new TorControlConnection control-socket)]
(.authenticate control-connection (make-array Byte/TYPE 0))
(start-with-timeout (merge ctx {:control-socket control-socket
:control-connection control-connection})
30)
(wait-bootstrap control-connection control-socket 30)
control-connection))


(defn- create-context []
(let [working-dir (->> (into-array java.nio.file.attribute.FileAttribute [])
(Files/createTempDirectory "tor-folder") .toFile)
proxy-context (new JavaOnionProxyContext working-dir)
hiddenservice-dir-name "hiddenservice"
torrc-name "torrc"]
{:proxy-context proxy-context
:working-dir working-dir
:torrc-name torrc-name
:torrc-file (new File working-dir torrc-name)
:hostname-file (new File working-dir
(str "/" hiddenservice-dir-name "/hostname"))}))
(defn- create-directory []
(->> (into-array java.nio.file.attribute.FileAttribute [])
(Files/createTempDirectory "tor-folder")
.toFile))


(defn- create-hostname-file [working-dir]
(new File working-dir "/hiddenservice/hostname"))


(defn publish-hidden-service
"Create an hidden service forwarding a port, return the address"
[local-port remote-port]
(let [ctx (create-context)
control-connection (-> ctx
connect-to-tor)
hostname-file (:hostname-file ctx)
observer (new-observer hostname-file)]
(let [control-connection (connect)
working-dir (create-directory)
hostname-file (create-hostname-file working-dir)]
(watch-dir #(let [file-name (-> % :file .getName)
full-path (-> % :file .getAbsolutePath)]
(if (= file-name "hostname")
(println "Serving at: " (slurp full-path))))
working-dir)
(set-conf control-connection
hostname-file
remote-port local-port)
(wait-observer observer 30)
(slurp hostname-file)))
; TODO: wait watcher or make it fully asynchronous
))

0 comments on commit 23b44bf

Please sign in to comment.