ModSecret.vb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. '由于包含加解密等安全信息,本文件中的部分代码已被删除
  2. Imports System.Net
  3. Imports System.Reflection
  4. Imports System.Security.Cryptography
  5. Friend Module ModSecret
  6. #Region "杂项"
  7. '在开源内容的注册表键与普通内容的注册表键隔离
  8. Public Const RegFolder As String = "PCLDebug"
  9. Friend Sub SecretOnApplicationStart()
  10. '提升 UI 线程优先级
  11. Thread.CurrentThread.Priority = ThreadPriority.Highest
  12. '确保 .NET Framework 版本
  13. Try
  14. Dim VersionTest As New FormattedText("", Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, Fonts.SystemTypefaces.First, 96, New MyColor, DPI)
  15. Catch ex As UriFormatException '修复 #3555
  16. Environment.SetEnvironmentVariable("windir", Environment.GetEnvironmentVariable("SystemRoot"), EnvironmentVariableTarget.User)
  17. Dim VersionTest As New FormattedText("", Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, Fonts.SystemTypefaces.First, 96, New MyColor, DPI)
  18. End Try
  19. '检测当前文件夹权限
  20. Try
  21. Directory.CreateDirectory(Path & "PCL")
  22. Catch ex As Exception
  23. MsgBox($"PCL 无法创建 PCL 文件夹({Path & "PCL"}),请尝试:" & vbCrLf &
  24. "1. 将 PCL 移动到其他文件夹" & If(Path.StartsWithF("C:", True), ",例如 C 盘和桌面以外的其他位置。", "。") & vbCrLf &
  25. "2. 删除当前目录中的 PCL 文件夹,然后再试。" & vbCrLf &
  26. "3. 右键 PCL 选择属性,打开 兼容性 中的 以管理员身份运行此程序。",
  27. MsgBoxStyle.Critical, "运行环境错误")
  28. Environment.[Exit](Result.Cancel)
  29. End Try
  30. If Not CheckPermission(Path & "PCL") Then
  31. MsgBox("PCL 没有对当前文件夹的写入权限,请尝试:" & vbCrLf &
  32. "1. 将 PCL 移动到其他文件夹" & If(Path.StartsWithF("C:", True), ",例如 C 盘和桌面以外的其他位置。", "。") & vbCrLf &
  33. "2. 删除当前目录中的 PCL 文件夹,然后再试。" & vbCrLf &
  34. "3. 右键 PCL 选择属性,打开 兼容性 中的 以管理员身份运行此程序。",
  35. MsgBoxStyle.Critical, "运行环境错误")
  36. Environment.[Exit](Result.Cancel)
  37. End If
  38. '开源版本提示
  39. MsgBox($"该版本中不包含以下特性:
  40. - CurseForge 查询:需要 API Key(你可以申请一个,然后添加到 SecretHeadersSign 方法中)
  41. - 更新与联网通知:避免滥用隐患
  42. - 主题切换:这是需要赞助解锁的纪念性质的功能,别让赞助者太伤心啦
  43. - 百宝箱:开发早期往里面塞了些开发工具,整理起来太麻烦了,干脆噶了", MsgBoxStyle.Information, "开源版本说明")
  44. End Sub
  45. ''' <summary>
  46. ''' 获取设备标识码。
  47. ''' </summary>
  48. Friend Function SecretGetUniqueAddress() As String
  49. Return "0000-0000-0000-0000"
  50. End Function
  51. Friend Sub SecretLaunchJvmArgs(ByRef DataList As List(Of String))
  52. Dim DataJvmCustom As String = Setup.Get("VersionAdvanceJvm", Version:=McVersionCurrent)
  53. DataList.Insert(0, If(DataJvmCustom = "", Setup.Get("LaunchAdvanceJvm"), DataJvmCustom)) '可变 JVM 参数
  54. McLaunchLog("当前剩余内存:" & Math.Round(My.Computer.Info.AvailablePhysicalMemory / 1024 / 1024 / 1024 * 10) / 10 & "G")
  55. DataList.Add("-Xmn" & Math.Floor(PageVersionSetup.GetRam(McVersionCurrent) * 1024 * 0.15) & "m")
  56. DataList.Add("-Xmx" & Math.Floor(PageVersionSetup.GetRam(McVersionCurrent) * 1024) & "m")
  57. If Not DataList.Any(Function(d) d.Contains("-Dlog4j2.formatMsgNoLookups=true")) Then DataList.Add("-Dlog4j2.formatMsgNoLookups=true")
  58. End Sub
  59. ''' <summary>
  60. ''' 打码字符串中的 AccessToken。
  61. ''' </summary>
  62. Friend Function SecretFilter(Raw As String, FilterChar As Char) As String
  63. '打码 "accessToken " 后的内容
  64. If Raw.Contains("accessToken ") Then
  65. For Each Token In RegexSearch(Raw, "(?<=accessToken ([^ ]{5}))[^ ]+(?=[^ ]{5})")
  66. Raw = Raw.Replace(Token, New String(FilterChar, Token.Count))
  67. Next
  68. End If
  69. '打码当前登录的结果
  70. Dim AccessToken As String = McLoginLoader.Output.AccessToken
  71. If AccessToken Is Nothing OrElse AccessToken.Length < 10 OrElse Not Raw.ContainsF(AccessToken, True) OrElse
  72. McLoginLoader.Output.Uuid = McLoginLoader.Output.AccessToken Then 'UUID 和 AccessToken 一样则不打码
  73. Return Raw
  74. Else
  75. Return Raw.Replace(AccessToken, Left(AccessToken, 5) & New String(FilterChar, AccessToken.Length - 10) & Right(AccessToken, 5))
  76. End If
  77. End Function
  78. #End Region
  79. #Region "网络鉴权"
  80. Friend Function SecretCdnSign(UrlWithMark As String)
  81. '只处理以 {CDN} 结尾的链接
  82. If Not UrlWithMark.EndsWithF("{CDN}") Then Return UrlWithMark
  83. Return UrlWithMark.Replace("{CDN}", "").Replace(" ", "%20")
  84. End Function
  85. ''' <summary>
  86. ''' 设置 Headers 的 UA、Referer。
  87. ''' </summary>
  88. Friend Sub SecretHeadersSign(Url As String, ByRef Client As CookieWebClient, Optional UseBrowserUserAgent As Boolean = False)
  89. If ApplicationStartTick < 1 Then Return '禁止在没有运行程序的情况下通过反射调用
  90. If UseBrowserUserAgent Then
  91. Client.Headers("User-Agent") = "PCL2/" & VersionStandardCode & " Mozilla/5.0 AppleWebKit/537.36 Chrome/63.0.3239.132 Safari/537.36"
  92. Else
  93. Client.Headers("User-Agent") = "PCL2/" & VersionStandardCode
  94. End If
  95. Client.Headers("Referer") = "http://" & VersionCode & ".pcl2.server/"
  96. '如果你有 CurseForge API Key,请添加到 Headers 中,以恢复对 CurseForge 的访问
  97. End Sub
  98. ''' <summary>
  99. ''' 设置 Headers 的 UA、Referer。
  100. ''' </summary>
  101. Friend Sub SecretHeadersSign(Url As String, ByRef Request As HttpWebRequest, Optional UseBrowserUserAgent As Boolean = False)
  102. If ApplicationStartTick < 1 Then Return '禁止在没有运行程序的情况下通过反射调用
  103. If UseBrowserUserAgent Then
  104. Request.UserAgent = "PCL2/" & VersionStandardCode & " Mozilla/5.0 AppleWebKit/537.36 Chrome/63.0.3239.132 Safari/537.36"
  105. Else
  106. Request.UserAgent = "PCL2/" & VersionStandardCode
  107. End If
  108. Request.Referer = "http://" & VersionCode & ".pcl2.server/"
  109. '如果你有 CurseForge API Key,请添加到 Headers 中,以恢复对 CurseForge 的访问
  110. End Sub
  111. #End Region
  112. #Region "字符串加解密"
  113. ''' <summary>
  114. ''' 获取八位密钥。
  115. ''' </summary>
  116. Private Function SecretKeyGet(Key As String) As String
  117. Return "00000000"
  118. End Function
  119. ''' <summary>
  120. ''' 加密字符串。
  121. ''' </summary>
  122. Friend Function SecretEncrypt(SourceString As String, Optional Key As String = "") As String
  123. Key = SecretKeyGet(Key)
  124. Dim btKey As Byte() = Encoding.UTF8.GetBytes(Key)
  125. Dim btIV As Byte() = Encoding.UTF8.GetBytes("87160295")
  126. Dim des As New DESCryptoServiceProvider
  127. Using MS As New MemoryStream
  128. Dim inData As Byte() = Encoding.UTF8.GetBytes(SourceString)
  129. Using cs As New CryptoStream(MS, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)
  130. cs.Write(inData, 0, inData.Length)
  131. cs.FlushFinalBlock()
  132. Return Convert.ToBase64String(MS.ToArray())
  133. End Using
  134. End Using
  135. End Function
  136. ''' <summary>
  137. ''' 解密字符串。
  138. ''' </summary>
  139. Friend Function SecretDecrypt(SourceString As String, Optional Key As String = "") As String
  140. Key = SecretKeyGet(Key)
  141. Dim btKey As Byte() = Encoding.UTF8.GetBytes(Key)
  142. Dim btIV As Byte() = Encoding.UTF8.GetBytes("87160295")
  143. Dim des As New DESCryptoServiceProvider
  144. Using MS As New MemoryStream
  145. Dim inData As Byte() = Convert.FromBase64String(SourceString)
  146. Using cs As New CryptoStream(MS, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)
  147. cs.Write(inData, 0, inData.Length)
  148. cs.FlushFinalBlock()
  149. Return Encoding.UTF8.GetString(MS.ToArray())
  150. End Using
  151. End Using
  152. End Function
  153. #End Region
  154. #Region "主题"
  155. Public Color1 As New MyColor(52, 61, 74)
  156. Public Color2 As New MyColor(11, 91, 203)
  157. Public Color3 As New MyColor(19, 112, 243)
  158. Public Color4 As New MyColor(72, 144, 245)
  159. Public Color5 As New MyColor(150, 192, 249)
  160. Public Color6 As New MyColor(213, 230, 253)
  161. Public Color7 As New MyColor(222, 236, 253)
  162. Public Color8 As New MyColor(234, 242, 254)
  163. Public ColorBg0 As New MyColor(150, 192, 249)
  164. Public ColorBg1 As New MyColor(190, Color7)
  165. Public ColorGray1 As New MyColor(64, 64, 64)
  166. Public ColorGray2 As New MyColor(115, 115, 115)
  167. Public ColorGray3 As New MyColor(140, 140, 140)
  168. Public ColorGray4 As New MyColor(166, 166, 166)
  169. Public ColorGray5 As New MyColor(204, 204, 204)
  170. Public ColorGray6 As New MyColor(235, 235, 235)
  171. Public ColorGray7 As New MyColor(240, 240, 240)
  172. Public ColorGray8 As New MyColor(245, 245, 245)
  173. Public ColorSemiTransparent As New MyColor(1, Color8)
  174. Public ThemeNow As Integer = -1
  175. Public ColorHue As Integer = 210, ColorSat As Integer = 85, ColorLightAdjust As Integer = 0, ColorHueTopbarDelta As Object = 0
  176. Public ThemeDontClick As Integer = 0
  177. Public Sub ThemeRefresh(Optional NewTheme As Integer = -1)
  178. Hint("该版本中不包含主题功能……")
  179. End Sub
  180. Public Sub ThemeRefreshMain()
  181. End Sub
  182. Public Sub ThemeCheckAll(EffectSetup As Boolean)
  183. End Sub
  184. Public Function ThemeCheckOne(Id As Integer) As Boolean
  185. Return True
  186. End Function
  187. Friend Function ThemeUnlock(Id As Integer, Optional ShowDoubleHint As Boolean = True, Optional UnlockHint As String = Nothing) As Boolean
  188. Return False
  189. End Function
  190. Public Function ThemeCheckGold(Optional Code As String = Nothing) As Boolean
  191. Return False
  192. End Function
  193. Public Function DonateCodeInput() As Boolean?
  194. End Function
  195. #End Region
  196. #Region "更新"
  197. Public IsUpdateStarted As Boolean = False
  198. Public IsUpdateWaitingRestart As Boolean = False
  199. Public Sub UpdateCheckByButton()
  200. Hint("该版本中不包含更新功能……")
  201. End Sub
  202. Public Sub UpdateStart(BaseUrl As String, Slient As Boolean, Optional ReceivedKey As String = Nothing, Optional ForceValidated As Boolean = False)
  203. End Sub
  204. Public Sub UpdateRestart(TriggerRestartAndByEnd As Boolean)
  205. End Sub
  206. Public Sub UpdateReplace(ProcessId As Integer, OldFileName As String, NewFileName As String, TriggerRestart As Boolean)
  207. End Sub
  208. #End Region
  209. #Region "联网通知"
  210. Public ServerLoader As New LoaderTask(Of Integer, Integer)("PCL 服务", Sub() Log("[Server] 该版本中不包含更新通知功能……"), Priority:=ThreadPriority.BelowNormal)
  211. #End Region
  212. End Module