init
This commit is contained in:
commit
715ac91099
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
target/
|
||||
data/
|
||||
.idea/
|
2504
Cargo.lock
generated
Normal file
2504
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
Cargo.toml
Normal file
15
Cargo.toml
Normal file
@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "dtan_relay"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
nostr-sdk = { git = "https://github.com/rust-nostr/nostr.git", rev = "7351b86c17cb06869a69cb49645d2e9809a9c1a9", package = "nostr-sdk", features = ["lmdb"] }
|
||||
nostr-relay-builder = { git = "https://github.com/rust-nostr/nostr.git", rev = "7351b86c17cb06869a69cb49645d2e9809a9c1a9", package = "nostr-relay-builder" }
|
||||
log = "0.4.22"
|
||||
config = { version = "0.15.4", features = ["yaml"] }
|
||||
pretty_env_logger = "0.5.0"
|
||||
tokio = { version = "1.42.0", features = ["rt", "rt-multi-thread", "macros"] }
|
||||
clap = { version = "4.5.23", features = ["derive"] }
|
||||
anyhow = "1.0.95"
|
||||
serde = { version = "1.0.216", features = ["derive"] }
|
11
config.yml
Normal file
11
config.yml
Normal file
@ -0,0 +1,11 @@
|
||||
# Location where the relay will store its data
|
||||
database_dir: "./data"
|
||||
|
||||
# Bootstrap relays to discover content
|
||||
relays:
|
||||
- "wss://relay.damus.io"
|
||||
- "wss://nos.lol"
|
||||
- "wss://relay.nostr.band"
|
||||
|
||||
# Listen address of dtan relay
|
||||
relay_listen: "0.0.0.0:8000"
|
92
src/main.rs
Normal file
92
src/main.rs
Normal file
@ -0,0 +1,92 @@
|
||||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
use config::Config;
|
||||
use nostr_relay_builder::builder::{PolicyResult, WritePolicy};
|
||||
use nostr_relay_builder::prelude::Event;
|
||||
use nostr_relay_builder::{LocalRelay, RelayBuilder};
|
||||
use nostr_sdk::prelude::async_trait;
|
||||
use nostr_sdk::{Client, NostrLMDB};
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashSet;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(about, version)]
|
||||
struct Args {
|
||||
#[arg(long)]
|
||||
pub config: Option<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Settings {
|
||||
/// Path to store events
|
||||
pub database_dir: Option<PathBuf>,
|
||||
|
||||
/// List of relays to bootstrap from
|
||||
pub relays: Vec<String>,
|
||||
|
||||
/// Listen addr for relay
|
||||
pub relay_listen: Option<SocketAddr>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct AcceptKinds(HashSet<u16>);
|
||||
|
||||
impl AcceptKinds {
|
||||
pub fn from<I>(input: I) -> Self
|
||||
where
|
||||
I: Into<HashSet<u16>>,
|
||||
{
|
||||
Self(input.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl WritePolicy for AcceptKinds {
|
||||
async fn admit_event(&self, event: &Event, _addr: &SocketAddr) -> PolicyResult {
|
||||
if !self.0.contains(&event.kind.as_u16()) {
|
||||
PolicyResult::Reject("kind not accepted".to_string())
|
||||
} else {
|
||||
PolicyResult::Accept
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
pretty_env_logger::init();
|
||||
|
||||
let args = Args::parse();
|
||||
|
||||
let config: Settings = Config::builder()
|
||||
.add_source(config::File::from(
|
||||
args.config.unwrap_or(PathBuf::from("./config.yml")),
|
||||
))
|
||||
.build()?
|
||||
.try_deserialize()?;
|
||||
|
||||
let db = NostrLMDB::open(config.database_dir.unwrap_or(PathBuf::from("./data")))?;
|
||||
let db = Arc::new(db);
|
||||
|
||||
let client = Client::builder().database(db.clone()).build();
|
||||
for relay in &config.relays {
|
||||
client.add_relay(relay).await?;
|
||||
}
|
||||
client.connect().await;
|
||||
|
||||
let addr = config
|
||||
.relay_listen
|
||||
.unwrap_or(SocketAddr::new([0, 0, 0, 0].into(), 8000));
|
||||
let relay = RelayBuilder::default()
|
||||
.database(db.clone())
|
||||
.addr(addr.ip())
|
||||
.port(addr.port())
|
||||
.write_policy(AcceptKinds::from([0, 2003, 2004]));
|
||||
|
||||
LocalRelay::run(relay).await?;
|
||||
|
||||
// start scraper
|
||||
Ok(())
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user