-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.ps1
60 lines (44 loc) · 2.09 KB
/
web.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
# This is a super **SIMPLE** example of how to create a very basic powershell webserver
# 2019-05-18 UPDATE — Created by me and and evalued by @jakobii and the comunity.
# from https://gist.github.com/19WAS85/5424431
# Http Server
$http = [System.Net.HttpListener]::new()
# Hostname and port to listen on
$http.Prefixes.Add("http://localhost:8080/")
# Start the Http Server
$http.Start()
# Log ready message to terminal
if ($http.IsListening) {
write-host " HTTP Server Ready! " -f 'black' -b 'gre'
write-host "now try going to $($http.Prefixes)" -f 'y'
write-host "then try going to $($http.Prefixes)other/path" -f 'y'
}
# INFINTE LOOP
# Used to listen for requests
while ($http.IsListening) {
# Get Request Url
# When a request is made in a web browser the GetContext() method will return a request object
# Our route examples below will use the request object properties to decide how to respond
$context = $http.GetContext()
# ROUTE EXAMPLE 1
# http://127.0.0.1/
if ($context.Request.HttpMethod -eq 'GET' -and $context.Request.RawUrl -eq '/') {
# We can log the request to the terminal
write-host "$($context.Request.UserHostAddress) => $($context.Request.Url)" -f 'mag'
# the html/data you want to send to the browser
# you could replace this with:
#[string]$html = Get-Content "C:\users\ys_so\MyDocuments\scripts\local\powersh\index.html" -Raw
[string]$html = Get-Content "index.html" -Raw
#[string]$html = @"
#<h1>A Powershell Webserver</h1><a href="index.html">website</a>
#"@
#resposed to the request
$buffer = [System.Text.Encoding]::UTF8.GetBytes($html) # convert htmtl to bytes
$context.Response.ContentLength64 = $buffer.Length
$context.Response.OutputStream.Write($buffer, 0, $buffer.Length) #stream to broswer
$context.Response.OutputStream.Close() # close the response
}
# powershell will continue looping and listen for new requests...
}
# Note:
# To end the loop you have to kill the powershell terminal. ctrl-c wont work :/