forked from ixrjog/opscloud4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
白衣
committed
Jul 23, 2021
1 parent
6d8e6af
commit 5af5207
Showing
36 changed files
with
482 additions
and
103 deletions.
There are no files selected for viewing
25 changes: 0 additions & 25 deletions
25
opscloud-common/src/main/java/com/baiyi/opscloud/common/redis/TerminalKeyUtil.java
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
opscloud-common/src/main/java/com/baiyi/opscloud/common/redis/TerminalLogUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.baiyi.opscloud.common.redis; | ||
|
||
import com.google.common.base.Joiner; | ||
|
||
/** | ||
* @Author baiyi | ||
* @Date 2020/5/30 1:03 下午 | ||
* @Version 1.0 | ||
*/ | ||
public class TerminalLogUtil { | ||
|
||
public static String toAuditLogKey(String sessionId, String instanceId) { | ||
return Joiner.on("#").join(sessionId, instanceId, "auditLog"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...ud-manage/src/main/java/com/baiyi/opscloud/controller/TerminalSessionAuditController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.baiyi.opscloud.controller; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.alibaba.fastjson.JSONObject; | ||
import com.baiyi.opscloud.controller.base.SimpleAuthentication; | ||
import com.baiyi.opscloud.terminal.audit.ITerminalAuditProcess; | ||
import com.baiyi.opscloud.terminal.audit.TerminalAuditProcessFactory; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
|
||
import javax.websocket.*; | ||
import javax.websocket.server.ServerEndpoint; | ||
import java.io.IOException; | ||
import java.util.concurrent.CopyOnWriteArraySet; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* @Author baiyi | ||
* @Date 2021/7/23 2:39 下午 | ||
* @Version 1.0 | ||
*/ | ||
@Slf4j | ||
@ServerEndpoint(value = "/api/ws/terminal/session/audit") | ||
@Component | ||
public class TerminalSessionAuditController extends SimpleAuthentication { | ||
|
||
private static final AtomicInteger onlineCount = new AtomicInteger(0); | ||
// concurrent包的线程安全Set,用来存放每个客户端对应的Session对象。 | ||
private static CopyOnWriteArraySet<Session> sessionSet = new CopyOnWriteArraySet<>(); | ||
|
||
private Session session = null; | ||
// 超时时间1H | ||
public static final Long WEBSOCKET_TIMEOUT = 60 * 60 * 1000L; | ||
|
||
/** | ||
* 连接建立成功调用的方法 | ||
*/ | ||
@OnOpen | ||
public void onOpen(Session session) { | ||
sessionSet.add(session); | ||
int cnt = onlineCount.incrementAndGet(); // 在线数加1 | ||
log.info("终端会话审计有连接加入,当前连接数为:{}", cnt); | ||
session.setMaxIdleTimeout(WEBSOCKET_TIMEOUT); | ||
this.session = session; | ||
// 线程启动 | ||
// Runnable run = new SentOutputTask(sessionId, session); | ||
// Thread thread = new Thread(run); | ||
// thread.start(); | ||
} | ||
|
||
/** | ||
* 连接关闭调用的方法 | ||
*/ | ||
@OnClose | ||
public void onClose() { | ||
|
||
|
||
// KubernetesTerminalProcessFactory.getProcessByKey(MessageState.CLOSE.getState()).process("", session, terminalSession); | ||
sessionSet.remove(session); | ||
int cnt = onlineCount.decrementAndGet(); | ||
log.info("有连接关闭,当前连接数为:{}", cnt); | ||
} | ||
|
||
protected String getState(String message) { | ||
JSONObject jsonObject = JSON.parseObject(message); | ||
return jsonObject.getString(MESSAGE_STATE); | ||
} | ||
|
||
/** | ||
* 收到客户端消息后调用的方法 | ||
* Session session | ||
* | ||
* @param message 客户端发送过来的消息 | ||
*/ | ||
@OnMessage | ||
public void onMessage(String message, Session session) { | ||
if (!session.isOpen() || StringUtils.isEmpty(message)) return; | ||
String state = getState(message); | ||
ITerminalAuditProcess iTerminalAuditProcess = TerminalAuditProcessFactory.getProcessByKey(state); | ||
if (iTerminalAuditProcess != null) iTerminalAuditProcess.process(message, session); | ||
} | ||
|
||
|
||
/** | ||
* 出现错误 | ||
* | ||
* @param session | ||
* @param error | ||
*/ | ||
@OnError | ||
public void onError(Session session, Throwable error) { | ||
log.error("发生错误:{},Session ID: {}", error.getMessage(), session.getId()); | ||
} | ||
|
||
/** | ||
* 发送消息,实践表明,每次浏览器刷新,session会发生变化。 | ||
* | ||
* @param session | ||
* @param message | ||
*/ | ||
public static void sendMessage(Session session, String message) { | ||
try { | ||
session.getBasicRemote().sendText(message); | ||
} catch (IOException e) { | ||
log.error("发送消息出错:{}", e.getMessage()); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...oud-ssh-core/src/main/java/com/baiyi/opscloud/sshcore/message/audit/BaseAuditMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.baiyi.opscloud.sshcore.message.audit; | ||
|
||
import com.baiyi.opscloud.domain.model.message.IState; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.Data; | ||
|
||
/** | ||
* @Author baiyi | ||
* @Date 2021/7/23 2:59 下午 | ||
* @Version 1.0 | ||
*/ | ||
@Data | ||
@JsonIgnoreProperties | ||
public class BaseAuditMessage implements IState { | ||
|
||
private String state; | ||
|
||
private String sessionId; | ||
|
||
private String instanceId; | ||
|
||
} |
Oops, something went wrong.