Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2018 The gRPC Authors
*
* 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.grpc.alts.internal;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.annotations.VisibleForTesting;
import io.grpc.alts.internal.TsiFrameProtector.Consumer;
import io.grpc.alts.internal.TsiHandshakeHandler.TsiHandshakeCompletionEvent;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelPromise;
import io.netty.channel.PendingWriteQueue;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.net.SocketAddress;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Encrypts and decrypts TSI Frames. Writes are buffered here until {@link #flush} is called. Writes
* must not be made before the TSI handshake is complete.
*/
public final class TsiFrameHandler extends ByteToMessageDecoder implements ChannelOutboundHandler {
private static final Logger logger = Logger.getLogger(TsiFrameHandler.class.getName());
private TsiFrameProtector protector;
private PendingWriteQueue pendingUnprotectedWrites;
private State state = State.HANDSHAKE_NOT_FINISHED;
private boolean closeInitiated = false;
@VisibleForTesting
enum State {
HANDSHAKE_NOT_FINISHED,
PROTECTED,
CLOSED,
HANDSHAKE_FAILED
}
public TsiFrameHandler() {}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
logger.finest("TsiFrameHandler added");
super.handlerAdded(ctx);
assert pendingUnprotectedWrites == null;
pendingUnprotectedWrites = new PendingWriteQueue(checkNotNull(ctx));
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object event) throws Exception {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "TsiFrameHandler user event triggered", new Object[]{event});
}
if (event instanceof TsiHandshakeCompletionEvent) {
TsiHandshakeCompletionEvent tsiEvent = (TsiHandshakeCompletionEvent) event;
if (tsiEvent.isSuccess()) {
setProtector(tsiEvent.protector());
} else {
state = State.HANDSHAKE_FAILED;
}
// Ignore errors. Another handler in the pipeline must handle TSI Errors.
}
// Keep propagating the message, as others may want to read it.
super.userEventTriggered(ctx, event);
}
@VisibleForTesting
void setProtector(TsiFrameProtector protector) {
logger.finest("TsiFrameHandler protector set");
checkState(this.protector == null);
this.protector = checkNotNull(protector);
this.state = State.PROTECTED;
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List