{"id":13776823,"url":"https://github.com/fffonion/lua-resty-multiplexer","last_synced_at":"2025-05-08T22:42:29.370Z","repository":{"id":150895102,"uuid":"128492343","full_name":"fffonion/lua-resty-multiplexer","owner":"fffonion","description":"Transparent port service multiplexer for stream subsystem","archived":false,"fork":false,"pushed_at":"2020-09-10T06:48:52.000Z","size":22,"stargazers_count":61,"open_issues_count":1,"forks_count":17,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-08T22:42:23.678Z","etag":null,"topics":["lua-resty-multiplexer","multiplexer","openresty","protocol-identifiers","stream-lua-nginx"],"latest_commit_sha":null,"homepage":"https://yooooo.us/2018/lua-resty-multiplexer","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fffonion.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-04-07T03:20:36.000Z","updated_at":"2024-10-10T01:53:24.000Z","dependencies_parsed_at":"2023-07-09T12:32:16.730Z","dependency_job_id":null,"html_url":"https://github.com/fffonion/lua-resty-multiplexer","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fffonion%2Flua-resty-multiplexer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fffonion%2Flua-resty-multiplexer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fffonion%2Flua-resty-multiplexer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fffonion%2Flua-resty-multiplexer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fffonion","download_url":"https://codeload.github.com/fffonion/lua-resty-multiplexer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253160727,"owners_count":21863624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lua-resty-multiplexer","multiplexer","openresty","protocol-identifiers","stream-lua-nginx"],"created_at":"2024-08-03T18:00:33.627Z","updated_at":"2025-05-08T22:42:29.353Z","avatar_url":"https://github.com/fffonion.png","language":"Lua","funding_links":[],"categories":["Libraries"],"sub_categories":[],"readme":"Name\n====\n\nlua-resty-multiplexer - Transparent port service multiplexer for stream subsystem \n\nTable of Contents\n=================\n\n- [Description](#description)\n- [Status](#status)\n- [Synopsis](#synopsis)\n- [Protocol](#protocol)\n    * [Add new protocol](#add-new-protocol)\n- [Matcher](#matcher)\n    * [client-host](#client-host)\n    * [protocol](#protocol)\n    * [time](#time)\n    * [default](#default)\n    * [Add new matcher](#add-new-matcher)\n- [API](#api)\n- [TODO](#todo)\n- [Copyright and License](#copyright-and-license)\n- [See Also](#see-also)\n\n\nDescription\n===========\n\nThis library implemented a transparent port service multiplexer, which can be used to run multiple TCP services on the same port.\n\nNote that nginx [stream module](https://nginx.org/en/docs/stream/ngx_stream_core_module.html) and [stream-lua-nginx-module](https://github.com/openresty/stream-lua-nginx-module) is required.\n\nTested on Openresty \u003e= 1.13.6.1.\n\nWith OpenResty 1.13.6.1, a customed [patch](patches/stream-lua-readpartial.patch) from [@fcicq](https://github.com/fcicq) is needed. The origin discussion can be found [here](https://github.com/fffonion/lua-resty-sniproxy/issues/1). And native\nproxying is not supported as `reqsock:peek` is missing.\n\nStarting OpenResty 1.15.8.1, only native proxying is supported and no patch is needed. Lua land proxying will be\npossible when stream-lua-nginx-module implemented `tcpsock:receiveany`.\n\n[Back to TOC](#table-of-contents)\n\nStatus\n========\n\nExperimental.\n\nSynopsis\n========\n\n\n```lua\nstream {\n    init_by_lua_block {\n        local mul = require(\"resty.multiplexer\")\n        mul.load_protocols(\n            \"http\", \"ssh\", \"dns\", \"tls\", \"xmpp\"\n        )\n        mul.set_rules(\n            {{\"client-host\", \"10.0.0.1\"}, \"internal-host\", 80},\n            {{\"protocol\", \"http\"}, {\"client-host\", \"10.0.0.2\"}, \"internal-host\", 8001},\n            {{\"protocol\", \"http\"}, \"example.com\", 80},\n            {{\"protocol\", \"ssh\"}, \"github.com\", 22},\n            {{\"protocol\", \"dns\"}, \"1.1.1.1\", 53},\n            {{\"protocol\", \"tls\"}, {\"time\", nil}, \"twitter.com\", 443},\n            {{\"protocol\", \"tls\"}, \"www.google.com\", 443},\n            {{\"default\", nil}, \"127.0.0.1\", 80}\n        )\n        mul.matcher_config.time = {\n            minute_match = {0, 30},\n            minute_not_match = {{31, 59}},\n        }\n    }\n\n    resolver 8.8.8.8;\n\n    # for OpenResty \u003e= 1.15.8.1, native Nginx proxying\n    lua_add_variable $multiplexer_upstream;\n    server {\n            error_log /var/log/nginx/multiplexer-error.log error;\n            listen 443;\n\n            resolver 8.8.8.8;\n\n            preread_by_lua_block {\n                local mul = require(\"resty.multiplexer\")\n                local mp = mul:new()\n                mp:preread_by()\n            }\n            proxy_pass $multiplexer_upstream;\n    }\n\n    # for OpenResty \u003c 1.15.8.1, with patch applied, Lua land proxying\n    server {\n            error_log /var/log/nginx/multiplexer-error.log error;\n            listen 443;\n\n            resolver 8.8.8.8;\n\n            server {\n                listen 80;\n                content_by_lua_block {\n                    local mul = require(\"resty.multiplexer\")\n                    local mp = mul:new()\n                    mp:content_by()\n                }\n            }\n    }\n}\n```\n\nThis module consists of two parts: protocol identifiers and matchers.\n\nProtocol identifies need to loaded through `load_protocols` in `init_by_lua_block` directive. See [protocol](#protocol) section for currently supported protocols and guide to add a new protocol.\n\nRules are defined through `set_rules` to route traffic to different upstreams. For every matcher that is defined in the rule, the corresponding matcher is loaded automatically. See [matcher](#matcher) section for currently implmented matchers and guide to add a new matcher.\n\nSee [API](#api) section for syntax of `load_protocols` and `set_rules`.\n\nThe rules defined is prioritized. In the example above, we defined a rule such that:\n\n- If client address is `10.0.0.1`, proxy to **internal-host.com:80**\n- If protocol is `HTTP` and client address is `10.0.0.2`, proxy to **internal-host:8001**\n- If protocol is `SSH`, proxy to **github.com:22**\n- If protocol is `DNS`, proxy to **1.1.1.1:53**\n- If protocol is `SSL/TLS` and current minute is between **0** and **30**,  proxy to **twitter:443**\n- If protocol is `SSL/TLS` and current minute is between **31** and **59**, proxy to **www.google.com:443**\n- Otherwise, proxy to **127.0.0.1:80**\n\n[Back to TOC](#table-of-contents)\n\n\nProtocol\n=======\n\nThe protocol part analyzes the first request that is sent from client and try to match it using known protocol signatures.\n\nCurrently supported: `dns`, `http`, `ssh`, `tls`, `xmpp`. Based on the bytes of signature, each protocol may have different possibilities to be falsely identified.\n\n| Protocol  |  Length of signature |   False rate  |\n|---|---|---|\n| dns  |  9 1/4 | 5.29e-23  |\n| http  | 4  |  2.33e-10 |\n| ssh  |  4  |  2.33e-10 |\n| tls  |  6 |  3.55e-15 |\n| xmpp  |  6 in 8 1/4 |  ? |\n\n\n[Back to TOC](#table-of-contents)\n\nAdd new protocol\n-----------------\n\nCreate a new `protocol_name.lua` file under `resty/multiplexer/protocol` in the format of:\n\n```lua\nreturn {\n    required_bytes = ?,\n    check = function(buf)\n    -- check with the buf and return true if the protocol is identified\n    end\n}\n```\n\n`required_bytes` is the length of bytes we need to read before identifying the protocol. \n\n[Back to TOC](#table-of-contents)\n\n\nMatcher\n=======\n\nclient-host\n-----------\n\nMatch if `$remote_addr` equals to expected value.\n\n[Back to TOC](#table-of-contents)\n\nprotocol\n--------\n\nMatch if protocol equals to expected value.\n\n[Back to TOC](#table-of-contents)\n\ntime\n----\n\nMatch if current time is in configured range in `mul.matcher_config.time`. If no range is defined, the matcher will always return *false*.\n\nFor example, to match year `2018`, `January` and `March` and hour `6` to `24` except for hour `12`:\n\n```lua\n init_by_lua_block {\n    local mul = require(\"resty.multiplexer\")\n    mul.load_protocols(\n        \"http\", \"ssh\", \"dns\", \"tls\", \"xmpp\"\n    )\n    mul.set_rules(\n        {{\"time\", \"\"}, \"twitter.com\", 443}\n    )\n    mul.matcher_config.time = {\n        year_match = {2018},\n        year_not_match = {},\n        month_match = {{1}, {3}},\n        month_not_match = {},\n        day_match = {}, -- day of month\n        day_not_match = {},\n        hour_match = {{6, 24}},\n        hour_not_match = {{12}},\n        minute_match = {},\n        minute_not_match = {},\n        second_match = {},\n        second_not_match = {},\n    }\n }\n```\n\n[Back to TOC](#table-of-contents)\n\ndefault\n-------\n\nAlways matches.\n\n[Back to TOC](#table-of-contents)\n\nAdd new matcher\n---------------\n\nCreate a new `matcher_name.lua` file under `resty/multiplexer/matchers` in the format of:\n\n```lua\nlocal _M = {}\n\nfunction _M.match(protocol, expected)\n    -- return true if it's a match\nend\n\nreturn _M\n```\n\nWhere `protocol` is the identified protocol in lowercase string, and `expected` is the expected value for this matcher defined in `set_rules`.\n\n[Back to TOC](#table-of-contents)\n\n\nAPI\n=======\n\nmultiplexer.new\n--------------------------\n**syntax:** *multiplexer:new(connect_timeout, send_timeout, read_timeout)*\n\nInitialize the multiplexer instance. And sets the connect timeout thresold, send timeout threshold, and read timeout threshold, as in [tcpsock:settimeouts](https://github.com/openresty/lua-nginx-module#tcpsocksettimeouts).\n\n\n[Back to TOC](#table-of-contents)\n\nmultiplexer.load_protocols\n--------------------------\n**syntax:** *multiplexer:load_protocols(\"protocol-1\", \"protocol-2\", ...)*\n\nLoad the protocol modules into memory.\n\nSupported protocols can be found in [protocol](lib/resty/multiplexer/protocol).\n\n[Back to TOC](#table-of-contents)\n\nmultiplexer.set_rules\n--------------------------\n**syntax:** *multiplexer:set_rules(rule1, rule2, ...)*\n\nLoad rules in order. Each *rule* is an array table that is in the format of:\n\n```lua\n{{\"matcher-1\", \"expected-value-1\"}, {\"matcher-2\", \"expected-value-2\"}, ..., \"upstream_host\", upstream_port}\n```\n\nSupported matchers can be found in [matcher](lib/resty/multiplexer/matcher).\n\n[Back to TOC](#table-of-contents)\n\n\nTODO\n====\n\n- Add tests.\n\n[Back to TOC](#table-of-contents)\n\n\nCopyright and License\n=====================\n\nThis module is licensed under the BSD license.\n\nCopyright (C) 2018, by fffonion \u003cfffonion@gmail.com\u003e.\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n[Back to TOC](#table-of-contents)\n\nSee Also\n========\n* [Original patch to add the read partial mode](https://gist.github.com/fcicq/82e1c6d0c85cbc2d3f8e9f1523bfd1d1)\n* [stream-lua-nginx-module](https://github.com/openresty/stream-lua-nginx-module)\n* [yrutschle/sslh](https://github.com/yrutschle/sslh)\n\n[Back to TOC](#table-of-contents)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffffonion%2Flua-resty-multiplexer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffffonion%2Flua-resty-multiplexer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffffonion%2Flua-resty-multiplexer/lists"}