17
17
import org .jumpserver .chen .framework .jms .impl .ReplayHandlerImpl ;
18
18
import org .jumpserver .chen .framework .session .QueryAuditFunction ;
19
19
import org .jumpserver .chen .framework .session .SessionManager ;
20
+ import org .jumpserver .chen .framework .session .controller .dialog .Button ;
20
21
import org .jumpserver .chen .framework .session .controller .dialog .Dialog ;
21
22
import org .jumpserver .chen .framework .session .controller .message .MessageLevel ;
22
23
import org .jumpserver .chen .framework .session .exception .SessionException ;
27
28
28
29
import java .sql .Connection ;
29
30
import java .sql .SQLException ;
30
- import java .text . SimpleDateFormat ;
31
+ import java .time . Duration ;
31
32
import java .time .Instant ;
33
+ import java .time .LocalDateTime ;
34
+ import java .time .ZoneOffset ;
35
+ import java .time .format .DateTimeFormatter ;
32
36
import java .util .List ;
33
37
34
38
@ Slf4j
@@ -45,9 +49,15 @@ public class JMSSession extends BaseSession {
45
49
private final List <Common .CommandACL > commandACLs ;
46
50
private final long maxIdleTimeDelta ;
47
51
private final long expireTime ;
48
- private long lastActiveTime ;
49
52
50
- private int maxSessionTime ;
53
+
54
+ private LocalDateTime lastActiveTime ;
55
+
56
+ private LocalDateTime maxSessionEndTime ;
57
+ private int maxSessionEndHours ;
58
+ private LocalDateTime dynamicEndTime ;
59
+ private String dynamicEndReason ;
60
+
51
61
private Thread waitIdleTimeThread ;
52
62
@ Setter
53
63
private String gatewayId ;
@@ -86,13 +96,41 @@ public JMSSession(Common.Session session,
86
96
this .commandACLs = tokenResp .getData ().getFilterRulesList ();
87
97
this .expireTime = tokenResp .getData ().getExpireInfo ().getExpireAt ();
88
98
this .maxIdleTimeDelta = tokenResp .getData ().getSetting ().getMaxIdleTime ();
89
- this .maxSessionTime = tokenResp .getData ().getSetting ().getMaxSessionTime ();
99
+
100
+ this .maxSessionEndHours = tokenResp .getData ().getSetting ().getMaxSessionTime ();
101
+ this .maxSessionEndTime = LocalDateTime .now ().plusHours (tokenResp .getData ().getSetting ().getMaxSessionTime ());
102
+ this .dynamicEndTime = this .maxSessionEndTime ;
103
+
90
104
this .canUpload = tokenResp .getData ().getPermission ().getEnableUpload ();
91
105
this .canDownload = tokenResp .getData ().getPermission ().getEnableDownload ();
92
106
this .canCopy = tokenResp .getData ().getPermission ().getEnableCopy ();
93
107
this .canPaste = tokenResp .getData ().getPermission ().getEnablePaste ();
94
108
}
95
109
110
+
111
+ public void setDynamicEndInfo (String reason ) {
112
+
113
+ SessionManager .setContext (this .getWebToken ());
114
+
115
+ this .dynamicEndReason = reason ;
116
+ this .dynamicEndTime = LocalDateTime .now ().plusMinutes (10 );
117
+
118
+ var dialog = new Dialog (MessageUtils .get ("PermissionExpiredDialogTitle" ));
119
+
120
+ dialog .setBody (MessageUtils .get ("PermissionExpiredDialogMessage" ));
121
+
122
+ dialog .addButton (new Button (MessageUtils .get ("Cancel" ), "cancel" , () -> this .getController ().closeDialog ()));
123
+
124
+ this .getController ().showDialog (dialog );
125
+
126
+ }
127
+
128
+ public void resetDynamicEndInfo () {
129
+ this .dynamicEndReason = "" ;
130
+ this .dynamicEndTime = this .maxSessionEndTime ;
131
+ }
132
+
133
+
96
134
@ Override
97
135
public void recordCommand (String command ) {
98
136
CommandRecord commandRecord = new CommandRecord (command );
@@ -167,27 +205,40 @@ private void recordLifecycle(ServiceOuterClass.SessionLifecycleLogRequest.EventT
167
205
}
168
206
169
207
private void startWaitIdleTime () {
170
- this .lastActiveTime = System .currentTimeMillis ();
208
+ this .lastActiveTime = LocalDateTime .now ();
209
+
210
+ var token = SessionManager .getContextToken ();
211
+
171
212
this .waitIdleTimeThread = new Thread (() -> {
213
+ SessionManager .setContext (token );
214
+
172
215
while (this .isActive ()) {
173
216
try {
174
217
Thread .sleep (5000 );
218
+
175
219
synchronized (this ) {
176
- long now = System . currentTimeMillis ( );
177
- var expireTime = new SimpleDateFormat ( "yyyy-MM-dd hh:mm:ss" ). format ( this . expireTime * 1000 );
178
- if (now > this . expireTime * 1000 ) {
179
- this .close ("PermissionsExpiredOn" , "permission_expired" , expireTime );
220
+ var expireTime = LocalDateTime . ofEpochSecond ( this . expireTime , 0 , ZoneOffset . ofHours ( 8 ) );
221
+
222
+ if (LocalDateTime . now (). isAfter ( expireTime ) ) {
223
+ this .close ("PermissionsExpiredOn" , "permission_expired" , expireTime . format ( DateTimeFormatter . ofPattern ( "yyyy-MM-dd HH:mm:ss" )) );
180
224
return ;
181
225
}
182
- if (now - this .lastActiveTime > this .maxIdleTimeDelta * 1000 * 60 ) {
226
+
227
+ if (Math .abs (Duration .between (LocalDateTime .now (), this .lastActiveTime ).toMinutes ()) > this .maxIdleTimeDelta ) {
183
228
this .close ("OverMaxIdleTimeError" , "idle_disconnect" , this .maxIdleTimeDelta );
184
229
return ;
185
230
}
186
231
187
- if (now - this . lastActiveTime > ( long ) this .maxSessionTime * 1000 * 60 * 60 ) {
188
- this .close ("OverMaxSessionTimeError" , "max_session_timeout" , this .maxSessionTime );
232
+ if (LocalDateTime . now (). isAfter ( this .maxSessionEndTime ) ) {
233
+ this .close ("OverMaxSessionTimeError" , "max_session_timeout" , this .maxSessionEndHours );
189
234
return ;
190
235
}
236
+
237
+ if (LocalDateTime .now ().isAfter (this .dynamicEndTime )) {
238
+ this .close ("PermissionAlreadyExpired" , this .dynamicEndReason );
239
+ return ;
240
+ }
241
+
191
242
}
192
243
} catch (InterruptedException e ) {
193
244
log .info ("JMSSession waitIdleTimeThread interrupted, close it" );
@@ -259,7 +310,7 @@ private void closeGateway() {
259
310
@ Override
260
311
public SQLQueryResult withAudit (String command , QueryAuditFunction queryAuditFunction ) throws SQLException , CommandRejectException {
261
312
synchronized (this ) {
262
- this .lastActiveTime = System . currentTimeMillis ();
313
+ this .lastActiveTime = LocalDateTime . now ();
263
314
}
264
315
if (this .locked ) {
265
316
throw new CommandRejectException (MessageUtils .get ("SessionLockedError" ));
0 commit comments