Search in sources :

Example 36 with CommandResponse

use of com.akaxin.common.command.CommandResponse in project openzaly by akaxincom.

the class HttpFriendService method apply.

/**
 * 提供扩展服务中添加好友的功能
 *
 * @param command
 * @return
 */
public CommandResponse apply(Command command) {
    CommandResponse commandResponse = new CommandResponse();
    ErrorCode2 errCode = ErrorCode2.ERROR;
    try {
        HaiFriendApplyProto.HaiFriendApplyRequest request = HaiFriendApplyProto.HaiFriendApplyRequest.parseFrom(command.getParams());
        String siteUserId = command.getSiteUserId();
        String siteFriendId = request.getSiteFriendId();
        String applyReason = request.getApplyReason();
        LogUtils.requestDebugLog(logger, command, request.toString());
        if (StringUtils.isBlank(siteUserId)) {
            errCode = ErrorCode2.ERROR_PARAMETER;
        } else if (siteUserId.equals(siteFriendId)) {
            errCode = ErrorCode2.ERROR2_FRIEND_APPLYSELF;
        } else {
            int applyTimes = UserFriendDao.getInstance().getApplyCount(siteFriendId, siteUserId);
            if (applyTimes >= 5) {
                errCode = ErrorCode2.ERROR2_FRIEND_APPLYCOUNT;
            } else {
                if (UserFriendDao.getInstance().saveFriendApply(siteUserId, siteFriendId, applyReason)) {
                    errCode = ErrorCode2.SUCCESS;
                }
            }
        }
        if (ErrorCode2.SUCCESS.equals(errCode)) {
            logger.debug("user apply friend notice. to siteUserId={}", siteFriendId);
            new User2Notice().applyFriendNotice(siteFriendId);
        }
    } catch (Exception e) {
        errCode = ErrorCode2.ERROR_SYSTEMERROR;
        LogUtils.requestErrorLog(logger, command, e);
    }
    return commandResponse.setErrCode2(errCode);
}
Also used : HaiFriendApplyProto(com.akaxin.proto.plugin.HaiFriendApplyProto) ErrorCode2(com.akaxin.common.constant.ErrorCode2) User2Notice(com.akaxin.site.business.impl.notice.User2Notice) CommandResponse(com.akaxin.common.command.CommandResponse)

Example 37 with CommandResponse

use of com.akaxin.common.command.CommandResponse in project openzaly by akaxincom.

the class Bootstrap method startNettyServer.

/**
 * 启动Netty服务器,提供用户与站点服务之间的长链接功能
 *
 * @param address
 * @param port
 * @throws Exception
 */
private static void startNettyServer(String address, int port) throws Exception {
    new NettyServer() {

        @Override
        public void loadExecutor(AbstracteExecutor<Command, CommandResponse> executor) {
            executor.addChain(RequestAction.IM_SITE.getName(), new ImSiteAuthHandler());
            executor.addChain(RequestAction.IM.getName(), new ImMessageHandler());
            executor.addChain(RequestAction.API.getName(), new ApiRequestHandler());
        }
    }.start(address, port);
    logger.info("{} start netty server {}:{} ok.", AkxProject.PLN, address, port);
}
Also used : Command(com.akaxin.common.command.Command) ApiRequestHandler(com.akaxin.site.connector.handler.ApiRequestHandler) ImSiteAuthHandler(com.akaxin.site.connector.handler.ImSiteAuthHandler) CommandResponse(com.akaxin.common.command.CommandResponse) ImMessageHandler(com.akaxin.site.connector.handler.ImMessageHandler) NettyServer(com.akaxin.site.connector.netty.NettyServer)

Example 38 with CommandResponse

use of com.akaxin.common.command.CommandResponse in project openzaly by akaxincom.

the class ImSiteAuthHandler method helloResponse.

