Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Peer ID personalization #5

Merged
merged 5 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ Share anything with teammates across machines via CLI. Share is a tool for secur
- [Files](#files)
- [Messages](#messages)
- [Configuration](#configuration)
- [Whitelists](#whitelists)
- [Signed Certs](#SignedCertificate)
- [Whitelists](#whitelistsblacklists-ip-addresses)
- [Signed Certs](#signed-certificate)
- [Seed Key](#seeds-seed-key)
- [Update](#update)
- [Roadmap](#roadmap)
- [Contributing](#contributing)
Expand Down Expand Up @@ -125,28 +126,24 @@ The sender then attempts to send the secret, and if it is successful, `scs` rela

```yaml
port: 5555 #An optional port defaults to 0 if not present
# The folder path to store all items.
# Secrets will be stored at <path>/secrets.json
# Messages at <path>/messages.txt
# Files at <path>/nameoffile
## If "default" is passed, the folder path will be `scs`'s directory in the machine's local folder.
save_path: "default"
secret: #Optional during receive
secret: # Optional during receive
- key: foo
value: bar
- key: baz
value: woo
message: #Optional during receive
message: # Optional during receive
- new message from me
- test message
file: #Optional during receive
file: # Optional during receive
- "./dev_build.sh"
debug: 1 #Compulsory. 0 is for off, and 1 and above for on
debug: 1 # Compulsory. 0 is for off, and 1 and above for on
blacklists:
- 34.138.139.178
whitelists:
- 34.193.14.12
connection: trusted #or self
connection: trusted # or self
seed: "scsiscool"
```

```shell
Expand All @@ -164,6 +161,10 @@ connection: trusted #or self
Receivers can configure `scs` to only allow connections from users using a signed certificate from the CA. or just self-signed certificates.
Add a `connection: trusted` or `connection: self` to the configuration file.

### Seeds (Seed Key)
The backbone of `scs` is `PeerId`. A `PeerId` a randomly generated key whenever a session is started for both the receiver and the sender. As of `v0.1.3` of `scs`, `PeerId`s can now be deterministic, that is, a single `PeerId` can be used for life. To do this, you need to set what is called a "seed". The `PeerId` is generated with respect to this seed. As long as the seed key remains the same, the `PeerId` will remain the same.
The "seed" key is a string of any length lesser than 32. But for ease and optimal configuration, we recommend 4 or 5 letter words as in the above configuration file.

# Contributing

Contributions of any kind are welcome! See the [contributing guide](contributing.md).
Expand All @@ -173,7 +174,8 @@ Contributions of any kind are welcome! See the [contributing guide](contributing
# Roadmap

### Utilities
- [ ] Personalize peer ID + allow saving recipient info (address, port, etc.) and giving a proper name so one can do "scs send dante -m Hello"
- [x] Personalize peer ID.
- [ ] Allow saving recipient info (address, port, etc.) and giving a proper name so one can do "scs send dante -m Hello"
- [ ] Allow to always listen to specific addresses for an accessible data flow.

### Protocols
Expand Down
3 changes: 2 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ file: #Optional during receive
debug: 1 #Compulsory. 0 is for off and 1 and above for on
# blacklists:
# - 127.0.0.1
# - 34.138.139.178
# - 34.138.139.178
seed: "sour"
64 changes: 64 additions & 0 deletions share/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use anyhow::{Context, Result};
use libp2p::PeerId;
use rand::{distributions::Alphanumeric, Rng};
use serde::{Deserialize, Serialize};

use crate::{item::Secret, Cli, Mode};
Expand All @@ -21,6 +22,7 @@
save_path: PathBuf,
whitelists: Option<HashSet<Ipv4Addr>>,
blacklists: Option<HashSet<Ipv4Addr>>,
seed: String,
}

impl Config {
Expand Down Expand Up @@ -50,6 +52,11 @@
save_path: Config::create_default_path()?,
whitelists: None,
blacklists: None,
seed: rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(32)
.map(char::from)
.collect(),

Check warning on line 59 in share/src/config.rs

View check run for this annotation

Codecov / codecov/patch

share/src/config.rs#L55-L59

Added lines #L55 - L59 were not covered by tests
};
Ok(config)
}
Expand Down Expand Up @@ -119,6 +126,19 @@
pub fn blacklists(&self) -> Option<HashSet<Ipv4Addr>> {
self.blacklists.clone()
}

fn pad_seed_key(&self, mut s: String) -> String {
while s.len() < 32 {
s.push(' ');
}
s.truncate(32);
s
}

pub fn seed_key(&self) -> String {
let seed = self.seed.clone();
self.pad_seed_key(seed)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -155,6 +175,7 @@
save_path: PathBuf::from(test_file.parent().unwrap()),
whitelists: None,
blacklists: None,
seed: "test".to_string(),
};
Ok(config)
}
Expand All @@ -174,6 +195,7 @@
- new message from me
- test message
debug: 1
seed: test
"
);
let file = assert_fs::NamedTempFile::new("config.yml")?;
Expand All @@ -186,6 +208,7 @@

assert!(path.exists());
assert_eq!(config.save_path(), PathBuf::from(path));
assert_eq!(config.seed.len(), 4);

file.close()?;
Ok(())
Expand Down Expand Up @@ -249,4 +272,45 @@
assert_eq!(config.whitelists(), None);
Ok(())
}

#[test]
fn seed() -> Result<()> {
let seed_key = "greyhounds";

let yaml_config = format!(
"
port: 5555
save_path: 'default'
secret:
- key: foo
value: bar
- key: baz
value: woo
message:
- new message from me
- test message
debug: 1
seed: {seed_key}
"
);
let config: Config = serde_yaml::from_str(&yaml_config)?;

let padded_string = config.seed_key();
let padded_string = &padded_string[seed_key.len()..padded_string.len()];
assert_eq!(padded_string.len(), (32 - seed_key.len()));
Ok(())
}

#[test]
fn pad_string() -> Result<()> {
let s = "hi".to_string();
let config = make_config()?;
let padded_str = config.pad_seed_key(s.clone());

assert_eq!(padded_str.len(), 32);
assert_ne!(s, padded_str);
assert!(padded_str.contains(&s));

Ok(())
}
}
1 change: 1 addition & 0 deletions share/src/handlers/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ mod tests {
- {addr}
whitelists:
- {addr}
seed: config
"
);
let config: Config = serde_yaml::from_str(&yaml_config)?;
Expand Down
1 change: 1 addition & 0 deletions share/src/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ mod tests {
- new message from me
- test message
debug: 1
seed: make
"
);
let config: Config = serde_yaml::from_str(&yaml_config)?;
Expand Down
10 changes: 4 additions & 6 deletions share/src/network/hole_puncher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
swarm::{SwarmBuilder, SwarmEvent},
tcp, yamux, Multiaddr, PeerId, Transport,
};
use rand::Rng;
use tracing::{debug, error, info, instrument};

#[instrument(level = "trace")]
Expand All @@ -33,7 +32,7 @@
.to_string()
.parse()
.unwrap();
let secret_key_seed = rand::thread_rng().gen_range(0..100);
let secret_key_seed = config.seed_key();
let port = config.port();

let local_key = generate_ed25519(secret_key_seed);
Expand Down Expand Up @@ -211,9 +210,8 @@
})
}

fn generate_ed25519(secret_key_seed: u8) -> identity::Keypair {
let mut bytes = [0u8; 32];
bytes[0] = secret_key_seed;

fn generate_ed25519(mut secret_key_seed: String) -> identity::Keypair {
let bytes = unsafe { secret_key_seed.as_bytes_mut() };
println!("{}", bytes.len());

Check warning on line 215 in share/src/network/hole_puncher.rs

View check run for this annotation

Codecov / codecov/patch

share/src/network/hole_puncher.rs#L213-L215

Added lines #L213 - L215 were not covered by tests
identity::Keypair::ed25519_from_bytes(bytes).expect("only errors on wrong length")
}
Loading