增加https的支持

master
yutent 2024-02-21 14:52:53 +08:00
parent e3fb4fcf41
commit fce36595f4
5 changed files with 72 additions and 16 deletions

View File

@ -22,6 +22,8 @@
> 在项目根目录中放置 `.httpserver`文件, 并配置好之后, 重启或重新加载窗口, 以启动服务。
## 插件通用配置
> 通用配置可以设置`domain`,`key`,`cert`3个字段, 可省的每个项目配置一次。通用配置优先级低于项目的配置。
## .httpserver 文件
> 使用`.httpserver`来配置web服务器信息, 该文件应为一个json格式的。
@ -30,6 +32,11 @@
{
"enabled": true, // 这里配置为 true 才会启动web服务
"port": 23333, // 【可选】 默认使用 23333 端口, 如果被使用了, 会向上查找可用端口
"https": true, // 【可选】 是否使用https
"http2": true, // 【可选】 是否使用http2, 等价于https, 用哪个都可以
"domain" "", // 【可选】有域名, 就会用域名打开页面, 没有就用IP
"key": "", // 【可选】域名证书的绝对路径(仅当开启https/2时需要)
"cert": "", // 【可选】域名证书的绝对路径(仅当开启https/2时需要)
"root": ".", // 【可选】 web服务的根目录, 默认为当前项目的根目录, 可填写相对项目根目录的路径。
"headers": { // 【可选】支持注入一些头信息, 方便特殊需要的调试
"cache-control": "no-store" // 默认为 no-store

View File

@ -8,6 +8,7 @@ const vsc = require('vscode')
const { join, basename } = require('path')
const { parse } = require('url')
const http = require('http')
const http2 = require('http2')
const fs = require('iofs')
const { WebSocketServer } = require('ws')
@ -42,6 +43,7 @@ const MIME_TYPES = {
other: 'application/octet-stream'
}
const SERVER_OPTIONS = { allowHTTP1: true }
const COMMON_HEADERS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
@ -61,6 +63,8 @@ let port = 23333
let baseUrl = 'http://127.0.0.1:' + port
let server
let ws = null
let domain
let isHttps = false
const HMR_SCRIPT = `
<script>
@ -118,8 +122,12 @@ function createServer() {
}
console.log(`尝试使用${port}端口...`)
server = http
.createServer(function (req, res) {
server = (isHttps ? http2.createSecureServer : http.createServer)(
isHttps ? SERVER_OPTIONS : void 0
)
server
.on('request', function (req, res) {
let pathname = parse(req.url).pathname.slice(1)
let paths = pathname.split('/')
@ -153,7 +161,7 @@ function createServer() {
res.setHeader('content-length', stat.size)
}
res.writeHead(200, 'OK')
res.writeHead(200, isHttps ? void 0 : 'OK')
if (ext === 'html') {
res.end(code)
@ -172,7 +180,7 @@ function createServer() {
res.setHeader('accept-ranges', 'bytes')
res.setHeader('content-type', MIME_TYPES.html)
res.setHeader('content-length', Buffer.byteLength(code))
res.writeHead(200, 'OK')
res.writeHead(200, isHttps ? void 0 : 'OK')
res.end(code)
return
@ -188,7 +196,7 @@ function createServer() {
res.setHeader('accept-ranges', 'bytes')
res.setHeader('content-type', MIME_TYPES.html)
res.setHeader('content-length', Buffer.byteLength(code))
res.writeHead(200, 'OK')
res.writeHead(200, isHttps ? void 0 : 'OK')
res.end(code)
return
@ -196,27 +204,29 @@ function createServer() {
}
res.setHeader('content-length', 0)
res.setHeader('x-url', file)
res.writeHead(404, 'Not Found')
res.writeHead(404, isHttps ? void 0 : 'Not Found')
res.end(null)
}
})
.listen(port)
.on('error', err => {
console.log(`${port}端口被占用~~~`)
port++
createServer()
})
.on('listening', _ => {
baseUrl = `http${isHttps ? 's' : ''}://${domain || '127.0.0.1'}:${port}`
ws = new WebSocket(server)
statItem.text = `http.server (🟢)`
statItem.show()
})
.listen(port)
server.on('listening', _ => {
baseUrl = 'http://127.0.0.1:' + port
ws = new WebSocket(server)
statItem.text = `http.server (🟢)`
statItem.show()
})
return server
}
function __init__() {
let folders = vsc.workspace.workspaceFolders
let commonConfig = vsc.workspace.getConfiguration('HttpServer')
enabled = false
@ -241,6 +251,7 @@ function __init__() {
if (conf.root) {
root = join(root, conf.root)
}
isHttps = conf.https || conf.http2
if (conf.headers) {
for (let k in conf.headers) {
@ -252,6 +263,20 @@ function __init__() {
}
}
if (isHttps) {
let key = conf.key || commonConfig.key
let cert = conf.cert || commonConfig.cert
if (key) {
SERVER_OPTIONS.key = fs.cat(key)
}
if (cert) {
SERVER_OPTIONS.cert = fs.cat(cert)
}
}
domain = conf.domain || commonConfig.domain
if (conf.enabled) {
enabled = true
port = conf.port || 23333
@ -270,6 +295,8 @@ export function deactivate() {}
export function activate(ctx) {
__init__()
vsc.workspace.onDidChangeConfiguration(__init__)
vsc.workspace.onDidSaveTextDocument(doc => {
let file = doc.fileName
if (file?.startsWith(root)) {

View File

@ -2,7 +2,7 @@
"name": "simple-http",
"displayName": "simple http.server",
"description": "🔥 简单的http服务器, 方便临时调试html",
"version": "1.6.1",
"version": "1.7.0",
"publisher": "yutent",
"author": "Yutent [@yutent]",
"icon": "logo.png",
@ -31,6 +31,27 @@
"title": "在浏览器中打开"
}
]
},
"configuration": {
"type": "object",
"title": "HttpServer 通用配置",
"properties": {
"HttpServer.domain": {
"type": "string",
"default": "",
"description": "默认域名(需要手动绑hosts或DNS解析)"
},
"HttpServer.key": {
"type": "string",
"default": "",
"description": "域名的证书的绝对路径"
},
"HttpServer.cert": {
"type": "string",
"default": "",
"description": "域名的证书的绝对路径"
}
}
}
},
"repository": {

View File

@ -1,4 +1,5 @@
{
"enabled": false,
"enabled": true,
"https": false,
"port": 9090
}

View File

@ -11,6 +11,6 @@
</head>
<body>
sdsdsdsdsssdsdsd
dsdsd
ssssdddfdfd
</body>
</html>