Explorar el Código

Remove rust server component

Thomas Dy hace 3 años
padre
commit
36ab677a37

+ 0 - 1
rust-server/.gitignore

@@ -1 +0,0 @@
-target

+ 0 - 326
rust-server/Cargo.lock

@@ -1,326 +0,0 @@
-[root]
-name = "cloudflare-webui"
-version = "0.1.6"
-dependencies = [
- "hyper 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
- "mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
- "sha1 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "bitflags"
-version = "0.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "cookie"
-version = "0.2.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "openssl 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)",
- "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
- "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
- "url 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "gcc"
-version = "0.3.26"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "gdi32-sys"
-version = "0.1.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "hpack"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "httparse"
-version = "1.1.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "hyper"
-version = "0.9.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "cookie 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
- "mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
- "openssl 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)",
- "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
- "solicit 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
- "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
- "traitobject 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "url 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "idna"
-version = "0.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "unicode-bidi 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
- "unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "kernel32-sys"
-version = "0.2.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
- "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "language-tags"
-version = "0.2.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "lazy_static"
-version = "0.1.15"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "libc"
-version = "0.2.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "libressl-pnacl-sys"
-version = "2.1.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "pnacl-build-helper 1.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "log"
-version = "0.3.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "matches"
-version = "0.1.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "mime"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "num_cpus"
-version = "0.2.11"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "libc 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "openssl"
-version = "0.7.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)",
- "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
- "openssl-sys 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)",
- "openssl-sys-extras 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "openssl-sys"
-version = "0.7.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "gdi32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
- "libressl-pnacl-sys 2.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
- "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
- "user32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "openssl-sys-extras"
-version = "0.7.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
- "openssl-sys 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "pkg-config"
-version = "0.3.8"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "pnacl-build-helper"
-version = "1.4.10"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "rand"
-version = "0.3.14"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "libc 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "rustc-serialize"
-version = "0.3.19"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "rustc_version"
-version = "0.1.7"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "semver"
-version = "0.1.20"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "sha1"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "solicit"
-version = "0.4.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "hpack 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "tempdir"
-version = "0.3.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "time"
-version = "0.1.35"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
- "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "traitobject"
-version = "0.0.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "typeable"
-version = "0.1.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "unicase"
-version = "1.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "unicode-bidi"
-version = "0.2.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "unicode-normalization"
-version = "0.1.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "url"
-version = "0.5.8"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
- "unicode-bidi 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
- "unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "uuid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "url"
-version = "1.2.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "idna 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "user32-sys"
-version = "0.1.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
- "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "uuid"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "winapi"
-version = "0.2.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "winapi-build"
-version = "0.1.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-

+ 0 - 10
rust-server/Cargo.toml

@@ -1,10 +0,0 @@
-[package]
-name = "cloudflare-webui"
-version = "0.1.6"
-authors = ["Thomas Dy <thatsmydoing@gmail.com>"]
-
-[dependencies]
-hyper = "0.9"
-mime = "0.2"
-rustc-serialize = "0.3.0"
-sha1 = "0.2.0"

+ 0 - 29
rust-server/Makefile

@@ -1,29 +0,0 @@
-.phony: clean
-
-OPENSSL_VER ?= 1.0.2j
-WEBUI_VER ?= 0.1.6
-
-BUILD_DIR = /tmp/openssl-static-build
-OPENSSL_PKG = openssl-$(OPENSSL_VER).tar.gz
-OPENSSL_SRC = $(BUILD_DIR)/openssl-$(OPENSSL_VER)
-
-cloudflare-webui-$(WEBUI_VER).tar.gz: target/release/cloudflare-webui
-	tar -C target/release -czf cloudflare-webui-$(WEBUI_VER).tar.gz cloudflare-webui
-
-$(BUILD_DIR):
-	mkdir -p $(BUILD_DIR)
-
-$(BUILD_DIR)/$(OPENSSL_PKG): $(BUILD_DIR)
-	cd $(BUILD_DIR) && curl -LO https://www.openssl.org/source/$(OPENSSL_PKG)
-
-$(OPENSSL_SRC): $(BUILD_DIR)/$(OPENSSL_PKG)
-	cd $(BUILD_DIR) && tar xf $(OPENSSL_PKG)
-
-$(BUILD_DIR)/lib/libssl.a: $(OPENSSL_SRC)
-	cd $(OPENSSL_SRC) && ./config -fPIC no-shared no-zlib --prefix=$(BUILD_DIR) && make && make install
-
-target/release/cloudflare-webui: $(BUILD_DIR)/lib/libssl.a
-	env OPENSSL_LIB_DIR=$(BUILD_DIR)/lib OPENSSL_INCLUDE_DIR=$(BUILD_DIR)/include OPENSSL_STATIC=yes cargo build --release
-
-clean:
-	cargo clean

+ 0 - 31
rust-server/src/config.rs

