forked from MultiPoolMiner/MultiPoolMiner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.psm1
129 lines (117 loc) · 4.69 KB
/
API.psm1
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
Function Start-APIServer {
# Create a global synchronized hashtable that all threads can access to pass data between the main script and API
$Global:API = [hashtable]::Synchronized(@{})
# Setup flags for controlling script execution
$API.Stop = $false
$API.Pause = $false
# Setup runspace to launch the API webserver in a separate thread
$newRunspace = [runspacefactory]::CreateRunspace()
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("API", $API)
$newRunspace.SessionStateProxy.Path.SetLocation($(pwd))
$apiserver = [PowerShell]::Create().AddScript({
# Setup the listener
$Server = New-Object System.Net.HttpListener
# Listening on anything other than localhost requires admin priviledges
$Server.Prefixes.Add("http://localhost:3999/")
$Server.Start()
While ($Server.IsListening) {
$Context = $Server.GetContext()
$Request = $Context.Request
$URL = $Request.Url.OriginalString
# Determine the requested resource - remove any query strings and trailing slashes
$RequestedResource = ($Request.RawUrl -Split '\?')[0].TrimEnd('/')
# Create a new response and the defaults for associated settings
$Response = $Context.Response
$ContentType = "application/json"
$StatusCode = 200
$Data = ""
# Set the proper content type, status code and data for each resource
Switch($RequestedResource) {
"" {
$ContentType = "text/html"
$Data = Get-Content('APIDocs.html')
break
}
"/version" {
$Data = $API.Version | ConvertTo-Json
break
}
"/activeminers" {
$Data = $API.ActiveMiners | ConvertTo-Json
break
}
"/runningminers" {
$Data = $API.RunningMiners | ConvertTo-Json
Break
}
"/failedminers" {
$Data = $API.FailedMiners | ConvertTo-Json
Break
}
"/pools" {
$Data = $API.Pools | ConvertTo-Json
Break
}
"/newpools" {
$Data = $API.NewPools | ConvertTo-Json
Break
}
"/allpools" {
$Data = $API.AllPools | ConvertTo-Json
Break
}
"/miners" {
$Data = $API.Miners | ConvertTo-Json
Break
}
"/config" {
$Data = $API.Config | ConvertTo-Json
Break
}
"/debug" {
$Data = $API | ConvertTo-Json
Break
}
"/devices" {
$Data = $API.Devices | ConvertTo-Json
Break
}
"/stats" {
$Data = $API.Stats | ConvertTo-Json
Break
}
"/watchdogtimers" {
$Data = $API.WatchdogTimers | ConvertTo-Json
Break
}
"/stop" {
$API.Stop = $true
$Data = "Stopping"
break
}
default {
$StatusCode = 404
$ContentType = "text/html"
$Data = "URI '$RequestedResource' is not a valid resource."
}
}
# If $Data is null, the API will just return whatever data was in the previous request. Instead, show an error
# This happens if the script just started and hasn't filled all the properties in yet.
If($Data -eq $Null) {
$Data = @{'Error' = "API data not available"} | ConvertTo-Json
}
# Send the response
$Response.Headers.Add("Content-Type", $ContentType)
$Response.StatusCode = $StatusCode
$ResponseBuffer = [System.Text.Encoding]::UTF8.GetBytes($Data)
$Response.ContentLength64 = $ResponseBuffer.Length
$Response.OutputStream.Write($ResponseBuffer,0,$ResponseBuffer.Length)
$Response.Close()
}
# Only gets here if something is wrong and the server couldn't start or stops listening
$Server.Stop()
}) #end of $apiserver
$apiserver.Runspace = $newRunspace
$apihandle = $apiserver.BeginInvoke()
}