xray_core/
lib.rs

1use {
2    app::{
3        log::command::logger_service_client::LoggerServiceClient,
4        proxyman::command::handler_service_client::HandlerServiceClient,
5        router::command::routing_service_client::RoutingServiceClient,
6        stats::command::stats_service_client::StatsServiceClient,
7    },
8    core::observatory::command::observatory_service_client::ObservatoryServiceClient,
9    tonic::transport::{Channel, Error},
10    transport::internet::grpc::grpc_service_client::GrpcServiceClient,
11};
12
13pub use prost_types;
14pub use tonic;
15
16pub mod app {
17    pub mod commander {
18        tonic::include_proto!("xray.app.commander");
19    }
20
21    pub mod dispatcher {
22        tonic::include_proto!("xray.app.dispatcher");
23    }
24
25    /// <https://xtls.github.io/en/config/dns.html>
26    pub mod dns {
27        tonic::include_proto!("xray.app.dns");
28
29        /// <https://xtls.github.io/en/config/fakedns.html>
30        pub mod fakedns {
31            tonic::include_proto!("xray.app.dns.fakedns");
32        }
33    }
34
35    /// <https://xtls.github.io/en/config/log.html>
36    pub mod log {
37        tonic::include_proto!("xray.app.log");
38
39        #[cfg(feature = "client")]
40        pub mod command {
41            tonic::include_proto!("xray.app.log.command");
42        }
43    }
44
45    /// <https://xtls.github.io/en/config/metrics.html>
46    pub mod metrics {
47        tonic::include_proto!("xray.app.metrics");
48    }
49
50    /// <https://xtls.github.io/en/config/policy.html>
51    pub mod policy {
52        tonic::include_proto!("xray.app.policy");
53    }
54
55    pub mod proxyman {
56        tonic::include_proto!("xray.app.proxyman");
57
58        #[cfg(feature = "client")]
59        pub mod command {
60            tonic::include_proto!("xray.app.proxyman.command");
61        }
62    }
63
64    /// <https://xtls.github.io/en/config/reverse.html>
65    pub mod reverse {
66        tonic::include_proto!("xray.app.reverse");
67    }
68
69    /// <https://xtls.github.io/en/config/routing.html>
70    pub mod router {
71        tonic::include_proto!("xray.app.router");
72
73        #[cfg(feature = "client")]
74        pub mod command {
75            tonic::include_proto!("xray.app.router.command");
76        }
77    }
78
79    /// <https://xtls.github.io/en/config/stats.html>
80    pub mod stats {
81        tonic::include_proto!("xray.app.stats");
82
83        #[cfg(feature = "client")]
84        pub mod command {
85            tonic::include_proto!("xray.app.stats.command");
86        }
87    }
88}
89
90pub mod common {
91    pub mod log {
92        tonic::include_proto!("xray.common.log");
93    }
94
95    pub mod net {
96        tonic::include_proto!("xray.common.net");
97    }
98
99    pub mod protocol {
100        tonic::include_proto!("xray.common.protocol");
101    }
102
103    pub mod serial {
104        tonic::include_proto!("xray.common.serial");
105    }
106}
107
108/// <https://xtls.github.io/en/config/>
109pub mod core {
110    tonic::include_proto!("xray.core");
111
112    /// <https://xtls.github.io/en/config/observatory.html>
113    pub mod observatory {
114        tonic::include_proto!("xray.core.app.observatory");
115
116        pub mod burst {
117            tonic::include_proto!("xray.core.app.observatory.burst");
118        }
119
120        #[cfg(feature = "client")]
121        pub mod command {
122            tonic::include_proto!("xray.core.app.observatory.command");
123        }
124    }
125}
126
127pub mod proxy {
128    /// <https://xtls.github.io/en/config/outbounds/blackhole.html>
129    pub mod blackhole {
130        tonic::include_proto!("xray.proxy.blackhole");
131    }
132
133    /// <https://xtls.github.io/en/config/outbounds/dns.html>
134    pub mod dns {
135        tonic::include_proto!("xray.proxy.dns");
136    }
137
138    /// <https://xtls.github.io/en/config/inbounds/dokodemo.html>
139    pub mod dokodemo {
140        tonic::include_proto!("xray.proxy.dokodemo");
141    }
142
143    /// <https://xtls.github.io/en/config/outbounds/freedom.html>
144    pub mod freedom {
145        tonic::include_proto!("xray.proxy.freedom");
146    }
147
148    /// <https://xtls.github.io/en/config/inbounds/http.html><br>
149    /// <https://xtls.github.io/en/config/outbounds/http.html>
150    pub mod http {
151        tonic::include_proto!("xray.proxy.http");
152    }
153
154    /// <https://xtls.github.io/en/config/outbounds/loopback.html>
155    pub mod loopback {
156        tonic::include_proto!("xray.proxy.loopback");
157    }
158
159    /// <https://xtls.github.io/en/config/inbounds/shadowsocks.html><br>
160    /// <https://xtls.github.io/en/config/outbounds/shadowsocks.html>
161    pub mod shadowsocks {
162        tonic::include_proto!("xray.proxy.shadowsocks");
163    }
164
165    /// <https://xtls.github.io/en/config/inbounds/shadowsocks.html><br>
166    /// <https://xtls.github.io/en/config/outbounds/shadowsocks.html>
167    pub mod shadowsocks_2022 {
168        tonic::include_proto!("xray.proxy.shadowsocks_2022");
169    }
170    
171    /// <https://xtls.github.io/en/config/inbounds/socks.html><br>
172    /// <https://xtls.github.io/en/config/outbounds/socks.html>
173    pub mod socks {
174        tonic::include_proto!("xray.proxy.socks");
175    }
176
177    /// <https://xtls.github.io/en/config/inbounds/trojan.html><br>
178    /// <https://xtls.github.io/en/config/outbounds/trojan.html>
179    pub mod trojan {
180        tonic::include_proto!("xray.proxy.trojan");
181    }
182
183    /// <https://xtls.github.io/en/development/protocols/vless.html>
184    pub mod vless {
185        tonic::include_proto!("xray.proxy.vless");
186
187        pub mod encoding {
188            tonic::include_proto!("xray.proxy.vless.encoding");
189        }
190        
191        /// <https://xtls.github.io/en/config/inbounds/vless.html>
192        pub mod inbound {
193            tonic::include_proto!("xray.proxy.vless.inbound");
194        }
195
196        /// <https://xtls.github.io/en/config/outbounds/vless.html>
197        pub mod outbound {
198            tonic::include_proto!("xray.proxy.vless.outbound");
199        }
200    }
201
202    /// <https://xtls.github.io/en/development/protocols/vmess.html>
203    pub mod vmess {
204        tonic::include_proto!("xray.proxy.vmess");
205
206        /// <https://xtls.github.io/en/config/inbounds/vmess.html>
207        pub mod inbound {
208            tonic::include_proto!("xray.proxy.vmess.inbound");
209        }
210
211        /// <https://xtls.github.io/en/config/outbounds/vmess.html>
212        pub mod outbound {
213            tonic::include_proto!("xray.proxy.vmess.outbound");
214        }
215    }
216
217    /// <https://xtls.github.io/en/config/inbounds/wireguard.html><br>
218    /// <https://xtls.github.io/en/config/outbounds/wireguard.html>
219    pub mod wireguard {
220        tonic::include_proto!("xray.proxy.wireguard");
221    }
222}
223
224
225pub mod transport {
226    pub mod internet {
227        tonic::include_proto!("xray.transport.internet");
228
229        /// <https://xtls.github.io/en/config/transports/grpc.html>
230        pub mod grpc {
231            tonic::include_proto!("xray.transport.internet.grpc.encoding");
232        }
233
234        /// <https://xtls.github.io/en/config/transports/httpupgrade.html>
235        pub mod httpupgrade {
236            tonic::include_proto!("xray.transport.internet.httpupgrade");
237        }
238
239        /// <https://xtls.github.io/en/config/transports/mkcp.html>
240        pub mod kcp {
241            tonic::include_proto!("xray.transport.internet.kcp");
242        }
243
244        pub mod quic {
245            // tonic::include_proto!("xray.transport.internet.quic");
246        }
247
248        pub mod reality {
249            tonic::include_proto!("xray.transport.internet.reality");
250        }
251
252        /// <https://xtls.github.io/en/config/transports/splithttp.html>
253        pub mod splithttp {
254            tonic::include_proto!("xray.transport.internet.splithttp");
255        }
256
257        /// <https://xtls.github.io/en/config/transports/tcp.html>
258        pub mod tcp {
259            tonic::include_proto!("xray.transport.internet.tcp");
260        }
261
262        /// <https://xtls.github.io/en/config/transport.html#tlsobject>
263        pub mod tls {
264            tonic::include_proto!("xray.transport.internet.tls");
265        }
266
267        pub mod udp {
268            tonic::include_proto!("xray.transport.internet.udp");
269        }
270
271        /// <https://xtls.github.io/en/config/transports/websocket.html>
272        pub mod websocket {
273            tonic::include_proto!("xray.transport.internet.websocket");
274        }
275
276        pub mod headers {
277            pub mod dns {
278                tonic::include_proto!("xray.transport.internet.headers.dns");
279            }
280
281            pub mod http {
282                tonic::include_proto!("xray.transport.internet.headers.http");
283            }
284
285            pub mod noop {
286                tonic::include_proto!("xray.transport.internet.headers.noop");
287            }
288
289            pub mod srtp {
290                tonic::include_proto!("xray.transport.internet.headers.srtp");
291            }
292
293            pub mod tls {
294                tonic::include_proto!("xray.transport.internet.headers.tls");
295            }
296
297            pub mod utp {
298                tonic::include_proto!("xray.transport.internet.headers.utp");
299            }
300
301            pub mod wechat {
302                tonic::include_proto!("xray.transport.internet.headers.wechat");
303            }
304
305            pub mod wireguard {
306                tonic::include_proto!("xray.transport.internet.headers.wireguard");
307            }
308        }
309    }
310}
311
312/// Connect creates a channel to xray GRPC socket.
313///
314/// This helper intended to be used in conjunction with [Tokio](https://tokio.rs) runtime.
315#[cfg(feature = "connect")]
316pub async fn connect<S: AsRef<str>>(path: S) -> Result<Channel, Error> {
317    use tonic::transport::Endpoint;
318
319    let path = path.as_ref();
320    
321    let channel = Endpoint::try_from(format!("https://{path}"))?
322        .connect()
323        .await?;
324
325    Ok(channel)
326}
327
328/// Wrapper around tonic-generated client impl.
329#[cfg(feature = "client")]
330pub struct Client {
331    channel: Channel,
332}
333
334#[cfg(feature = "client")]
335impl Client {
336    #[cfg(feature = "connect")]
337    pub async fn from_url<S: AsRef<str>>(url: S) -> Result<Self, Error> {
338        let channel = connect(url).await?;
339        Ok(Self { channel })
340    }
341
342    #[inline]
343    pub fn logger(&self) -> LoggerServiceClient<Channel> {
344        LoggerServiceClient::new(self.channel.clone())
345    }
346
347    #[inline]
348    pub fn handler(&self) -> HandlerServiceClient<Channel> {
349        HandlerServiceClient::new(self.channel.clone())
350    }
351
352    #[inline]
353    pub fn routing(&self) -> RoutingServiceClient<Channel> {
354        RoutingServiceClient::new(self.channel.clone())
355    }
356
357    #[inline]
358    pub fn stats(&self) -> StatsServiceClient<Channel> {
359        StatsServiceClient::new(self.channel.clone())
360    }
361
362    #[inline]
363    pub fn observatory(&self) -> ObservatoryServiceClient<Channel> {
364        ObservatoryServiceClient::new(self.channel.clone())
365    }
366
367    #[inline]
368    pub fn grpc(&self) -> GrpcServiceClient<Channel> {
369        GrpcServiceClient::new(self.channel.clone())
370    }
371}
372
373#[cfg(test)]
374mod tests {
375    use prost_types::Any;
376    use crate::app::proxyman::InboundConfig;
377
378    #[test]
379    fn any_roundtrip() {
380        let original = InboundConfig {};
381
382        let any = Any::from_msg(&original).expect("should not fail to encode");
383        let decoded: InboundConfig = any.to_msg().expect("should not fail to decode");
384
385        assert_eq!(original, decoded)
386    }
387}