-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
193 lines (153 loc) · 6.04 KB
/
index.js
File metadata and controls
193 lines (153 loc) · 6.04 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import {exec} from 'child_process'
import os from 'os'
import http from 'http'
import {URL} from 'url'
import fs from 'fs'
import fsp from 'fs/promises'
import {v4 as uuidv4} from 'uuid'
import {Server as SSDPServer} from 'node-ssdp'
// import webhook from "./webhook.js";
const port = 57339
const getUUID = async () => {
try {
await fsp.access('uuid', fs.constants.F_OK)
} catch (err) {
await fsp.writeFile('uuid', uuidv4(), {encoding: 'utf8'})
}
return await fsp.readFile('uuid', 'utf8')
}
const uuid = await getUUID()
http.createServer((request, response) => {
const url = new URL(request.url, `http://${request.headers.host}`)
switch (url.pathname) {
case '/discovery':
const requestIP = request.headers.host.split(':')[0]
let ip, mac, interfaceName
for (const [iName, addresses] of Object.entries(os.networkInterfaces())) {
for (const address of addresses) {
if (address.address === requestIP) {
interfaceName = iName
ip = address.address
mac = address.mac
}
}
}
response.setHeader('Content-Type', 'application/json')
return response.end(JSON.stringify({
id: uuid,
name: process.env['COMPUTERNAME'] || os.hostname() || 'PC Control',
'interface': interfaceName,
ip, port, mac
}))
case '/':
fs.readFile('index.html', (error, content) => {
response.end(content, 'utf-8')
})
break
case '/sleep':
exec('pc-control sleep', {windowsHide: true})
return response.end('sleep')
case '/lock':
exec('pc-control lock', {windowsHide: true})
return response.end('lock')
case '/display/off':
exec('pc-control monitor off', {windowsHide: true})
return response.end('display off')
case '/display/on':
exec('pc-control monitor on', {windowsHide: true})
return response.end('display on')
case '/display/switch/external':
exec('DisplaySwitch.exe /external', {windowsHide: true})
return response.end('display external')
case '/display/switch/internal':
exec('DisplaySwitch.exe /internal', {windowsHide: true})
return response.end('display internal')
case '/display/switch/extend':
exec('DisplaySwitch.exe /extend', {windowsHide: true})
return response.end('display extend')
case '/display/switch/clone':
exec('DisplaySwitch.exe /clone', {windowsHide: true})
return response.end('display clone')
case '/volume':
let volume = parseInt(url.searchParams.get('volume'))
if (isNaN(volume)) {
response.statusCode = 400
return response.end('Invalid volume')
}
volume = Math.min(Math.max(volume, 0), 100)
exec(`pc-control volume ${volume}`, {windowsHide: true})
return response.end(`volume: ${volume}`)
case '/mute':
exec(`pc-control mute on`, {windowsHide: true})
return response.end('mute')
case '/unmute':
exec(`pc-control mute off`, {windowsHide: true})
return response.end('unmute')
case '/media/play_pause':
exec(`pc-control media play_pause`, {windowsHide: true})
return response.end('media play_pause')
case '/media/stop':
exec(`pc-control media stop`, {windowsHide: true})
return response.end('media stop')
case '/media/next':
exec(`pc-control media next`, {windowsHide: true})
return response.end('media next')
case '/media/previous':
exec(`pc-control media previous`, {windowsHide: true})
return response.end('media previous')
case '/status':
response.setHeader('Content-Type', 'application/json')
exec('pc-control status', (error, stdout, stderr) => {
let status = stdout.trim().split(' ');
response.end(JSON.stringify({
volume: Math.round(parseFloat(status[0])),
mute: status[1] === 'on' ? 'muted' : 'unmuted',
}))
});
return
// case '/webhook/update':
// const webhook = url.searchParams.get('url')
// fsp.writeFile('webhook', webhook, {encoding: 'utf8'})
// return response.end()
//
// case '/webhook/delete':
// fsp.writeFile('webhook', '', {encoding: 'utf8'})
// return response.end()
default:
response.statusCode = 404
return response.end('404 Not Found')
}
}).listen(port)
const ssdpServer = new SSDPServer({
explicitSocketBind: true,
// interfaces: ['Ethernet'/*, 'Wi-Fi'*/],
suppressRootDeviceAdvertisements: true,
allowWildcards: false,
adInterval: 60 * 1000,
location: {
port: port,
path: '/discovery'
},
udn: uuid,
// customLogger: console.log
})
// server.addUSN('upnp:rootdevice')
// server.addUSN('urn:schemas-upnp-org:device:MediaServer:1')
// server.addUSN('urn:schemas-upnp-org:service:ContentDirectory:1')
// server.addUSN('urn:schemas-upnp-org:service:ConnectionManager:1')
ssdpServer.addUSN('urn:SmartExpanse:service:PCControl:1')
ssdpServer.on('advertise-alive', (heads) => {
// console.log('advertise-alive', heads)
// Expire old devices from your cache.
// Register advertising device somewhere (as designated in http headers heads)
})
ssdpServer.on('advertise-bye', (heads) => {
// console.log('advertise-bye', heads)
// Remove specified device from cache.
})
// start server on all interfaces
ssdpServer.start().then(() => {
console.log('SSDP Server started.')
}).catch(e => {
console.log('SSDP Failed to start server:', e)
})