com.hazelcast.openshift.TunnelServer.java Source code

Java tutorial

Introduction

Here is the source code for com.hazelcast.openshift.TunnelServer.java

Source

/*
 * Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
 *
 * 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 com.hazelcast.openshift;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.OpenSsl;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.util.SelfSignedCertificate;

public class TunnelServer extends AbstractTunnel {

    private final boolean ssl;
    private final String forwardHost;
    private final int forwardPort;

    public TunnelServer(int localPort, boolean ssl, String forwardHost, int forwardPort) {
        super(localPort);

        this.ssl = ssl;
        this.forwardHost = forwardHost;
        this.forwardPort = forwardPort;
    }

    @Override
    protected ServerBootstrap createBootstrap(int localPort) throws Exception {
        SslContext sslContext;
        if (!ssl) {
            sslContext = null;

        } else {
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslContext = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        }

        System.out.println("Creating serverside http-socket: (" + localPort + ") => (" + forwardHost + ":"
                + forwardPort + ")");
        return new ServerBootstrap().option(ChannelOption.SO_BACKLOG, 20).group(getBossGroup(), getWorkerGroup())
                .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel channel) throws Exception {
                        ChannelPipeline pipeline = channel.pipeline();
                        if (sslContext != null) {
                            pipeline.addLast("ssl", sslContext.newHandler(channel.alloc()));
                        }
                        pipeline.addLast("http-codec", new HttpServerCodec());
                        pipeline.addLast(new TunnelClientAcceptor(getWorkerGroup(), forwardHost, forwardPort));
                    }
                });

    }
}