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

feat: 优化会话过期逻辑 #49

Merged
merged 2 commits into from
Oct 9, 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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM jumpserver/chen-base:20240913_102042 AS stage-build
FROM jumpserver/chen-base:20241009_104417 AS stage-build
ENV LANG=en_US.UTF-8

WORKDIR /opt/chen/
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile-base
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ RUN set -ex \
&& chmod 755 /usr/local/bin/check \
&& rm -f check-${CHECK_VERSION}-linux-${TARGETARCH}.tar.gz

ARG WISP_VERSION=v0.2.1
ARG WISP_VERSION=v0.2.2
RUN set -ex \
&& wget https://github.com/jumpserver/wisp/releases/download/${WISP_VERSION}/wisp-${WISP_VERSION}-linux-${TARGETARCH}.tar.gz \
&& tar -xf wisp-${WISP_VERSION}-linux-${TARGETARCH}.tar.gz -C /usr/local/bin/ --strip-components=1 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.jumpserver.chen.framework.jms.impl.ReplayHandlerImpl;
import org.jumpserver.chen.framework.session.QueryAuditFunction;
import org.jumpserver.chen.framework.session.SessionManager;
import org.jumpserver.chen.framework.session.controller.dialog.Button;
import org.jumpserver.chen.framework.session.controller.dialog.Dialog;
import org.jumpserver.chen.framework.session.controller.message.MessageLevel;
import org.jumpserver.chen.framework.session.exception.SessionException;
Expand All @@ -29,6 +30,7 @@
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
Expand All @@ -45,9 +47,14 @@ public class JMSSession extends BaseSession {
private final List<Common.CommandACL> commandACLs;
private final long maxIdleTimeDelta;
private final long expireTime;


private long lastActiveTime;

private int maxSessionTime;
private LocalDateTime maxSessionEndTime;
private LocalDateTime dynamicEndTime;
private String dynamicEndReason;

private Thread waitIdleTimeThread;
@Setter
private String gatewayId;
Expand Down Expand Up @@ -86,13 +93,37 @@ public JMSSession(Common.Session session,
this.commandACLs = tokenResp.getData().getFilterRulesList();
this.expireTime = tokenResp.getData().getExpireInfo().getExpireAt();
this.maxIdleTimeDelta = tokenResp.getData().getSetting().getMaxIdleTime();
this.maxSessionTime = tokenResp.getData().getSetting().getMaxSessionTime();

this.maxSessionEndTime = LocalDateTime.now().plusHours(tokenResp.getData().getSetting().getMaxSessionTime());
this.dynamicEndTime = LocalDateTime.now().plusHours(tokenResp.getData().getSetting().getMaxSessionTime());

this.canUpload = tokenResp.getData().getPermission().getEnableUpload();
this.canDownload = tokenResp.getData().getPermission().getEnableDownload();
this.canCopy = tokenResp.getData().getPermission().getEnableCopy();
this.canPaste = tokenResp.getData().getPermission().getEnablePaste();
}


public void setDynamicEndInfo(String reason) {
this.dynamicEndReason = reason;
this.dynamicEndTime = LocalDateTime.now().plusMinutes(10);

var dialog = new Dialog(MessageUtils.get("PermissionExpiredDialogTitle"));

dialog.setBody(MessageUtils.get("PermissionExpiredDialogMessage"));

dialog.addButton(new Button(MessageUtils.get("Cancel"), "cancel", () -> this.getController().closeDialog()));

this.getController().showDialog(dialog);

}

public void resetDynamicEndInfo() {
this.dynamicEndReason = "";
this.dynamicEndTime = this.maxSessionEndTime;
}


@Override
public void recordCommand(String command) {
CommandRecord commandRecord = new CommandRecord(command);
Expand Down Expand Up @@ -183,11 +214,15 @@ private void startWaitIdleTime() {
this.close("OverMaxIdleTimeError", "idle_disconnect", this.maxIdleTimeDelta);
return;
}

if (now - this.lastActiveTime > (long) this.maxSessionTime * 1000 * 60 * 60) {
this.close("OverMaxSessionTimeError", "max_session_timeout", this.maxSessionTime);
if (LocalDateTime.now().isAfter(this.maxSessionEndTime)) {
this.close("OverMaxSessionTimeError", "max_session_timeout", this.maxSessionEndTime);
return;
}
if (LocalDateTime.now().isAfter(this.dynamicEndTime)) {
this.close("OverMaxSessionTimeError", this.dynamicEndReason, this.dynamicEndTime);
return;
}

}
} catch (InterruptedException e) {
log.info("JMSSession waitIdleTimeThread interrupted, close it");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ public void onNext(ServiceOuterClass.TaskResponse taskResponse) {
if (targetSession != null) {
switch (taskResponse.getTask().getAction()) {
case KillSession ->
targetSession.close("SessionClosedBy","admin_terminate", taskResponse.getTask().getTerminatedBy());
targetSession.close("SessionClosedBy", "admin_terminate", taskResponse.getTask().getTerminatedBy());

case LockSession -> targetSession.lockSession(taskResponse.getTask().getCreatedBy());
case UnlockSession ->
targetSession.unloadSession(taskResponse.getTask().getCreatedBy());
case TokenPermExpired ->
targetSession.setDynamicEndInfo(taskResponse.getTask().getTokenStatus().getDetail());
case TokenPermValid -> targetSession.resetDynamicEndInfo();
}
var req = ServiceOuterClass.FinishedTaskRequest
.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ private Common.Session createJMSSession(ServiceOuterClass.TokenResponse tokenRes
.setProtocol(tokenResp.getData().getAsset().getProtocols(0).getName())
.setDateStart(System.currentTimeMillis() / 1000)
.setRemoteAddr(remoteAddr)
.setTokenId(tokenResp.getData().getKeyId())
.build();

var sessionResp = this.serviceBlockingStub.createSession(
Expand Down
Loading