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
use crate::relay_service::relay::Relay;
use crate::{Context, RelayServiceOptions};
use core::str::from_utf8;
use ockam_core::compat::boxed::Box;
use ockam_core::{Address, Any, DenyAll, Result, Routed, Worker};
use ockam_node::WorkerBuilder;

/// Alias worker to register remote workers under local names.
///
/// To talk with this worker, you can use the
/// [`RemoteRelay`](crate::remote::RemoteRelay) which is a
/// compatible client for this server.
#[non_exhaustive]
pub struct RelayService {
    options: RelayServiceOptions,
}

impl RelayService {
    /// Start a forwarding service
    pub async fn create(
        ctx: &Context,
        address: impl Into<Address>,
        options: RelayServiceOptions,
    ) -> Result<()> {
        let address = address.into();

        options.setup_flow_control_for_relay_service(ctx.flow_controls(), &address);

        let service_incoming_access_control = options.service_incoming_access_control.clone();

        let s = Self { options };

        WorkerBuilder::new(s)
            .with_address(address.clone())
            .with_incoming_access_control_arc(service_incoming_access_control)
            .with_outgoing_access_control(DenyAll)
            .start(ctx)
            .await?;

        info!("Relay service started at {address}");

        Ok(())
    }
}

#[crate::worker]
impl Worker for RelayService {
    type Context = Context;
    type Message = Any;

    async fn handle_message(
        &mut self,
        ctx: &mut Self::Context,
        msg: Routed<Self::Message>,
    ) -> Result<()> {
        let forward_route = msg.return_route();
        let payload = msg.payload();

        let random_address = Address::random_tagged("Relay.service");

        // TODO: assume that the first byte is length, ignore it.
        // We have to improve this actually parse the payload.
        let address = match payload.get(1..) {
            Some(address) => match from_utf8(address) {
                Ok(v) if v != "register" => Address::from_string(v),
                _ => random_address,
            },
            None => random_address,
        };

        self.options
            .setup_flow_control_for_relay(ctx.flow_controls(), &address);

        Relay::create(
            ctx,
            address,
            forward_route,
            payload.to_vec(),
            self.options.relays_incoming_access_control.clone(),
        )
        .await?;

        Ok(())
    }
}