lingshu-gateway 0.10.0

Multi-platform messaging gateway for the Lingshu agent
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
//! # SMS adapter — Twilio REST API + webhook
//!
//! Sends outbound SMS via Twilio REST API. Receives inbound messages
//! via an axum webhook endpoint.
//!
//! ## Environment variables
//!
//! | Variable              | Required | Description                          |
//! |-----------------------|----------|--------------------------------------|
//! | `TWILIO_ACCOUNT_SID`  | Yes      | Twilio Account SID                   |
//! | `TWILIO_AUTH_TOKEN`    | Yes      | Twilio Auth Token                    |
//! | `TWILIO_PHONE_NUMBER`  | Yes      | From-number in E.164 format          |
//! | `SMS_WEBHOOK_PORT`    | No       | Webhook port (default: 8082)         |
//! | `SMS_ALLOWED_USERS`   | No       | Comma-separated allowed phone numbers|
//!
//! ## Limits
//!
//! - Max message length: **1600** characters (~10 SMS segments)

use std::env;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use axum::extract::State;
use axum::routing::{get, post};
use axum::{Form, Router};
use base64::Engine;
use lingshu_types::Platform;
use serde::Deserialize;
use tokio::sync::mpsc;
use tracing::{debug, error, info, warn};

use crate::platform::{IncomingMessage, MessageMetadata, OutgoingMessage, PlatformAdapter};

const MAX_MESSAGE_LENGTH: usize = 1600;
const DEFAULT_WEBHOOK_PORT: u16 = 8082;
const TWILIO_API_BASE: &str = "https://api.twilio.com/2010-04-01/Accounts";

pub struct SmsAdapter {
    account_sid: String,
    auth_token: String,
    from_number: String,
    webhook_port: u16,
    allowed_users: Vec<String>,
}

impl SmsAdapter {
    pub fn from_env() -> Option<Self> {
        let account_sid = env::var("TWILIO_ACCOUNT_SID").ok()?;
        let auth_token = env::var("TWILIO_AUTH_TOKEN").ok()?;
        let from_number = env::var("TWILIO_PHONE_NUMBER").ok()?;
        let webhook_port = env::var("SMS_WEBHOOK_PORT")
            .ok()
            .and_then(|p| p.parse().ok())
            .unwrap_or(DEFAULT_WEBHOOK_PORT);
        let allowed_users = env::var("SMS_ALLOWED_USERS")
            .ok()
            .map(|s| {
                s.split(',')
                    .filter_map(normalize_phone_number)
                    .collect::<Vec<_>>()
            })
            .unwrap_or_default();

        Some(Self {
            account_sid,
            auth_token,
            from_number: normalize_phone_number(&from_number).unwrap_or(from_number),
            webhook_port,
            allowed_users,
        })
    }

    pub fn is_available() -> bool {
        env::var("TWILIO_ACCOUNT_SID").is_ok() && env::var("TWILIO_AUTH_TOKEN").is_ok()
    }

    fn basic_auth_header(&self) -> String {
        let creds = format!("{}:{}", self.account_sid, self.auth_token);
        let encoded = base64::engine::general_purpose::STANDARD.encode(creds.as_bytes());
        format!("Basic {encoded}")
    }
}

fn normalize_phone_number(value: &str) -> Option<String> {
    let compact: String = value
        .chars()
        .filter(|c| !c.is_ascii_whitespace() && !matches!(c, '-' | '(' | ')'))
        .collect();
    if compact.is_empty() {
        return None;
    }

    if let Some(rest) = compact.strip_prefix('+') {
        if !rest.is_empty() && rest.chars().all(|c| c.is_ascii_digit()) {
            return Some(format!("+{rest}"));
        }
        return None;
    }

    if let Some(rest) = compact.strip_prefix("00") {
        if !rest.is_empty() && rest.chars().all(|c| c.is_ascii_digit()) {
            return Some(format!("+{rest}"));
        }
        return None;
    }

    if compact.chars().all(|c| c.is_ascii_digit()) {
        return Some(format!("+{compact}"));
    }

    None
}

