Files
grin-wallet/config/src/comments.rs
T
ardocratandwiesche 9e3ec0ed5b Global config instance (#769)
* config: global instance

* fix: cli tests

* fix: owner api tests

* config: save tor config

* fix: owner rpc tests

* fix: set config file path on init and owner api call

* tor: store launched services, get global config at command loop, remove unused listen args, check for empty global config to set default, do not save empty tor config at owner

* api: update config in memory after successful file write

* arti: restart proxy in loop

* fix: comment for tor config save

* config: set or init, update without error

* config: scoped tor config

* build: update lock file

* config: ability to store multiple configurations at memory from config path

* tor: restart service on config change

* fix: owner api test

* foreign: pass tor config from args, stop service, error handling

* arti: move runtime wrapper to single file

* config: read existing config from file at create_config

* config: handle error on read

* arti: single dir for cache

* foreign: register config listener before launch

* config: save temp

* config: remove unused error type

* config: make path and members not optional, return an error on config creation if it exists, pass default tor config to send if not set

* arti: non public runtime wrapper

* config: migrate comments

* fix: rpc tests

* config: return from cache if exists

* config: save disabled tor config when null provided at owner and tor section was not set

* config: update tor with args at listener

* config: pass pathbuf directly on creation

* config: unique tmp file name, setting permissions and providing config version

* fix global config handling

* fix config persistence

* fix tor service restart

* fix config synchronization

* require wallet close before changing data directory

* unify global config updates

---------

Co-authored-by: wiesche <wiesche89@googlemail.com>
2026-08-09 19:49:29 +02:00

343 lines
7.2 KiB
Rust

// Copyright 2021 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Comments for configuration + injection into output .toml
use std::collections::HashMap;
/// maps entries to Comments that should precede them
fn comments() -> HashMap<String, String> {
let mut retval = HashMap::new();
retval.insert(
"config_file_version".to_string(),
"
#Version of the Generated Configuration File for the Grin Wallet (DO NOT EDIT)
"
.to_string(),
);
retval.insert(
"[wallet]".to_string(),
"
#########################################
### WALLET CONFIGURATION ###
#########################################
"
.to_string(),
);
retval.insert(
"api_listen_port".to_string(),
"
#path of TLS certificate file, self-signed certificates are not supported
#tls_certificate_file = \"\"
#private key for the TLS certificate
#tls_certificate_key = \"\"
#port for wallet listener
"
.to_string(),
);
retval.insert(
"owner_api_listen_port".to_string(),
"
#port for wallet owner api
"
.to_string(),
);
retval.insert(
"api_secret_path".to_string(),
"
#path of the secret token used by the API to authenticate the calls
#comment it to disable basic auth
"
.to_string(),
);
retval.insert(
"check_node_api_http_addr".to_string(),
"
#where the wallet should find a running node
"
.to_string(),
);
retval.insert(
"node_api_request_timeout_secs".to_string(),
"
#total timeout for requests to node API
"
.to_string(),
);
retval.insert(
"node_api_secret_path".to_string(),
"
#location of the node api secret for basic auth on the Grin API
"
.to_string(),
);
retval.insert(
"owner_api_include_foreign".to_string(),
"
#include the foreign API endpoints on the same port as the owner
#API. Useful for networking environments like AWS ECS that make
#it difficult to access multiple ports on a single service.
"
.to_string(),
);
retval.insert(
"data_file_dir".to_string(),
"
#where to find wallet files (seed, data, etc)
"
.to_string(),
);
retval.insert(
"no_commit_cache".to_string(),
"
#If true, don't store calculated commits in the database
#better privacy, but at a performance cost of having to
#re-calculate commits every time they're used
"
.to_string(),
);
retval.insert(
"dark_background_color_scheme".to_string(),
"
#Whether to use the black background color scheme for command line
"
.to_string(),
);
retval.insert(
"accept_fee_base".to_string(),
"
#Minimum acceptable fee per unit of transaction weight
"
.to_string(),
);
retval.insert(
"[logging]".to_string(),
"
#Type of proxy, eg \"socks4\", \"socks5\", \"http\", \"https\"
#transport = \"https\"
#Proxy address, eg IP:PORT or Hostname
#server = \"\"
#Username for the proxy server authentification
#user = \"\"
#Password for the proxy server authentification
#pass = \"\"
#This computer goes through a firewall that only allows connections to certain ports (Optional)
#allowed_port = [80, 443]
#########################################
### LOGGING CONFIGURATION ###
#########################################
"
.to_string(),
);
retval.insert(
"log_to_stdout".to_string(),
"
#whether to log to stdout
"
.to_string(),
);
retval.insert(
"stdout_log_level".to_string(),
"
#log level for stdout: Error, Warning, Info, Debug, Trace
"
.to_string(),
);
retval.insert(
"log_to_file".to_string(),
"
#whether to log to a file
"
.to_string(),
);
retval.insert(
"file_log_level".to_string(),
"
#log level for file: Error, Warning, Info, Debug, Trace
"
.to_string(),
);
retval.insert(
"log_file_path".to_string(),
"
#log file path
"
.to_string(),
);
retval.insert(
"log_file_append".to_string(),
"
#whether to append to the log file (true), or replace it on every run (false)
"
.to_string(),
);
retval.insert(
"log_max_size".to_string(),
"
#maximum log file size in bytes before performing log rotation
#comment it to disable log rotation
"
.to_string(),
);
retval.insert(
"[tor]".to_string(),
"
#########################################
### TOR CONFIGURATION ###
#########################################
"
.to_string(),
);
retval.insert(
"use_integrated".to_string(),
"
#Whether to use integrated Tor library
"
.to_string(),
);
retval.insert(
"skip_send_attempt".to_string(),
"
#Whether to skip send attempts (default false)
"
.to_string(),
);
retval.insert(
"use_tor_listener".to_string(),
"
#Whether to start tor listener on listener startup (default true)
"
.to_string(),
);
retval.insert(
"socks_proxy_addr".to_string(),
"
#Address of the running TOR (SOCKS) server
"
.to_string(),
);
retval.insert(
"send_config_dir".to_string(),
"
#Directory to output TOR configuration to when sending
"
.to_string(),
);
retval.insert(
"request_timeout_secs".to_string(),
"
#Tor request timeout in seconds
"
.to_string(),
);
retval.insert(
"bootstrap_timeout_secs".to_string(),
"
#Tor bootstrap timeout in seconds
"
.to_string(),
);
retval.insert(
"[tor.bridge]".to_string(),
"
#########################################
### TOR BRIDGE ###
#########################################
"
.to_string(),
);
retval.insert(
"[tor.proxy]".to_string(),
"
#Tor bridge relay: allow to send and receive via TOR in a country where it is censored.
#Enable it by entering a single bridge line. To disable it, you must comment it.
#Support of the transport: webtunnel, obfs4, and snowflake.
#webtunnel, obfs4proxy or snowflake client binary must be installed and on your path.
#Custom path for client binary
#bridge_bin_path = \"\"
#For example, the bridge line must be in the following format for obfs4 transport: \"obfs4 [IP:PORT] [FINGERPRINT] cert=[CERT] iat-mode=[IAT-MODE]\"
#bridge_line = \"\"
#Plugging client option, needed only for snowflake (let it empty if you want to use the default option of tor) or debugging purpose
#client_option = \"\"
#########################################
### TOR PROXY ###
#########################################
"
.to_string(),
);
retval
}
fn get_key(line: &str) -> String {
if line.starts_with('[') && line.ends_with(']') {
line.to_owned()
} else if let Some((key, _)) = line.split_once('=') {
key.trim().to_owned()
} else {
"NOT_FOUND".to_owned()
}
}
pub fn insert_comments(orig: String) -> String {
let comments = comments();
let lines: Vec<&str> = orig.split('\n').collect();
let mut out_lines = vec![];
for l in lines {
let key = get_key(l);
if let Some(v) = comments.get(&key) {
out_lines.push(v.to_owned());
}
out_lines.push(l.to_owned());
out_lines.push("\n".to_owned());
}
let mut ret_val = String::from("");
for l in out_lines {
ret_val.push_str(&l);
}
ret_val
}