Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
125 commits
Select commit Hold shift + click to select a range
7f4de35
使用typescript写个多语言http服务器
masx200 Apr 19, 2020
c0ef276
Update README.md
masx200 Apr 19, 2020
d219174
Update README.md
masx200 Apr 19, 2020
3682338
Update README.md
masx200 Apr 19, 2020
8c92594
Update README.md
masx200 Apr 19, 2020
b69eea2
Update README.md
masx200 Apr 19, 2020
bcf431d
测试
masx200 Apr 19, 2020
30f0eb6
代码格式化
masx200 Apr 19, 2020
eafd7f2
Update index.ts
masx200 Apr 19, 2020
c1b958d
格式化
masx200 Apr 19, 2020
eb50c72
测试
masx200 Apr 19, 2020
d4c4f72
优化
masx200 Apr 19, 2020
8c8c4a0
Update index.ts
masx200 Apr 19, 2020
aca6da6
Update index.ts
masx200 Apr 19, 2020
a8bbadc
重构
masx200 Apr 19, 2020
bfd4197
优化
masx200 Apr 19, 2020
921e878
测试
masx200 Apr 19, 2020
eef3695
优化
masx200 Apr 19, 2020
6b6e665
初步实现http
masx200 Apr 19, 2020
e5ca081
Update index.ts
masx200 Apr 19, 2020
1f8c6ac
终于成功了
masx200 Apr 19, 2020
558c364
Update README.md
masx200 Apr 19, 2020
4b1c7dd
Update README.md
masx200 Apr 19, 2020
deda8c6
Update README.md
masx200 Apr 19, 2020
6f38adf
格式化
masx200 Apr 19, 2020
38ed20a
Update README.md
masx200 Apr 19, 2020
5bae407
优化
masx200 Apr 19, 2020
7605a23
Update index.js
masx200 Apr 19, 2020
27182c1
Update index.ts
masx200 Apr 19, 2020
05e014b
Update index.js
masx200 Apr 19, 2020
1097002
添加了upgradeListener
masx200 Apr 19, 2020
f5604bf
优化
masx200 Apr 19, 2020
7d102db
添加ws
masx200 Apr 19, 2020
a733ac3
Update three.js
masx200 Apr 19, 2020
bf1d96a
优化
masx200 Apr 19, 2020
f971bc7
Update README.md
masx200 Apr 19, 2020
6a8e2dc
优化类型声明
masx200 Apr 19, 2020
9dabbdf
测试
masx200 Apr 19, 2020
bb7cd57
Update one.js
masx200 Apr 19, 2020
eea01e4
Update one.js
masx200 Apr 19, 2020
d698337
Update one.js
masx200 Apr 19, 2020
16e41cc
Update index.ts
masx200 Apr 19, 2020
b5c4220
Update index.ts
masx200 Apr 19, 2020
094b4e8
Update three.js
masx200 Apr 20, 2020
600ad76
格式化
masx200 Apr 20, 2020
b690d5e
Update index.ts
masx200 Apr 20, 2020
609b315
优化
masx200 Apr 20, 2020
093613e
美化
masx200 Apr 20, 2020
e0c209d
优化
masx200 Apr 20, 2020
f978a72
优化
masx200 Apr 20, 2020
109ced8
优化push测试
masx200 Apr 20, 2020
1c4705c
Update index.d.ts
masx200 Apr 20, 2020
e78f719
优化类型声明
masx200 Apr 20, 2020
3e20ee7
优化
masx200 Apr 20, 2020
829a47f
Update notfound.js
masx200 Apr 20, 2020
65504c4
优化
masx200 Apr 20, 2020
1cbf672
转发http事件到server
masx200 Apr 20, 2020
b1db1ad
Update README.md
masx200 Apr 20, 2020
b1948bd
Update README.md
masx200 Apr 20, 2020
4902f41
Update README.md
masx200 Apr 20, 2020
591c28b
Update README.md
masx200 Apr 20, 2020
6712f8c
美化
masx200 Apr 20, 2020
39ecb18
Update redirect.js
masx200 Apr 20, 2020
cd64180
Update simple.js
masx200 Apr 20, 2020
a4848e6
Update package.json
masx200 Apr 20, 2020
34e3554
Update index.ts
masx200 Apr 20, 2020
fa27a29
Update index.ts
masx200 Apr 20, 2020
6602731
Update index.ts
masx200 Apr 20, 2020
ac85dfa
Add files via upload
masx200 Apr 20, 2020
21d9277
Rename onlyonelistener.js to onlyonelistener.ts
masx200 Apr 20, 2020
0505866
Update onlyonelistener.ts
masx200 Apr 20, 2020
f6ff9e6
美化格式
Apr 20, 2020
688957b
简化结构重构
masx200 Apr 20, 2020
409bc4d
websocket测试
masx200 Apr 20, 2020
ee3edd9
格式化
masx200 Apr 20, 2020
d7c70e9
Update index.js
masx200 Apr 20, 2020
3938fd2
Update index.ts
masx200 Apr 20, 2020
947bbc2
Update index.js
masx200 Apr 20, 2020
5240718
Update README.md
masx200 Apr 20, 2020
db73d2d
Update README.md
masx200 Apr 20, 2020
2d3f0a8
Update README.md
masx200 Apr 20, 2020
7735a8a
重新生成ssl证书
masx200 Apr 20, 2020
87253dc
替换证书
masx200 Apr 20, 2020
e6c988d
Update package.json
masx200 Apr 20, 2020
a894966
改名
masx200 Apr 20, 2020
d48b8a5
把cert和key提取到分开模块
masx200 Apr 20, 2020
ecee2c9
移动证书文件
masx200 Apr 20, 2020
f801437
Update key-cert.js
masx200 Apr 20, 2020
c2b628d
Update fetch.js
masx200 Apr 20, 2020
0746555
美化
masx200 Apr 20, 2020
3ba5993
美化代码
masx200 Apr 20, 2020
a63ea6e
拆分模块
masx200 Apr 20, 2020
415982c
重构fetch
masx200 Apr 20, 2020
7d1969e
优化测试
masx200 Apr 20, 2020
0146883
优化测试
masx200 Apr 20, 2020
7b8f17a
优化
masx200 Apr 20, 2020
8707ff1
Update fetch2.js
masx200 Apr 20, 2020
ec1a029
Update fetch2.js
masx200 Apr 20, 2020
c526cca
优化
masx200 Apr 20, 2020
a40c30d
优化
masx200 Apr 20, 2020
1a3d086
优化
masx200 Apr 20, 2020
7eab567
整理
masx200 Apr 20, 2020
55e2789
Update redirect.js
masx200 Apr 20, 2020
b47ffba
整理
masx200 Apr 20, 2020
0e4a9cf
优化
masx200 Apr 20, 2020
9a882e5
Update redirect.js
masx200 Apr 20, 2020
0c7ce1b
Update README.md
masx200 Apr 20, 2020
77265ca
Update index.ts
masx200 Apr 20, 2020
5008bdd
update .gitignore
masx200 Apr 20, 2020
3545b4a
格式整理
masx200 Apr 20, 2020
d098b5c
Update README.md
masx200 Apr 20, 2020
a3afb21
Update index.ts
masx200 Apr 20, 2020
54440c1
美化
masx200 Apr 21, 2020
4be3e5b
Create early-disconnect.js
masx200 Apr 21, 2020
e915253
Update index.ts
masx200 Apr 21, 2020
d247eb6
assert(Reflect.get(socket, "allowHalfOpen") === false);
masx200 Apr 21, 2020
0d60ca5
Update package.json
masx200 Apr 21, 2020
4a4362e
Update README.md
masx200 Apr 21, 2020
bddefe9
优化测试
masx200 Apr 21, 2020
c26390c
Update index.js
masx200 Apr 21, 2020
75f8cc0
优化测试
masx200 Apr 21, 2020
1c01d71
优化测试
masx200 Apr 21, 2020
6ab839a
修复bug
masx200 Apr 21, 2020
4f4d25a
添加了 "@microsoft/api-extractor"
masx200 Apr 21, 2020
09a1c57
Update LICENSE
masx200 Apr 21, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.js linguist-language=Typescript
*.css linguist-language=Typescript
*.html linguist-language=Typescript
*.jsx linguist-language=Typescript
*.ts linguist-language=Typescript
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
*.pem
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "tabWidth": 4, "arrowParens": "always" }
39 changes: 31 additions & 8 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
Copyright 2014 Brian White. All rights reserved.

