包含:
- 端口占用检测
- 心跳包网络断线检测
- 传入的数据是否为合法有效的目录路径检测
- 读取计算文件夹大小(不含软链接|symlink)
- 传回查询到的文件夹大小
Powershell Service
| |
| [cmdletbinding()] Param($Port = 8888) |
| |
| $VerbosePreference = "Continue" |
| |
| |
| |
| Write-Verbose("Check port {0} busy/available" -f $Port) |
| $Connection = Get-NetTCPConnection -LocalPort $Port -ErrorAction SilentlyContinue |
| if($Connection){ |
| if($Connection.OwningProcess.GetType().Name -eq "UInt32"){ |
| $Process_ID = $Connection.OwningProcess |
| } |
| else{ |
| $Process_ID = $Connection.OwningProcess[1] |
| |
| } |
| if($Process_ID){ |
| Write-Error("Port {0} is busy, using by process ID {1}." -f $Port, $Process_ID) |
| Exit |
| } |
| else{ |
| Write-Verbose("Port {0} is available" -f $Port) |
| } |
| } |
| else{ |
| Write-Verbose("Port {0} is available" -f $Port) |
| } |
| $Connection = $Null |
| |
| $Listener = New-Object System.Net.Sockets.TcpListener -ArgumentList $Port |
| try{ |
| $Listener.Start() |
| |
| |
| |
| |
| Write-Verbose("Start linsten.") |
| } |
| catch { |
| Write-Error("Linsten failed.") |
| throw |
| } |
| |
| Write-Verbose "Server started." |
| |
| While ($True) { |
| Write-Host("Waiting connection from port {0}" -f $Port) |
| |
| $Socket_TCP_Client = $Listener.AcceptTcpClient() |
| |
| |
| $Client_Address = $Socket_TCP_Client.client.RemoteEndPoint.ToString() |
| Write-Verbose ("New connection from {0}" -f $Client_Address) |
| |
| |
| |
| $Stream = $Socket_TCP_Client.GetStream() |
| $Stream.ReadTimeout = 1000 |
| |
| $Stream_Reader = [System.IO.StreamReader]::new($Stream) |
| $Stream_Writer = New-Object System.IO.StreamWriter($Stream) |
| |
| $Previous_Communication_Time = Get-Date -UFormat %s |
| |
| $string = $Null |
| While ($Socket_TCP_Client.Connected) { |
| Write-Verbose "Try to read line." |
| try{ |
| $string = $Stream_Reader.ReadLine() |
| } |
| catch{ |
| |
| |
| |
| Write-Verbose "Timeout, waiting data." |
| } |
| |
| if($string -eq "exit"){ |
| Write-Verbose "Exit." |
| |
| $Stream_Reader.Dispose() |
| Write-Verbose "Stream_Reader.Dispose done." |
| $Stream_Reader.Close() |
| Write-Verbose "Stream_Reader.Close done." |
| |
| $Stream_Writer.Dispose() |
| Write-Verbose "Stream_Writer.Dispose done." |
| $Stream_Writer.Close() |
| Write-Verbose "Stream_Writer.Close done." |
| |
| $Stream.Dispose() |
| Write-Verbose "Stream.Dispose done." |
| $Stream.Close() |
| Write-Verbose "Stream.Close done." |
| |
| $Socket_TCP_Client.Dispose() |
| Write-Verbose "Socket_TCP_Client.Dispose done." |
| $Socket_TCP_Client.Close() |
| Write-Verbose "Socket_TCP_Client.Close done." |
| |
| break |
| } |
| if ($string) { |
| Write-Verbose "Data available, line read." |
| Write-Host("Message received from {0}:`n {1}" -f $Client_Address, $string) |
| |
| $Previous_Communication_Time = Get-Date -UFormat %s |
| |
| $Path = $string |
| $string = $Null |
| |
| $Is_Path_Exist_Folder = $Null |
| try{ |
| $Is_Path_Exist_Folder = Test-Path -Path $Path -PathType Container |
| |
| |
| }catch{} |
| if($Is_Path_Exist_Folder){ |
| Write-Verbose "Path valid - Path is a folder." |
| } |
| else{ |
| Write-Verbose "Path invalid - Path is not a folder, skip." |
| } |
| |
| if($Is_Path_Exist_Folder){ |
| Write-Verbose "Measure size (length) of folder.." |
| $Command = 'Get-ChildItem -Path "'+ $Path + '" -Recurse | Measure-Object -Property Length -Sum' |
| $GenericMeasureInfo = Invoke-Expression $Command |
| $Size = $GenericMeasureInfo.Sum |
| Write-Verbose("Folder size (length) is {0}." -f $Size) |
| |
| Write-Verbose "Send size to client." |
| $Stream_Writer.WriteLine($Size) |
| $Stream_Writer.Flush() |
| } |
| else{ |
| Write-Verbose "Send error code to client." |
| $Stream_Writer.WriteLine("") |
| $Stream_Writer.Flush() |
| } |
| } |
| ElseIf((($Current_Time = (Get-Date -UFormat %s)) - $Previous_Communication_Time) -gt 3){ |
| $bytes = 0 |
| try{ |
| Write-Verbose "Heart-beat test." |
| |
| $Stream_Writer.WriteLine("") |
| $Stream_Writer.Flush() |
| $Previous_Communication_Time = $Current_Time |
| Write-Verbose("Connection {0} alive." -f $Client_Address) |
| }catch{ |
| Write-Verbose("Connection {0} failed." -f $Client_Address) |
| break |
| } |
| } |
| } |
| if($string -eq "exit"){ |
| break |
| }else{ |
| Write-Host "Client disconnect." |
| } |
| } |
| |
| $Listener.Stop() |
| Write-Verbose "Listener.Stop done." |
| Write-Verbose "Server stopped." |
| |
| Write-Host "Exit." |
| Exit |
Lua Client
| |
| local Socket = require("socket") |
| |
| local function Query_Size(self,Path) |
| local Client = self._Client |
| Client:send(Path..'\n') |
| local result,response = '' |
| repeat |
| response = Client:receive"*l" |
| result = result .. (response or '') |
| until not response or result~='' |
| return result |
| end |
| local function Close(self) |
| local Client = self._Client |
| |
| Client:close() |
| end |
| local Metatable = {__gc = Close,} |
| |
| local Master = { |
| Connect = function(Host_Name_or_IP,Port) |
| local Client = assert(Socket.connect(Host_Name_or_IP or "localhost", Port or 8888)) |
| |
| local Client_Agent = setmetatable( |
| { |
| Query_Size = Query_Size, |
| Query = Query_Size, |
| Close_Client = Close, |
| Close = Close, |
| _Client = Client, |
| Close_Service = function() |
| Client:send"Exit\n" |
| Client:close() |
| end, |
| },Metatable |
| ) |
| return Client_Agent |
| end, |
| } |
| |
| if not ... then |
| local Client = Master.Connect() |
| print(Client:Query_Size[[H:\Ghost]]) |
| print(Client:Query[[H:\土豆]]) |
| Client.Close_Service() |
| Client:Close() |
| end |
| |
| return Master |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!