solana-recover 1.1.3

A comprehensive Solana wallet recovery and account management tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use crate::core::{Result, SolanaRecoverError};
use std::sync::Arc;
use std::time::{Duration, Instant};
use dashmap::DashMap;
use std::sync::atomic::{AtomicU64, Ordering};

/// Token bucket implementation for rate limiting
#[derive(Debug)]
struct TokenBucket {
    max_tokens: u32,
    tokens: tokio::sync::Mutex<u32>,
    refill_interval: Duration,
    last_refill: tokio::sync::Mutex<Instant>,
}

impl TokenBucket {
    fn new(max_tokens: u32) -> Self {
        Self {
            max_tokens,
            tokens: tokio::sync::Mutex::new(max_tokens),
            refill_interval: Duration::from_secs(1) / max_tokens as u32,
            last_refill: tokio::sync::Mutex::new(Instant::now()),
        }
    }

    async fn acquire(&self) -> Result<()> {
        self.refill_tokens().await;
        
        let mut tokens = self.tokens.lock().await;
        if *tokens > 0 {
            *tokens -= 1;
            Ok(())
        } else {
            Err(SolanaRecoverError::RateLimitExceeded("Rate limit exceeded".to_string()))
        }
    }

    async fn refill_tokens(&self) {
        let mut tokens = self.tokens.lock().await;
        let mut last_refill = self.last_refill.lock().await;
        
        let now = Instant::now();
        let elapsed = now.saturating_duration_since(*last_refill);
        
        if elapsed >= self.refill_interval {
            let tokens_to_add = (elapsed.as_millis() / self.refill_interval.as_millis()) as u32;
            *tokens = (*tokens + tokens_to_add).min(self.max_tokens);
            *last_refill = now;
        }
    }
}

/// Distributed rate limiter that prevents bypass through concurrent connections
#[derive(Debug)]
pub struct DistributedRateLimiter {
    // Per-key rate limiting buckets
    buckets: Arc<DashMap<String, Arc<TokenBucket>>>,
    // Global rate limiting
    global_limit: Arc<AtomicU64>,
    window_start: Arc<AtomicU64>,
    max_requests_per_window: u64,
    window_duration: Duration,
    // Configuration
    default_bucket_size: u32,
    max_buckets: usize,
}

impl DistributedRateLimiter {
    /// Create a new distributed rate limiter
    /// 
    /// # Arguments
    /// * `max_requests_per_window` - Maximum requests per time window globally
    /// * `window_duration` - Duration of the time window
    /// * `default_bucket_size` - Default tokens per bucket
    /// * `max_buckets` - Maximum number of buckets to prevent memory exhaustion
    pub fn new(
        max_requests_per_window: u64,
        window_duration: Duration,
        default_bucket_size: u32,
        max_buckets: usize,
    ) -> Self {
        Self {
            buckets: Arc::new(DashMap::new()),
            global_limit: Arc::new(AtomicU64::new(0)),
            window_start: Arc::new(AtomicU64::new(
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs() / window_duration.as_secs()
            )),
            max_requests_per_window,
            window_duration,
            default_bucket_size,
            max_buckets,
        }
    }

    /// Create with sensible defaults
    pub fn with_defaults() -> Self {
        Self::new(
            1000, // 1000 requests per minute globally
            Duration::from_secs(60),
            100,  // 100 requests per second per key
            10000, // Max 10k buckets
        )
    }

    /// Acquire a permit for the given key
    pub async fn acquire(&self, key: &str) -> Result<()> {
        // Check global rate limit first
        self.check_global_limit().await?;
        
        // Check per-key rate limit
        self.check_key_limit(key).await?;
        
        // Increment global counter
        self.global_limit.fetch_add(1, Ordering::Relaxed);
        
        Ok(())
    }

    /// Check global rate limit
    async fn check_global_limit(&self) -> Result<()> {
        let current_window = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs() / self.window_duration.as_secs();
        let window_start = self.window_start.load(Ordering::Relaxed);
        
        // Reset if we're in a new window
        if current_window != window_start {
            self.window_start.store(current_window, Ordering::Relaxed);
            self.global_limit.store(0, Ordering::Relaxed);
        }
        
        let global_count = self.global_limit.load(Ordering::Relaxed);
        if global_count >= self.max_requests_per_window {
            return Err(SolanaRecoverError::RateLimitExceeded(
                format!("Global rate limit exceeded: {}/{}", global_count, self.max_requests_per_window)
            ));
        }
        
        Ok(())
    }

