Compare commits
90 Commits
ts
...
afbf3097f1
| Author | SHA1 | Date | |
|---|---|---|---|
|
afbf3097f1
|
|||
|
|
5198ab8bd4 | ||
|
|
78c21fc78d | ||
|
|
8b85f9e03d | ||
|
|
88fb4b58d2 | ||
|
fe38f051ff
|
|||
|
|
3d8f95676c | ||
|
|
3ebaf8a37a | ||
| 97c94ad816 | |||
|
|
bfe93f2fa4 | ||
|
|
badc201e74 | ||
|
|
13f60b0f40 | ||
|
|
e8eea4aaa6 | ||
|
|
2a8b7a2bba | ||
|
|
2f35f428cf | ||
|
|
6140301f3e | ||
|
|
1468c97f38 | ||
|
|
34b0f96ecb | ||
|
|
8df3a8076d | ||
|
|
b3a3edaaec | ||
|
|
a6ad917ef5 | ||
|
|
a80bc3d234 | ||
|
|
8993c83b67 | ||
|
5b1a7a68c5
|
|||
|
bb20b3b90a
|
|||
|
65e3cb9797
|
|||
|
d54b69ce80
|
|||
|
|
2996a6056e | ||
|
|
c8508a4030 | ||
|
|
68a753383d | ||
|
|
eb111712ba | ||
|
|
30d309d1f3 | ||
|
|
26a1dacf28 | ||
|
71432d7194
|
|||
|
775b7b287b
|
|||
|
cdfe4c4fc8
|
|||
|
6d1a2fdc39
|
|||
|
|
0aa31874f2 | ||
|
|
c8b8717d7a | ||
|
|
81a414e503 | ||
|
|
21e33bc179 | ||
|
42928444aa
|
|||
|
|
e787b70da9 | ||
|
|
f64128dc6a | ||
|
|
767153d599 | ||
|
|
962c5f117b | ||
|
|
c30e9926ba | ||
|
|
df2aa05266 | ||
|
|
5291076424 | ||
|
|
1b0e09faa8 | ||
|
|
981bb34cbb | ||
|
|
a776bfa53e | ||
|
|
28e1b9c408 | ||
|
|
c7b47a66ca | ||
|
|
127d179f5c | ||
|
|
2c06e60f59 | ||
|
|
d58a803976 | ||
|
|
767c6d6ef4 | ||
|
|
3085d1c4f2 | ||
|
|
4a1605f0e2 | ||
|
|
65ee3a08d0 | ||
|
|
e9c6531633 | ||
|
|
787d054030 | ||
|
|
045cb9d644 | ||
|
|
e6b7154ae9 | ||
|
|
263aca4379 | ||
|
|
5a3a8ad93d | ||
|
|
9bc0f27a78 | ||
|
|
d401d868ca | ||
|
|
a3ba7c74fa | ||
|
|
2253bdcbd2 | ||
|
|
412b4783c9 | ||
|
|
ce840e35ff | ||
|
|
e9b9f43537 | ||
|
|
476e703a14 | ||
|
|
dd98be71ce | ||
|
|
52729124c0 | ||
|
|
96b087aad4 | ||
|
|
1ebdd97107 | ||
|
|
0e8f3af0ae | ||
|
|
750a392fa8 | ||
|
|
16b6493d6a | ||
|
|
5993af0b84 | ||
|
|
716dfd8a25 | ||
|
|
0aac5004ec | ||
|
|
796c6e6159 | ||
|
|
1d4321df09 | ||
|
|
7f2f18a4f9 | ||
|
|
9e5c2aa616 | ||
|
|
4fbcf1f82c |
30
.github/workflows/node.js.yml
vendored
Normal file
30
.github/workflows/node.js.yml
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
|
||||
|
||||
name: Node.js CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "main" ]
|
||||
pull_request:
|
||||
branches: [ "main" ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [12.x, 14.x, 16.x]
|
||||
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
cache: 'npm'
|
||||
- run: npm ci
|
||||
- run: npm run build --if-present
|
||||
21
CHANGELOG.md
Normal file
21
CHANGELOG.md
Normal file
@@ -0,0 +1,21 @@
|
||||
### 30/11/2022
|
||||
|
||||
- Fixed response data in axios request with accept-encoding to identity
|
||||
- Allow to force change server url for init
|
||||
|
||||
### 11/11/2022
|
||||
|
||||
- Fixed axios is not a function
|
||||
- Add generate token with data (clientId, apiKey) to server
|
||||
- Add generate token with options (-f | --force) to regenerate and override
|
||||
|
||||
### 20/10/2022
|
||||
|
||||
- Upgraded packages and fixed some bugs
|
||||
|
||||
### Initialized
|
||||
|
||||
- HTTP tunnel between server and client via https link
|
||||
- Custom profile (using `-p myprofile`)
|
||||
- Suffix url (using `-s`)
|
||||
- Config (get/set token,access,server,client,key configs)
|
||||
36
README.md
36
README.md
@@ -9,56 +9,68 @@ npm i -g @cubetiq/hlt
|
||||
|
||||
OR
|
||||
|
||||
npx @cubetiq/hlt
|
||||
npx -y @cubetiq/hlt
|
||||
|
||||
```
|
||||
|
||||
### Usage
|
||||
### Usages
|
||||
|
||||
- If installed to global (bin), please using cli
|
||||
|
||||
```shell
|
||||
hlt [command] [options]
|
||||
```
|
||||
|
||||
- Initialize Client and Start (Quick)
|
||||
|
||||
```shell
|
||||
# Initialize a client and token for connect (default's profile)
|
||||
npx @cubetiq/hlt init
|
||||
npx -y @cubetiq/hlt init
|
||||
|
||||
# Start port 3000 to remote server
|
||||
npx @cubetiq/hlt start 3000
|
||||
npx -y @cubetiq/hlt start 3000
|
||||
```
|
||||
|
||||
# Start port 3000 with suffix to remote server
|
||||
|
||||
npx -y @cubetiq/hlt start 3000 -s mytest
|
||||
|
||||
````
|
||||
|
||||
- Initialize Client and Start (Quick with custom's profile)
|
||||
|
||||
```shell
|
||||
# Initialize a client and token for connect (mytest's profile)
|
||||
npx @cubetiq/hlt init -p mytest
|
||||
npx -y @cubetiq/hlt init -p mytest
|
||||
|
||||
# Start port 3000 to remote server (mytest's profile)
|
||||
npx @cubetiq/hlt start 3000 -p mytest
|
||||
```
|
||||
npx -y @cubetiq/hlt start 3000 -p mytest
|
||||
````
|
||||
|
||||
### Custom Config
|
||||
|
||||
- Generate Client Key
|
||||
|
||||
```shell
|
||||
npx @cubetiq/hlt config client new
|
||||
npx -y @cubetiq/hlt config client new
|
||||
```
|
||||
|
||||
- Set Client Token (Required, contact to vendor)
|
||||
|
||||
```shell
|
||||
npx @cubetiq/hlt config token $TOKEN
|
||||
npx -y @cubetiq/hlt config token $TOKEN
|
||||
```
|
||||
|
||||
- Custom Server
|
||||
- Set Custom Server
|
||||
|
||||
```shell
|
||||
npx @cubetiq/hlt config server https://lt.ctdn.net
|
||||
npx -y @cubetiq/hlt config server https://lt.ctdn.net
|
||||
```
|
||||
|
||||
- Start Client
|
||||
|
||||
```shell
|
||||
npx @cubetiq/hlt start $YOUR_PORT
|
||||
npx -y @cubetiq/hlt start $YOUR_PORT
|
||||
```
|
||||
|
||||
### Contributors
|
||||
|
||||
2
bin/hlt
2
bin/hlt
@@ -1,3 +1,3 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
require('../dist/client');
|
||||
require('../dist/cli');
|
||||
118
package-lock.json
generated
118
package-lock.json
generated
@@ -1,15 +1,15 @@
|
||||
{
|
||||
"name": "@cubetiq/hlt",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.5",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@cubetiq/hlt",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.5",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^0.27.2",
|
||||
"axios": "^1.2.0",
|
||||
"commander": "^9.3.0",
|
||||
"https-proxy-agent": "^5.0.1",
|
||||
"socket.io-client": "^4.5.1"
|
||||
@@ -90,9 +90,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "18.0.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.0.3.tgz",
|
||||
"integrity": "sha512-HzNRZtp4eepNitP+BD6k2L6DROIDG4Q0fm4x+dwfsr6LGmROENnok75VGw40628xf+iR24WeMFcHuuBDUAzzsQ==",
|
||||
"version": "18.11.18",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz",
|
||||
"integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/strip-bom": {
|
||||
@@ -164,12 +164,13 @@
|
||||
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
|
||||
},
|
||||
"node_modules/axios": {
|
||||
"version": "0.27.2",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz",
|
||||
"integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==",
|
||||
"version": "1.2.2",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-1.2.2.tgz",
|
||||
"integrity": "sha512-bz/J4gS2S3I7mpN/YZfGFTqhXTYzRho8Ay38w2otuuDR322KzFIWm/4W2K6gIwvWaws5n+mnb7D1lN9uD+QH6Q==",
|
||||
"dependencies": {
|
||||
"follow-redirects": "^1.14.9",
|
||||
"form-data": "^4.0.0"
|
||||
"follow-redirects": "^1.15.0",
|
||||
"form-data": "^4.0.0",
|
||||
"proxy-from-env": "^1.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/balanced-match": {
|
||||
@@ -254,9 +255,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/commander": {
|
||||
"version": "9.4.0",
|
||||
"resolved": "https://registry.npmjs.org/commander/-/commander-9.4.0.tgz",
|
||||
"integrity": "sha512-sRPT+umqkz90UA8M1yqYfnHlZA7fF6nSphDtxeywPZ49ysjxDQybzk13CL+mXekDRG92skbcqCLVovuCusNmFw==",
|
||||
"version": "9.4.1",
|
||||
"resolved": "https://registry.npmjs.org/commander/-/commander-9.4.1.tgz",
|
||||
"integrity": "sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==",
|
||||
"engines": {
|
||||
"node": "^12.20.0 || >=14"
|
||||
}
|
||||
@@ -316,9 +317,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/engine.io-client": {
|
||||
"version": "6.2.2",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.2.2.tgz",
|
||||
"integrity": "sha512-8ZQmx0LQGRTYkHuogVZuGSpDqYZtCM/nv8zQ68VZ+JkOpazJ7ICdsSpaO6iXwvaU30oFg5QJOJWj8zWqhbKjkQ==",
|
||||
"version": "6.2.3",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.2.3.tgz",
|
||||
"integrity": "sha512-aXPtgF1JS3RuuKcpSrBtimSjYvrbhKW9froICH4s0F3XQWLxsKNxqzG39nnvQZQnva4CMvUK63T7shevxRyYHw==",
|
||||
"dependencies": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.1",
|
||||
@@ -636,6 +637,11 @@
|
||||
"url": "https://github.com/sponsors/jonschlinkert"
|
||||
}
|
||||
},
|
||||
"node_modules/proxy-from-env": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
|
||||
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
|
||||
},
|
||||
"node_modules/readdirp": {
|
||||
"version": "3.6.0",
|
||||
"resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz",
|
||||
@@ -678,23 +684,23 @@
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-client": {
|
||||
"version": "4.5.1",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.5.1.tgz",
|
||||
"integrity": "sha512-e6nLVgiRYatS+AHXnOnGi4ocOpubvOUCGhyWw8v+/FxW8saHkinG6Dfhi9TU0Kt/8mwJIAASxvw6eujQmjdZVA==",
|
||||
"version": "4.5.4",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.5.4.tgz",
|
||||
"integrity": "sha512-ZpKteoA06RzkD32IbqILZ+Cnst4xewU7ZYK12aS1mzHftFFjpoMz69IuhP/nL25pJfao/amoPI527KnuhFm01g==",
|
||||
"dependencies": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.2",
|
||||
"engine.io-client": "~6.2.1",
|
||||
"socket.io-parser": "~4.2.0"
|
||||
"engine.io-client": "~6.2.3",
|
||||
"socket.io-parser": "~4.2.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-parser": {
|
||||
"version": "4.2.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.0.tgz",
|
||||
"integrity": "sha512-tLfmEwcEwnlQTxFB7jibL/q2+q8dlVQzj4JdRLJ/W/G1+Fu9VSxCx1Lo+n1HvXxKnM//dUuD0xgiA7tQf57Vng==",
|
||||
"version": "4.2.1",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.1.tgz",
|
||||
"integrity": "sha512-V4GrkLy+HeF1F/en3SpUaM+7XxYXpuMUWLGde1kSSh5nQMN4hLrbPIkD+otwh6q9R6NOQBN4AMaOZ2zVjui82g==",
|
||||
"dependencies": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.1"
|
||||
@@ -863,9 +869,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/typescript": {
|
||||
"version": "4.7.4",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.4.tgz",
|
||||
"integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==",
|
||||
"version": "4.9.4",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz",
|
||||
"integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"tsc": "bin/tsc",
|
||||
@@ -996,9 +1002,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"@types/node": {
|
||||
"version": "18.0.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.0.3.tgz",
|
||||
"integrity": "sha512-HzNRZtp4eepNitP+BD6k2L6DROIDG4Q0fm4x+dwfsr6LGmROENnok75VGw40628xf+iR24WeMFcHuuBDUAzzsQ==",
|
||||
"version": "18.11.18",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz",
|
||||
"integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/strip-bom": {
|
||||
@@ -1055,12 +1061,13 @@
|
||||
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
|
||||
},
|
||||
"axios": {
|
||||
"version": "0.27.2",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz",
|
||||
"integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==",
|
||||
"version": "1.2.2",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-1.2.2.tgz",
|
||||
"integrity": "sha512-bz/J4gS2S3I7mpN/YZfGFTqhXTYzRho8Ay38w2otuuDR322KzFIWm/4W2K6gIwvWaws5n+mnb7D1lN9uD+QH6Q==",
|
||||
"requires": {
|
||||
"follow-redirects": "^1.14.9",
|
||||
"form-data": "^4.0.0"
|
||||
"follow-redirects": "^1.15.0",
|
||||
"form-data": "^4.0.0",
|
||||
"proxy-from-env": "^1.1.0"
|
||||
}
|
||||
},
|
||||
"balanced-match": {
|
||||
@@ -1125,9 +1132,9 @@
|
||||
}
|
||||
},
|
||||
"commander": {
|
||||
"version": "9.4.0",
|
||||
"resolved": "https://registry.npmjs.org/commander/-/commander-9.4.0.tgz",
|
||||
"integrity": "sha512-sRPT+umqkz90UA8M1yqYfnHlZA7fF6nSphDtxeywPZ49ysjxDQybzk13CL+mXekDRG92skbcqCLVovuCusNmFw=="
|
||||
"version": "9.4.1",
|
||||
"resolved": "https://registry.npmjs.org/commander/-/commander-9.4.1.tgz",
|
||||
"integrity": "sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw=="
|
||||
},
|
||||
"concat-map": {
|
||||
"version": "0.0.1",
|
||||
@@ -1170,9 +1177,9 @@
|
||||
}
|
||||
},
|
||||
"engine.io-client": {
|
||||
"version": "6.2.2",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.2.2.tgz",
|
||||
"integrity": "sha512-8ZQmx0LQGRTYkHuogVZuGSpDqYZtCM/nv8zQ68VZ+JkOpazJ7ICdsSpaO6iXwvaU30oFg5QJOJWj8zWqhbKjkQ==",
|
||||
"version": "6.2.3",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.2.3.tgz",
|
||||
"integrity": "sha512-aXPtgF1JS3RuuKcpSrBtimSjYvrbhKW9froICH4s0F3XQWLxsKNxqzG39nnvQZQnva4CMvUK63T7shevxRyYHw==",
|
||||
"requires": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.1",
|
||||
@@ -1403,6 +1410,11 @@
|
||||
"integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==",
|
||||
"dev": true
|
||||
},
|
||||
"proxy-from-env": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
|
||||
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
|
||||
},
|
||||
"readdirp": {
|
||||
"version": "3.6.0",
|
||||
"resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz",
|
||||
@@ -1433,20 +1445,20 @@
|
||||
}
|
||||
},
|
||||
"socket.io-client": {
|
||||
"version": "4.5.1",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.5.1.tgz",
|
||||
"integrity": "sha512-e6nLVgiRYatS+AHXnOnGi4ocOpubvOUCGhyWw8v+/FxW8saHkinG6Dfhi9TU0Kt/8mwJIAASxvw6eujQmjdZVA==",
|
||||
"version": "4.5.4",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.5.4.tgz",
|
||||
"integrity": "sha512-ZpKteoA06RzkD32IbqILZ+Cnst4xewU7ZYK12aS1mzHftFFjpoMz69IuhP/nL25pJfao/amoPI527KnuhFm01g==",
|
||||
"requires": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.2",
|
||||
"engine.io-client": "~6.2.1",
|
||||
"socket.io-parser": "~4.2.0"
|
||||
"engine.io-client": "~6.2.3",
|
||||
"socket.io-parser": "~4.2.1"
|
||||
}
|
||||
},
|
||||
"socket.io-parser": {
|
||||
"version": "4.2.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.0.tgz",
|
||||
"integrity": "sha512-tLfmEwcEwnlQTxFB7jibL/q2+q8dlVQzj4JdRLJ/W/G1+Fu9VSxCx1Lo+n1HvXxKnM//dUuD0xgiA7tQf57Vng==",
|
||||
"version": "4.2.1",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.1.tgz",
|
||||
"integrity": "sha512-V4GrkLy+HeF1F/en3SpUaM+7XxYXpuMUWLGde1kSSh5nQMN4hLrbPIkD+otwh6q9R6NOQBN4AMaOZ2zVjui82g==",
|
||||
"requires": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.1"
|
||||
@@ -1553,9 +1565,9 @@
|
||||
}
|
||||
},
|
||||
"typescript": {
|
||||
"version": "4.7.4",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.4.tgz",
|
||||
"integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==",
|
||||
"version": "4.9.4",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz",
|
||||
"integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==",
|
||||
"dev": true
|
||||
},
|
||||
"v8-compile-cache-lib": {
|
||||
|
||||
10
package.json
10
package.json
@@ -1,13 +1,15 @@
|
||||
{
|
||||
"name": "@cubetiq/hlt",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.6",
|
||||
"description": "A lightweight http tunnel client using nodejs and socket.io client",
|
||||
"main": "dist/client.js",
|
||||
"main": "dist/cli.js",
|
||||
"bin": {
|
||||
"hlt": "bin/hlt"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "ts-node-dev --respawn --transpile-only src/client.ts",
|
||||
"start": "ts-node-dev --respawn --transpile-only src/cli.ts",
|
||||
"test": "ts-node-dev --respawn --transpile-only src/test.ts",
|
||||
"local": "ts-node-dev --respawn --transpile-only src/cli.ts start -p local",
|
||||
"build": "tsc"
|
||||
},
|
||||
"repository": {
|
||||
@@ -21,7 +23,7 @@
|
||||
"author": "Sambo Chea <sombochea@cubetiqs.com>",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^0.27.2",
|
||||
"axios": "^1.2.0",
|
||||
"commander": "^9.3.0",
|
||||
"https-proxy-agent": "^5.0.1",
|
||||
"socket.io-client": "^4.5.1"
|
||||
|
||||
336
src/api.ts
Normal file
336
src/api.ts
Normal file
@@ -0,0 +1,336 @@
|
||||
import * as fs from "fs";
|
||||
import * as http from "http";
|
||||
import { HttpsProxyAgent } from "https-proxy-agent";
|
||||
import * as os from "os";
|
||||
import * as path from "path";
|
||||
import { Socket, io } from "socket.io-client";
|
||||
import { TunnelRequest, TunnelResponse } from "./lib";
|
||||
import { addPrefixOnHttpSchema, generateUUID } from "./util";
|
||||
|
||||
import { PROFILE_DEFAULT, PROFILE_PATH, SERVER_DEFAULT_URL, TOKEN_FREE } from "./constant";
|
||||
import { ClientOptions, Options } from "./interface";
|
||||
import { getTokenFree } from './sdk';
|
||||
|
||||
// create socket instance
|
||||
let socket: Socket | null = null;
|
||||
let keepAliveTimer: NodeJS.Timeout | null = null;
|
||||
|
||||
function keepAlive() {
|
||||
keepAliveTimer = setTimeout(() => {
|
||||
if (socket && socket.connected) {
|
||||
socket.send("ping");
|
||||
}
|
||||
keepAlive();
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
// Init the Client
|
||||
const initClient = async (options: any) => {
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirSync(configDir);
|
||||
console.log(`config file ${configDir} was created`);
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
}
|
||||
|
||||
// Force reset config server from client init
|
||||
if (!config.server || options.force) {
|
||||
config.server = options.server || SERVER_DEFAULT_URL;
|
||||
}
|
||||
|
||||
if (!config.token && options.token) {
|
||||
config.token = options.token;
|
||||
}
|
||||
|
||||
if (!config.access) {
|
||||
config.access = options.access || TOKEN_FREE;
|
||||
}
|
||||
|
||||
if (!config.clientId) {
|
||||
config.clientId = options.client || generateUUID();
|
||||
}
|
||||
|
||||
if (!config.apiKey && options.key) {
|
||||
config.apiKey = options.key;
|
||||
}
|
||||
|
||||
let errorCode = 0;
|
||||
if (!config.token || options.force) {
|
||||
console.log(`Generating token from server: ${config.server}`);
|
||||
await getTokenFree(config.server, {
|
||||
timestamp: (new Date().getTime()),
|
||||
clientId: config.clientId,
|
||||
apiKey: config.apiKey,
|
||||
})
|
||||
.then((resp: any) => {
|
||||
if (resp.data?.token) {
|
||||
console.log("Token generated successfully!");
|
||||
config.token = resp.data?.token;
|
||||
} else {
|
||||
errorCode = 1;
|
||||
console.error("Generate free token failed, return with null or empty from server!", resp);
|
||||
return;
|
||||
}
|
||||
})
|
||||
.catch((err: any) => {
|
||||
errorCode = 1;
|
||||
console.error("cannot get free token from server", err);
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
if (errorCode === 0) {
|
||||
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
||||
console.log(`initialized config saved successfully to: ${configFilePath}`);
|
||||
}
|
||||
};
|
||||
|
||||
// Start Client
|
||||
const initStartClient = async (options: Options) => {
|
||||
// Please change this if your domain goes wrong here
|
||||
// Current style using sub-domain: https://{{clientId}}-tunnel.myhostingdomain.com
|
||||
// (Original server: https://tunnel.myhostingdomain.com)
|
||||
const profile = options.profile || PROFILE_DEFAULT;
|
||||
const clientId = `${options.apiKey || options.clientId || generateUUID()}`;
|
||||
const clientIdSub =
|
||||
profile === PROFILE_DEFAULT ? `${clientId}-` : `${clientId}-${profile}-`;
|
||||
const clientEndpoint = (
|
||||
options.suffix ? `${clientIdSub}${options.suffix}-` : clientIdSub
|
||||
)
|
||||
.toLowerCase()
|
||||
.trim();
|
||||
const serverUrl = addPrefixOnHttpSchema(options.server || SERVER_DEFAULT_URL, clientEndpoint);
|
||||
|
||||
// extra options for socket to identify the client (authentication and options of tunnel)
|
||||
const defaultParams = {
|
||||
apiKey: options.apiKey,
|
||||
clientId: options.clientId,
|
||||
profile: options.profile,
|
||||
clientIdSub: clientIdSub,
|
||||
clientEndpoint: clientEndpoint,
|
||||
serverUrl: serverUrl,
|
||||
access: options.access,
|
||||
keep_connection: options.keep_connection || true,
|
||||
};
|
||||
|
||||
// extra info for notify about the running of the tunnel (it's private info, other platfom cannot access this)
|
||||
// this using for internal only (don't worry about this)
|
||||
const osInfo = {
|
||||
hostname: os.hostname(),
|
||||
platform: os.platform(),
|
||||
arch: os.arch(),
|
||||
release: os.release(),
|
||||
};
|
||||
|
||||
const initParams: any = {
|
||||
path: "/$cubetiq_http_tunnel",
|
||||
transports: ["websocket"],
|
||||
auth: {
|
||||
token: options.token,
|
||||
...defaultParams,
|
||||
},
|
||||
headers: {
|
||||
...defaultParams,
|
||||
os: osInfo,
|
||||
},
|
||||
// reconnection: true,
|
||||
};
|
||||
|
||||
const http_proxy = process.env.https_proxy || process.env.http_proxy;
|
||||
if (http_proxy) {
|
||||
initParams.agent = new HttpsProxyAgent(http_proxy);
|
||||
}
|
||||
|
||||
// Connecting to socket server and agent here...
|
||||
console.log(`client connecting to server: ${serverUrl}`);
|
||||
socket = io(serverUrl, initParams);
|
||||
|
||||
const clientLogPrefix = `client: ${clientId} on profile: ${profile}`;
|
||||
socket.on("connect", () => {
|
||||
if (socket!.connected) {
|
||||
console.log(`${clientLogPrefix} is connected to server successfully!`);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("connect_error", (e) => {
|
||||
console.error(
|
||||
`${clientLogPrefix} connect error:`,
|
||||
(e && e.message) || "something wrong"
|
||||
);
|
||||
if (e && e.message && e.message.startsWith("[40")) {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("disconnect", (reason) => {
|
||||
console.warn(`${clientLogPrefix} disconnected: ${reason}!`);
|
||||
});
|
||||
|
||||
socket.on("disconnect_exit", (reason) => {
|
||||
console.warn(`${clientLogPrefix} disconnected and exited ${reason}!`);
|
||||
socket?.disconnect();
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
socket.on("request", (requestId, request) => {
|
||||
const isWebSocket = request.headers.upgrade === "websocket";
|
||||
console.log(`${isWebSocket ? "WS" : request.method}: `, request.path);
|
||||
request.port = options.port;
|
||||
request.hostname = options.host;
|
||||
|
||||
if (options.origin) {
|
||||
request.headers.host = options.origin;
|
||||
}
|
||||
|
||||
const tunnelRequest = new TunnelRequest(socket!, requestId);
|
||||
|
||||
const localReq = http.request(request);
|
||||
tunnelRequest.pipe(localReq);
|
||||
|
||||
const onTunnelRequestError = (e: any) => {
|
||||
console.error("tunnel request error: ", e);
|
||||
tunnelRequest.off("end", onTunnelRequestEnd);
|
||||
localReq.destroy(e);
|
||||
};
|
||||
|
||||
const onTunnelRequestEnd = () => {
|
||||
tunnelRequest.off("error", onTunnelRequestError);
|
||||
};
|
||||
|
||||
tunnelRequest.once("error", onTunnelRequestError);
|
||||
tunnelRequest.once("end", onTunnelRequestEnd);
|
||||
|
||||
const onLocalResponse = (localRes: any) => {
|
||||
localReq.off("error", onLocalError);
|
||||
|
||||
if (isWebSocket && localRes.upgrade) {
|
||||
return;
|
||||
}
|
||||
|
||||
const tunnelResponse = new TunnelResponse(socket!, requestId);
|
||||
|
||||
tunnelResponse.writeHead(
|
||||
localRes.statusCode,
|
||||
localRes.statusMessage,
|
||||
localRes.headers,
|
||||
localRes.httpVersion
|
||||
);
|
||||
|
||||
localRes.pipe(tunnelResponse);
|
||||
};
|
||||
|
||||
const onLocalError = (error: any) => {
|
||||
console.error("local error:", error);
|
||||
localReq.off("response", onLocalResponse);
|
||||
socket?.emit("request-error", requestId, error && error.message);
|
||||
tunnelRequest.destroy(error);
|
||||
};
|
||||
|
||||
const onUpgrade = (localRes: any, localSocket: any, localHead: any) => {
|
||||
// localSocket.once('error', onTunnelRequestError);
|
||||
if (localHead && localHead.length) localSocket.unshift(localHead);
|
||||
|
||||
const tunnelResponse = new TunnelResponse(socket!, requestId, true);
|
||||
tunnelResponse.writeHead(null, null, localRes.headers);
|
||||
localSocket.pipe(tunnelResponse).pipe(localSocket);
|
||||
};
|
||||
|
||||
localReq.once("error", onLocalError);
|
||||
localReq.once("response", onLocalResponse);
|
||||
|
||||
if (isWebSocket) {
|
||||
localReq.on("upgrade", onUpgrade);
|
||||
}
|
||||
});
|
||||
|
||||
// reconnect manually
|
||||
// const tryReconnect = () => {
|
||||
// setTimeout(() => {
|
||||
// socket!.io.open((err) => {
|
||||
// if (err) {
|
||||
// tryReconnect();
|
||||
// }
|
||||
// });
|
||||
// }, 2000);
|
||||
// };
|
||||
// socket.io.on("close", tryReconnect);
|
||||
|
||||
keepAlive();
|
||||
};
|
||||
|
||||
const startClient = (clientOptions: ClientOptions) => {
|
||||
const { port, options = {} } = clientOptions;
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirSync(configDir);
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile || PROFILE_DEFAULT}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
}
|
||||
|
||||
if (!config.server) {
|
||||
config.server = SERVER_DEFAULT_URL;
|
||||
}
|
||||
|
||||
if (!config.token) {
|
||||
console.info(`please init or set token for ${config.server}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!config.clientId) {
|
||||
if (!config.apiKey) {
|
||||
console.info(`please init or create a client for ${config.server}`);
|
||||
} else {
|
||||
config.clientId = config.apiKey;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
options.port = port;
|
||||
options.token = config.token;
|
||||
options.access = config.access;
|
||||
options.server = config.server;
|
||||
options.clientId = config.clientId;
|
||||
options.apiKey = options.key || config.apiKey;
|
||||
|
||||
if (options.suffix === "port" || options.suffix === "true") {
|
||||
options.suffix = `${port}`;
|
||||
} else if (options.suffix === "false") {
|
||||
options.suffix = undefined;
|
||||
} else if (options.suffix === "gen" || options.suffix === "uuid") {
|
||||
options.suffix = generateUUID();
|
||||
}
|
||||
|
||||
initStartClient(options);
|
||||
};
|
||||
|
||||
const stopClient = () => {
|
||||
if (socket) {
|
||||
socket.disconnect();
|
||||
socket.close();
|
||||
socket = null;
|
||||
keepAliveTimer && clearInterval(keepAliveTimer);
|
||||
|
||||
console.log("client stopped");
|
||||
}
|
||||
};
|
||||
|
||||
export {
|
||||
initClient,
|
||||
startClient,
|
||||
stopClient,
|
||||
};
|
||||
214
src/cli.ts
Normal file
214
src/cli.ts
Normal file
@@ -0,0 +1,214 @@
|
||||
import { Argument, InvalidArgumentError, program } from "commander";
|
||||
import * as fs from "fs";
|
||||
import * as os from "os";
|
||||
import * as path from "path";
|
||||
import { initClient, startClient } from "./api";
|
||||
import { PROFILE_DEFAULT, PROFILE_PATH, SERVER_DEFAULT_URL, TOKEN_FREE } from "./constant";
|
||||
import { getTokenFree } from './sdk';
|
||||
import { generateUUID } from "./util";
|
||||
|
||||
const packageInfo = require("../package.json");
|
||||
|
||||
program
|
||||
.name("hlt")
|
||||
.description(
|
||||
"CUBETIQ HTTP tunnel client with free access for local tunneling"
|
||||
)
|
||||
.version(`v${packageInfo.version}`);
|
||||
|
||||
// init
|
||||
program
|
||||
.command("init")
|
||||
.description("generate a new client and token with free access")
|
||||
.option("-s --server <string>", "setting server url", SERVER_DEFAULT_URL)
|
||||
.option(
|
||||
"-t --token <string>",
|
||||
"setting token (default generate FREE access token)",
|
||||
""
|
||||
)
|
||||
.option("-a --access <string>", "setting token access type", TOKEN_FREE)
|
||||
.option("-c --client <string>", "setting client (auto generate uuid)")
|
||||
.option(
|
||||
"-k --key <string>",
|
||||
"setting client api key for authentication access"
|
||||
)
|
||||
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.option("-f --force", "force to generate new client and token", false)
|
||||
.action(async (options) => {
|
||||
initClient(options);
|
||||
});
|
||||
|
||||
// start
|
||||
program
|
||||
.command("start")
|
||||
.description("start a connection with specific port")
|
||||
.argument("<port>", "local server port number", (value) => {
|
||||
const port = parseInt(value, 10);
|
||||
if (isNaN(port)) {
|
||||
throw new InvalidArgumentError("Not a number.");
|
||||
}
|
||||
return port;
|
||||
})
|
||||
.option("-s, --suffix <string>", "suffix for client name")
|
||||
.option(
|
||||
"-K, --keep_connection <boolean>",
|
||||
"keep connection for client and old connection will be closed (override connection)",
|
||||
true
|
||||
)
|
||||
.option(
|
||||
"-k --key <string>",
|
||||
"setting client api key for authentication access"
|
||||
)
|
||||
.option("-a, --access <string>", "access type (FREE)", TOKEN_FREE)
|
||||
.option("-p, --profile <string>", "profile name", PROFILE_DEFAULT)
|
||||
.option("-h, --host <string>", "local host value", "localhost")
|
||||
.option("-o, --origin <string>", "change request origin")
|
||||
.action((port, options) => {
|
||||
startClient({
|
||||
port,
|
||||
options,
|
||||
})
|
||||
});
|
||||
|
||||
// config
|
||||
program
|
||||
.command("config")
|
||||
.description("create and update config file for connection")
|
||||
.addArgument(
|
||||
new Argument("<type>", "config type").choices([
|
||||
"access",
|
||||
"token",
|
||||
"server",
|
||||
"client",
|
||||
"key",
|
||||
])
|
||||
)
|
||||
.argument("<value>", "config value")
|
||||
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.action(async (type, value, options) => {
|
||||
if (!type) {
|
||||
console.error("type config is required!");
|
||||
return;
|
||||
}
|
||||
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirSync(configDir);
|
||||
console.log(`config file ${configDir} was created`);
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
}
|
||||
|
||||
if (!config.server) {
|
||||
config.server = SERVER_DEFAULT_URL;
|
||||
}
|
||||
|
||||
// Error Code status
|
||||
let errorCode = 0;
|
||||
|
||||
if (type === "token" || type === "jwt") {
|
||||
config.token = value;
|
||||
} else if (type === "server") {
|
||||
config.server = value;
|
||||
} else if (type === "clientId" || type === "client") {
|
||||
if (!value || value === "" || value === "new") {
|
||||
config.clientId = generateUUID();
|
||||
} else {
|
||||
config.clientId = value;
|
||||
}
|
||||
console.log(`client: ${config.clientId} was set to config`);
|
||||
} else if (type === "apiKey" || type === "key") {
|
||||
config.apiKey = value;
|
||||
} else if (type === "access") {
|
||||
config.access = (value && value.toUpperCase().trim()) || TOKEN_FREE;
|
||||
|
||||
// FREE
|
||||
if (config.access === TOKEN_FREE) {
|
||||
await getTokenFree(config.server)
|
||||
.then((resp: any) => {
|
||||
if (resp.data?.token) {
|
||||
config.token = resp.data?.token;
|
||||
} else {
|
||||
errorCode = 1;
|
||||
console.error("Generate free token failed, return with null or empty from server!", resp);
|
||||
return;
|
||||
}
|
||||
})
|
||||
.catch((err: any) => {
|
||||
errorCode = 1;
|
||||
console.error("cannot get free token from server", err);
|
||||
return;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!config.clientId && config.apiKey) {
|
||||
config.clientId = config.apiKey;
|
||||
}
|
||||
|
||||
if (errorCode === 0) {
|
||||
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
||||
console.log(`${type} config saved successfully to: ${configFilePath}`);
|
||||
}
|
||||
});
|
||||
|
||||
// config
|
||||
program
|
||||
.command("config-get")
|
||||
.description("get type from config file")
|
||||
.addArgument(
|
||||
new Argument("<type>", "config type").choices([
|
||||
"access",
|
||||
"token",
|
||||
"server",
|
||||
"client",
|
||||
"key",
|
||||
])
|
||||
)
|
||||
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.action(async (type, options) => {
|
||||
if (!type) {
|
||||
console.error("type config is required!");
|
||||
return;
|
||||
}
|
||||
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
if (!fs.existsSync(configDir)) {
|
||||
console.log(`config file ${configDir} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
} else {
|
||||
console.log(`config file ${configFilePath} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === "token" || type === "jwt") {
|
||||
console.log(config.token);
|
||||
} else if (type === "server") {
|
||||
console.log(config.server);
|
||||
} else if (type === "clientId" || type === "client") {
|
||||
console.log(config.clientId);
|
||||
} else if (type === "apiKey" || type === "key") {
|
||||
console.log(config.apiKey);
|
||||
} else if (type === "access") {
|
||||
console.log(config.access);
|
||||
} else {
|
||||
console.log('no config found for type: "' + type + '"');
|
||||
}
|
||||
});
|
||||
|
||||
program.parse();
|
||||
497
src/client.ts
497
src/client.ts
@@ -1,497 +0,0 @@
|
||||
import * as os from "os";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import * as http from "http";
|
||||
import { io } from "socket.io-client";
|
||||
import { HttpsProxyAgent } from "https-proxy-agent";
|
||||
import { program, InvalidArgumentError, Argument } from "commander";
|
||||
import { TunnelRequest, TunnelResponse } from "./lib";
|
||||
import { generateUUID, addPrefixOnHttpSchema } from "./util";
|
||||
import { Socket } from 'socket.io-client';
|
||||
|
||||
import { getTokenFree } from './sdk'
|
||||
|
||||
const packageInfo = require("../package.json");
|
||||
|
||||
// constants
|
||||
const PROFILE_DEFAULT = "default";
|
||||
const PROFILE_PATH = ".hlt";
|
||||
const SERVER_DEFAULT_URL = "https://lt.ctdn.net";
|
||||
const TOKEN_FREE = "FREE";
|
||||
|
||||
// create socket instance
|
||||
let socket: Socket | null = null;
|
||||
|
||||
function keepAlive() {
|
||||
setTimeout(() => {
|
||||
if (socket && socket.connected) {
|
||||
socket.send("ping");
|
||||
}
|
||||
keepAlive();
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
function initClient(options: any) {
|
||||
// Please change this if your domain goes wrong here
|
||||
// Current style using sub-domain: https://{{clientId}}-tunnel.myhostingdomain.com
|
||||
// (Original server: https://tunnel.myhostingdomain.com)
|
||||
const profile = options.profile || PROFILE_DEFAULT;
|
||||
const clientId = `${options.apiKey || options.clientId || generateUUID()}`;
|
||||
const clientIdSub =
|
||||
profile === PROFILE_DEFAULT ? `${clientId}-` : `${clientId}-${profile}-`;
|
||||
const clientEndpoint = (
|
||||
options.suffix ? `${clientIdSub}${options.suffix}-` : clientIdSub
|
||||
)
|
||||
.toLowerCase()
|
||||
.trim();
|
||||
const serverUrl = addPrefixOnHttpSchema(options.server, clientEndpoint);
|
||||
|
||||
// extra options for socket to identify the client (authentication and options of tunnel)
|
||||
const defaultParams = {
|
||||
apiKey: options.apiKey,
|
||||
clientId: options.clientId,
|
||||
profile: options.profile,
|
||||
clientIdSub: clientIdSub,
|
||||
clientEndpoint: clientEndpoint,
|
||||
serverUrl: serverUrl,
|
||||
access: options.access,
|
||||
keep_connection: options.keep_connection || true,
|
||||
};
|
||||
|
||||
// extra info for notify about the running of the tunnel (it's private info, other platfom cannot access this)
|
||||
// this using for internal only (don't worry about this)
|
||||
const osInfo = {
|
||||
hostname: os.hostname(),
|
||||
platform: os.platform(),
|
||||
arch: os.arch(),
|
||||
release: os.release(),
|
||||
};
|
||||
|
||||
const initParams: any = {
|
||||
path: "/$cubetiq_http_tunnel",
|
||||
transports: ["websocket"],
|
||||
auth: {
|
||||
token: options.token,
|
||||
...defaultParams,
|
||||
},
|
||||
headers: {
|
||||
...defaultParams,
|
||||
os: osInfo,
|
||||
},
|
||||
// reconnection: true,
|
||||
};
|
||||
|
||||
const http_proxy = process.env.https_proxy || process.env.http_proxy;
|
||||
if (http_proxy) {
|
||||
initParams.agent = new HttpsProxyAgent(http_proxy);
|
||||
}
|
||||
|
||||
// Connecting to socket server and agent here...
|
||||
console.log(`client connecting to server: ${serverUrl}`);
|
||||
socket = io(serverUrl, initParams);
|
||||
|
||||
const clientLogPrefix = `client: ${clientId} on profile: ${profile}`;
|
||||
socket.on("connect", () => {
|
||||
if (socket!.connected) {
|
||||
console.log(`${clientLogPrefix} is connected to server successfully!`);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("connect_error", (e) => {
|
||||
console.log(
|
||||
`${clientLogPrefix} connect error:`,
|
||||
(e && e.message) || "something wrong"
|
||||
);
|
||||
if (e && e.message && e.message.startsWith("[40")) {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("disconnect", (reason) => {
|
||||
console.log(`${clientLogPrefix} disconnected: ${reason}!`);
|
||||
});
|
||||
|
||||
socket.on("disconnect_exit", (reason) => {
|
||||
console.log(`${clientLogPrefix} disconnected and exited ${reason}!`);
|
||||
socket?.disconnect();
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
socket.on("request", (requestId, request) => {
|
||||
const isWebSocket = request.headers.upgrade === "websocket";
|
||||
console.log(`${isWebSocket ? "WS" : request.method}: `, request.path);
|
||||
request.port = options.port;
|
||||
request.hostname = options.host;
|
||||
|
||||
if (options.origin) {
|
||||
request.headers.host = options.origin;
|
||||
}
|
||||
|
||||
const tunnelRequest = new TunnelRequest(socket!, requestId);
|
||||
|
||||
const localReq = http.request(request);
|
||||
tunnelRequest.pipe(localReq);
|
||||
|
||||
const onTunnelRequestError = (e: any) => {
|
||||
tunnelRequest.off("end", onTunnelRequestEnd);
|
||||
localReq.destroy(e);
|
||||
};
|
||||
|
||||
const onTunnelRequestEnd = () => {
|
||||
tunnelRequest.off("error", onTunnelRequestError);
|
||||
};
|
||||
|
||||
tunnelRequest.once("error", onTunnelRequestError);
|
||||
tunnelRequest.once("end", onTunnelRequestEnd);
|
||||
|
||||
const onLocalResponse = (localRes: any) => {
|
||||
localReq.off("error", onLocalError);
|
||||
|
||||
if (isWebSocket && localRes.upgrade) {
|
||||
return;
|
||||
}
|
||||
|
||||
const tunnelResponse = new TunnelResponse(socket!, requestId);
|
||||
|
||||
tunnelResponse.writeHead(
|
||||
localRes.statusCode,
|
||||
localRes.statusMessage,
|
||||
localRes.headers,
|
||||
localRes.httpVersion
|
||||
);
|
||||
|
||||
localRes.pipe(tunnelResponse);
|
||||
};
|
||||
|
||||
const onLocalError = (error: any) => {
|
||||
console.log(error);
|
||||
localReq.off("response", onLocalResponse);
|
||||
socket?.emit("request-error", requestId, error && error.message);
|
||||
tunnelRequest.destroy(error);
|
||||
};
|
||||
|
||||
const onUpgrade = (localRes: any, localSocket: any, localHead: any) => {
|
||||
// localSocket.once('error', onTunnelRequestError);
|
||||
if (localHead && localHead.length) localSocket.unshift(localHead);
|
||||
|
||||
const tunnelResponse = new TunnelResponse(socket!, requestId, true);
|
||||
tunnelResponse.writeHead(null, null, localRes.headers);
|
||||
localSocket.pipe(tunnelResponse).pipe(localSocket);
|
||||
};
|
||||
|
||||
localReq.once("error", onLocalError);
|
||||
localReq.once("response", onLocalResponse);
|
||||
|
||||
if (isWebSocket) {
|
||||
localReq.on("upgrade", onUpgrade);
|
||||
}
|
||||
});
|
||||
|
||||
// reconnect manually
|
||||
const tryReconnect = () => {
|
||||
setTimeout(() => {
|
||||
socket!.io.open((err) => {
|
||||
if (err) {
|
||||
tryReconnect();
|
||||
}
|
||||
});
|
||||
}, 2000);
|
||||
};
|
||||
|
||||
// socket.io.on("close", tryReconnect);
|
||||
|
||||
keepAlive();
|
||||
}
|
||||
|
||||
program
|
||||
.name("hlt")
|
||||
.description(
|
||||
"CUBETIQ HTTP tunnel client with free access for local tunneling"
|
||||
)
|
||||
.version(`v${packageInfo.version}`);
|
||||
|
||||
// init
|
||||
program
|
||||
.command("init")
|
||||
.description("generate a new client and token with free access")
|
||||
.option("-s --server <string>", "setting server url", SERVER_DEFAULT_URL)
|
||||
.option(
|
||||
"-t --token <string>",
|
||||
"setting token (default generate FREE access token)",
|
||||
""
|
||||
)
|
||||
.option("-a --access <string>", "setting token access type", TOKEN_FREE)
|
||||
.option("-c --client <string>", "setting client (auto generate uuid)")
|
||||
.option(
|
||||
"-k --key <string>",
|
||||
"setting client api key for authentication access"
|
||||
)
|
||||
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.action(async (options) => {
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirSync(configDir);
|
||||
console.log(`config file ${configDir} was created`);
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
}
|
||||
|
||||
if (!config.server) {
|
||||
config.server = options.server || SERVER_DEFAULT_URL;
|
||||
}
|
||||
|
||||
if (!config.token && options.token) {
|
||||
config.token = options.token;
|
||||
}
|
||||
|
||||
if (!config.access) {
|
||||
config.access = options.access || TOKEN_FREE;
|
||||
}
|
||||
|
||||
if (!config.clientId) {
|
||||
config.clientId = options.client || generateUUID();
|
||||
}
|
||||
|
||||
if (!config.apiKey && options.key) {
|
||||
config.apiKey = options.key;
|
||||
}
|
||||
|
||||
if (!config.token) {
|
||||
console.log("Generating token...");
|
||||
await getTokenFree(config.server)
|
||||
.then((resp: any) => {
|
||||
if (resp.data?.token) {
|
||||
config.token = resp.data?.token;
|
||||
} else {
|
||||
console.log("free token return with null or empty from server");
|
||||
return;
|
||||
}
|
||||
})
|
||||
.catch((err: any) => {
|
||||
console.error("cannot get free token from server", err);
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
||||
console.log(`initialized config saved successfully to: ${configFilePath}`);
|
||||
});
|
||||
|
||||
// start
|
||||
program
|
||||
.command("start")
|
||||
.description("start a connection with specific port")
|
||||
.argument("<port>", "local server port number", (value) => {
|
||||
const port = parseInt(value, 10);
|
||||
if (isNaN(port)) {
|
||||
throw new InvalidArgumentError("Not a number.");
|
||||
}
|
||||
return port;
|
||||
})
|
||||
.option("-s, --suffix <string>", "suffix for client name")
|
||||
.option(
|
||||
"-K, --keep_connection <boolean>",
|
||||
"keep connection for client and old connection will be closed (override connection)",
|
||||
true
|
||||
)
|
||||
.option(
|
||||
"-k --key <string>",
|
||||
"setting client api key for authentication access"
|
||||
)
|
||||
.option("-a, --access <string>", "access type (FREE)", TOKEN_FREE)
|
||||
.option("-p, --profile <string>", "profile name", PROFILE_DEFAULT)
|
||||
.option("-h, --host <string>", "local host value", "localhost")
|
||||
.option("-o, --origin <string>", "change request origin")
|
||||
.action((port, options) => {
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirSync(configDir);
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
}
|
||||
|
||||
if (!config.server) {
|
||||
config.server = SERVER_DEFAULT_URL;
|
||||
}
|
||||
|
||||
if (!config.token) {
|
||||
console.info(`please init or set token for ${config.server}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!config.clientId) {
|
||||
if (!config.apiKey) {
|
||||
console.info(`please init or create a client for ${config.server}`);
|
||||
} else {
|
||||
config.clientId = config.apiKey;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
options.port = port;
|
||||
options.token = config.token;
|
||||
options.access = config.access;
|
||||
options.server = config.server;
|
||||
options.clientId = config.clientId;
|
||||
options.apiKey = options.key || config.apiKey;
|
||||
|
||||
if (options.suffix === "port" || options.suffix === "true") {
|
||||
options.suffix = `${port}`;
|
||||
} else if (options.suffix === "false") {
|
||||
options.suffix = undefined;
|
||||
} else if (options.suffix === "gen" || options.suffix === "uuid") {
|
||||
options.suffix = generateUUID();
|
||||
}
|
||||
|
||||
initClient(options);
|
||||
});
|
||||
|
||||
// config
|
||||
program
|
||||
.command("config")
|
||||
.description("create and update config file for connection")
|
||||
.addArgument(
|
||||
new Argument("<type>", "config type").choices([
|
||||
"access",
|
||||
"token",
|
||||
"server",
|
||||
"client",
|
||||
"key",
|
||||
])
|
||||
)
|
||||
.argument("<value>", "config value")
|
||||
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.action(async (type, value, options) => {
|
||||
if (!type) {
|
||||
console.error("type config is required!");
|
||||
return;
|
||||
}
|
||||
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirSync(configDir);
|
||||
console.log(`config file ${configDir} was created`);
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
}
|
||||
|
||||
if (!config.server) {
|
||||
config.server = SERVER_DEFAULT_URL;
|
||||
}
|
||||
|
||||
if (type === "token" || type === "jwt") {
|
||||
config.token = value;
|
||||
} else if (type === "server") {
|
||||
config.server = value;
|
||||
} else if (type === "clientId" || type === "client") {
|
||||
if (!value || value === "" || value === "new") {
|
||||
config.clientId = generateUUID();
|
||||
} else {
|
||||
config.clientId = value;
|
||||
}
|
||||
console.log(`client: ${config.clientId} was set to config`);
|
||||
} else if (type === "apiKey" || type === "key") {
|
||||
config.apiKey = value;
|
||||
} else if (type === "access") {
|
||||
config.access = (value && value.toUpperCase().trim()) || TOKEN_FREE;
|
||||
|
||||
// FREE
|
||||
if (config.access === TOKEN_FREE) {
|
||||
await getTokenFree(config.server)
|
||||
.then((resp: any) => {
|
||||
if (resp.data?.token) {
|
||||
config.token = resp.data?.token;
|
||||
} else {
|
||||
console.log("free token return with null or empty from server");
|
||||
return;
|
||||
}
|
||||
})
|
||||
.catch((err: any) => {
|
||||
console.error("cannot get free token from server", err);
|
||||
return;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!config.clientId && config.apiKey) {
|
||||
config.clientId = config.apiKey;
|
||||
}
|
||||
|
||||
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
||||
console.log(`${type} config saved successfully to: ${configFilePath}`);
|
||||
});
|
||||
|
||||
// config
|
||||
program
|
||||
.command("config-get")
|
||||
.description("get type from config file")
|
||||
.addArgument(
|
||||
new Argument("<type>", "config type").choices([
|
||||
"access",
|
||||
"token",
|
||||
"server",
|
||||
"client",
|
||||
"key",
|
||||
])
|
||||
)
|
||||
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.action(async (type, options) => {
|
||||
if (!type) {
|
||||
console.error("type config is required!");
|
||||
return;
|
||||
}
|
||||
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
if (!fs.existsSync(configDir)) {
|
||||
console.log(`config file ${configDir} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
} else {
|
||||
console.log(`config file ${configFilePath} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === "token" || type === "jwt") {
|
||||
console.log(config.token);
|
||||
} else if (type === "server") {
|
||||
console.log(config.server);
|
||||
} else if (type === "clientId" || type === "client") {
|
||||
console.log(config.clientId);
|
||||
} else if (type === "apiKey" || type === "key") {
|
||||
console.log(config.apiKey);
|
||||
} else if (type === "access") {
|
||||
console.log(config.access);
|
||||
} else {
|
||||
console.log('no config found for type: "' + type + '"');
|
||||
}
|
||||
});
|
||||
|
||||
program.parse();
|
||||
5
src/constant.ts
Normal file
5
src/constant.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
// constants
|
||||
export const PROFILE_DEFAULT = "default";
|
||||
export const PROFILE_PATH = ".hlt";
|
||||
export const SERVER_DEFAULT_URL = "https://lt.ctdn.net";
|
||||
export const TOKEN_FREE = "FREE";
|
||||
21
src/interface.ts
Normal file
21
src/interface.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export interface Options {
|
||||
server?: string;
|
||||
profile?: string;
|
||||
key?: string;
|
||||
apiKey?: string;
|
||||
access?: string;
|
||||
suffix?: string;
|
||||
clientId?: string;
|
||||
keep_connection?: boolean;
|
||||
token?: string;
|
||||
origin?: string;
|
||||
port?: number;
|
||||
host?: string;
|
||||
|
||||
// [key: string]: any;
|
||||
}
|
||||
|
||||
export interface ClientOptions {
|
||||
port: number;
|
||||
options?: Options;
|
||||
}
|
||||
@@ -57,7 +57,7 @@ class TunnelResponse extends stream.Duplex {
|
||||
super();
|
||||
|
||||
if (duplex) {
|
||||
// for websocket request: bidirection
|
||||
// for websocket request: bidirection
|
||||
const onResponsePipe = (responseId: string, data: any) => {
|
||||
if (this.responseId === responseId) {
|
||||
this.push(data);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const axios = require("axios").default;
|
||||
const axios = require("axios");
|
||||
|
||||
const getTokenFree = async (baseUrl: string, data: any = {}) => {
|
||||
const url = `${baseUrl}/__free__/api/get_token`;
|
||||
@@ -10,6 +10,7 @@ const getTokenFree = async (baseUrl: string, data: any = {}) => {
|
||||
},
|
||||
headers: {
|
||||
"x-access-type": "FREE",
|
||||
"Accept-Encoding": "identity",
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
9
src/test.ts
Normal file
9
src/test.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { startClient, stopClient } from './api';
|
||||
|
||||
startClient({
|
||||
port: 3001,
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
stopClient();
|
||||
}, 5000);
|
||||
Reference in New Issue
Block a user