/// Twilio webhook form data for inbound SMS.
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct TwilioWebhookForm {
    #[serde(rename = "From")]
    from: Option<String>,
    #[serde(rename = "To")]
    to: Option<String>,
    #[serde(rename = "Body")]
    body: Option<String>,
    #[serde(rename = "MessageSid")]
    message_sid: Option<String>,
}

/// Validate Twilio `X-Twilio-Signature` header using HMAC-SHA1.
///
/// Algorithm (Twilio spec):
///   1. Concatenate webhook URL + sorted POST params as `key=value`
///   2. HMAC-SHA1 with the auth token as key
///   3. base64-encode and compare with the header value
///
/// Uses constant-time comparison to prevent timing side-channel attacks.
fn validate_twilio_signature(
    auth_token: &str,
    url: &str,
    params: &std::collections::BTreeMap<String, String>,
    signature_header: &str,
) -> bool {
    use hmac::{Hmac, Mac};
    use sha1::Sha1;
    use subtle::ConstantTimeEq;

    let mut data = url.to_string();
    for (key, value) in params {
        data.push_str(key);
        data.push_str(value);
    }

    let Ok(mut mac) = Hmac::<Sha1>::new_from_slice(auth_token.as_bytes()) else {
        return false;
    };
    mac.update(data.as_bytes());
    let expected = base64::engine::general_purpose::STANDARD.encode(mac.finalize().into_bytes());

    bool::from(expected.as_bytes().ct_eq(signature_header.as_bytes()))
}

/// Shared state for the webhook handler.
struct WebhookState {
    tx: mpsc::Sender<IncomingMessage>,
    allowed_users: Vec<String>,
    auth_token: String,
    webhook_url: String,
}

async fn handle_twilio_webhook(
    State(state): State<Arc<WebhookState>>,
    headers: axum::http::HeaderMap,
    Form(form): Form<TwilioWebhookForm>,
) -> &'static str {
    // ── Twilio signature validation ──────────────────────────────────
    if !state.auth_token.is_empty() {
        let sig = headers
            .get("X-Twilio-Signature")
            .and_then(|v| v.to_str().ok())
            .unwrap_or("");
        let mut params = std::collections::BTreeMap::new();
        if let Some(ref v) = form.from {
            params.insert("From".to_string(), v.clone());
        }
        if let Some(ref v) = form.to {
            params.insert("To".to_string(), v.clone());
        }
        if let Some(ref v) = form.body {
            params.insert("Body".to_string(), v.clone());
        }
        if let Some(ref v) = form.message_sid {
            params.insert("MessageSid".to_string(), v.clone());
        }
        if !validate_twilio_signature(&state.auth_token, &state.webhook_url, &params, sig) {
            warn!("Twilio signature validation failed — rejecting webhook");
            return "<Response></Response>";
        }
    }

    let from = normalize_phone_number(&form.from.unwrap_or_default()).unwrap_or_default();
    let body = form.body.unwrap_or_default();
    let message_sid = form.message_sid.unwrap_or_default();

    if body.trim().is_empty() {
        return "<Response></Response>";
    }

    if !state.allowed_users.is_empty() && !state.allowed_users.contains(&from) {
        debug!("SMS from unauthorized number: {}", from);
        return "<Response></Response>";
    }

    let incoming = IncomingMessage {
        platform: Platform::Sms,
        user_id: from.clone(),
        channel_id: Some(from.clone()),
        chat_type: crate::platform::ChatType::Dm,
        text: body,
        thread_id: None,
        metadata: MessageMetadata {
            message_id: Some(message_sid),
            channel_id: Some(from),
            ..Default::default()
        },
    };

    if state.tx.send(incoming).await.is_err() {
        warn!("SMS message channel closed");
    }

    // TwiML empty response (no auto-reply)
    "<Response></Response>"
}

#[async_trait]
impl PlatformAdapter for SmsAdapter {
    fn platform(&self) -> Platform {
        Platform::Sms
    }

