基于 Yamux 的内网穿透简易实现 链接到标题 Server 链接到标题 模拟真实服务器,假设运行在内网环境,端口 8881。
package main import ( "fmt" "log" "net/http" ) func main() { http.HandleFunc("/hello", HelloHandler) fmt.Println("Server started at port 8881") log.Fatal(http.ListenAndServe(":8881", nil)) } func HelloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, there\n") } Hub 链接到标题 运行在公网环境的 Hub,用于 Agent 连接,并保持会话。端口 8882。
提供 session 管理机制,主要用来保存 Yamux session 和 Agent 对应关系。 每个内网可以运行多个 Agent,每次新建连接会从已有的 Agent session 列表中随机选择一个 session,并通过创建一个新的 Yamux Stream 机制复用连接。
type SessionManager interface { AddSession(key string, sess *yamux.