mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-04-06 22:11:28 +03:00
Optimize HTTP calls
This commit is contained in:
@@ -9,7 +9,7 @@ import 'package:get/get.dart';
|
|||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
import '../common.dart';
|
import '../common.dart';
|
||||||
import '../utils/http_service.dart';
|
import '../utils/http_service.dart' as httpR;
|
||||||
import 'model.dart';
|
import 'model.dart';
|
||||||
import 'platform_model.dart';
|
import 'platform_model.dart';
|
||||||
|
|
||||||
@@ -136,9 +136,7 @@ class UserModel {
|
|||||||
/// throw [RequestException]
|
/// throw [RequestException]
|
||||||
Future<LoginResponse> login(LoginRequest loginRequest) async {
|
Future<LoginResponse> login(LoginRequest loginRequest) async {
|
||||||
final url = await bind.mainGetApiServer();
|
final url = await bind.mainGetApiServer();
|
||||||
final resp = await HttpService().sendRequest(
|
final resp = await httpR.post('$url/api/login', body: jsonEncode(loginRequest.toJson()));
|
||||||
'$url/api/login', HttpMethod.post,
|
|
||||||
body: jsonEncode(loginRequest.toJson()));
|
|
||||||
|
|
||||||
final Map<String, dynamic> body;
|
final Map<String, dynamic> body;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -7,21 +7,65 @@ enum HttpMethod { get, post, put, delete }
|
|||||||
|
|
||||||
class HttpService {
|
class HttpService {
|
||||||
Future<http.Response> sendRequest(
|
Future<http.Response> sendRequest(
|
||||||
String url,
|
String url,
|
||||||
HttpMethod method, {
|
HttpMethod method, {
|
||||||
Map<String, String>? headers,
|
Map<String, String>? headers,
|
||||||
dynamic body,
|
dynamic body,
|
||||||
}) async {
|
}) async {
|
||||||
headers ??= {'Content-Type': 'application/json'};
|
headers ??= {'Content-Type': 'application/json'};
|
||||||
String headersJson = jsonEncode(headers);
|
String headersJson = jsonEncode(headers);
|
||||||
String methodName = method.toString().split('.').last;
|
String methodName = method.toString().split('.').last;
|
||||||
|
|
||||||
await bind.mainHttpRequest(url: url, method: methodName.toLowerCase(), body: body, header: headersJson);
|
// Determine if there is currently a proxy setting, and if so, use FFI to call the Rust HTTP method.
|
||||||
|
final isProxy = await bind.mainGetProxyStatus();
|
||||||
|
|
||||||
|
if (!isProxy) {
|
||||||
|
return await _pollFultterHttp(url, method, headers: headers, body: body);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (body is! String) {
|
||||||
|
throw Exception('Unsupported HTTP body type');
|
||||||
|
}
|
||||||
|
|
||||||
|
await bind.mainHttpRequest(
|
||||||
|
url: url,
|
||||||
|
method: methodName.toLowerCase(),
|
||||||
|
body: body,
|
||||||
|
header: headersJson);
|
||||||
|
|
||||||
var resJson = await _pollForResponse();
|
var resJson = await _pollForResponse();
|
||||||
return _parseHttpResponse(resJson);
|
return _parseHttpResponse(resJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<http.Response> _pollFultterHttp(
|
||||||
|
String url,
|
||||||
|
HttpMethod method, {
|
||||||
|
Map<String, String>? headers,
|
||||||
|
dynamic body,
|
||||||
|
}) async {
|
||||||
|
var response = http.Response('', 400); // 默认响应
|
||||||
|
Uri uri = Uri.parse(url);
|
||||||
|
|
||||||
|
switch (method) {
|
||||||
|
case HttpMethod.get:
|
||||||
|
response = await http.get(uri, headers: headers);
|
||||||
|
break;
|
||||||
|
case HttpMethod.post:
|
||||||
|
response = await http.post(uri, headers: headers, body: body);
|
||||||
|
break;
|
||||||
|
case HttpMethod.put:
|
||||||
|
response = await http.put(uri, headers: headers, body: body);
|
||||||
|
break;
|
||||||
|
case HttpMethod.delete:
|
||||||
|
response = await http.delete(uri, headers: headers, body: body);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw Exception('Unsupported HTTP method');
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
Future<String> _pollForResponse() async {
|
Future<String> _pollForResponse() async {
|
||||||
String responseJson = await bind.mainGetAsyncStatus();
|
String responseJson = await bind.mainGetAsyncStatus();
|
||||||
while (responseJson == " ") {
|
while (responseJson == " ") {
|
||||||
@@ -45,4 +89,26 @@ class HttpService {
|
|||||||
throw Exception('Failed to parse response: $e');
|
throw Exception('Failed to parse response: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<http.Response> get(String url, {Map<String, String>? headers}) async {
|
||||||
|
return await HttpService().sendRequest(url, HttpMethod.get, headers: headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<http.Response> post(String url,
|
||||||
|
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
|
||||||
|
return await HttpService()
|
||||||
|
.sendRequest(url, HttpMethod.post, body: body, headers: headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<http.Response> put(String url,
|
||||||
|
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
|
||||||
|
return await HttpService()
|
||||||
|
.sendRequest(url, HttpMethod.put, body: body, headers: headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<http.Response> delete(String url,
|
||||||
|
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
|
||||||
|
return await HttpService()
|
||||||
|
.sendRequest(url, HttpMethod.delete, body: body, headers: headers);
|
||||||
|
}
|
||||||
|
|||||||
@@ -801,6 +801,10 @@ pub fn main_set_socks(proxy: String, username: String, password: String) {
|
|||||||
set_socks(proxy, username, password)
|
set_socks(proxy, username, password)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn main_get_proxy_status() -> bool {
|
||||||
|
get_proxy_status()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn main_get_socks() -> Vec<String> {
|
pub fn main_get_socks() -> Vec<String> {
|
||||||
get_socks()
|
get_socks()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -895,6 +895,9 @@ pub async fn set_socks(value: config::Socks5Server) -> ResultType<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_proxy_status() -> bool {
|
||||||
|
Config::get_socks().is_some()
|
||||||
|
}
|
||||||
#[tokio::main(flavor = "current_thread")]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
pub async fn test_rendezvous_server() -> ResultType<()> {
|
pub async fn test_rendezvous_server() -> ResultType<()> {
|
||||||
let mut c = connect(1000, "").await?;
|
let mut c = connect(1000, "").await?;
|
||||||
|
|||||||
@@ -421,6 +421,14 @@ pub fn set_socks(proxy: String, username: String, password: String) {
|
|||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_proxy_status() -> bool {
|
||||||
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||||
|
return ipc::get_proxy_status();
|
||||||
|
#[cfg(any(target_os = "android", target_os = "ios"))]
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(any(target_os = "android", target_os = "ios"))]
|
#[cfg(any(target_os = "android", target_os = "ios"))]
|
||||||
pub fn set_socks(_: String, _: String, _: String) {}
|
pub fn set_socks(_: String, _: String, _: String) {}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user