Skip to content

Commit 05f1559

Browse files
committed
新增: 频道同步输入
1 parent 9745390 commit 05f1559

File tree

11 files changed

+376
-128
lines changed

11 files changed

+376
-128
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.tshell.controller;
2+
3+
import com.tshell.common.response.BaseResponse;
4+
import com.tshell.service.SyncChannelService;
5+
import io.smallrye.common.annotation.RunOnVirtualThread;
6+
7+
import javax.inject.Inject;
8+
import javax.ws.rs.*;
9+
import javax.ws.rs.core.MediaType;
10+
11+
/**
12+
*
13+
* 频道同步
14+
* @author TheBlind
15+
*/
16+
@Path("/syncChannel")
17+
@Produces(MediaType.APPLICATION_JSON)
18+
@RunOnVirtualThread
19+
public class SyncChannelController {
20+
21+
@Inject
22+
SyncChannelService syncChannelService;
23+
24+
@GET
25+
public BaseResponse<String> get(@QueryParam("sshChannelId")String sshChannelId){
26+
return BaseResponse.ok("success", syncChannelService.get(sshChannelId));
27+
}
28+
29+
@POST
30+
@Path("/bind")
31+
public BaseResponse bind(@FormParam("syncChannelId")String syncChannelId,@FormParam("sshChannelId") String sshChannelId) {
32+
syncChannelService.bind(syncChannelId,sshChannelId);
33+
return BaseResponse.ok();
34+
}
35+
36+
@POST
37+
@Path("/removeBind")
38+
public BaseResponse remove(@FormParam("sshChannelId") String sshChannelId) {
39+
syncChannelService.remove(sshChannelId);
40+
return BaseResponse.ok();
41+
}
42+
43+
44+
45+
}

server/src/main/java/com/tshell/core/TerminalService.java

+28-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.tshell.module.dto.terminal.SshInitConnectDTO;
88
import com.tshell.module.entity.Session;
99
import com.tshell.module.entity.SshSession;
10+
import com.tshell.service.SyncChannelService;
1011
import com.tshell.service.ConnectionLogService;
1112
import com.tshell.socket.WebSocket;
1213
import lombok.extern.slf4j.Slf4j;
@@ -15,7 +16,6 @@
1516
import javax.inject.Singleton;
1617
import javax.ws.rs.WebApplicationException;
1718
import java.io.IOException;
18-
import java.util.Optional;
1919
import java.util.concurrent.ExecutorService;
2020
import java.util.concurrent.Executors;
2121
import java.util.function.Consumer;
@@ -40,6 +40,9 @@ public class TerminalService {
4040
@Inject
4141
ConnectionLogService connectionLogService;
4242

43+
@Inject
44+
SyncChannelService syncChannelService;
45+
4346

4447
private Parameter.SshParameter convertToSshParameter(SshInitConnectDTO sshInitConnectDTO) {
4548
String sessionId = sshInitConnectDTO.sessionId();
@@ -141,14 +144,30 @@ private void receiveMsg(Consumer<String> msgHandle, TtyConnector ttyConnector) {
141144
});
142145
}
143146

147+
148+
144149
public void executeCmd(String key, String cmd) {
145-
ttyConnectorManager.getConnector(key).ifPresent((ttyConnector -> {
146-
try {
147-
ttyConnector.write(cmd);
148-
} catch (IOException e) {
149-
throw new RuntimeException(e);
150-
}
151-
}));
150+
syncChannelService.getSshChannelIds(key).ifPresentOrElse((sshChannelIds) -> {
151+
sshChannelIds.forEach((sshChannelId) -> {
152+
ttyConnectorManager.getConnector(sshChannelId).ifPresent((ttyConnector -> {
153+
try {
154+
ttyConnector.write(cmd);
155+
} catch (IOException e) {
156+
throw new RuntimeException(e);
157+
}
158+
}));
159+
});
160+
161+
}, () -> {
162+
ttyConnectorManager.getConnector(key).ifPresent((ttyConnector -> {
163+
try {
164+
ttyConnector.write(cmd);
165+
} catch (IOException e) {
166+
throw new RuntimeException(e);
167+
}
168+
}));
169+
});
170+
152171
}
153172

154173

@@ -159,6 +178,7 @@ public void changeWindow(String key, TtySize ttySize) throws IOException {
159178

160179
public void close(String channelId) {
161180
ttyConnectorManager.getConnector(channelId).ifPresent(ttyConnector -> {
181+
syncChannelService.remove(channelId);
162182
int sessionCount = ttyConnectorManager.getSessionCount(ttyConnector.getSessionId());
163183
if (sessionCount == 1) {
164184
fileManagerService.pauseTransfer(channelId);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.tshell.service;
2+
3+
import cn.hutool.core.collection.CollUtil;
4+
5+
import javax.enterprise.context.ApplicationScoped;
6+
import java.util.*;
7+
8+
/**
9+
* @author TheBlind
10+
*/
11+
@ApplicationScoped
12+
public class SyncChannelService {
13+
14+
15+
public static final Map<String, List<String>> syncChannels = HashMap.newHashMap(5);
16+
public static final Map<String, String> sshChannelIdMapChannels = HashMap.newHashMap(5);
17+
18+
19+
public void bind(String channelId, String sshChannelId) {
20+
List<String> list = syncChannels.computeIfAbsent(channelId, (k)->new ArrayList<>(3));
21+
if (sshChannelIdMapChannels.containsKey(sshChannelId)) {
22+
String s = sshChannelIdMapChannels.get(sshChannelId);
23+
remove(s);
24+
}
25+
sshChannelIdMapChannels.put(sshChannelId, channelId);
26+
list.add(sshChannelId);
27+
}
28+
29+
public void remove(String sshChannelId) {
30+
String channelId = sshChannelIdMapChannels.get(sshChannelId);
31+
List<String> list = syncChannels.get(channelId);
32+
if(CollUtil.isNotEmpty(list)){
33+
list.remove(sshChannelId);
34+
sshChannelIdMapChannels.remove(sshChannelId);
35+
}
36+
}
37+
38+
39+
public Optional<List<String>> getSshChannelIds(String sshChannelId) {
40+
String channelId = sshChannelIdMapChannels.get(sshChannelId);
41+
return Optional.ofNullable(syncChannels.get(channelId));
42+
}
43+
44+
45+
public String get(String sshChannelId) {
46+
return sshChannelIdMapChannels.get(sshChannelId);
47+
}
48+
}

0 commit comments

Comments
 (0)