-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp-server-launcher.ps1
More file actions
162 lines (130 loc) · 4.52 KB
/
Copy pathphp-server-launcher.ps1
File metadata and controls
162 lines (130 loc) · 4.52 KB
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
param([string]$FolderPath, [string]$PhpExePath)
Add-Type -AssemblyName System.Windows.Forms
if ([string]::IsNullOrWhiteSpace($FolderPath)) {
$FolderPath = Get-Location
}
if (!(Test-Path $FolderPath)) {
[System.Windows.Forms.MessageBox]::Show("Folder not found: $FolderPath", "Error", "OK", "Error")
exit 1
}
$phpCmd = $null
if ($PhpExePath -and (Test-Path $PhpExePath)) { $phpCmd = $PhpExePath }
elseif (Get-Command php -ErrorAction SilentlyContinue) { $phpCmd = (Get-Command php).Source }
if (-not $phpCmd) { [System.Windows.Forms.MessageBox]::Show("PHP not found! Please install first.", "Error", "OK", "Error"); exit 1 }
$portFile = "$env:TEMP\php-server-ports.json"
$activePorts = @{}
if (Test-Path $portFile) {
try {
$savedData = Get-Content $portFile -Raw | ConvertFrom-Json
if ($savedData) {
$savedData.PSObject.Properties | ForEach-Object {
$activePorts[$_.Name] = $_.Value
}
}
} catch {}
}
function Test-PortAvailable {
param([int]$Port)
try {
$connections = Get-NetTCPConnection -LocalPort $Port -ErrorAction SilentlyContinue
if ($connections) {
return $false
}
$netstat = netstat -an | Select-String ":$Port\s"
if ($netstat) {
return $false
}
$listener = [System.Net.Sockets.TcpListener]::new([System.Net.IPAddress]::Any, $Port)
$listener.Start()
$listener.Stop()
return $true
} catch {
return $false
}
}
function Get-PortForFolder {
param([string]$FolderPath)
$folderKey = $FolderPath
if ($activePorts.ContainsKey($folderKey)) {
$assignedPort = $activePorts[$folderKey]
if (Test-PortAvailable -Port $assignedPort) {
if ($activePorts.Values -notcontains $assignedPort -or $activePorts[$folderKey] -eq $assignedPort) {
return $assignedPort
}
}
$activePorts.Remove($folderKey)
}
if ($activePorts.Values -notcontains 80) {
if (Test-PortAvailable -Port 80) {
$activePorts[$folderKey] = 80
return 80
}
}
$md5 = [System.Security.Cryptography.MD5]::Create()
$hashBytes = $md5.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($FolderPath))
$folderHash = [BitConverter]::ToString($hashBytes).Replace("-", "").Substring(0, 8)
$hashInt = [Convert]::ToInt32($folderHash, 16)
$basePort = 8000 + ($hashInt % 1000)
$port = $basePort
$maxAttempts = 100
$attempt = 0
while ($attempt -lt $maxAttempts) {
if ($activePorts.Values -contains $port) {
$port = 8000 + (($basePort + $attempt) % 1000)
$attempt++
continue
}
if (Test-PortAvailable -Port $port) {
$activePorts[$folderKey] = $port
return $port
}
$port = 8000 + (($basePort + $attempt) % 1000)
$attempt++
}
$random = Get-Random -Minimum 8000 -Maximum 9000
for ($i = 0; $i -lt 100; $i++) {
$testPort = $random + $i
if ($testPort -gt 8999) { $testPort = 8000 + ($testPort - 9000) }
if ($activePorts.Values -notcontains $testPort) {
if (Test-PortAvailable -Port $testPort) {
$activePorts[$folderKey] = $testPort
return $testPort
}
}
}
if (Test-PortAvailable -Port 8000) {
$activePorts[$folderKey] = 8000
return 8000
}
for ($p = 8001; $p -lt 9000; $p++) {
if ($activePorts.Values -notcontains $p) {
if (Test-PortAvailable -Port $p) {
$activePorts[$folderKey] = $p
return $p
}
}
}
return 8000
}
$port = Get-PortForFolder -FolderPath $FolderPath
try {
$activePorts | ConvertTo-Json -Depth 10 | Set-Content $portFile -ErrorAction SilentlyContinue
} catch {}
$url = "http://localhost:$port"
$folderName = Split-Path -Leaf $FolderPath
try {
$Host.UI.RawUI.WindowTitle = "PHP - $folderName : Port $port"
} catch {}
Write-Host "Starting PHP server..." -ForegroundColor Green
Write-Host "Folder: $FolderPath" -ForegroundColor Cyan
Write-Host "Port: $port" -ForegroundColor Cyan
Write-Host "URL: $url" -ForegroundColor Cyan
Write-Host ""
try {
Start-Process $url
} catch {
Write-Host "Warning: Could not open browser automatically. Please open $url manually." -ForegroundColor Yellow
}
Push-Location $FolderPath
& $phpCmd -S localhost:$port
Pop-Location