Skip to content

Commit

Permalink
fix bug and update readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
totravel committed Jan 12, 2022
1 parent 340d309 commit 2a0b0c0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 56 deletions.
50 changes: 15 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
![License](https://img.shields.io/github/license/totravel/shadowsocks-ws)
![GitHub last commit](https://img.shields.io/github/last-commit/totravel/shadowsocks-ws)

shadowsocks-ws 是基于 WebSocket 的 Shadowsocks,可以部署在 [Heroku](https://www.heroku.com/)
shadowsocks-ws 是基于 WebSocket 的 Shadowsocks,可以部署在 [Heroku](https://www.heroku.com/) 等托管平台

```
socks5 tcp websocket tcp
Expand All @@ -28,56 +28,36 @@ shadowsocks-ws 的客户端只负责转发经过加密的流量,须配合现
- [ws](https://github.com/websockets/ws)
- [futoin-hkdf](https://github.com/futoin/util-js-hkdf)

## 部署到 Heroku
## 部署

### 一键部署

点击下面的按钮并根据提示操作。
### Heroku

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy)

### 手动部署

打开终端,登录你的 Heroku 账户:
### Railway

```shell
$ heroku auth:login -i
heroku: Enter your login credentials
Email: [email protected]
Password: *****
Logged in as [email protected]
```
Create a empty project.

将你的 SSH 公钥添加到 Heroku:
Connect to the project:

```shell
$ heroku keys:add
Found an SSH public key at /path/to/id_rsa.pub
? Would you like to upload it to Heroku? (Y/n) y
```

新建一个 APP:

```shell
$ heroku create
Creating app... done, ⬢ xxxxx
https://<your-app>.herokuapp.com/ | https://git.heroku.com/<your-app>.git
$ git clone https://github.com/totravel/shadowsocks-ws.git
$ cd shadowsocks-ws
$ railway link [projectId]
```

设置加密方法、密码:
Add some variables:

```shell
$ heroku config:set METHOD="chacha20-ietf-poly1305" PASS="your-password" --app <your-app>
$ railway variables set METHOD=aes-256-gcm
$ railway variables set PASS=secret
$ railway variables set PORT=80
```

仅支持 `chacha20-ietf-poly1305``aes-256-gcm` 两种加密方法。

克隆代码到本地,再推送到 APP:
Create a deployment:

```shell
$ git clone https://github.com/totravel/shadowsocks-ws.git
$ cd shadowsocks-ws
$ git push https://git.heroku.com/<your-app>.git master
$ railway up
```

## 本地配置
Expand Down
33 changes: 18 additions & 15 deletions local.js → local.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict";

const url = require('url');
const net = require('net');
const http = require('http');
const https = require('https');
const colors = require('colors');
const WebSocket = require('ws');
const DnsOverHttpResolver = require('dns-over-http-resolver');
const { loadFile, parseJSON } = require('./helper');
import url from 'url';
import net from 'net';
import http from 'http';
import https from 'https';
import colors from 'colors';
import WebSocket, { createWebSocketStream } from 'ws';
import DnsOverHttpResolver from 'dns-over-http-resolver';
import { loadFile, parseJSON } from './helper.js';

(async () => {
console.clear();
Expand All @@ -29,7 +29,7 @@ const { loadFile, parseJSON } = require('./helper');
const options = {
timeout,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding': 'gzip, deflate, br'
Expand All @@ -41,9 +41,10 @@ const { loadFile, parseJSON } = require('./helper');
return;
}

options.origin = parsed.protocol === 'wss:' ? 'https://' : 'http://';
options.origin = parsed.protocol === 'wss:' ? 'https://' : 'http://'; // for ws
options.origin += hostname;
options.headers.Host = hostname;
options.servername = hostname; // for tls

if (net.isIP(config.lookup)) {
start(parsed.protocol + '//' + config.lookup, config.local_port, options);
Expand Down Expand Up @@ -126,7 +127,7 @@ function attempt(protocol, options) {
if (verbose) {
res.setEncoding('utf8');
res.once('data', chunk => {
console.log(chunk.gray);
console.log(res.headers['content-encoding'] ? 'zipped'.gray : chunk.gray);
resolve(true);
});
} else {
Expand All @@ -135,12 +136,14 @@ function attempt(protocol, options) {
});

req.on('timeout', () => {
if (verbose) console.error('timeout'.red);
req.destroy();
req.destroy(new Error('timeout')); // see 'error' event
});

req.on('error', err => {
if (verbose) console.error(err.message.red);
resolve(false);
});

req.on('error', err => null);
req.end();
});
}
Expand All @@ -152,7 +155,7 @@ function start(remote_address, local_port, options) {
const ws = new WebSocket(remote_address, options);

ws.on('open', () => {
ws.s = WebSocket.createWebSocketStream(ws);
ws.s = createWebSocketStream(ws);
ws.s.pipe(c);
c.pipe(ws.s);

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
"license": "MIT",
"dependencies": {
"colors": "^1.4.0",
"dns-over-http-resolver": "^1.2.3",
"futoin-hkdf": "^1.4.2",
"ws": "^7.5.3"
"dns-over-http-resolver": "^2.0.1",
"futoin-hkdf": "^1.4.3",
"ws": "^8.4.0"
},
"engines": {
"node": "v16.5.0",
"npm": "v7.19.1"
"node": "v16.13.2",
"npm": "v8.1.2"
}
}
2 changes: 1 addition & 1 deletion setup.cmd
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node local
node local.mjs
pause

0 comments on commit 2a0b0c0

Please sign in to comment.