xray_core/
lib.rs

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
use {
    app::{
        log::command::logger_service_client::LoggerServiceClient,
        proxyman::command::handler_service_client::HandlerServiceClient,
        router::command::routing_service_client::RoutingServiceClient,
        stats::command::stats_service_client::StatsServiceClient,
    },
    core::observatory::command::observatory_service_client::ObservatoryServiceClient,
    tonic::transport::{Channel, Error},
    transport::internet::grpc::grpc_service_client::GrpcServiceClient,
};

pub use prost_types;
pub use tonic;

pub mod app {
    pub mod commander {
        tonic::include_proto!("xray.app.commander");
    }

    pub mod dispatcher {
        tonic::include_proto!("xray.app.dispatcher");
    }

    /// <https://xtls.github.io/en/config/dns.html>
    pub mod dns {
        tonic::include_proto!("xray.app.dns");

        /// <https://xtls.github.io/en/config/fakedns.html>
        pub mod fakedns {
            tonic::include_proto!("xray.app.dns.fakedns");
        }
    }

    /// <https://xtls.github.io/en/config/log.html>
    pub mod log {
        tonic::include_proto!("xray.app.log");

        #[cfg(feature = "client")]
        pub mod command {
            tonic::include_proto!("xray.app.log.command");
        }
    }

    /// <https://xtls.github.io/en/config/metrics.html>
    pub mod metrics {
        tonic::include_proto!("xray.app.metrics");
    }

    /// <https://xtls.github.io/en/config/policy.html>
    pub mod policy {
        tonic::include_proto!("xray.app.policy");
    }

    pub mod proxyman {
        tonic::include_proto!("xray.app.proxyman");

        #[cfg(feature = "client")]
        pub mod command {
            tonic::include_proto!("xray.app.proxyman.command");
        }
    }

    /// <https://xtls.github.io/en/config/reverse.html>
    pub mod reverse {
        tonic::include_proto!("xray.app.reverse");
    }

    /// <https://xtls.github.io/en/config/routing.html>
    pub mod router {
        tonic::include_proto!("xray.app.router");

        #[cfg(feature = "client")]
        pub mod command {
            tonic::include_proto!("xray.app.router.command");
        }
    }

    /// <https://xtls.github.io/en/config/stats.html>
    pub mod stats {
        tonic::include_proto!("xray.app.stats");

        #[cfg(feature = "client")]
        pub mod command {
            tonic::include_proto!("xray.app.stats.command");
        }
    }
}

pub mod common {
    pub mod log {
        tonic::include_proto!("xray.common.log");
    }

    pub mod net {
        tonic::include_proto!("xray.common.net");
    }

    pub mod protocol {
        tonic::include_proto!("xray.common.protocol");
    }

    pub mod serial {
        tonic::include_proto!("xray.common.serial");
    }
}

/// <https://xtls.github.io/en/config/>
pub mod core {
    tonic::include_proto!("xray.core");

    /// <https://xtls.github.io/en/config/observatory.html>
    pub mod observatory {
        tonic::include_proto!("xray.core.app.observatory");

        #[cfg(feature = "client")]
        pub mod command {
            tonic::include_proto!("xray.core.app.observatory.command");
        }
    }
}

pub mod proxy {
    /// <https://xtls.github.io/en/config/outbounds/blackhole.html>
    pub mod blackhole {
        tonic::include_proto!("xray.proxy.blackhole");
    }

    /// <https://xtls.github.io/en/config/outbounds/dns.html>
    pub mod dns {
        tonic::include_proto!("xray.proxy.dns");
    }

    /// <https://xtls.github.io/en/config/inbounds/dokodemo.html>
    pub mod dokodemo {
        tonic::include_proto!("xray.proxy.dokodemo");
    }

    /// <https://xtls.github.io/en/config/outbounds/freedom.html>
    pub mod freedom {
        tonic::include_proto!("xray.proxy.freedom");
    }

    /// <https://xtls.github.io/en/config/inbounds/http.html><br>
    /// <https://xtls.github.io/en/config/outbounds/http.html>
    pub mod http {
        tonic::include_proto!("xray.proxy.http");
    }

    /// <https://xtls.github.io/en/config/outbounds/loopback.html>
    pub mod loopback {
        tonic::include_proto!("xray.proxy.loopback");
    }

    /// <https://xtls.github.io/en/config/inbounds/shadowsocks.html><br>
    /// <https://xtls.github.io/en/config/outbounds/shadowsocks.html>
    pub mod shadowsocks {
        tonic::include_proto!("xray.proxy.shadowsocks");
    }

    /// <https://xtls.github.io/en/config/inbounds/shadowsocks.html><br>
    /// <https://xtls.github.io/en/config/outbounds/shadowsocks.html>
    pub mod shadowsocks_2022 {
        tonic::include_proto!("xray.proxy.shadowsocks_2022");
    }

    /// <https://xtls.github.io/en/config/inbounds/socks.html><br>
    /// <https://xtls.github.io/en/config/outbounds/socks.html>
    pub mod socks {
        tonic::include_proto!("xray.proxy.socks");
    }

    /// <https://xtls.github.io/en/config/inbounds/trojan.html><br>
    /// <https://xtls.github.io/en/config/outbounds/trojan.html>
    pub mod trojan {
        tonic::include_proto!("xray.proxy.trojan");
    }

    /// <https://xtls.github.io/en/development/protocols/vless.html>
    pub mod vless {
        tonic::include_proto!("xray.proxy.vless");

        /// <https://xtls.github.io/en/config/inbounds/vless.html>
        pub mod inbound {
            tonic::include_proto!("xray.proxy.vless.inbound");
        }

        /// <https://xtls.github.io/en/config/outbounds/vless.html>
        pub mod outbound {
            tonic::include_proto!("xray.proxy.vless.outbound");
        }
    }

