Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix telnet ctrl+c issue #13812 #13921

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.dubbo.qos.server.handler;

import org.apache.dubbo.qos.common.QosConstants;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

public class CtrlCHandler extends SimpleChannelInboundHandler<ByteBuf> {
/**
* When type 'Ctrl+C', telnet client will send the following sequence:
* 'FF F4 FF FD 06', it can be divided into two parts:
* <p>
* 1. 'FF F4' is telnet interrupt process command.
* <p>
* 2. 'FF FD 06' is to suppress the output of the process that is to be
* interrupted by the interrupt process command.
* <p>
* We need to response with 'FF FC 06' to ignore it and tell the client continue
* display output.
*/
private byte[] CTRLC_BYTES_SEQUENCE = new byte[] {(byte) 0xff, (byte) 0xf4, (byte) 0xff, (byte) 0xfd, (byte) 0x06};

private byte[] RESPONSE_SEQUENCE = new byte[] {(byte) 0xff, (byte) 0xfc, 0x06};

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
// find ctrl+c
final int readerIndex = buffer.readerIndex();
for (int i = readerIndex; i < buffer.writerIndex(); i++) {
if (buffer.readableBytes() - i < CTRLC_BYTES_SEQUENCE.length) {
break;
}
boolean match = true;
for (int j = 0; j < CTRLC_BYTES_SEQUENCE.length; j++) {
if (CTRLC_BYTES_SEQUENCE[j] != buffer.getByte(i + j)) {
match = false;
break;
}
}

if (match) {
buffer.readerIndex(readerIndex + buffer.readableBytes());
ctx.writeAndFlush(Unpooled.wrappedBuffer(RESPONSE_SEQUENCE));
ctx.writeAndFlush(Unpooled.wrappedBuffer(
(QosConstants.BR_STR + QosProcessHandler.PROMPT).getBytes(CharsetUtil.UTF_8)));

return;
}
}
ctx.fireChannelRead(buffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
p.addLast(new HttpProcessHandler(frameworkModel, qosConfiguration));
p.remove(this);
} else {
p.addLast(new CtrlCHandler());
p.addLast(new LineBasedFrameDecoder(2048));
p.addLast(new StringDecoder(CharsetUtil.UTF_8));
p.addLast(new StringEncoder(CharsetUtil.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.dubbo.qos.server.handler;

import org.apache.dubbo.qos.common.QosConstants;

import java.nio.charset.StandardCharsets;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;
import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

public class CtrlCHandlerTest {
private byte[] CTRLC_BYTES_SEQUENCE = new byte[] {(byte) 0xff, (byte) 0xf4, (byte) 0xff, (byte) 0xfd, (byte) 0x06};

private byte[] RESPONSE_SEQUENCE = new byte[] {(byte) 0xff, (byte) 0xfc, 0x06};

@Test
void testMatchedExactly() throws Exception {
ChannelHandlerContext context = mock(ChannelHandlerContext.class);
CtrlCHandler ctrlCHandler = new CtrlCHandler();
ctrlCHandler.channelRead(context, Unpooled.wrappedBuffer(CTRLC_BYTES_SEQUENCE));
verify(context).writeAndFlush(Unpooled.wrappedBuffer(RESPONSE_SEQUENCE));
verify(context)
.writeAndFlush(Unpooled.wrappedBuffer(
(QosConstants.BR_STR + QosProcessHandler.PROMPT).getBytes(CharsetUtil.UTF_8)));
}

@Test
void testMatchedNotExactly() throws Exception {
ChannelHandlerContext context = mock(ChannelHandlerContext.class);
CtrlCHandler ctrlCHandler = new CtrlCHandler();
// before 'ctrl c', user typed other command like 'help'
String arbitraryCommand = "help";
byte[] commandBytes = arbitraryCommand.getBytes(StandardCharsets.UTF_8);
ctrlCHandler.channelRead(context, Unpooled.wrappedBuffer(commandBytes, CTRLC_BYTES_SEQUENCE));
verify(context).writeAndFlush(Unpooled.wrappedBuffer(RESPONSE_SEQUENCE));
verify(context)
.writeAndFlush(Unpooled.wrappedBuffer(
(QosConstants.BR_STR + QosProcessHandler.PROMPT).getBytes(CharsetUtil.UTF_8)));
}

@Test
void testNotMatched() throws Exception {
ChannelHandlerContext context = mock(ChannelHandlerContext.class);
CtrlCHandler ctrlCHandler = new CtrlCHandler();
String arbitraryCommand = "help" + QosConstants.BR_STR;
byte[] commandBytes = arbitraryCommand.getBytes(StandardCharsets.UTF_8);
ctrlCHandler.channelRead(context, Unpooled.wrappedBuffer(commandBytes));
verify(context, never()).writeAndFlush(Unpooled.wrappedBuffer(RESPONSE_SEQUENCE));
verify(context, never())
.writeAndFlush(Unpooled.wrappedBuffer(
(QosConstants.BR_STR + QosProcessHandler.PROMPT).getBytes(CharsetUtil.UTF_8)));
}
}
Loading