-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebServer_Logic.ps1
161 lines (140 loc) · 5.61 KB
/
WebServer_Logic.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
param(
[IO.DirectoryInfo]$root = $PWD.Path
,[String[]]$hosts = @("*")
,[int[]]$Https = @()
,[int[]]$Http = @()
,[Switch]$NoFolderListing
)
$global:restart = $false
$script:listener = $null
$counter = 0
If (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
write-host "Du benötigst Adminrechte um dies auszuführen!"
return
}
# make sure base folder is a directory
if( !($base= (resolve-path "$($root.FullName)\" -ea 0).Path) ) {
return write-error "Der Ordner '$root' existiert nicht"
}
function Stop-Listening{
Get-EventSubscriber | Unregister-Event
if( $script:listener ) {
$script:listener.Stop()
$script:listener = $null
}
}
function Set-ContentType($ext) {
$k = [Microsoft.Win32.Registry]::ClassesRoot.OpenSubKey($ext)
if( $k ) {
$v = $k.GetValue("Content Type")
if( $v ) { $script:rs.ContentType=$v }
$k.Dispose()
}
}
function Send-Content($content) {
try {
$script:rs.ContentLength64 = $content.Length
$script:rs.OutputStream.Write($content, 0, $content.Length)
$script:rs.StatusCode = 200
} catch {
$script:rs.StatusCode = 500
}
finally {
$script:rs.Close()
}
}
function Send-Text($content) {
Send-Content ([System.Text.Encoding]::UTF8.GetBytes($content))
}
function Send-Html($content) {
Set-ContentType ".html"
Send-Text "<html><body><pre>$content</pre></body></html>"
}
function Send-DirectoryInfo( $dir, $path) {
if( $path ) { $path = "/" + $path }
send-html $(
"Directory: $dir<br><br>"
"Mode Letzte Änderung Größe Name<br>"
"------ ------------------- -------------- ------------------------<br>"
if( $dir -ne $base ) { "d----- $(''.PadLeft(46))<a href='$path/..'>..</a><br>" }
dir $dir |% {
$each = $_
switch( $each.GetType() ) {
([IO.DirectoryInfo]) { "$($each.Mode) $($each.LastWriteTime) $(''.PadLeft(13)) <a href='$path/$($each.Name)'>$($each.Name)</a><br>" }
([IO.FileInfo]) { "$($each.Mode) $($each.LastWriteTime) $("$($each.length)".PadLeft(13)) <a href='$path/$($each.Name)'>$($each.Name)</a><br>" }
}
}
)
}
function Send-File($file) {
Set-ContentType ((dir $file).Extension)
Send-Content (get-content $file -raw -encoding byte)
}
function Send-404 {
$script:rs.StatusCode = 404
$script:rs.Close()
}
# clean up any leftover listeners
Stop-Listening
# track if this script changes so we can restart.
$fsw = New-Object IO.FileSystemWatcher $PSScriptRoot, $MyInvocation.MyCommand.Name -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite';EnableRaisingEvents = $true }
$null = Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action { $global:restart = $true }
try {
$script:listener = New-Object System.Net.HttpListener
# if nothing specified do both on default ports
if( (-not $Http) -and (-not $Https ) ) { $http = @("80"); $https=@("443") }
# bind addresses/ports
$hosts |% {
$h = $_
$http |% { "http://$($h):$_/" }
$https |% { "https://$($h):$_/" }
} |% {
$script:listener.Prefixes.Add($_)
Write-Host "Lauscht auf [$_]"
}
# start listening
$script:listener.Start()
$task = $script:listener.GetContextAsync()
while ($script:listener.IsListening -and (-not $global:restart) )
{
# Incoming Request.
if( $task.IsCompleted ) {
$requestUrl = $task.Result.Request.Url
$script:rs = $task.Result.Response
$task = $script:listener.GetContextAsync()
switch( $path = ($requestUrl.LocalPath) -replace '//','/' ) {
# handle a few paths manually
"/about" { Send-Html "MinersWin's PowerShell WebServer<br><br>Source on <a href='https://github.com/MinersWin/PowerShell-WebServer'>github</a><br>Follow me on <a href='https://twitter.com/minerswins'>Twitter</a>" }
"/quit" { Send-Html "Verlasse..."; Write-Host "`nQuitting..."; break; }
"/restart" {Send-Html "Starte neu..."; $global:restart= $true;}
# files or directories
default {
if( ($local = (resolve-path "$base\$path" -ea 0)) ) {
switch((Get-Item $local).GetType() ) {
# makes it easier to add more types later :)
([Io.DirectoryInfo]) { if( $NoFolderListing ) { Send-404 } else { Send-DirectoryInfo $local.Path $path.trim("/") } }
([IO.FileInfo]) { Send-File $local}
default { Send-404 }
}
} else { Send-404 }
}
}
Write-Host "$($script:rs.StatusCode): [$requestUrl] > $local"
} else {
[Threading.Thread]::Sleep(1)
if( ($counter += 0.01) -gt 100 ){
$counter = 0
}
Write-Progress -Activity "Listening..." -PercentComplete $counter -CurrentOperation "Idle..." -Status "Warte auf Anfrage."
}
}
} finally {
Stop-Listening
}
if( $global:restart ) {
Write-Host "`nStarte neu"
$global:restart = $null
. "$PSCommandPath" -root $base -hosts $hosts -https $https -http $http -NoFolderListing:$NoFolderListing
} else {
Write-Host "`nFertig."
}