    /// <https://xtls.github.io/en/development/protocols/vmess.html>
    pub mod vmess {
        tonic::include_proto!("xray.proxy.vmess");

        /// <https://xtls.github.io/en/config/inbounds/vmess.html>
        pub mod inbound {
            tonic::include_proto!("xray.proxy.vmess.inbound");
        }

        /// <https://xtls.github.io/en/config/outbounds/vmess.html>
        pub mod outbound {
            tonic::include_proto!("xray.proxy.vmess.outbound");
        }
    }

    /// <https://xtls.github.io/en/config/inbounds/wireguard.html><br>
    /// <https://xtls.github.io/en/config/outbounds/wireguard.html>
    pub mod wireguard {
        tonic::include_proto!("xray.proxy.wireguard");
    }
}


pub mod transport {
    tonic::include_proto!("xray.transport");

    pub mod internet {
        tonic::include_proto!("xray.transport.internet");

        pub mod domainsocket {
            tonic::include_proto!("xray.transport.internet.domainsocket");
        }

        /// <https://xtls.github.io/en/config/transports/grpc.html>
        pub mod grpc {
            tonic::include_proto!("xray.transport.internet.grpc.encoding");
        }

        /// <https://xtls.github.io/en/config/transports/h2.html>
        pub mod http {
            tonic::include_proto!("xray.transport.internet.http");
        }

        /// <https://xtls.github.io/en/config/transports/httpupgrade.html>
        pub mod httpupgrade {
            tonic::include_proto!("xray.transport.internet.httpupgrade");
        }

        /// <https://xtls.github.io/en/config/transports/mkcp.html>
        pub mod kcp {
            tonic::include_proto!("xray.transport.internet.kcp");
        }

        pub mod quic {
            tonic::include_proto!("xray.transport.internet.quic");
        }

        pub mod reality {
            tonic::include_proto!("xray.transport.internet.reality");
        }

        /// <https://xtls.github.io/en/config/transports/splithttp.html>
        pub mod splithttp {
            tonic::include_proto!("xray.transport.internet.splithttp");
        }

        /// <https://xtls.github.io/en/config/transports/tcp.html>
        pub mod tcp {
            tonic::include_proto!("xray.transport.internet.tcp");
        }

        /// <https://xtls.github.io/en/config/transport.html#tlsobject>
        pub mod tls {
            tonic::include_proto!("xray.transport.internet.tls");
        }

        pub mod udp {
            tonic::include_proto!("xray.transport.internet.udp");
        }

        /// <https://xtls.github.io/en/config/transports/websocket.html>
        pub mod websocket {
            tonic::include_proto!("xray.transport.internet.websocket");
        }

        pub mod headers {
            pub mod dns {
                tonic::include_proto!("xray.transport.internet.headers.dns");
            }

            pub mod http {
                tonic::include_proto!("xray.transport.internet.headers.http");
            }

            pub mod noop {
                tonic::include_proto!("xray.transport.internet.headers.noop");
            }

            pub mod srtp {
                tonic::include_proto!("xray.transport.internet.headers.srtp");
            }

            pub mod tls {
                tonic::include_proto!("xray.transport.internet.headers.tls");
            }

            pub mod utp {
                tonic::include_proto!("xray.transport.internet.headers.utp");
            }

            pub mod wechat {
                tonic::include_proto!("xray.transport.internet.headers.wechat");
            }

            pub mod wireguard {
                tonic::include_proto!("xray.transport.internet.headers.wireguard");
            }
        }
    }
}

/// Connect creates a channel to xray GRPC socket.
///
/// This helper intended to be used in conjunction with [Tokio](https://tokio.rs) runtime.
#[cfg(feature = "connect")]
pub async fn connect<S: AsRef<str>>(path: S) -> Result<Channel, Error> {
    use tonic::transport::Endpoint;

    let path = path.as_ref();
    
    let channel = Endpoint::try_from(format!("https://{path}"))?
        .connect()
        .await?;

    Ok(channel)
}

/// Wrapper around tonic-generated client impl.
#[cfg(feature = "client")]
pub struct Client {
    channel: Channel,
}

#[cfg(feature = "client")]
impl Client {
    #[cfg(feature = "connect")]
    pub async fn from_url<S: AsRef<str>>(url: S) -> Result<Self, Error> {
        let channel = connect(url).await?;
        Ok(Self { channel })
    }

    #[inline]
    pub fn logger(&self) -> LoggerServiceClient<Channel> {
        LoggerServiceClient::new(self.channel.clone())
    }

    #[inline]
    pub fn handler(&self) -> HandlerServiceClient<Channel> {
        HandlerServiceClient::new(self.channel.clone())
    }

    #[inline]
    pub fn routing(&self) -> RoutingServiceClient<Channel> {
        RoutingServiceClient::new(self.channel.clone())
    }

    #[inline]
    pub fn stats(&self) -> StatsServiceClient<Channel> {
        StatsServiceClient::new(self.channel.clone())
    }

    #[inline]
    pub fn observatory(&self) -> ObservatoryServiceClient<Channel> {
        ObservatoryServiceClient::new(self.channel.clone())
    }

    #[inline]
    pub fn grpc(&self) -> GrpcServiceClient<Channel> {
        GrpcServiceClient::new(self.channel.clone())
    }
}

#[cfg(test)]
mod tests {
    use prost_types::Any;
    use crate::app::proxyman::InboundConfig;

    #[test]
    fn any_roundtrip() {
        let original = InboundConfig {};

        let any = Any::from_msg(&original).expect("should not fail to encode");
        let decoded: InboundConfig = any.to_msg().expect("should not fail to decode");

        assert_eq!(original, decoded)
    }
}