277 lines
12 KiB
VB.net
277 lines
12 KiB
VB.net
|
|
Imports System.Net.Http
|
|||
|
|
Imports System.Text
|
|||
|
|
Imports System.Web.Script.Serialization
|
|||
|
|
Imports Microsoft.VisualBasic.FileIO
|
|||
|
|
Imports System.Net
|
|||
|
|
Imports System.Security.Cryptography.X509Certificates
|
|||
|
|
Imports System.Net.Security
|
|||
|
|
Imports System.Threading.Tasks
|
|||
|
|
Imports System.Threading
|
|||
|
|
|
|||
|
|
Public Class ApiInterface
|
|||
|
|
|
|||
|
|
' متد برای تنظیم SSL/TLS
|
|||
|
|
Private Sub ConfigureSSL()
|
|||
|
|
' تنظیم پروتکل SSL/TLS
|
|||
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls
|
|||
|
|
|
|||
|
|
' تنظیم اعتبارسنجی گواهی SSL (برای محیط تست)
|
|||
|
|
ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
|
|||
|
|
|
|||
|
|
' تنظیم timeout
|
|||
|
|
ServicePointManager.DefaultConnectionLimit = 10
|
|||
|
|
|
|||
|
|
' تنظیمات اضافی برای حل مشکل SSL
|
|||
|
|
ServicePointManager.Expect100Continue = False
|
|||
|
|
ServicePointManager.UseNagleAlgorithm = False
|
|||
|
|
End Sub
|
|||
|
|
|
|||
|
|
' متد اعتبارسنجی گواهی SSL
|
|||
|
|
Private Function ValidateServerCertificate(sender As Object, certificate As X509Certificate, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) As Boolean
|
|||
|
|
' برای محیط تست، همه گواهیها را قبول میکنیم
|
|||
|
|
' در محیط تولید باید اعتبارسنجی مناسب انجام شود
|
|||
|
|
Return True
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
' متد برای تنظیمات پیشرفته SSL (اختیاری)
|
|||
|
|
Public Sub ConfigureAdvancedSSL(enableCertificateValidation As Boolean)
|
|||
|
|
If enableCertificateValidation Then
|
|||
|
|
' فعال کردن اعتبارسنجی گواهی برای محیط تولید
|
|||
|
|
ServicePointManager.ServerCertificateValidationCallback = Nothing
|
|||
|
|
Else
|
|||
|
|
' غیرفعال کردن اعتبارسنجی گواهی برای محیط تست
|
|||
|
|
ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
|
|||
|
|
End If
|
|||
|
|
End Sub
|
|||
|
|
|
|||
|
|
' متد کمکی برای تنظیم timeout
|
|||
|
|
Private Sub SetWebClientTimeout(url As String, timeoutMs As Integer)
|
|||
|
|
Dim servicePoint As ServicePoint = ServicePointManager.FindServicePoint(New Uri(url))
|
|||
|
|
servicePoint.ConnectionLeaseTimeout = timeoutMs
|
|||
|
|
End Sub
|
|||
|
|
|
|||
|
|
' متد تست اتصال SSL
|
|||
|
|
Public Function TestSSLConnection() As String
|
|||
|
|
Try
|
|||
|
|
ConfigureSSL()
|
|||
|
|
|
|||
|
|
Dim webClient As New Net.WebClient
|
|||
|
|
|
|||
|
|
Dim testUrl As String = My.Settings.apiUrl.Replace("https://", "https://")
|
|||
|
|
' تنظیم timeout برای WebClient (از طریق ServicePointManager)
|
|||
|
|
SetWebClientTimeout(testUrl, 10000) ' 10 ثانیه برای تست
|
|||
|
|
Logger.Instance.LogInfo($"تست اتصال SSL به: {testUrl}")
|
|||
|
|
|
|||
|
|
Dim result As String = webClient.DownloadString(testUrl)
|
|||
|
|
Logger.Instance.LogInfo("تست اتصال SSL موفق انجام شد")
|
|||
|
|
Return "اتصال SSL موفق"
|
|||
|
|
|
|||
|
|
Catch ex As Exception
|
|||
|
|
Logger.Instance.LogError("خطا در تست اتصال SSL", ex)
|
|||
|
|
Return $"خطا در اتصال SSL: {ex.Message}"
|
|||
|
|
End Try
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
Public Function send(requestUri As String) As String
|
|||
|
|
' تنظیم SSL/TLS
|
|||
|
|
ConfigureSSL()
|
|||
|
|
|
|||
|
|
Dim webClient As New Net.WebClient
|
|||
|
|
Dim fullUrl As String = My.Settings.apiUrl + requestUri
|
|||
|
|
|
|||
|
|
' تنظیم timeout برای WebClient (از طریق ServicePointManager)
|
|||
|
|
SetWebClientTimeout(fullUrl, 30000) ' 30 ثانیه
|
|||
|
|
|
|||
|
|
' ثبت درخواست
|
|||
|
|
Logger.Instance.LogRequest("GET", fullUrl, "", "")
|
|||
|
|
|
|||
|
|
Try
|
|||
|
|
Dim result As String = webClient.DownloadString(fullUrl)
|
|||
|
|
' ثبت پاسخ موفق
|
|||
|
|
Logger.Instance.LogResponse("200", result, True)
|
|||
|
|
Return result
|
|||
|
|
Catch ex As Exception
|
|||
|
|
' ثبت خطا
|
|||
|
|
Logger.Instance.LogError($"خطا در ارسال درخواست به {fullUrl}", ex)
|
|||
|
|
Return ""
|
|||
|
|
End Try
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
' متد async برای ارسال درخواست بدون مسدود کردن UI
|
|||
|
|
Public Async Function sendAsync(requestUri As String) As Task(Of String)
|
|||
|
|
Return Await Task.Run(Function()
|
|||
|
|
Return send(requestUri)
|
|||
|
|
End Function)
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
Public Function login(token As String) As Dictionary(Of String, Object)
|
|||
|
|
' تنظیم SSL/TLS
|
|||
|
|
ConfigureSSL()
|
|||
|
|
|
|||
|
|
Dim webClient As New Net.WebClient
|
|||
|
|
webClient.Headers.Add("api-key", token)
|
|||
|
|
|
|||
|
|
Dim fullUrl As String = My.Settings.apiUrl + "/api/user/check/login"
|
|||
|
|
' تنظیم timeout برای WebClient (از طریق ServicePointManager)
|
|||
|
|
SetWebClientTimeout(fullUrl, 30000) ' 30 ثانیه
|
|||
|
|
Dim headers As String = $"api-key: {token}"
|
|||
|
|
|
|||
|
|
' ثبت درخواست ورود
|
|||
|
|
Logger.Instance.LogRequest("GET", fullUrl, headers, "")
|
|||
|
|
|
|||
|
|
Try
|
|||
|
|
Dim result As String = webClient.DownloadString(fullUrl)
|
|||
|
|
Dim serializer As New JavaScriptSerializer()
|
|||
|
|
Dim json As Dictionary(Of String, Object) = serializer.Deserialize(Of Dictionary(Of String, Object))(result)
|
|||
|
|
|
|||
|
|
' ثبت پاسخ ورود
|
|||
|
|
If json.Count > 0 Then
|
|||
|
|
Logger.Instance.LogResponse("200", result, True)
|
|||
|
|
Logger.Instance.LogInfo("ورود موفق انجام شد")
|
|||
|
|
Else
|
|||
|
|
Logger.Instance.LogResponse("401", result, False)
|
|||
|
|
Logger.Instance.LogInfo("ورود ناموفق - توکن نامعتبر")
|
|||
|
|
End If
|
|||
|
|
|
|||
|
|
Return json
|
|||
|
|
Catch ex As Exception
|
|||
|
|
Logger.Instance.LogError("خطا در فرآیند ورود", ex)
|
|||
|
|
Return New Dictionary(Of String, Object)()
|
|||
|
|
End Try
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
' متد جدید برای دریافت پاسخ خام سرور
|
|||
|
|
Public Function loginWithResponse(token As String) As String
|
|||
|
|
' تنظیم SSL/TLS
|
|||
|
|
ConfigureSSL()
|
|||
|
|
|
|||
|
|
Dim webClient As New Net.WebClient
|
|||
|
|
webClient.Headers.Add("api-key", token)
|
|||
|
|
|
|||
|
|
Dim fullUrl As String = My.Settings.apiUrl + "/api/user/check/login"
|
|||
|
|
' تنظیم timeout برای WebClient (از طریق ServicePointManager)
|
|||
|
|
SetWebClientTimeout(fullUrl, 30000) ' 30 ثانیه
|
|||
|
|
Dim headers As String = $"api-key: {token}"
|
|||
|
|
|
|||
|
|
' ثبت درخواست ورود
|
|||
|
|
Logger.Instance.LogRequest("GET", fullUrl, headers, "")
|
|||
|
|
|
|||
|
|
Try
|
|||
|
|
Dim result As String = webClient.DownloadString(fullUrl)
|
|||
|
|
|
|||
|
|
' ثبت پاسخ ورود
|
|||
|
|
Dim serializer As New JavaScriptSerializer()
|
|||
|
|
Dim json As Dictionary(Of String, Object) = serializer.Deserialize(Of Dictionary(Of String, Object))(result)
|
|||
|
|
|
|||
|
|
If json.Count > 0 Then
|
|||
|
|
Logger.Instance.LogResponse("200", result, True)
|
|||
|
|
Logger.Instance.LogInfo("ورود موفق انجام شد")
|
|||
|
|
Else
|
|||
|
|
Logger.Instance.LogResponse("401", result, False)
|
|||
|
|
Logger.Instance.LogInfo("ورود ناموفق - توکن نامعتبر")
|
|||
|
|
End If
|
|||
|
|
|
|||
|
|
Return result
|
|||
|
|
Catch ex As Exception
|
|||
|
|
Logger.Instance.LogError("خطا در فرآیند ورود", ex)
|
|||
|
|
Return ex.Message
|
|||
|
|
End Try
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
Public Function syncPersons() As Boolean
|
|||
|
|
' تنظیم SSL/TLS
|
|||
|
|
ConfigureSSL()
|
|||
|
|
|
|||
|
|
Dim webClient As New Net.WebClient
|
|||
|
|
webClient.Headers.Add("api-key", My.Settings.token)
|
|||
|
|
|
|||
|
|
Dim fullUrl As String = My.Settings.apiUrl + "/api/person/list"
|
|||
|
|
' تنظیم timeout برای WebClient (از طریق ServicePointManager)
|
|||
|
|
SetWebClientTimeout(fullUrl, 30000) ' 30 ثانیه
|
|||
|
|
Dim headers As String = $"api-key: {My.Settings.token}"
|
|||
|
|
|
|||
|
|
' ثبت درخواست همگامسازی اشخاص
|
|||
|
|
Logger.Instance.LogRequest("GET", fullUrl, headers, "")
|
|||
|
|
|
|||
|
|
Try
|
|||
|
|
Dim result As String = webClient.DownloadString(fullUrl)
|
|||
|
|
My.Settings.persons = result
|
|||
|
|
Logger.Instance.LogResponse("200", result, True)
|
|||
|
|
Logger.Instance.LogInfo("همگامسازی اشخاص موفق انجام شد")
|
|||
|
|
Return True
|
|||
|
|
Catch ex As Exception
|
|||
|
|
Logger.Instance.LogError("خطا در همگامسازی اشخاص", ex)
|
|||
|
|
Return False
|
|||
|
|
End Try
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
Public Function getLastPrint() As String
|
|||
|
|
' تنظیم SSL/TLS
|
|||
|
|
ConfigureSSL()
|
|||
|
|
|
|||
|
|
Dim webClient As New Net.WebClient
|
|||
|
|
webClient.Headers.Add("api-key", My.Settings.token)
|
|||
|
|
webClient.Headers.Add("printer-key", My.Settings.printerToken)
|
|||
|
|
|
|||
|
|
Dim fullUrl As String = My.Settings.apiUrl + "/api/print/last"
|
|||
|
|
' تنظیم timeout برای WebClient (از طریق ServicePointManager)
|
|||
|
|
SetWebClientTimeout(fullUrl, 30000) ' 30 ثانیه
|
|||
|
|
Dim headers As String = $"api-key: {My.Settings.token}, printer-key: {My.Settings.printerToken}"
|
|||
|
|
|
|||
|
|
' ثبت درخواست دریافت آخرین چاپ
|
|||
|
|
Logger.Instance.LogRequest("GET", fullUrl, headers, "")
|
|||
|
|
|
|||
|
|
Try
|
|||
|
|
Dim result As String = webClient.DownloadString(fullUrl)
|
|||
|
|
Logger.Instance.LogResponse("200", result, True)
|
|||
|
|
Logger.Instance.LogInfo("دریافت آخرین چاپ موفق انجام شد")
|
|||
|
|
Return result
|
|||
|
|
Catch ex As Exception
|
|||
|
|
Logger.Instance.LogError("خطا در دریافت آخرین چاپ", ex)
|
|||
|
|
Return ""
|
|||
|
|
End Try
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
' متد async برای دریافت آخرین چاپ بدون مسدود کردن UI
|
|||
|
|
Public Async Function getLastPrintAsync() As Task(Of String)
|
|||
|
|
Return Await Task.Run(Function()
|
|||
|
|
Return getLastPrint()
|
|||
|
|
End Function)
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
Public Function downloadFile(ByVal name As String, ByVal type As String) As Boolean
|
|||
|
|
' تنظیم SSL/TLS
|
|||
|
|
ConfigureSSL()
|
|||
|
|
|
|||
|
|
Dim webClient As New Net.WebClient
|
|||
|
|
webClient.Headers.Add("api-key", My.Settings.token)
|
|||
|
|
|
|||
|
|
Dim fileName As String = SpecialDirectories.CurrentUserApplicationData.ToString + "\" + type + ".pdf"
|
|||
|
|
Dim fullUrl As String = My.Settings.apiUrl + "/front/print/" + name
|
|||
|
|
' تنظیم timeout برای WebClient (از طریق ServicePointManager) - بیشتر برای دانلود فایل
|
|||
|
|
SetWebClientTimeout(fullUrl, 60000) ' 60 ثانیه برای دانلود فایل
|
|||
|
|
Dim headers As String = $"api-key: {My.Settings.token}"
|
|||
|
|
|
|||
|
|
' ثبت درخواست دانلود فایل
|
|||
|
|
Logger.Instance.LogRequest("GET", fullUrl, headers, "")
|
|||
|
|
Logger.Instance.LogInfo($"دانلود فایل: {name} (نوع: {type})")
|
|||
|
|
|
|||
|
|
Try
|
|||
|
|
webClient.DownloadFile(fullUrl, fileName)
|
|||
|
|
Logger.Instance.LogResponse("200", $"فایل با موفقیت دانلود شد: {fileName}", True)
|
|||
|
|
Logger.Instance.LogInfo($"دانلود فایل {type} موفق انجام شد")
|
|||
|
|
Return True
|
|||
|
|
Catch ex As Exception
|
|||
|
|
Logger.Instance.LogError($"خطا در دانلود فایل {name}", ex)
|
|||
|
|
Return False
|
|||
|
|
End Try
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
' متد async برای دانلود فایل بدون مسدود کردن UI
|
|||
|
|
Public Async Function downloadFileAsync(ByVal name As String, ByVal type As String) As Task(Of Boolean)
|
|||
|
|
Return Await Task.Run(Function()
|
|||
|
|
Return downloadFile(name, type)
|
|||
|
|
End Function)
|
|||
|
|
End Function
|
|||
|
|
End Class
|