-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ps1
More file actions
77 lines (76 loc) · 2.94 KB
/
server.ps1
File metadata and controls
77 lines (76 loc) · 2.94 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
param(
$prefixes = @("http://localhost:80/","https://localhost:443/"),
$responses=@{
"/setup"="Get set up, dweeb";
"/heartbeat"= @("<i>Ah ah ah ah staying alive staying alive, Ah ah ah ah</i>","text/html",@{
"staying-alii-ii-ii-" = "iiive"
});
"/report/summary"=(@("{`"result`":`"It happened`"`}","application/json"));
"/report/detailed"=(@("<result message=`"Man, I don't know. You did things. A lot of them. You won doing things. Congratulations.`"><secret>Achievement Unlocked:Easter Egg</secret></result>",
[System.Net.Mime.MediaTypeNames+Text]::Xml,@{}, 425));
}
)
<#
HTTPS support requires a cert. use the makecert -pe flag when making the ssl cert, the root cert works as written.
certlm, not certmgr. import from there, don't install from file.
https://stackoverflow.com/questions/11403333/httplistener-class-with-https-support
This guid works `{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7`}
#>
function processRequest(){
$context=$listener.GetContext();
$request=$context.request;
$response=$context.response;
$url = $request.url
$response.statuscode = 418
$buffer=[byte[]]("<h1>Hello world, there's nothing here</h1>").ToCharArray();
$response.addheader("Teapot","True")
write-host $url.AbsolutePath -foregroundcolor magenta
if($responses.containskey($url.AbsolutePath)){
$response.statuscode = 200
write-host ("We have a response "+$responses[$url.AbsolutePath].GetType().Name+" "+($responses[$url.AbsolutePath].GetType().Name -eq "String")+":") -foregroundcolor yellow
if($responses[$url.AbsolutePath].GetType().Name -eq "String"){
write-host $responses[$url.AbsolutePath]
write-host "Raw Response" -foregroundcolor red
$buffer=[byte[]]($responses[$url.AbsolutePath]).ToCharArray();
}
else{
write-host "Complex Response" -foregroundcolor red
write-host $responses[$url.AbsolutePath][0]
write-host $responses[$url.AbsolutePath][1]
if( $responses[$url.AbsolutePath][2]-ne $null){
$responses[$url.AbsolutePath][2].keys|%{
write-host $_
$response.addheader($_,$responses[$url.AbsolutePath][2][$_])
}
}
if($responses[$url.AbsolutePath][3] -ne $null){
$response.statuscode=$responses[$url.AbsolutePath][3]
}
$response.ContentType=$responses[$url.AbsolutePath][1]
$buffer=[byte[]]($responses[$url.AbsolutePath][0]).ToCharArray();
}
}
$response.contentlength64=$buffer.length;
$output=$response.outputstream;
$output.write($buffer,0,$buffer.length)
$output.close()
write-host "Returning request and response" -foregroundcolor magenta
if($responses.containskey($url.AbsolutePath)){
$request
$response
}
}
$listener = [System.Net.HttpListener]::new()
$prefixes|%{$listener.Prefixes.Add($_)}
$listener.start()
do{
write-output (processRequest)
if ([Console]::KeyAvailable)
{
$keyInfo = [Console]::ReadKey($true)
if($keyinfo.Key -eq [ConsoleKey]::X){
break
}
}
}while($true)
$listener.stop()