private CommandResponse helloResponse(Channel channel) {
    CommandResponse commandResponse = new CommandResponse().setVersion(CommandConst.PROTOCOL_VERSION).setAction(CommandConst.ACTION_RES);
    ImSiteHelloProto.ImSiteHelloResponse response = ImSiteHelloProto.ImSiteHelloResponse.newBuilder().setSiteVersion(CommandConst.SITE_VERSION).build();
    commandResponse.setErrCode(ErrorCode.SUCCESS);
    commandResponse.setParams(response.toByteArray());
    ChannelWriter.write(channel, commandResponse);
    return commandResponse;
}
Also used : ImSiteHelloProto(com.akaxin.proto.site.ImSiteHelloProto) CommandResponse(com.akaxin.common.command.CommandResponse)

Example 39 with CommandResponse

use of com.akaxin.common.command.CommandResponse in project openzaly by akaxincom.

the class ImSiteAuthHandler method authResponse.

private CommandResponse authResponse(Channel channel, Command command, boolean result) {
    CommandResponse commandResponse = new CommandResponse().setVersion(CommandConst.PROTOCOL_VERSION).setAction(CommandConst.ACTION_RES);
    ErrorCode2 errCode = ErrorCode2.ERROR2_IMAUTH_FAIL;
    if (result) {
        String siteServer = ServerAddressUtils.getAddressPort();
        ImSiteAuthProto.ImSiteAuthResponse authResponse = ImSiteAuthProto.ImSiteAuthResponse.newBuilder().setSiteServer(siteServer).build();
        commandResponse.setParams(authResponse.toByteArray());
        errCode = ErrorCode2.SUCCESS;
        ChannelWriter.write(channel, commandResponse.setErrCode2(errCode));
    } else {
        ChannelWriter.writeAndClose(channel, commandResponse.setErrCode2(errCode));
    }
    logger.debug("{} client={} siteUserId={} action={} auth response result={}", AkxProject.PLN, command.getClientIp(), command.getSiteUserId(), RequestAction.IM_SITE_AUTH, errCode.toString());
    return commandResponse;
}
Also used : ImSiteAuthProto(com.akaxin.proto.site.ImSiteAuthProto) ErrorCode2(com.akaxin.common.constant.ErrorCode2) CommandResponse(com.akaxin.common.command.CommandResponse)

Example 40 with CommandResponse

use of com.akaxin.common.command.CommandResponse in project openzaly by akaxincom.