@@ -1,31 +0,0 @@
-use rustc_serialize::json;
-
-use std::fs::File;
-use std::io::prelude::*;
-use std::io::Error;
-
-#[derive(RustcDecodable, Debug)]
-pub struct Config {
-    pub port: Option<u16>,
-    pub email: String,
-    pub token: String,
-    pub whitelist: Option<Vec<String>>
-}
-
-fn stringify(err: Error) -> String {
-    format!("{}", err)
-}
-
-pub fn load(path: &str) -> Result<Config, String> {
-    File::open(path)
-        .map_err(stringify)
-        .and_then(|mut f| {
-            let mut text = String::new();
-            f.read_to_string(&mut text)
-                .map_err(stringify)
-                .map(|_| text)
-        })
-        .and_then(|text| {
-            json::decode(&text).map_err(|e| format!("{}", e))
-        })
-}

+ 0 - 211
rust-server/src/handler.rs

@@ -1,211 +0,0 @@
-use config::Config;
-use mime::Mime;
-use std::io;
-use std::collections::HashSet;
-use hyper::Get;
-use hyper::Client;
-use hyper::client;
-use hyper::header::{ContentType, ETag, EntityTag, IfNoneMatch, Headers};
-use hyper::method::Method;
-use hyper::server::{Handler, Request, Response};
-use hyper::status::StatusCode::{InternalServerError, NotFound, NotModified, Unauthorized};
-use hyper::uri::RequestUri::AbsolutePath;
-use hyper::Url;
-use rustc_serialize::json;
-use rustc_serialize::json::Json;
-use sha1::Sha1;
-
-const API_ENDPOINT: &'static str = "https://api.cloudflare.com/client/v4";
-
-const INDEX_HTML: &'static str = include_str!("../../index.html");
-const BUNDLE_JS: &'static str = include_str!("../../assets/bundle.js");
-
-struct Etags {
-    index: EntityTag,
-    bundle: EntityTag
-}
-
-pub struct SiteHandler {
-    cfg: Config,
-    client: Client,
-    etags: Etags,
-    whitelist: HashSet<String>
-}
-
-impl SiteHandler {
-    fn make_headers(&self) -> Headers {
-        let mut headers = Headers::new();
-        headers.set_raw("x-auth-email", vec![self.cfg.email.as_bytes().to_vec()]);
-        headers.set_raw("x-auth-key", vec![self.cfg.token.as_bytes().to_vec()]);
-        headers.set(ContentType::json());
-        headers
-    }
-
-    fn request(&self, method: Method, url: &str, mut body: Option<Request>) -> client::Response {
-        let url = API_ENDPOINT.to_owned() + url;
-        let mut url = Url::parse(&url).unwrap();
-        if method == Get {
-            url.query_pairs_mut().append_pair("per_page", "999");
-        }
-        let request = self.client.request(method, url).headers(self.make_headers());
-        let request = match body.as_mut() {
-            Some(body) => request.body(body),
-            None => request
-        };
-        request.send().unwrap()
-    }
-
-    fn has_whitelist(&self) -> bool {
-        self.cfg.whitelist.is_some()
-    }
-
-    fn is_valid(&self, path: &str) -> bool {
-        if self.has_whitelist() {
-            let zone: String = path.chars()
-                .skip(1)
-                .skip_while(|c| *c != '/')
-                .skip(1)
-                .take_while(|c| *c != '/')
-                .collect();
-            self.whitelist.contains(&zone)
-        }
-        else {
-            true
-        }
-    }
-}
-
-fn make_etag(source: &str) -> EntityTag {
-    let mut m = Sha1::new();
-    m.update(source.as_bytes());
-    let digest = m.digest().to_string();
-    EntityTag::new(false, digest)
-}
-
-pub fn new(cfg: Config) -> SiteHandler {
-    let mut handler = SiteHandler {
-        cfg: cfg,
-        client: Client::new(),
-        etags: Etags {
-            index: make_etag(INDEX_HTML),
-            bundle: make_etag(BUNDLE_JS)
-        },
-        whitelist: HashSet::new()
-    };
-    let mut response = handler.request(Get, "/zones", None);
-    if let Some(ref domain_whitelist) = handler.cfg.whitelist {
-        let mut whitelist: HashSet<String> = HashSet::new();
-        match Json::from_reader(&mut response) {
-            Ok(body) => {
-                let zones = body.find("result").and_then(|result| result.as_array()).unwrap();
-                for zone in zones {
-                    let id = zone.find("id").and_then(|id| id.as_string()).unwrap();
-                    let name = zone.find("name").and_then(Json::as_string).unwrap();
-                    if domain_whitelist.into_iter().any(|domain| domain == name) {
-                        whitelist.insert(id.to_owned());
-                    }
-                }
-            },
-            Err(error) => {
-                println!("Error: {}", error);
-            }
-        }
-        handler.whitelist = whitelist;
-    }
-    handler
-}
-
-fn serve(req: &Request, mut res: Response, content: &str, etag: &EntityTag, mime: Mime) {
-    let empty_vec = vec!();
-    let etags = req.headers.get::<IfNoneMatch>();
-    let etags: &Vec<EntityTag> = match etags {
-        Some(&IfNoneMatch::Items(ref items)) => items,
-        _ => &empty_vec
-    };
-    let is_cached = etags.iter().find(|&etag_b| etag.weak_eq(etag_b)).is_some();
-
-    res.headers_mut().set(ContentType(mime));
-    res.headers_mut().set(ETag(etag.to_owned()));
-    if is_cached {
-        *res.status_mut() = NotModified;
-    }
-    else {
-        res.send(content.as_bytes()).unwrap();
-    }
-}
-
-impl Handler for SiteHandler {
-    fn handle(&self, req: Request, mut res: Response) {
-        let uri = req.uri.clone();
-        let method = req.method.clone();
-        match uri {
-            AbsolutePath(ref path) => match (&method, &path[..]) {
-                (&Get, "/") =>
-                    serve(&req, res, INDEX_HTML, &self.etags.index, mime!(Text/Html; Charset=Utf8)),
-                (&Get, "/assets/bundle.js") =>
-                    serve(&req, res, BUNDLE_JS, &self.etags.bundle, mime!(Application/Javascript; Charset=Utf8)),
-                (method, url) if path.starts_with("/api") => {
-                    let method = method.clone();
-                    let path: String = url.chars().skip(4).collect();
-
-                    let whitelist = &self.whitelist;
-                    if path == "/zones" {
-                        let mut proxy_res = self.request(method, &path, None);
-                        if self.has_whitelist() {
-                            match Json::from_reader(&mut proxy_res) {
-                                Ok(mut body) => {
-                                    // filter out non-whitelisted domains
-                                    body.as_object_mut()
-                                        .and_then(|mut root| root.get_mut("result"))
-                                        .and_then(|mut result| result.as_array_mut())
-                                        .map(|mut zones| {
-                                            zones.retain(|zone| {
-                                                let id = zone.find("id").and_then(|id| id.as_string()).unwrap();
-                                                whitelist.contains(id)
-                                            });
-                                        });
-
-                                    let json = json::encode(&body).unwrap();
-                                    res.headers_mut().set(ContentType(mime!(Application/Json; Charset=Utf8)));
-                                    res.send(json.as_bytes()).unwrap();
-                                },
-                                Err(error) => {
-                                    println!("Error: {}", error);
-                                    *res.status_mut() = InternalServerError;
-                                    res.send(b"Unexpected response from CloudFlare").unwrap();
-                                }
-                            };
-                        }
-                        else {
-                            res.headers_mut().set(ContentType(mime!(Application/Json; Charset=Utf8)));
-                            let mut res = res.start().unwrap();
-                            io::copy(&mut proxy_res, &mut res).ok().expect("Failed to proxy");
-                            res.end().unwrap();
-                        }
-                    }
-                    else if self.is_valid(&path) {
-                        let mut proxy_res = self.request(method, &path, Some(req));
-                        res.headers_mut().set(ContentType(mime!(Application/Json; Charset=Utf8)));
-                        let mut res = res.start().unwrap();
-                        io::copy(&mut proxy_res, &mut res).ok().expect("Failed to proxy");
-                        res.end().unwrap();
-                    }
-                    else {
-                        *res.status_mut() = Unauthorized;
-                        res.send(b"Unauthorized").unwrap();
-                    }
-                },
-                (&Get, _) =>
-                    serve(&req, res, INDEX_HTML, &self.etags.index, mime!(Text/Html; Charset=Utf8)),
-                _ => {
-                    *res.status_mut() = NotFound;
-                    res.send(b"Not Found").unwrap();
-                }
-            },
-            _ => {
-                *res.status_mut() = NotFound;
-                res.send(b"Not Found").unwrap();
-            }
-        }
-    }
-}

+ 0 - 36
rust-server/src/main.rs

@@ -1,36 +0,0 @@
-#[macro_use]
-extern crate mime;
-extern crate hyper;
-extern crate rustc_serialize;
-extern crate sha1;
-mod config;
-mod handler;
-
-use std::env;
-use std::process;
-use hyper::Server;
-
-fn actual_main() -> i32 {
-    let config_path = env::args().nth(1).unwrap_or("./config.json".to_string());
-    println!("Loading configuration from {}", config_path);
-    let cfg = match config::load(&config_path) {
-        Ok(cfg) => cfg,
-        Err(err) => {
-            println!("Failed to load configuration: {}", err);
-            return 1;
-        }
-    };
-    let port = cfg.port.unwrap_or(8000);
-    let site = handler::new(cfg);
-
-    let listen = format!("127.0.0.1:{}", port);
-    println!("Listening on {}", listen);
-    Server::http(listen.as_ref() as &str).unwrap()
-        .handle(site).unwrap();
-    return 0;
-}
-
-fn main() {
-    let exit_code = actual_main();
-    process::exit(exit_code);
-}