1 Star 0 Fork 0

simon/smallnest

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
tee.go 872 Bytes
Copy Edit Raw Blame History
simon authored 2021-09-04 14:58 +08:00 . 旧版
package serverplugin
import (
"io"
"net"
)
// TeeConnPlugin is a plugin that copy requests from clients and send to a io.Writer.
type TeeConnPlugin struct {
w io.Writer
}
func NewTeeConnPlugin(w io.Writer) *TeeConnPlugin {
return &TeeConnPlugin{w: w}
}
// Update can start a stream copy by setting a non-nil w.
// If you set a nil w, it doesn't copy stream.
func (plugin *TeeConnPlugin) Update(w io.Writer) {
plugin.w = w
}
// HandleConnAccept check ip.
func (plugin *TeeConnPlugin) HandleConnAccept(conn net.Conn) (net.Conn, bool) {
tc := &teeConn{conn, plugin.w}
return tc, true
}
type teeConn struct {
net.Conn
w io.Writer
}
func (t *teeConn) Read(p []byte) (n int, err error) {
n, err = t.Conn.Read(p)
if n > 0 && t.w != nil {
t.w.Write(p[:n])
// if _, err := t.w.Write(p[:n]); err != nil {
// return n, err //discard error
// }
}
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/simon_git_code/smallnest.git
git@gitee.com:simon_git_code/smallnest.git
simon_git_code
smallnest
smallnest
e483c3e07d35

Search