    /// Check per-key rate limit
    async fn check_key_limit(&self, key: &str) -> Result<()> {
        // Enforce maximum number of buckets to prevent memory exhaustion
        if self.buckets.len() >= self.max_buckets {
            // Try to clean up old buckets
            self.cleanup_old_buckets().await;
            
            if self.buckets.len() >= self.max_buckets {
                return Err(SolanaRecoverError::RateLimitExceeded(
                    "Too many concurrent rate limiters".to_string()
                ));
            }
        }
        
        let bucket = self.buckets.entry(key.to_string())
            .or_insert_with(|| Arc::new(TokenBucket::new(self.default_bucket_size)));
        
        bucket.acquire().await
    }

    /// Clean up old buckets to prevent memory leaks
    async fn cleanup_old_buckets(&self) {
        let now = Instant::now();
        let mut to_remove = Vec::new();
        
        for entry in self.buckets.iter() {
            let bucket = entry.value();
            let last_refill = bucket.last_refill.lock().await;
            
            // Remove buckets that haven't been used for 5 minutes
            if now.duration_since(*last_refill) > Duration::from_secs(300) {
                to_remove.push(entry.key().clone());
            }
        }
        
        for key in to_remove {
            self.buckets.remove(&key);
        }
    }

    /// Get current statistics
    pub fn get_stats(&self) -> RateLimiterStats {
        RateLimiterStats {
            global_requests: self.global_limit.load(Ordering::Relaxed),
            max_global_requests: self.max_requests_per_window,
            active_buckets: self.buckets.len(),
            max_buckets: self.max_buckets,
        }
    }

    /// Reset all rate limits (for testing/admin use)
    pub async fn reset(&self) {
        self.global_limit.store(0, Ordering::Relaxed);
        self.buckets.clear();
    }

    /// Set custom rate limit for a specific key
    pub async fn set_key_rate_limit(&self, key: &str, tokens_per_second: u32) -> Result<()> {
        let bucket = Arc::new(TokenBucket::new(tokens_per_second));
        self.buckets.insert(key.to_string(), bucket);
        Ok(())
    }

    /// Remove rate limit for a specific key
    pub fn remove_key_rate_limit(&self, key: &str) {
        self.buckets.remove(key);
    }
}

/// Rate limiter statistics
#[derive(Debug, Clone)]
pub struct RateLimiterStats {
    pub global_requests: u64,
    pub max_global_requests: u64,
    pub active_buckets: usize,
    pub max_buckets: usize,
}

impl RateLimiterStats {
    /// Calculate global utilization percentage
    pub fn global_utilization(&self) -> f64 {
        if self.max_global_requests == 0 {
            0.0
        } else {
            (self.global_requests as f64 / self.max_global_requests as f64) * 100.0
        }
    }

    /// Calculate bucket utilization percentage
    pub fn bucket_utilization(&self) -> f64 {
        if self.max_buckets == 0 {
            0.0
        } else {
            (self.active_buckets as f64 / self.max_buckets as f64) * 100.0
        }
    }
}

#[async_trait::async_trait]
impl crate::rpc::RateLimiter for DistributedRateLimiter {
    async fn acquire(&self) -> Result<()> {
        // Use a default key for the trait implementation
        self.acquire("default").await
    }
}

/// Enhanced rate limiter with IP-based and user-based limiting
#[derive(Debug)]
pub struct EnhancedRateLimiter {
    inner: Arc<DistributedRateLimiter>,
    // IP-based limiting
    ip_buckets: Arc<DashMap<String, Arc<TokenBucket>>>,
    // User-based limiting
    user_buckets: Arc<DashMap<String, Arc<TokenBucket>>>,
    // Endpoint-based limiting
    endpoint_buckets: Arc<DashMap<String, Arc<TokenBucket>>>,
}

impl EnhancedRateLimiter {
    pub fn new(config: RateLimiterConfig) -> Self {
        let inner = Arc::new(DistributedRateLimiter::new(
            config.global_requests_per_window,
            config.window_duration,
            config.default_bucket_size,
            config.max_buckets,
        ));

        Self {
            inner,
            ip_buckets: Arc::new(DashMap::new()),
            user_buckets: Arc::new(DashMap::new()),
            endpoint_buckets: Arc::new(DashMap::new()),
        }
    }

