Skip to content

Commit

Permalink
规避域名备案
Browse files Browse the repository at this point in the history
  • Loading branch information
lzghzr committed Jun 15, 2019
1 parent 9bece79 commit 400a61e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions bilive/@types/bilive.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface config {
[index: string]: boolean | number | number[] | string | string[]
defaultUserID: number
serverURL: string
serverDomains: string[]
eventRooms: number[]
}
interface userCollection {
Expand Down
16 changes: 14 additions & 2 deletions bilive/lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import ws from 'ws'
import url from 'url'
// @ts-ignore d.ts没有跟进
import { promises as dnsPromises } from 'dns'
import { EventEmitter } from 'events'
import tools from './tools'
import Options from '../options'
/**
* Blive客户端, 用于连接服务器和发送事件
*
Expand Down Expand Up @@ -72,10 +76,18 @@ class Client extends EventEmitter {
*
* @memberof Client
*/
public Connect() {
public async Connect() {
if (this._connected) return
this._connected = true
this._wsClient = new ws(this._server, [this._protocol])
// 规避域名备案
const { host } = url.parse(this._server)
if (host !== undefined && Options._.config.serverDomains.includes(host)) {
let server = this._server
const ip: { address: string, family: number } = await dnsPromises.lookup(host).catch(() => undefined)
if (ip !== undefined) server = server.replace(host, ip.family === 4 ? ip.address : `[${ip.address}]`)
this._wsClient = new ws(server, [this._protocol], { rejectUnauthorized: false, headers: { host } })
}
else this._wsClient = new ws(this._server, [this._protocol])
this._wsClient
.on('error', error => this._ClientErrorHandler(error))
.on('close', () => this.Close())
Expand Down
3 changes: 3 additions & 0 deletions bilive/options.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"config": {
"defaultUserID": 0,
"serverURL": "wss://bilive.halaal.win/server/#164c48292a6c773c85716093589cd60e",
"serverDomains": [
"bilive.halaal.win"
],
"eventRooms": [
3
]
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bilive_client",
"version": "2.0.3",
"version": "2.0.4",
"description": "基于Node.JS的bilibili直播挂机系统",
"main": "index.js",
"scripts": {
Expand All @@ -19,12 +19,12 @@
"devDependencies": {
"@types/bootstrap": "^4.3.0",
"@types/jquery": "^3.3.29",
"@types/node": "^10.14.6",
"@types/node": "^12.0.8",
"@types/request": "^2.48.1",
"@types/ws": "^5.1.2",
"npm-run-posix-or-windows": "^2.0.2",
"rimraf": "^2.6.3",
"typescript": "^3.4.5"
"typescript": "^3.5.2"
},
"dependencies": {
"request": "^2.88.0",
Expand Down

0 comments on commit 400a61e

Please sign in to comment.