The MIT License (MIT)



Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
Expand All @@ -14,6 +17,26 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


特此授予获得副本的任何人免费的许可
本软件和相关文档文件(以下简称“软件”)
不受限制地处理软件,包括但不限于
使用,复制,修改,合并,发布,分发,再许可和/或授权的权利
出售本软件的副本,并允许本软件所涉及的人
具备以下条件:

上述版权声明和此许可声明应包含在
软件的所有副本或实质性部分。

本软件按“原样”提供,没有任何形式的明示或明示担保。
暗示(包括但不限于对适销性的保证),
适用于特定目的和非侵权。在任何情况下都不会
作者或版权持有人对任何索赔,损害或其他责任
无论是在合同,侵权或其他情况下的责任
来自,与软件或使用或其他交易有关的,与之相关的
在软件中。

157 changes: 101 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,81 +1,126 @@
Description
===========
# Description

A module for serving http and https connections over the same port.
Serve http and https and spdy and http2 connections over the same port with node.js

Forked from the original [`httpolyglot`](https://github.com/mscdex/httpolyglot) to fix various issues required for [HTTP Toolkit](https://httptoolkit.tech), including:
# Requirements

* Fixing `tlsClientError`: https://github.com/mscdex/httpolyglot/pull/11.
* Exposing the lost bytes from https://github.com/mscdex/httpolyglot/issues/13 on the socket, as `__httpPeekedData`.
* Dropping support for old versions of Node (and thereby simplifying the code somewhat)
- [node.js](http://nodejs.org/) -- v12.0.0 or newer

Requirements
============
# Install

* [node.js](http://nodejs.org/) -- v8.0.0 or newer
```shell
yarn add spdy @masx200/http-https-spdy-http2-polyglot
```

# Connection protocol judgment

Determine if the connection is over tls.

```js
const istls = "encrypted" in req.socket;
```

Determine if the connection is `http/2`.

```js
const ishttp2 = "h2" === req.socket.alpnProtocol;
```

# Examples

Install
============
- http2 server push

npm install @httptoolkit/httpolyglot
https://github.com/masx200/http-https-spdy-http2-polyglot/blob/master/test/push.js

- Websocket server

Examples
========
https://github.com/masx200/http-https-spdy-http2-polyglot/blob/master/test/websocket.js

* Simple usage:
- Simple Determine the connection protocol

```javascript
const httpolyglot = require('@httptoolkit/httpolyglot');
const fs = require('fs');

httpolyglot.createServer({
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
}, function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end((req.socket.encrypted ? 'HTTPS' : 'HTTP') + ' Connection!');
}).listen(9000, 'localhost', function() {
console.log('httpolyglot server listening on port 9000');
// visit http://localhost:9000 and https://localhost:9000 in your browser ...
const httpolyglot = require("@masx200/http-https-spdy-http2-polyglot");
const fs = require("fs");
const port = 9000;
const server = httpolyglot.createServer(
{
key: fs.readFileSync("server.key.pem"),
cert: fs.readFileSync("server.crt.pem"),
},
function (req, res) {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(
("encrypted" in req.socket ? "HTTPS" : "HTTP") + " Connection!"
);
}
);
server.listen(port, "localhost", function () {
console.log("httpolyglot server listening on port " + port);
});
```

* Simple redirect of all http connections to https:
- redirect all http connections to https:

```javascript
const httpolyglot = require('@httptoolkit/httpolyglot');
const fs = require('fs');

httpolyglot.createServer({
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
}, function(req, res) {
if (!req.socket.encrypted) {
res.writeHead(301, { 'Location': 'https://localhost:9000' });
return res.end();
}
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Welcome, HTTPS user!');
}).listen(9000, 'localhost', function() {
console.log('httpolyglot server listening on port 9000');
// visit http://localhost:9000 and https://localhost:9000 in your browser ...
});
https://github.com/masx200/http-https-spdy-http2-polyglot/blob/master/test/redirect.js

- create a "404 not found" server

https://github.com/masx200/http-https-spdy-http2-polyglot/blob/master/test/notfound.js

# API

## Exports

https://github.com/masx200/http-https-spdy-http2-polyglot/blob/master/lib/index.d.ts

- **createServer** - Creates and returns a new Server instance.

```ts
declare function createServer(
config: ServerOptions,
requestListener?: RequestListener,
upgradeListener?: UpgradeListener
): net.Server;
```

The `requestListener` is a function which is automatically added to the 'request' event

The `upgradeListener` is a function which is automatically added to the 'upgrade' event

If no "requestListener" or "upgradeListener" is provided, the default "404 not found" listener will be used instead.

# How it Works

API
===
https://github.com/lvgithub/blog/blob/master/http_and_https_over_same_port/README.MD

Exports
-------
TLS and HTTP connections are easy to distinguish based on the first byte sent by clients trying to connect. See this comment for more information.

* **Server** - A class similar to https.Server (except instances have `setTimeout()` from http.Server).
https://github.com/mscdex/httpolyglot/issues/3#issuecomment-173680155

* **createServer**(< _object_ >tlsConfig[, < _function_ >requestListener]) - _Server_ - Creates and returns a new Server instance.
https://github.com/httptoolkit/httpolyglot/blob/master/lib/index.js

How it Works
============
# test

```powershell
yarn install
```

```powershell
./generate-ssl-cert.ps1
```

TLS and HTTP connections are easy to distinguish based on the first byte sent by clients trying to connect. See [this comment](https://github.com/mscdex/httpolyglot/issues/3#issuecomment-173680155) for more information.
```powershell
yarn build
```

```powershell
yarn serve
```

```powershell
yarn test
```

```powershell
yarn open
```
21 changes: 21 additions & 0 deletions api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "<projectFolder>/lib/index.d.ts",
"bundledPackages": [],
"compiler": {},
"apiReport": { "enabled": false },
"docModel": { "enabled": false },
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "<projectFolder>/dist/index.d.ts"
},
"tsdocMetadata": { "enabled": false },
"messages": {
"compilerMessageReporting": { "default": { "logLevel": "warning" } },
"extractorMessageReporting": {
"ae-missing-release-tag": { "logLevel": "none" },
"default": { "logLevel": "warning" }
},
"tsdocMessageReporting": { "default": { "logLevel": "warning" } }
}
}
62 changes: 62 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/// <reference types="node" />
import http from "http";
import https from "https";
import net from "net";
import spdy from "spdy";
import stream from "stream";
import tls from "tls";

export declare function createServer(
config: ServerOptions,
requestListener?: RequestListener,
upgradeListener?: UpgradeListener
): net.Server;

export declare interface PushOptions {
status?: number;
method?: string;
request?: http.OutgoingHttpHeaders;
response?: http.OutgoingHttpHeaders;
}

export declare type RequestListener = (
req: ServerRequest,
res: ServerResponse
) => void;

export declare const requestNotFound: (
req: ServerRequest,
res: ServerResponse
) => void;

export declare type ServerOptions = spdy.ServerOptions & {
allowHalfOpen?: boolean | undefined;
pauseOnConnect?: boolean | undefined;
} & http.ServerOptions &
tls.TlsOptions &
https.ServerOptions;

export declare interface ServerRequest extends http.IncomingMessage {
socket: Socket;
}

export declare interface ServerResponse extends http.ServerResponse {
socket: Socket;
push?: (pathname: string, options?: PushOptions) => stream.Writable;
}

export declare type Socket = Partial<tls.TLSSocket> & net.Socket;

export declare type UpgradeListener = (
req: ServerRequest,
socket: Socket,
head: Buffer
) => void;

export declare const upgradeNotFound: (
req: ServerRequest,
socket: Socket,
head: Buffer
) => void;

export {};
1 change: 1 addition & 0 deletions generate-ssl-cert.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openssl req -x509 -days 365 -newkey rsa:2048 -nodes -sha256 -subj "/C=CN/ST=Shanghai/L=Shanghai/O=boyusc/OU=boyusc/CN=localhost" -keyout "server.key.pem" -out "server.crt.pem"
46 changes: 46 additions & 0 deletions lib/declaration.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/// <reference types="node" />
import http from "http";
import net from "net";
import spdy from "spdy";
import tls from "tls";
import stream from "stream";
import https from "https";
export interface ServerRequest extends http.IncomingMessage {
socket: Socket;
}
export interface PushOptions {
status?: number;
method?: string;
request?: http.OutgoingHttpHeaders;
response?: http.OutgoingHttpHeaders;
}
export interface ServerResponse extends http.ServerResponse {
socket: Socket;
push?: (pathname: string, options?: PushOptions) => stream.Writable;
}
export declare type Socket = Partial<tls.TLSSocket> & net.Socket;
export declare type RequestListener = (
req: ServerRequest,
res: ServerResponse
) => void;
export declare type UpgradeListener = (
req: ServerRequest,
socket: Socket,
head: Buffer
) => void;
export declare type ServerOptions = spdy.ServerOptions & {
allowHalfOpen?: boolean | undefined;
pauseOnConnect?: boolean | undefined;
} & http.ServerOptions &
tls.TlsOptions &
https.ServerOptions;
export declare const requestNotFound: (
req: ServerRequest,
res: ServerResponse
) => void;
export declare const upgradeNotFound: (
req: ServerRequest,
socket: Socket,
head: Buffer
) => void;
//# sourceMappingURL=declaration.d.ts.map
1 change: 1 addition & 0 deletions lib/declaration.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions lib/declaration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const requestNotFound = function (req, res) {
res.statusCode = 404;
res.setHeader("content-type", "text/html");
res.write("404");
res.end();
};
export const upgradeNotFound = function (req, socket, head) {
socket.write(`HTTP/1.1 404 Not Found\r\nConnection: keep-alive\r\n\r\n`);
socket.destroy();
};
Loading