Skip to content

Commit

Permalink
Merge pull request #52 from jumpserver/pr@dev@fix_session_expired_i18n
Browse files Browse the repository at this point in the history
fix: 修复会话超时提示
  • Loading branch information
Aaron3S authored Oct 15, 2024
2 parents b1bbc5a + 906081c commit 7c7ebeb
Showing 1 changed file with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
Expand All @@ -49,9 +51,10 @@ public class JMSSession extends BaseSession {
private final long expireTime;


private long lastActiveTime;
private LocalDateTime lastActiveTime;

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

Expand Down Expand Up @@ -94,8 +97,10 @@ public JMSSession(Common.Session session,
this.expireTime = tokenResp.getData().getExpireInfo().getExpireAt();
this.maxIdleTimeDelta = tokenResp.getData().getSetting().getMaxIdleTime();

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

this.canUpload = tokenResp.getData().getPermission().getEnableUpload();
this.canDownload = tokenResp.getData().getPermission().getEnableDownload();
Expand Down Expand Up @@ -198,26 +203,34 @@ private void recordLifecycle(ServiceOuterClass.SessionLifecycleLogRequest.EventT
}

private void startWaitIdleTime() {
this.lastActiveTime = System.currentTimeMillis();
this.lastActiveTime = LocalDateTime.now();

var token = SessionManager.getContextToken();

this.waitIdleTimeThread = new Thread(() -> {
SessionManager.setContext(token);

while (this.isActive()) {
try {
Thread.sleep(5000);
synchronized (this) {
long now = System.currentTimeMillis();
var expireTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(this.expireTime * 1000);
if (now > this.expireTime * 1000) {
this.close("PermissionsExpiredOn", "permission_expired", expireTime);
var expireTime = LocalDateTime.ofEpochSecond(this.expireTime, 0, ZoneOffset.UTC);

if (LocalDateTime.now().isAfter(expireTime)) {
this.close("PermissionsExpiredOn", "permission_expired", expireTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
return;
}
if (now - this.lastActiveTime > this.maxIdleTimeDelta * 1000 * 60) {

if (Math.abs(Duration.between(LocalDateTime.now(), this.lastActiveTime).toMinutes()) > this.maxIdleTimeDelta) {
this.close("OverMaxIdleTimeError", "idle_disconnect", this.maxIdleTimeDelta);
return;
}

if (LocalDateTime.now().isAfter(this.maxSessionEndTime)) {
this.close("OverMaxSessionTimeError", "max_session_timeout", this.maxSessionEndTime);
this.close("OverMaxSessionTimeError", "max_session_timeout", this.maxSessionEndHours);
return;
}

if (LocalDateTime.now().isAfter(this.dynamicEndTime)) {
this.close("PermissionAlreadyExpired", this.dynamicEndReason);
return;
Expand Down Expand Up @@ -294,7 +307,7 @@ private void closeGateway() {
@Override
public SQLQueryResult withAudit(String command, QueryAuditFunction queryAuditFunction) throws SQLException, CommandRejectException {
synchronized (this) {
this.lastActiveTime = System.currentTimeMillis();
this.lastActiveTime = LocalDateTime.now();
}
if (this.locked) {
throw new CommandRejectException(MessageUtils.get("SessionLockedError"));
Expand Down

0 comments on commit 7c7ebeb

Please sign in to comment.