@@ -343,134 +343,4 @@ public static void StartNaidAuthorize(Action? completeCallback = null)
343343 }
344344
345345 #endregion
346-
347- #region 旧的 HTTP 服务端实现
348-
349- /* TODO ERROR: Skipped IfDirectiveTrivia
350- #If False
351- */ /* TODO ERROR: Skipped DisabledTextTrivia
352- Private Server As HttpListener
353- Public Class HttpServer
354- Public Sub New()
355- Server = New HttpListener()
356- Server.Prefixes.Add("http://127.0.0.1:29992/")
357- Server.Start()
358- Task.Run(
359- Async Function()
360- While True
361- Try
362- Dim Context As HttpListenerContext = Await Server.GetContextAsync()
363- ApiRoute(Context)
364- Catch ex As Exception
365- Log(ex, "[Server] 处理响应时发生错误")
366- End Try
367- End While
368- End Function)
369- End Sub
370-
371- Private CurrentStatus As New OAuthCompleteStatus()
372-
373- Public Sub ApiRoute(Context As HttpListenerContext)
374-
375-
376- Dim RequestUrl As String = Context.Request.Url.AbsolutePath
377- Dim OAuthCode As String = Nothing
378-
379- ' 多斜杠处理
380- While RequestUrl.Contains("//")
381- RequestUrl = RequestUrl.Replace("//", "/")
382- End While
383-
384- Select Case RequestUrl
385- Case "/api/naid/oauth20/callback"
386-
387- Dim Query = Context.Request.Url.Query
388- If Query.StartsWith("?") Then Query = Query.Substring(1)
389-
390- '在 URL 参数中寻找授权码
391- For Each Param As String In Query.Split("&"c)
392- If Param.StartsWithF("code=") Then
393- OAuthCode = Param.Substring(5)
394- End If
395- Next
396-
397- '设置状态信息
398- If OAuthCode IsNot Nothing Then
399- Dim result = NatayarkProfileManager.GetNaidDataSync(OAuthCode)
400- If result Then
401- CurrentStatus.success = True
402- CurrentStatus.username = NatayarkProfileManager.NaidProfile.Username
403- Else
404- CurrentStatus.success = False
405- CurrentStatus.message = $"获取用户信息失败,请尝试重新登录"
406- CurrentStatus.stacktrace = NatayarkProfileManager.Exception.ToString()
407- End If
408- Else
409- CurrentStatus.success = False
410- CurrentStatus.message = $"回调参数无效: {Query}"
411- End If
412-
413- '重定向至结束页
414- Context.Response.StatusCode = HttpStatusCode.Redirect
415- Context.Response.AddHeader("location", "/complete")
416- Context.Response.Close()
417- Case "/complete"
418- Try
419- Dim Data = GetResourceStream("Resources/oauth-complete.html")
420- If Data Is Nothing Then GoTo NotFound
421- Context.Response.StatusCode = HttpStatusCode.OK
422- Context.Response.AddHeader("Content-Type", "text/html, charset=utf-8")
423- Data.CopyTo(Context.Response.OutputStream)
424- Context.Response.OutputStream.Dispose()
425- Context.Response.Close()
426- Catch ex As Exception
427- GoTo NotFound
428- End Try
429- Case "/assets/background"
430- SyncLock ChangeLock
431- If PicAddress Is Nothing OrElse String.IsNullOrWhiteSpace(PicAddress) Then GoTo NotFound
432- Using FileReadStream As New FileStream(PicAddress, FileMode.Open, FileAccess.Read, FileShare.None, 16384, True)
433- Context.Response.StatusCode = HttpStatusCode.OK
434- Context.Response.AddHeader("Content-Type", "application/octet-stream")
435- FileReadStream.CopyTo(Context.Response.OutputStream)
436- Context.Response.OutputStream.Dispose()
437- Context.Response.Close()
438- End Using
439- End SyncLock
440- Case "/assets/icon.ico"
441- Try
442- Dim Data = GetResourceStream("Images/icon.ico")
443- If Data Is Nothing Then GoTo NotFound
444- Context.Response.StatusCode = HttpStatusCode.OK
445- Context.Response.AddHeader("Content-Type", "application/octet-stream")
446- Data.CopyTo(Context.Response.OutputStream)
447- Context.Response.OutputStream.Dispose()
448- Context.Response.Close()
449- Catch ex As Exception
450- GoTo NotFound
451- End Try
452- Case "/api/naid/oauth20/status"
453- Try
454- Dim status = JsonConvert.SerializeObject(CurrentStatus)
455- Dim buffer = Encoding.UTF8.GetBytes(status)
456- Context.Response.StatusCode = HttpStatusCode.OK
457- Context.Response.AddHeader("Content-Type", "application/json, charset=utf-8")
458- Context.Response.OutputStream.Write(buffer, 0, buffer.Length)
459- Context.Response.OutputStream.Dispose()
460- Context.Response.Close()
461- Catch ex As Exception
462- GoTo NotFound
463- End Try
464- Case Else
465- NotFound:
466- Context.Response.StatusCode = HttpStatusCode.NotFound
467- Context.Response.Close()
468- End Select
469- End Sub
470- End Class
471- */ /* TODO ERROR: Skipped EndIfDirectiveTrivia
472- #End If
473- */
474-
475- #endregion
476346}
0 commit comments