-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.ps1
194 lines (172 loc) · 4.29 KB
/
base.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
$script:FountEncoding = [System.Text.Encoding]::UTF8
$script:FountClient = $null
$script:FountStream = $null
function Set-FountClient($ComputerName = "localhost", $Port = 16698, $timeout = 100) {
if ($script:FountClient) {
if ($script:FountClient.Connected) {
return $script:FountClient
}
else {
Close-FountClient
}
}
# 创建新的 TCP 客户端
$Client = New-Object System.Net.Sockets.TcpClient
if ($Client.ConnectAsync($ComputerName, $Port).Wait($timeout)) {
if ($Client.Connected) {
Write-Verbose "成功连接到 Fount 服务器。"
$script:FountClient = $Client
return
}
}
Write-Error "无法连接到 Fount 服务器" -ErrorAction Stop
}
function Close-FountClient {
if ($script:FountStream) {
$script:FountStream.Dispose()
$script:FountStream = $null
}
if ($script:FountClient) {
if ($script:FountClient.Connected) {
$script:FountClient.Close()
}
$script:FountClient.Dispose()
$script:FountClient = $null
}
}
function Invoke-FountIPC(
[string]$Type,
[hashtable]$Data,
[string]$ComputerName = "localhost",
[int]$Port = 16698
) {
# 构建要发送的完整 JSON 对象
$CommandObject = @{
type = $Type
data = $Data
}
$JsonCommand = $CommandObject | ConvertTo-Json -Compress -Depth 100
$JsonCommand += "`n" # 添加换行符作为消息结束符
try {
# 获取或建立连接
if (-not $script:FountClient) {
Set-FountClient -ComputerName $ComputerName -Port $Port
if (-not $script:FountClient) {
Write-Error "无法连接到 Fount 服务器" -ErrorAction Stop
}
}
if (-not $script:FountClient.Connected) {
$script:FountClient.Connect($ComputerName, $Port)
if ($script:FountStream) {
$script:FountStream.Dispose()
}
$script:FountStream = $null
}
if (-not $script:FountStream) {
$script:FountStream = $script:FountClient.GetStream()
}
# 获取网络流
$Stream = $script:FountStream
$Encoding = $script:FountEncoding
# 发送数据
$Bytes = $Encoding.GetBytes($JsonCommand)
$Stream.Write($Bytes, 0, $Bytes.Length)
$Stream.Flush()
# 读取响应(支持大响应和分块读取)
$MemoryStream = New-Object System.IO.MemoryStream
$Buffer = New-Object byte[] 4096
$FoundTerminator = $false
do {
$Read = $Stream.Read($Buffer, 0, $Buffer.Length)
if ($Read -gt 0) {
$MemoryStream.Write($Buffer, 0, $Read)
# 检查是否包含换行符
$CurrentData = $Encoding.GetString($MemoryStream.ToArray())
if ($CurrentData -match "`n") {
$FoundTerminator = $true
break
}
}
} while ($Read -gt 0 -and -not $FoundTerminator)
# 提取有效响应
$ResponseString = $Encoding.GetString($MemoryStream.ToArray())
# 解析 JSON 响应
if (-not [string]::IsNullOrEmpty($ResponseString)) {
$result = ConvertFrom-Json -InputObject $ResponseString
if ($result.status -ne 'ok') {
Write-Error "与 Fount 服务器通信失败: $($result.message)" -ErrorAction Stop
}
else {
return $result.data
}
}
else {
Write-Error "收到空响应" -ErrorAction Stop
}
}
finally {
if ($MemoryStream) {
$MemoryStream.Dispose()
}
}
}
function Start-FountShell {
param(
[Parameter(Mandatory)]
[string]$UserName,
[Parameter(Mandatory)]
[string]$ShellName,
[Parameter(ValueFromRemainingArguments)]
[array]$Arguments
)
Invoke-FountIPC -Type "runshell" -Data @{
username = $UserName
shellname = $ShellName
args = $Arguments
}
}
function Invoke-FountShell {
param (
[Parameter(Mandatory)]
[string]$UserName,
[Parameter(Mandatory)]
[string]$ShellName,
$Data
)
Invoke-FountIPC -Type "invokeshell" -Data @{
username = $UserName
shellname = $ShellName
data = $Data
}
}
function Test-FountRunning {
try {
Invoke-FountIPC -Type "ping" -ErrorAction Stop | Out-Null
$true
}
catch {
$false
}
}
function Install-Fount {
$scriptContent = Invoke-RestMethod https://raw.githubusercontent.com/steve02081504/fount/refs/heads/master/src/runner/main.ps1
Invoke-Expression "function fountInstaller { $scriptContent }"
fountInstaller @args
}
function Start-Fount(
[switch] $NoAutoInstall
) {
if (Get-Command fount -ErrorAction SilentlyContinue) {
fount @args
}
elseif (!$NoAutoInstall) {
Install-Fount @args
}
else {
Write-Error "Fount 未安装"
}
}
function Stop-Fount {
Invoke-FountIPC -Type "shutdown"
Close-FountClient
}