    async fn start(&self, tx: mpsc::Sender<IncomingMessage>) -> anyhow::Result<()> {
        info!(
            "SMS adapter starting — webhook on port {}",
            self.webhook_port
        );

        let webhook_url = env::var("SMS_WEBHOOK_URL")
            .unwrap_or_else(|_| format!("http://localhost:{}/webhooks/twilio", self.webhook_port));

        let state = Arc::new(WebhookState {
            tx,
            allowed_users: self.allowed_users.clone(),
            auth_token: self.auth_token.clone(),
            webhook_url,
        });

        let app = Router::new()
            .route("/webhooks/twilio", post(handle_twilio_webhook))
            .route("/health", get(|| async { "ok" }))
            .with_state(state);

        let listener =
            tokio::net::TcpListener::bind(format!("0.0.0.0:{}", self.webhook_port)).await?;
        axum::serve(listener, app).await?;

        Ok(())
    }

    async fn send(&self, msg: OutgoingMessage) -> anyhow::Result<()> {
        let raw_to = msg
            .metadata
            .channel_id
            .as_deref()
            .ok_or_else(|| anyhow::anyhow!("No recipient phone number"))?;
        let to = normalize_phone_number(raw_to).ok_or_else(|| {
            anyhow::anyhow!(
                "Invalid SMS recipient '{}'; use an E.164 phone number or digits only",
                raw_to
            )
        })?;

        let url = format!("{}/{}/Messages.json", TWILIO_API_BASE, self.account_sid);
        let client = reqwest::Client::new();

        let resp = client
            .post(&url)
            .header("Authorization", self.basic_auth_header())
            .form(&[
                ("From", self.from_number.as_str()),
                ("To", to.as_str()),
                ("Body", &msg.text),
            ])
            .timeout(Duration::from_secs(30))
            .send()
            .await?;

        if !resp.status().is_success() {
            let status = resp.status();
            let text = resp.text().await.unwrap_or_default();
            error!("Twilio API error {}: {}", status, text);
            anyhow::bail!("Twilio API error {status}");
        }

        debug!("SMS sent to {}", to);
        Ok(())
    }

    fn format_response(&self, text: &str, _metadata: &MessageMetadata) -> String {
        text.to_string()
    }

    fn max_message_length(&self) -> usize {
        MAX_MESSAGE_LENGTH
    }

    fn supports_markdown(&self) -> bool {
        false
    }

    fn supports_images(&self) -> bool {
        false
    }

    fn supports_files(&self) -> bool {
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sms_max_length() {
        assert_eq!(MAX_MESSAGE_LENGTH, 1600);
    }

    #[test]
    fn phone_numbers_are_normalized_to_e164() {
        assert_eq!(
            normalize_phone_number("+1 (555) 123-4567"),
            Some("+15551234567".into())
        );
        assert_eq!(
            normalize_phone_number("00447911123456"),
            Some("+447911123456".into())
        );
        assert_eq!(
            normalize_phone_number("15551234567"),
            Some("+15551234567".into())
        );
    }

    #[test]
    fn invalid_phone_number_is_rejected() {
        assert_eq!(normalize_phone_number("abc123"), None);
        assert_eq!(normalize_phone_number(""), None);
    }

    #[test]
    fn twilio_signature_valid() {
        use std::collections::BTreeMap;
        let auth_token = "12345";
        let url = "https://mycompany.com/myapp.php?foo=1&bar=2";
        let mut params = BTreeMap::new();
        params.insert("CallSid".to_string(), "CA1234567890ABCDE".to_string());
        params.insert("Caller".to_string(), "+14158675310".to_string());
        params.insert("Digits".to_string(), "1234".to_string());
        params.insert("From".to_string(), "+14158675310".to_string());
        params.insert("To".to_string(), "+18005551212".to_string());

        // Compute expected signature for this test data
        use hmac::{Hmac, Mac};
        use sha1::Sha1;
        let mut data = url.to_string();
        for (key, value) in &params {
            data.push_str(key);
            data.push_str(value);
        }
        let mut mac = Hmac::<Sha1>::new_from_slice(auth_token.as_bytes()).expect("hmac");
        mac.update(data.as_bytes());
        let expected_sig =
            base64::engine::general_purpose::STANDARD.encode(mac.finalize().into_bytes());

        assert!(validate_twilio_signature(
            auth_token,
            url,
            &params,
            &expected_sig
        ));
    }

    #[test]
    fn twilio_signature_tampered() {
        use std::collections::BTreeMap;
        let params = BTreeMap::new();
        assert!(!validate_twilio_signature(
            "secret",
            "https://example.com/webhook",
            &params,
            "invalid-signature"
        ));
    }
}