Skip to content

Commit

Permalink
feat: getting xray logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawsh committed Mar 10, 2024
1 parent 6441c27 commit b74a3ec
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
6 changes: 5 additions & 1 deletion marznode/service/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ message XrayConfig {
string configuration = 1;
}

message XrayLogsRequest {
bool include_buffer = 1;
}

service MarzService {
rpc SyncUsers(stream UserData) returns (Empty);
rpc RepopulateUsers(UsersData) returns (Empty);
rpc FetchInbounds(Empty) returns (InboundsResponse);
rpc FetchUsersStats(Empty) returns (UsersStats);
rpc FetchXrayConfig(Empty) returns (XrayConfig);
rpc RestartXray(XrayConfig) returns (InboundsResponse);
rpc StreamXrayLogs(Empty) returns(stream LogLine);
rpc StreamXrayLogs(XrayLogsRequest) returns(stream LogLine);
}
19 changes: 12 additions & 7 deletions marznode/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from marznode.xray_api.exceptions import EmailExistsError, EmailNotFoundError
from marznode.xray_api.types.account import accounts_map
from .service_grpc import MarzServiceBase
from .service_pb2 import UserData, Empty, InboundsResponse, Inbound, UsersStats
from .service_pb2 import UserData, Empty, InboundsResponse, Inbound, UsersStats, LogLine
from .service_pb2 import XrayConfig as XrayConfig_pb2
from .. import config
from ..xray.base import XrayCore
Expand Down Expand Up @@ -130,12 +130,17 @@ async def FetchUsersStats(self,
await stream.send_message(UsersStats(users_stats=user_stats))

async def StreamXrayLogs(self,
stream: 'grpclib.server.Stream[marznode.service.service_pb2.Empty,'
'marznode.service.service_pb2.LogLine]') -> None:
pass

async def FetchXrayConfig(self, stream: 'grpclib.server.Stream[marznode.service.service_pb2.Empty,'
'marznode.service.service_pb2.XrayConfig]') -> None:
stream: Stream[Empty, LogLine]) -> None:
req = await stream.recv_message()
if req.include_buffer:
for line in self.xray.get_buffer():
await stream.send_message(LogLine(line=line))
log_stm = await self.xray.get_logs_stm()
async with log_stm:
async for line in log_stm:
await stream.send_message(LogLine(line=line))

async def FetchXrayConfig(self, stream: Stream[Empty, XrayConfig_pb2]) -> None:
await stream.recv_message()
with open(config.XRAY_CONFIG_PATH, 'r') as f:
content = f.read()
Expand Down
6 changes: 3 additions & 3 deletions marznode/service/service_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def RestartXray(self, stream: 'grpclib.server.Stream[marznode.service.serv
pass

@abc.abstractmethod
async def StreamXrayLogs(self, stream: 'grpclib.server.Stream[marznode.service.service_pb2.Empty, marznode.service.service_pb2.LogLine]') -> None:
async def StreamXrayLogs(self, stream: 'grpclib.server.Stream[marznode.service.service_pb2.XrayLogsRequest, marznode.service.service_pb2.LogLine]') -> None:
pass

def __mapping__(self) -> typing.Dict[str, grpclib.const.Handler]:
Expand Down Expand Up @@ -83,7 +83,7 @@ def __mapping__(self) -> typing.Dict[str, grpclib.const.Handler]:
'/marznode.MarzService/StreamXrayLogs': grpclib.const.Handler(
self.StreamXrayLogs,
grpclib.const.Cardinality.UNARY_STREAM,
marznode.service.service_pb2.Empty,
marznode.service.service_pb2.XrayLogsRequest,
marznode.service.service_pb2.LogLine,
),
}
Expand Down Expand Up @@ -131,6 +131,6 @@ def __init__(self, channel: grpclib.client.Channel) -> None:
self.StreamXrayLogs = grpclib.client.UnaryStreamMethod(
channel,
'/marznode.MarzService/StreamXrayLogs',
marznode.service.service_pb2.Empty,
marznode.service.service_pb2.XrayLogsRequest,
marznode.service.service_pb2.LogLine,
)
8 changes: 5 additions & 3 deletions marznode/service/service_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b74a3ec

Please sign in to comment.