Skip to content

Commit

Permalink
Merge pull request #119 from cryptape/default-use-rustls
Browse files Browse the repository at this point in the history
default remove openssl dependence
  • Loading branch information
driftluo authored May 22, 2019
2 parents 31133ba + 9639c8b commit 6167b75
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 36 deletions.
6 changes: 3 additions & 3 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ $ cargo install --path .
```
会安装在 `~/.cargo/bin/cita-cli`

如果需要支持 https 请求, 确保 build 环境中存在 `openssl` 并按如下命令编译:
如果需要使用 `openssl` 去支持 https 请求, 确保 build 环境中存在 `openssl` 并按如下命令编译:

```bash
$ cd cita-cli/cita-cli
$ cargo install --features tls --path .
$ cargo install --features openssl --path .
```

> `openssl` 静态编译在 [release](https://github.com/cryptape/cita-cli/releases)
> `rustls` 静态编译在 [release](https://github.com/cryptape/cita-cli/releases)
> 并默认支持 https 请求。
#### 编译 Linux 跨平台版本
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ $ cargo install --path .
```
It will install to `~/.cargo/bin/cita-cli`.

If you want to support https requests, make sure that `openssl` exists in the build environment and
If you want to use `openssl` to support https requests, make sure that `openssl` exists in the build environment and
compile with the following command:

```bash
$ cd cita-cli/cita-cli
$ cargo install --features tls --path .
$ cargo install --features openssl --path .
```

> `openssl` is statically compiled in [release](https://github.com/cryptape/cita-cli/releases),
> `rustls` is statically compiled in [release](https://github.com/cryptape/cita-cli/releases),
> and https requests is supported by default.
#### Compile the Linux cross-platform version
Expand Down
7 changes: 2 additions & 5 deletions cita-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cita-cli"
version = "0.19.5"
version = "0.19.6"
authors = ["piaoliu <[email protected]>", "Qian Linfeng <[email protected]>"]
build = "build.rs"
edition = "2018"
Expand All @@ -20,9 +20,6 @@ dirs = "^1.0.3"
regex = "^1.0.4"
## lazy_static = "^1.0"

[build-dependencies]
git2 = "^0.7.5"

[features]
default = []
tls = ["cita-tool/tls"]
openssl = ["cita-tool/openssl"]
47 changes: 35 additions & 12 deletions cita-cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,53 @@ use std::fs::File;
use std::io::Write;
use std::path::Path;

use git2::Repository;

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("build_info.rs");
let mut f = File::create(&dest_path).unwrap();

let commit_id = match Repository::discover(".") {
Ok(repo) => repo
.revparse("HEAD")
.map(|rev_spec| rev_spec.from().map(|obj| obj.id().to_string()))
.unwrap()
.unwrap(),
Err(_) => ("unknown".to_string()),
};

let code = format!(
"
pub fn get_commit_id() -> &'static str {{
{:?}
}}
",
commit_id
format!(
"{} {}",
get_commit_describe().unwrap_or_default(),
get_commit_date().unwrap_or_default()
)
);

f.write_all(code.as_bytes()).unwrap();
}

pub fn get_commit_describe() -> Option<String> {
std::process::Command::new("git")
.args(&["describe", "--dirty", "--tags"])
.output()
.ok()
.and_then(|r| {
String::from_utf8(r.stdout).ok().map(|s| {
s.trim()
.splitn(2, "-")
.collect::<Vec<&str>>()
.pop()
.unwrap_or_default()
.to_string()
})
})
}

pub fn get_commit_date() -> Option<String> {
std::process::Command::new("git")
.env("TZ", "UTC")
.args(&["log", "-1", "--date=short-local", "--pretty=format:%cd"])
.output()
.ok()
.and_then(|r| {
String::from_utf8(r.stdout)
.ok()
.map(|s| s.trim().to_string())
})
}
8 changes: 4 additions & 4 deletions cita-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const DEFAULT_JSONRPC_URL: &str = "http://127.0.0.1:1337";
fn main() {
dotenv().ok();
let version = format!(
"{}+{}, {}",
"{}-{}, {}",
crate_version!(),
get_commit_id(),
feature_version()
Expand Down Expand Up @@ -79,9 +79,9 @@ fn main() {
}

fn feature_version() -> String {
if cfg!(feature = "tls") {
"support tls".to_owned()
if cfg!(feature = "openssl") {
"use openssl".to_owned()
} else {
"no other support".to_owned()
"use rustls".to_owned()
}
}
3 changes: 2 additions & 1 deletion cita-tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ uuid = { version = "0.7", features = ["serde", "v4"] }
failure = "^0.1.1"
ethabi = "^7.0"
tool-derive = { path = "../tool-derive" }
hyper-rustls = "0.16.1"
hyper-tls = { version = "^0.3", optional = true }

[features]
default = []
tls = ["hyper-tls"]
openssl = ["hyper-tls"]
15 changes: 7 additions & 8 deletions cita-tool/src/client/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use failure::Fail;
use futures::{future::join_all, future::JoinAll, sync, Future, Stream};
use hex::{decode, encode};
use hyper::{client::HttpConnector, Body, Client as HyperClient, Request, Uri};
#[cfg(feature = "tls")]
use hyper_tls::HttpsConnector;
use protobuf::{parse_from_bytes, Message};
use serde;
use serde_json;
Expand Down Expand Up @@ -1083,13 +1081,14 @@ where

impl Transfer<JsonRpcResponse, ToolError> for Client {}

#[cfg(feature = "tls")]
pub(crate) fn create_client() -> HyperClient<HttpsConnector<HttpConnector>> {
let https = HttpsConnector::new(4).unwrap();
#[cfg(feature = "openssl")]
pub(crate) fn create_client() -> HyperClient<hyper_tls::HttpsConnector<HttpConnector>> {
let https = hyper_tls::HttpsConnector::new(4).unwrap();
HyperClient::builder().build::<_, Body>(https)
}

#[cfg(not(feature = "tls"))]
pub(crate) fn create_client() -> HyperClient<HttpConnector> {
HyperClient::new()
#[cfg(not(feature = "openssl"))]
pub(crate) fn create_client() -> HyperClient<hyper_rustls::HttpsConnector<HttpConnector>> {
let https = hyper_rustls::HttpsConnector::new(4);
HyperClient::builder().build::<_, Body>(https)
}

0 comments on commit 6167b75

Please sign in to comment.