All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.aomsweet.cyber.http.HttpTunnelClientRelayHandler Maven / Gradle / Ivy

/*
  Copyright 2021 The Cyber Project

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */
package io.github.aomsweet.cyber.http;

import io.github.aomsweet.cyber.HandlerNames;
import io.github.aomsweet.cyber.CyberServer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

/**
 * @author aomsweet
 */
public class HttpTunnelClientRelayHandler extends BasicHttpClientRelayHandler {

    private final static InternalLogger logger = InternalLoggerFactory.getInstance(HttpTunnelClientRelayHandler.class);

    /**
     * tls client hello request
     */
    Object clientHello;

    public HttpTunnelClientRelayHandler(CyberServer cyber) {
        super(cyber, logger);
    }

    @Override
    public void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest httpRequest) throws Exception {
        ctx.pipeline().remove(HandlerNames.DECODER);
        serverAddress = resolveServerAddress(httpRequest);
        ByteBuf byteBuf = ctx.alloc().buffer(TUNNEL_ESTABLISHED_RESPONSE.length);
        ctx.writeAndFlush(byteBuf.writeBytes(TUNNEL_ESTABLISHED_RESPONSE));
        doConnectServer(ctx, ctx.channel(), httpRequest);
    }

    @Override
    public void handleHttpContent(ChannelHandlerContext ctx, HttpContent httpContent) throws Exception {
        ReferenceCountUtil.release(httpContent);
    }

    @Override
    public void handleUnknownMessage(ChannelHandlerContext ctx, Object message) {
        clientHello = message;
    }

    @Override
    protected void onConnected(ChannelHandlerContext ctx, Channel clientChannel, HttpRequest request) {
        doServerRelay(ctx);
        if (clientHello != null) {
            relayChannel.writeAndFlush(clientHello);
            clientHello = null;
        }
    }

    @Override
    public void destroy(ChannelHandlerContext ctx) {
        if (clientHello != null) {
            ReferenceCountUtil.release(clientHello);
            clientHello = null;
        }
        super.destroy(ctx);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy