# 主要就是封装了一下asyncio 的网络
# 实例1 一个tcp server
```python
from l0n0ltcp.tcp_server import TcpServer
from l0n0ltcp.json_handler import JsonHandler, json_rpc
from l0n0ltcp.tcp_simple_proto import TcpSimpleProto
# TcpSimpleProto简介
# 3 byte(后面的数据包大小size) + 4byte(控制字段,可自定义) + (size - 4)的数据
# JsonHandler 简介
# Json handler 会将 数据解析为一个json对象
# 格式为{"name": 函数名, args: 参数列表, serial_id: 调用ID}
# 然后根据 name 获取函数,将 [会话ID] 和 args 传递给 获取的函数 然后调用
# 函数的返回值 会被发送回调用方
# json_rpc简介
# json_rpc 用于调用远程的函数
# 原理:
# 1.如果调用有返回值: json_rpc(send_msg, "remote_func", [123,321])
# 第一步: 发送 {"name": "remote_func", "args": [123,321], "serial_id": 1}
# 第二步: 生成一个future 放到一个dict里面 futures[serial_id] = future, 等待返回值
# 第三步: 接收方(比如JsonHandler)收到后 执行 "remote_func"。
# 发送返回值 {"name": 1, "args": "返回数据" }
# 第四步: 调用方接收到返回数据, 根据 fu = futures.get(name) 获取到 等待的future。
# fu.set_result(args)
# 第五步: 获取到结果
# 2.如果没有返回值
# 发送 {"name": "remote_func", "args": [123,321]}
# TCP handler
g_msg_handler = JsonHandler() # 函数对象,也可以是一个 **[异步函数]**
def tcp_handle(func): # tcp handler装饰器
g_msg_handler.regist_handler(func)
return func
@tcp_handle
async def remote_func(session_id, a, b):
print(a, b)
return {"aaa":"bbb"}
loop = asyncio.get_event_loop()
g_tcp_server = TcpServer("0.0.0.0", # 监听地址
12345, # 监听端口
g_msg_handler, # 用来处理数据的【异步】函数
TcpSimpleProto(), # 用来解析包的对象
1, # 心跳时间间隔
5, # 当有~次心跳检测没有检测到数据包,就认定为该链接已经断开。
loop) # asyncio loop
```
## 如何自定义协议
```python
import struct
from l0n0ltcp.tcp_proto_base import TcpProtoBase, StreamReader
class TcpSimpleProto(TcpProtoBase):
def make_heart_msg(self) -> bytes:
return b"\x00\x00\x00"
def build_msg(self, data: bytes) -> bytes:
data_len = len(data) + 4
size = data_len & 0xFFFF
count = (data_len >> 16) & 0xFF
header = struct.pack("!HBI", size, count, 0)
return header + data
async def read_msg(self, reader: StreamReader):
header = await reader.readexactly(3)
size, count = struct.unpack("!HB", header)
size = (count << 16) | size
if size <= 0:
return
return await reader.readexactly(size)
```
## 穿越GFW
```
1.服务器执行
usage: l0n0ltranssocks5 [-h] listenhost listenport password sock5port
创建SOCKS5服务器,并使用加密通道代理
positional arguments:
listenhost 监听host
listenport 监听端口
password 密钥
sock5port socks5监听端口(并不需要再启动一个socks5,自动启动一个socks5)
optional arguments:
-h, --help show this help message and exit
2.本地主机执行
usage: l0n0ltransclient [-h] serverhost serverport localhost localport password
创建本地代理服务器
positional arguments:
serverhost 服务器监听host
serverport 服务器监听端口
localhost 本地服务host
localport 本地服务端口
password 密钥
optional arguments:
-h, --help show this help message and exit
```
## socks5 服务器
### 代码
```python
from l0n0ltcp.tcp_server import TcpServer
from l0n0ltcp.socks5_callback import Socks5ServerCallback
from l0n0ltcp.socks5_proto import Socks5ServerProto
server = TcpServer('0.0.0.0', 1080,
Socks5ServerCallback(),
Socks5ServerProto(),
loop=asyncio.get_event_loop())
server.start()
asyncio.get_event_loop().run_forever()
```
### 命令行
```
usage: l0n0lsocks5 [-h] listenhost listenport
创建SOCKS5服务器(目前仅支持CONNECT命令)
positional arguments:
listenhost 监听host
listenport 监听端口
optional arguments:
-h, --help show this help message and exit
```
## 加密信道
### 代码
```python
from l0n0ltcp.tcp_simple_proto import TcpSimpleProto
from l0n0ltcp.trans_callback import TransServerCallback, TransLocalServerCallback
from l0n0ltcp.tcp_server import TcpServer
from l0n0ltcp.tcp_proto_base import TcpProtoBase
from l0n0ltcp.sym_enc_chacha20 import SymEncChaCha20
# 远端服务器
serverremote = TcpServer('0.0.0.0', 12343,
TransServerCallback('baidu.com', 443, SymEncChaCha20("123")),
TcpSimpleProto(),loop=asyncio.get_event_loop())
serverremote.start()
# 本地服务器
serverlocal = TcpServer('0.0.0.0', 12345,
TransLocalServerCallback('127.0.0.1', 12343, SymEncChaCha20("123")),
TcpProtoBase(),
loop=asyncio.get_event_loop())
serverlocal.start()
asyncio.get_event_loop().run_forever()
# 访问 localhost:12345 相当于访问 baidu.com 443 中间的过程是chacha20加密的
```
### 命令行
```
1.服务器
usage: l0n0ltransserver [-h] listenhost listenport password targethost targetport
创建加密信道服务器
positional arguments:
listenhost 监听host
listenport 监听端口
password 密钥
targethost 要代理的host
targetport 要代理的端口
optional arguments:
-h, --help show this help message and exit
2.客户端
usage: l0n0ltransclient [-h] serverhost serverport localhost localport password
创建本地代理服务器
positional arguments:
serverhost 服务器监听host
serverport 服务器监听端口
localhost 本地服务host
localport 本地服务端口
password 密钥
optional arguments:
-h, --help show this help message and exit
```
Raw data
{
"_id": null,
"home_page": "https://gitee.com/l00n00l/l0n0ltcp",
"name": "l0n0ltcp",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "l0n0l",
"author_email": "1038352856@qq.com",
"download_url": "https://files.pythonhosted.org/packages/7d/9f/573dd15926eecb4ba3d01c3a9aeb34a255b60b47ca9b993e2e6bbdd92570/l0n0ltcp-1.3.9.tar.gz",
"platform": null,
"description": "# \u4e3b\u8981\u5c31\u662f\u5c01\u88c5\u4e86\u4e00\u4e0basyncio \u7684\u7f51\u7edc\n\n# \u5b9e\u4f8b1 \u4e00\u4e2atcp server\n```python\nfrom l0n0ltcp.tcp_server import TcpServer\nfrom l0n0ltcp.json_handler import JsonHandler, json_rpc\nfrom l0n0ltcp.tcp_simple_proto import TcpSimpleProto\n\n# TcpSimpleProto\u7b80\u4ecb\n# 3 byte(\u540e\u9762\u7684\u6570\u636e\u5305\u5927\u5c0fsize) + 4byte(\u63a7\u5236\u5b57\u6bb5\uff0c\u53ef\u81ea\u5b9a\u4e49) + (size - 4)\u7684\u6570\u636e\n\n# JsonHandler \u7b80\u4ecb\n\n# Json handler \u4f1a\u5c06 \u6570\u636e\u89e3\u6790\u4e3a\u4e00\u4e2ajson\u5bf9\u8c61\n# \u683c\u5f0f\u4e3a{\"name\": \u51fd\u6570\u540d, args: \u53c2\u6570\u5217\u8868, serial_id: \u8c03\u7528ID}\n# \u7136\u540e\u6839\u636e name \u83b7\u53d6\u51fd\u6570\uff0c\u5c06 [\u4f1a\u8bddID] \u548c args \u4f20\u9012\u7ed9 \u83b7\u53d6\u7684\u51fd\u6570 \u7136\u540e\u8c03\u7528\n# \u51fd\u6570\u7684\u8fd4\u56de\u503c \u4f1a\u88ab\u53d1\u9001\u56de\u8c03\u7528\u65b9\n\n# json_rpc\u7b80\u4ecb\n# json_rpc \u7528\u4e8e\u8c03\u7528\u8fdc\u7a0b\u7684\u51fd\u6570\n# \u539f\u7406\uff1a\n# 1.\u5982\u679c\u8c03\u7528\u6709\u8fd4\u56de\u503c\uff1a json_rpc(send_msg, \"remote_func\", [123,321])\n# \u7b2c\u4e00\u6b65\uff1a \u53d1\u9001 {\"name\": \"remote_func\", \"args\": [123,321], \"serial_id\": 1}\n# \u7b2c\u4e8c\u6b65\uff1a \u751f\u6210\u4e00\u4e2afuture \u653e\u5230\u4e00\u4e2adict\u91cc\u9762 futures[serial_id] = future, \u7b49\u5f85\u8fd4\u56de\u503c\n# \u7b2c\u4e09\u6b65\uff1a \u63a5\u6536\u65b9\uff08\u6bd4\u5982JsonHandler\uff09\u6536\u5230\u540e \u6267\u884c \"remote_func\"\u3002\n# \u53d1\u9001\u8fd4\u56de\u503c {\"name\": 1, \"args\": \"\u8fd4\u56de\u6570\u636e\" }\n# \u7b2c\u56db\u6b65\uff1a \u8c03\u7528\u65b9\u63a5\u6536\u5230\u8fd4\u56de\u6570\u636e\uff0c \u6839\u636e fu = futures.get(name) \u83b7\u53d6\u5230 \u7b49\u5f85\u7684future\u3002\n# fu.set_result(args) \n# \u7b2c\u4e94\u6b65\uff1a \u83b7\u53d6\u5230\u7ed3\u679c\n# 2.\u5982\u679c\u6ca1\u6709\u8fd4\u56de\u503c\n# \u53d1\u9001 {\"name\": \"remote_func\", \"args\": [123,321]}\n\n\n\n# TCP handler\ng_msg_handler = JsonHandler() # \u51fd\u6570\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a **[\u5f02\u6b65\u51fd\u6570]**\ndef tcp_handle(func): # tcp handler\u88c5\u9970\u5668\n g_msg_handler.regist_handler(func)\n return func\n\n\n@tcp_handle\nasync def remote_func(session_id, a, b):\n print(a, b)\n return {\"aaa\":\"bbb\"}\n\n\nloop = asyncio.get_event_loop()\ng_tcp_server = TcpServer(\"0.0.0.0\", # \u76d1\u542c\u5730\u5740\n 12345, # \u76d1\u542c\u7aef\u53e3\n g_msg_handler, # \u7528\u6765\u5904\u7406\u6570\u636e\u7684\u3010\u5f02\u6b65\u3011\u51fd\u6570\n TcpSimpleProto(), # \u7528\u6765\u89e3\u6790\u5305\u7684\u5bf9\u8c61\n 1, # \u5fc3\u8df3\u65f6\u95f4\u95f4\u9694\n 5, # \u5f53\u6709~\u6b21\u5fc3\u8df3\u68c0\u6d4b\u6ca1\u6709\u68c0\u6d4b\u5230\u6570\u636e\u5305\uff0c\u5c31\u8ba4\u5b9a\u4e3a\u8be5\u94fe\u63a5\u5df2\u7ecf\u65ad\u5f00\u3002\n loop) # asyncio loop\n```\n\n## \u5982\u4f55\u81ea\u5b9a\u4e49\u534f\u8bae\n```python\nimport struct\nfrom l0n0ltcp.tcp_proto_base import TcpProtoBase, StreamReader\n\nclass TcpSimpleProto(TcpProtoBase):\n def make_heart_msg(self) -> bytes:\n return b\"\\x00\\x00\\x00\"\n\n def build_msg(self, data: bytes) -> bytes:\n data_len = len(data) + 4\n size = data_len & 0xFFFF\n count = (data_len >> 16) & 0xFF\n header = struct.pack(\"!HBI\", size, count, 0)\n return header + data\n\n async def read_msg(self, reader: StreamReader):\n header = await reader.readexactly(3)\n size, count = struct.unpack(\"!HB\", header)\n size = (count << 16) | size\n if size <= 0:\n return\n return await reader.readexactly(size)\n```\n\n## \u7a7f\u8d8aGFW\n\n```\n1.\u670d\u52a1\u5668\u6267\u884c\nusage: l0n0ltranssocks5 [-h] listenhost listenport password sock5port\n\n\u521b\u5efaSOCKS5\u670d\u52a1\u5668,\u5e76\u4f7f\u7528\u52a0\u5bc6\u901a\u9053\u4ee3\u7406\n\npositional arguments:\n listenhost \u76d1\u542chost\n listenport \u76d1\u542c\u7aef\u53e3\n password \u5bc6\u94a5\n sock5port socks5\u76d1\u542c\u7aef\u53e3(\u5e76\u4e0d\u9700\u8981\u518d\u542f\u52a8\u4e00\u4e2asocks5,\u81ea\u52a8\u542f\u52a8\u4e00\u4e2asocks5)\n\noptional arguments:\n -h, --help show this help message and exit\n\n2.\u672c\u5730\u4e3b\u673a\u6267\u884c \nusage: l0n0ltransclient [-h] serverhost serverport localhost localport password\n\n\u521b\u5efa\u672c\u5730\u4ee3\u7406\u670d\u52a1\u5668\n\npositional arguments:\n serverhost \u670d\u52a1\u5668\u76d1\u542chost\n serverport \u670d\u52a1\u5668\u76d1\u542c\u7aef\u53e3\n localhost \u672c\u5730\u670d\u52a1host\n localport \u672c\u5730\u670d\u52a1\u7aef\u53e3\n password \u5bc6\u94a5\n\noptional arguments:\n -h, --help show this help message and exit\n```\n\n## socks5 \u670d\u52a1\u5668\n### \u4ee3\u7801\n```python\nfrom l0n0ltcp.tcp_server import TcpServer\nfrom l0n0ltcp.socks5_callback import Socks5ServerCallback\nfrom l0n0ltcp.socks5_proto import Socks5ServerProto\nserver = TcpServer('0.0.0.0', 1080,\n Socks5ServerCallback(),\n Socks5ServerProto(),\n loop=asyncio.get_event_loop())\nserver.start()\nasyncio.get_event_loop().run_forever()\n```\n### \u547d\u4ee4\u884c\n```\nusage: l0n0lsocks5 [-h] listenhost listenport\n\n\u521b\u5efaSOCKS5\u670d\u52a1\u5668(\u76ee\u524d\u4ec5\u652f\u6301CONNECT\u547d\u4ee4)\n\npositional arguments:\n listenhost \u76d1\u542chost\n listenport \u76d1\u542c\u7aef\u53e3\n\noptional arguments:\n -h, --help show this help message and exit\n```\n\n## \u52a0\u5bc6\u4fe1\u9053\n\n### \u4ee3\u7801\n```python\nfrom l0n0ltcp.tcp_simple_proto import TcpSimpleProto\nfrom l0n0ltcp.trans_callback import TransServerCallback, TransLocalServerCallback\nfrom l0n0ltcp.tcp_server import TcpServer\nfrom l0n0ltcp.tcp_proto_base import TcpProtoBase\nfrom l0n0ltcp.sym_enc_chacha20 import SymEncChaCha20\n# \u8fdc\u7aef\u670d\u52a1\u5668\nserverremote = TcpServer('0.0.0.0', 12343,\n TransServerCallback('baidu.com', 443, SymEncChaCha20(\"123\")),\n TcpSimpleProto(),loop=asyncio.get_event_loop())\nserverremote.start()\n\n# \u672c\u5730\u670d\u52a1\u5668\nserverlocal = TcpServer('0.0.0.0', 12345,\n TransLocalServerCallback('127.0.0.1', 12343, SymEncChaCha20(\"123\")),\n TcpProtoBase(),\n loop=asyncio.get_event_loop())\nserverlocal.start()\n\nasyncio.get_event_loop().run_forever()\n\n# \u8bbf\u95ee localhost:12345 \u76f8\u5f53\u4e8e\u8bbf\u95ee baidu.com 443 \u4e2d\u95f4\u7684\u8fc7\u7a0b\u662fchacha20\u52a0\u5bc6\u7684\n```\n\n### \u547d\u4ee4\u884c\n```\n1.\u670d\u52a1\u5668\nusage: l0n0ltransserver [-h] listenhost listenport password targethost targetport\n\n\u521b\u5efa\u52a0\u5bc6\u4fe1\u9053\u670d\u52a1\u5668\n\npositional arguments:\n listenhost \u76d1\u542chost\n listenport \u76d1\u542c\u7aef\u53e3\n password \u5bc6\u94a5\n targethost \u8981\u4ee3\u7406\u7684host\n targetport \u8981\u4ee3\u7406\u7684\u7aef\u53e3\n\noptional arguments:\n -h, --help show this help message and exit\n\n2.\u5ba2\u6237\u7aef\nusage: l0n0ltransclient [-h] serverhost serverport localhost localport password\n\n\u521b\u5efa\u672c\u5730\u4ee3\u7406\u670d\u52a1\u5668\n\npositional arguments:\n serverhost \u670d\u52a1\u5668\u76d1\u542chost\n serverport \u670d\u52a1\u5668\u76d1\u542c\u7aef\u53e3\n localhost \u672c\u5730\u670d\u52a1host\n localport \u672c\u5730\u670d\u52a1\u7aef\u53e3\n password \u5bc6\u94a5\n\noptional arguments:\n -h, --help show this help message and exit\n```\n",
"bugtrack_url": null,
"license": "",
"summary": "\u5bf9asyncio tcp\u7684\u4e00\u4e9b\u6613\u7528\u6027\u5c01\u88c5",
"version": "1.3.9",
"project_urls": {
"Homepage": "https://gitee.com/l00n00l/l0n0ltcp"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "92817f9c0d05d36c06401e68ce217563a4f93407bed6db9b2917dd047affa37d",
"md5": "8fdc7d73d0fc9ffffe50061e18205782",
"sha256": "0b34c594ffa6919327ce2b55aa261e7794ad43f6837c2ced9d5c14d8f06d927d"
},
"downloads": -1,
"filename": "l0n0ltcp-1.3.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8fdc7d73d0fc9ffffe50061e18205782",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 18187,
"upload_time": "2023-12-02T06:50:44",
"upload_time_iso_8601": "2023-12-02T06:50:44.743546Z",
"url": "https://files.pythonhosted.org/packages/92/81/7f9c0d05d36c06401e68ce217563a4f93407bed6db9b2917dd047affa37d/l0n0ltcp-1.3.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7d9f573dd15926eecb4ba3d01c3a9aeb34a255b60b47ca9b993e2e6bbdd92570",
"md5": "c853f2d3d1b3ebc9f59d68c35480a109",
"sha256": "f3ab9dac52d09773cbf1ca509b36d36acba6aa0693c841040a4cd3b2177de91a"
},
"downloads": -1,
"filename": "l0n0ltcp-1.3.9.tar.gz",
"has_sig": false,
"md5_digest": "c853f2d3d1b3ebc9f59d68c35480a109",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 17218,
"upload_time": "2023-12-02T06:50:46",
"upload_time_iso_8601": "2023-12-02T06:50:46.650912Z",
"url": "https://files.pythonhosted.org/packages/7d/9f/573dd15926eecb4ba3d01c3a9aeb34a255b60b47ca9b993e2e6bbdd92570/l0n0ltcp-1.3.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-02 06:50:46",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "l0n0ltcp"
}