    /// Acquire permit with multiple limiting strategies
    pub async fn acquire_multi(&self, request: &RateLimitRequest) -> Result<()> {
        // Check global limit
        self.inner.acquire("global").await?;
        
        // Check IP limit if provided
        if let Some(ip) = &request.ip_address {
            self.check_ip_limit(ip).await?;
        }
        
        // Check user limit if provided
        if let Some(user_id) = &request.user_id {
            self.check_user_limit(user_id).await?;
        }
        
        // Check endpoint limit if provided
        if let Some(endpoint) = &request.endpoint {
            self.check_endpoint_limit(endpoint).await?;
        }
        
        Ok(())
    }

    async fn check_ip_limit(&self, ip: &str) -> Result<()> {
        let bucket = self.ip_buckets.entry(ip.to_string())
            .or_insert_with(|| Arc::new(TokenBucket::new(50))); // 50 requests per second per IP
        
        bucket.acquire().await
    }

    async fn check_user_limit(&self, user_id: &str) -> Result<()> {
        let bucket = self.user_buckets.entry(user_id.to_string())
            .or_insert_with(|| Arc::new(TokenBucket::new(20))); // 20 requests per second per user
        
        bucket.acquire().await
    }

    async fn check_endpoint_limit(&self, endpoint: &str) -> Result<()> {
        let bucket = self.endpoint_buckets.entry(endpoint.to_string())
            .or_insert_with(|| Arc::new(TokenBucket::new(200))); // 200 requests per second per endpoint
        
        bucket.acquire().await
    }

    /// Get comprehensive statistics
    pub fn get_comprehensive_stats(&self) -> ComprehensiveRateLimiterStats {
        let inner_stats = self.inner.get_stats();
        
        ComprehensiveRateLimiterStats {
            global: inner_stats,
            ip_buckets: self.ip_buckets.len(),
            user_buckets: self.user_buckets.len(),
            endpoint_buckets: self.endpoint_buckets.len(),
        }
    }
}

/// Rate limiter configuration
#[derive(Debug, Clone)]
pub struct RateLimiterConfig {
    pub global_requests_per_window: u64,
    pub window_duration: Duration,
    pub default_bucket_size: u32,
    pub max_buckets: usize,
}

impl Default for RateLimiterConfig {
    fn default() -> Self {
        Self {
            global_requests_per_window: 1000,
            window_duration: Duration::from_secs(60),
            default_bucket_size: 100,
            max_buckets: 10000,
        }
    }
}

/// Rate limit request
#[derive(Debug, Clone)]
pub struct RateLimitRequest {
    pub ip_address: Option<String>,
    pub user_id: Option<String>,
    pub endpoint: Option<String>,
}

/// Comprehensive rate limiter statistics
#[derive(Debug, Clone)]
pub struct ComprehensiveRateLimiterStats {
    pub global: RateLimiterStats,
    pub ip_buckets: usize,
    pub user_buckets: usize,
    pub endpoint_buckets: usize,
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[tokio::test]
    async fn test_distributed_rate_limiter() {
        let limiter = DistributedRateLimiter::new(10, Duration::from_secs(1), 5, 100);
        
        // Should allow first 5 requests
        for i in 0..5 {
            let result = limiter.acquire("test_key").await;
            assert!(result.is_ok(), "Request {} should succeed", i);
        }
        
        // 6th request should fail
        let result = limiter.acquire("test_key").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_global_rate_limit() {
        let limiter = DistributedRateLimiter::new(3, Duration::from_secs(1), 10, 100);
        
        // Should allow first 3 requests globally
        for i in 0..3 {
            let result = limiter.acquire(&format!("key_{}", i)).await;
            assert!(result.is_ok(), "Global request {} should succeed", i);
        }
        
        // 4th request should fail globally
        let result = limiter.acquire("another_key").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_enhanced_rate_limiter() {
        let config = RateLimiterConfig::default();
        let limiter = EnhancedRateLimiter::new(config);
        
        let request = RateLimitRequest {
            ip_address: Some("127.0.0.1".to_string()),
            user_id: Some("test_user".to_string()),
            endpoint: Some("/api/scan".to_string()),
        };
        
        // Should succeed
        let result = limiter.acquire_multi(&request).await;
        assert!(result.is_ok());
    }
}