Skip to content

Commit

Permalink
Merge pull request #41 from liplum-dev/lock
Browse files Browse the repository at this point in the history
fixed session lock issue (might be)
  • Loading branch information
liplum authored Apr 30, 2024
2 parents aff97ea + 57eaa83 commit 337a14f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 27 deletions.
16 changes: 14 additions & 2 deletions lib/network/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,19 @@ class DioInit {
}
}

final _debugRequests = <RequestOptions>[];
final _debugResponses = <Response>[];

class PoorNetworkDioInterceptor extends Interceptor {
@override
Future<void> onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
final duration = Duration(milliseconds: _rand.nextInt(5000));
if(kDebugMode){
_debugRequests.add(options);
}
if(options.path == "http://sc.sit.edu.cn//public/init/index.action" || options.path == "http://sc.sit.edu.cn/public/init/index.action"){
print("!!!!!!!!!!");
}
final duration = Duration(milliseconds: _rand.nextInt(2000));
debugPrint("Start to request ${options.uri}");
await Future.delayed(duration);
debugPrint("Delayed Request ${options.uri} $duration");
Expand All @@ -63,7 +72,10 @@ class PoorNetworkDioInterceptor extends Interceptor {

@override
Future<void> onResponse(Response response, ResponseInterceptorHandler handler) async {
final duration = Duration(milliseconds: _rand.nextInt(5000));
if(kDebugMode){
_debugResponses.add(response);
}
final duration = Duration(milliseconds: _rand.nextInt(2000));
debugPrint("Start to response ${response.realUri}");
await Future.delayed(duration);
debugPrint("Delayed Response ${response.realUri} $duration");
Expand Down
7 changes: 5 additions & 2 deletions lib/session/class2nd.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:dio/dio.dart';
import 'package:sit/init.dart';
import 'package:sit/session/sso.dart';

class Class2ndSession {
Expand Down Expand Up @@ -33,13 +34,15 @@ class Class2ndSession {
final responseData = res.data;
// 如果返回值是登录页面,那就从 SSO 跳转一次以登录.
if (responseData is String && _needRedirectToLoginPage(responseData)) {
await ssoSession.request(
await Init.cookieJar.delete(Uri.parse(url), true);
// the response has been already redirected to the originally-requested one.
res = await ssoSession.request(
'https://authserver.sit.edu.cn/authserver/login?service=http%3A%2F%2Fsc.sit.edu.cn%2Flogin.jsp',
options: Options(
method: "GET",
),
);
res = await fetch();
// res = await fetch();
}
return res;
}
Expand Down
55 changes: 32 additions & 23 deletions lib/session/sso.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:math';

import 'package:async_locks/async_locks.dart';
import 'package:beautiful_soup_dart/beautiful_soup.dart';
import 'package:collection/collection.dart';
import 'package:cookie_jar/cookie_jar.dart';
Expand All @@ -16,7 +17,6 @@ import 'package:sit/r.dart';
import 'package:sit/session/auth.dart';
import 'package:sit/utils/error.dart';
import 'package:sit/utils/riverpod.dart';
import 'package:synchronized/synchronized.dart';
import 'package:encrypt/encrypt.dart';

import '../utils/dio.dart';
Expand Down Expand Up @@ -85,7 +85,7 @@ class SsoSession {

/// - User try to log in actively on a login page.
Future<Response> loginLocked(Credentials credentials) async {
return await _loginLock.synchronized(() async {
return await _loginLock.run(() async {
networkLogger.i("loginLocked ${DateTime.now().toIso8601String()}");
try {
final byAutoCaptcha = await _login(
Expand Down Expand Up @@ -306,26 +306,35 @@ class SsoSession {
/// Login the single sign-on
Future<Response> _postLoginRequest(String username, String hashedPassword, String captcha, String casTicket) async {
// Login
final res = await dio.post(_loginUrl,
data: {
'username': username,
'password': hashedPassword,
'captchaResponse': captcha,
'lt': casTicket,
'dllt': 'userNamePasswordLogin',
'execution': 'e1s1',
'_eventId': 'submit',
'rmShown': '1',
final res = await dio.post(
_loginUrl,
data: {
'username': username,
'password': hashedPassword,
'captchaResponse': captcha,
'lt': casTicket,
'dllt': 'userNamePasswordLogin',
'execution': 'e1s1',
'_eventId': 'submit',
'rmShown': '1',
},
options: Options(
contentType: Headers.formUrlEncodedContentType,
followRedirects: false,
validateStatus: (status) {
return status! < 400;
},
options: Options(
contentType: Headers.formUrlEncodedContentType,
followRedirects: false,
validateStatus: (status) {
return status! < 400;
},
headers: _neededHeaders,
));
return await processRedirect(dio, res, headers: _neededHeaders);
headers: _neededHeaders,
),
);
final debugDepths = <Response>[];
final finalResponse = await processRedirect(
dio,
res,
headers: _neededHeaders,
debugDepths: debugDepths,
);
return finalResponse;
}

Future<Response> request(
Expand All @@ -337,8 +346,8 @@ class SsoSession {
ProgressCallback? onReceiveProgress,
}) async {
networkLogger.i("$SsoSession.request ${DateTime.now().toIso8601String()}");
return await _ssoLock.synchronized<Response>(() async {
networkLogger.i("$SsoSession.request-synchronized ${DateTime.now().toIso8601String()}");
return await _ssoLock.run<Response>(() async {
networkLogger.i("$SsoSession.request-locked ${DateTime.now().toIso8601String()}");
try {
return await _request(
url,
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.11.0"
async_locks:
dependency: "direct main"
description:
name: async_locks
sha256: "11578df083ce7de82a4a76e14532169eee46ce85ed094a54b05317934164800c"
url: "https://pub.dev"
source: hosted
version: "3.4.0"
audio_session:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ dependencies:
rettulf: ^2.0.1
# lock
synchronized: ^3.1.0+1
async_locks: ^3.4.0
# parse stacktrace
stack_trace: ^1.11.0
email_validator: ^2.1.17
Expand Down

0 comments on commit 337a14f

Please sign in to comment.