the class HttpServerHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {
        /**
         * http-request包含: <br>
         * 1.请求行 ,Method Request-URI Http-Version CRLF<br>
         * 2.消息头 <br>
         * 3.请求正文 <br>
         */
        if (msg instanceof HttpRequest) {
            request = (HttpRequest) msg;
            if (!checkLegalRequest()) {
                logger.error("{} http request method error. please use post!", AkxProject.PLN);
                ctx.close();
                return;
            }
            String clientIp = request.headers().get(HttpConst.HTTP_H_FORWARDED);
            if (clientIp == null) {
                InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
                clientIp = address.getAddress().getHostAddress();
            }
            if (!checkLegalClientIp(clientIp)) {
                logger.error("{} http request illegal IP={}.", AkxProject.PLN, clientIp);
                ctx.close();
                return;
            }
            logger.debug("{} request uri:{} clientIp={}", AkxProject.PLN, request.uri(), clientIp);
        }
        /**
         * HttpContent:表示HTTP实体正文和内容标头的基类 <br>
         * method.name=POST 传输消息体存在内容
         */
        if (msg instanceof LastHttpContent) {
            HttpContent content = (HttpContent) msg;
            ByteBuf httpByteBuf = content.content();
            if (httpByteBuf == null) {
                return;
            }
            if (!checkLegalRequest()) {
                ctx.close();
                return;
            }
            String clientIp = request.headers().get(HttpConst.HTTP_H_FORWARDED);
            String sitePluginId = request.headers().get(PluginConst.SITE_PLUGIN_ID);
            byte[] contentBytes = new byte[httpByteBuf.readableBytes()];
            httpByteBuf.readBytes(contentBytes);
            httpByteBuf.release();
            // 查询扩展的auth——key
            String authKey = PluginSession.getInstance().getPluginAuthKey(sitePluginId);
            if (StringUtils.isNotEmpty(authKey)) {
                // byte[] tsk = AESCrypto.generateTSKey(authKey);
                byte[] tsk = authKey.getBytes(CharsetCoding.ISO_8859_1);
                byte[] decContent = AESCrypto.decrypt(tsk, contentBytes);
                contentBytes = decContent;
            }
            PluginProto.ProxyPluginPackage pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(contentBytes);
            Map<Integer, String> proxyHeader = pluginPackage.getPluginHeaderMap();
            String requestTime = proxyHeader.get(PluginProto.PluginHeaderKey.PLUGIN_TIMESTAMP_VALUE);
            long currentTime = System.currentTimeMillis();
            boolean timeOut = true;
            if (StringUtils.isNotEmpty(requestTime)) {
                long timeMills = Long.valueOf(requestTime);
                if (currentTime - timeMills < 10 * 1000l) {
                    timeOut = false;
                }
            }
            logger.debug("{} client={} http request timeOut={} currTime={} reqTime={}", AkxProject.PLN, clientIp, timeOut, currentTime, requestTime);
            if (!timeOut) {
                Command command = new Command();
                command.setField(PluginConst.PLUGIN_AUTH_KEY, authKey);
                if (proxyHeader != null) {
                    command.setSiteUserId(proxyHeader.get(PluginProto.PluginHeaderKey.CLIENT_SITE_USER_ID_VALUE));
                }
                command.setChannelContext(ctx);
                command.setUri(request.uri());
                command.setParams(Base64.getDecoder().decode(pluginPackage.getData()));
                command.setClientIp(clientIp);
                command.setStartTime(System.currentTimeMillis());
                logger.debug("{} client={} http server handler command={}", AkxProject.PLN, clientIp, command.toString());
                CommandResponse response = this.executor.execute(HttpUriAction.HTTP_ACTION.getUri(), command);
                LogUtils.requestResultLog(logger, command, response);
            } else {
                // 超时10s,认为此请求失效,直接断开连接
                ctx.close();
                logger.error("{} client={} http request error.timeOut={} currTime={} reqTime={}", AkxProject.PLN, clientIp, timeOut, currentTime, requestTime);
            }
        }
    } catch (Exception e) {
        ctx.close();
        logger.error(StringHelper.format("{} http request error.", AkxProject.PLN), e);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) InetSocketAddress(java.net.InetSocketAddress) PluginProto(com.akaxin.proto.core.PluginProto) CommandResponse(com.akaxin.common.command.CommandResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ByteBuf(io.netty.buffer.ByteBuf) Command(com.akaxin.common.command.Command) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent)

Aggregations

CommandResponse (com.akaxin.common.command.CommandResponse)82 ErrorCode2 (com.akaxin.common.constant.ErrorCode2)68 UserProto (com.akaxin.proto.core.UserProto)12 PluginBean (com.akaxin.site.storage.bean.PluginBean)7 ByteString (com.google.protobuf.ByteString)7 GroupProto (com.akaxin.proto.core.GroupProto)6 GroupProfileBean (com.akaxin.site.storage.bean.GroupProfileBean)6 SimpleUserBean (com.akaxin.site.storage.bean.SimpleUserBean)6 UserProfileBean (com.akaxin.site.storage.bean.UserProfileBean)6 UserDeviceBean (com.akaxin.site.storage.bean.UserDeviceBean)5 Command (com.akaxin.common.command.Command)4 DeviceProto (com.akaxin.proto.core.DeviceProto)4 ProtocolStringList (com.google.protobuf.ProtocolStringList)4 ConfigProto (com.akaxin.proto.core.ConfigProto)3 GroupMemberBean (com.akaxin.site.storage.bean.GroupMemberBean)3 UserGroupBean (com.akaxin.site.storage.bean.UserGroupBean)3 RedisCommand (com.akaxin.common.command.RedisCommand)2 ImStcPsnProto (com.akaxin.proto.client.ImStcPsnProto)2 CoreProto (com.akaxin.proto.core.CoreProto)2 FileProto (com.akaxin.proto.core.FileProto)2