6 Star 0 Fork 0

南宁橡芮科技有限公司 / lakutata-component-tunnel

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

NPM Version NODE Version Known Vulnerabilities NPM Download

Features

  • Internet Penetration
  • Integrate server side & client side
  • Support cluster (Share TCP stream via redis pub/sub)
  • Based on WebSocket transport
  • Support HTTP Basic Auth

Quickstart

How to use

import {Application, createApp} from '@lakutata/core'
import {AllowCallback, Tunnel, TunnelClientComponent, TunnelServerComponent} from '@lakutata-component/tunnel'
import {createServer, IncomingMessage} from 'http'
import {WebSocketServer} from 'ws'

const app = await createApp({
    id: 'test.app',
    name: 'tester',
    components: {
        server: {
            class: TunnelServerComponent,
            //Server bind port
            port: 80,
            //Server bind address
            address: '0.0.0.0',
            //Server bind domain
            domain: 'tunnel.example.com',
            //Server name
            serverName: 'ThisIsServerName',
            //On client connect to server callback
            allowConnect: (app: Application, tunnel: Tunnel, callback: AllowCallback) => {
                callback(null)
            },
            //On client request received callback
            allowRequest: (app: Application, tunnel: Tunnel, req: IncomingMessage, callback: AllowCallback) => {
                callback(null)
            },
            //On client upgrade request received callback
            allowUpgrade: (app: Application, tunnel: Tunnel, req: IncomingMessage, callback: AllowCallback) => {
                callback(null)
            },
            //Redis is optional, if redis options not set, the server will be single server mode, else the server support HA mode
            redisOptions: {
                port: 6379,
                host: 'localhost',
                db: 13
            }
        },
        client: {
            class: TunnelClientComponent
        }
    },
    workflows: [() => {
        //Establish a local server listen port 8080 for test
        const testServer = createServer((req, res) => {
            res.write('OK')
            res.end()
        }).listen(8080, '0.0.0.0')
        //Add websocket support for test
        const wss = new WebSocketServer({noServer: true, path: '/ws'})
        testServer.on('upgrade', (req, socket, head) => {
            wss.handleUpgrade(req, socket, head, client => {
                setInterval(() => {
                    client.send(`timestamp:${Date.now()}`)
                }, 1000)
            })
        })
    }]
})
const serverComponent: TunnelServerComponent = app.Components.get<TunnelServerComponent>('server')
serverComponent.on('upgrade_connection_close', (tunnel: Tunnel) => {
    //On http upgrade socket closed
    console.log('upgrade_connection_close', tunnel)
})
serverComponent.on('upgrade_connection_open', (tunnel: Tunnel) => {
    //On http upgrade socket established
    console.log('upgrade_connection_open', tunnel)
})
serverComponent.on('connection_close', (tunnel: Tunnel) => {
    //On http request socket closed
    console.log('connection_close', tunnel)
})
serverComponent.on('connection_open', (tunnel: Tunnel) => {
    //On received http request
    console.log('connection_open', tunnel)
})
const clientComponent: TunnelClientComponent = app.Components.get<TunnelClientComponent>('client')
const tunnelSocket: TunnelSocket = clientComponent.open({
    //Tunnel server URL
    server: 'http://tunnel.example.com',
    //Assign a subdomain
    subdomain: 'this-is-a-test',
    //Local service port
    port: 8080,
    //Local IP or hostname
    hostname: '127.0.0.1',
    //If local server listening tls port, set it true
    tls: false,
    //Rewrite host in headers when forward request to local
    rewriteHost: false,
    //Is allow wildcard domain request proxy
    allowWildcard: true,
    //Use HTTP basic auth to protect local service
    authorization: {
        //Username for Basic Auth
        username: 'user',
        //Password for Basic Auth
        password: 'pass',
        //Whether add username field and password field to HTTP header
        extract: false
    }
})

//Print URL
tunnel.on('url', (url: string) => console.log('URL:', url))//this-is-a-test.tunnel.example.com
//On open event
tunnel.on('open', (tunnelSocket: TunnelSocket) => console.log(tunnelSocket))
//On close event
tunnel.on('close', (tunnelSocket: TunnelSocket) => console.log(tunnelSocket))
//On destroy event
tunnel.on('destroy', () => console.log('destroyed'))
//On error handler
tunnel.on('error', (exception: Exception) => console.error(exception))

Server-Side Events

/**
 * On http upgrade socket closed
 */
on('upgrade_connection_close', (tunnel: Tunnel) => {
    //Client information
})
/**
 * On http upgrade socket established
 */
on('upgrade_connection_open', (tunnel: Tunnel) => {
    //Client information
})
/**
 * On http request socket closed
 */
on('connection_close', (tunnel: Tunnel) => {
    //Client information
})
/**
 * On received http request
 */
on('connection_open', (tunnel: Tunnel) => {
    //Client information
})

Examples

Examples can be found in project src/tests.

How to Contribute

Please let us know how can we help. Do check out issues for bug reports or suggestions first.

License

MIT

MIT License Copyright (c) 2023 Alex.Ma 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 furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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.

简介

隧道穿透组件 展开 收起
TypeScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
TypeScript
1
https://gitee.com/nnxr/lakutata-component-tunnel.git
git@gitee.com:nnxr/lakutata-component-tunnel.git
nnxr
lakutata-component-tunnel
lakutata-component-tunnel
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891