Enable access to WebSockets for dapps.

This commit is contained in:
Tomasz Drwięga 2017-10-18 16:28:45 +02:00
parent 2472a2be0c
commit 48ac6cbcf8
No known key found for this signature in database
GPG Key ID: D066F497E62CAF66
2 changed files with 22 additions and 11 deletions

View File

@ -862,6 +862,7 @@ impl Configuration {
fn ws_config(&self) -> Result<WsConfiguration, String> { fn ws_config(&self) -> Result<WsConfiguration, String> {
let ui = self.ui_config(); let ui = self.ui_config();
let http = self.http_config()?;
let conf = WsConfiguration { let conf = WsConfiguration {
enabled: self.ws_enabled(), enabled: self.ws_enabled(),
@ -873,6 +874,7 @@ impl Configuration {
signer_path: self.directories().signer.into(), signer_path: self.directories().signer.into(),
support_token_api: !self.args.flag_public_node, support_token_api: !self.args.flag_public_node,
ui_address: ui.address(), ui_address: ui.address(),
dapps_address: http.address(),
}; };
Ok(conf) Ok(conf)

View File

@ -150,6 +150,7 @@ pub struct WsConfiguration {
pub signer_path: PathBuf, pub signer_path: PathBuf,
pub support_token_api: bool, pub support_token_api: bool,
pub ui_address: Option<rpc::Host>, pub ui_address: Option<rpc::Host>,
pub dapps_address: Option<rpc::Host>,
} }
impl Default for WsConfiguration { impl Default for WsConfiguration {
@ -165,6 +166,7 @@ impl Default for WsConfiguration {
signer_path: replace_home(&data_dir, "$BASE/signer").into(), signer_path: replace_home(&data_dir, "$BASE/signer").into(),
support_token_api: true, support_token_api: true,
ui_address: Some("127.0.0.1:8180".into()), ui_address: Some("127.0.0.1:8180".into()),
dapps_address: Some("127.0.0.1:8545".into()),
} }
} }
} }
@ -220,8 +222,8 @@ pub fn new_ws<D: rpc_apis::Dependencies>(
let remote = deps.remote.clone(); let remote = deps.remote.clone();
let ui_address = conf.ui_address.clone(); let ui_address = conf.ui_address.clone();
let allowed_origins = into_domains(with_domain(conf.origins, domain, &ui_address)); let allowed_origins = into_domains(with_domain(conf.origins, domain, &ui_address, &conf.dapps_address));
let allowed_hosts = into_domains(with_domain(conf.hosts, domain, &Some(url.clone().into()))); let allowed_hosts = into_domains(with_domain(conf.hosts, domain, &Some(url.clone().into()), &None));
let signer_path; let signer_path;
let path = match conf.support_token_api && conf.ui_address.is_some() { let path = match conf.support_token_api && conf.ui_address.is_some() {
@ -269,7 +271,7 @@ pub fn new_http<D: rpc_apis::Dependencies>(
let remote = deps.remote.clone(); let remote = deps.remote.clone();
let cors_domains = into_domains(conf.cors); let cors_domains = into_domains(conf.cors);
let allowed_hosts = into_domains(with_domain(conf.hosts, domain, &Some(url.clone().into()))); let allowed_hosts = into_domains(with_domain(conf.hosts, domain, &Some(url.clone().into()), &None));
let start_result = rpc::start_http( let start_result = rpc::start_http(
&addr, &addr,
@ -311,20 +313,27 @@ fn into_domains<T: From<String>>(items: Option<Vec<String>>) -> DomainsValidatio
items.map(|vals| vals.into_iter().map(T::from).collect()).into() items.map(|vals| vals.into_iter().map(T::from).collect()).into()
} }
fn with_domain(items: Option<Vec<String>>, domain: &str, address: &Option<rpc::Host>) -> Option<Vec<String>> { fn with_domain(items: Option<Vec<String>>, domain: &str, ui_address: &Option<rpc::Host>, dapps_address: &Option<rpc::Host>) -> Option<Vec<String>> {
fn extract_port(s: &str) -> Option<u16> { fn extract_port(s: &str) -> Option<u16> {
s.split(':').nth(1).and_then(|s| s.parse().ok()) s.split(':').nth(1).and_then(|s| s.parse().ok())
} }
items.map(move |items| { items.map(move |items| {
let mut items = items.into_iter().collect::<HashSet<_>>(); let mut items = items.into_iter().collect::<HashSet<_>>();
if let Some(host) = address.clone() { {
items.insert(host.to_string()); let mut add_hosts = |address: &Option<rpc::Host>| {
items.insert(host.replace("127.0.0.1", "localhost")); if let Some(host) = address.clone() {
items.insert(format!("http://*.{}", domain)); //proxypac items.insert(host.to_string());
if let Some(port) = extract_port(&*host) { items.insert(host.replace("127.0.0.1", "localhost"));
items.insert(format!("http://*.{}:{}", domain, port)); items.insert(format!("http://*.{}", domain)); //proxypac
} if let Some(port) = extract_port(&*host) {
items.insert(format!("http://*.{}:{}", domain, port));
}
}
};
add_hosts(ui_address);
add_hosts(dapps_address);
} }
items.into_iter().collect() items.into_iter().collect()
}) })