-
Notifications
You must be signed in to change notification settings - Fork 0
check if client is using clientcontext tracker #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,14 +9,20 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package clientcontext | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stdbufio "bufio" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "bytes" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "net" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/sagernet/sing-box/adapter" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/sagernet/sing-box/log" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/sagernet/sing/common/buf" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/sagernet/sing/common/bufio" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/sagernet/sing/common/metadata" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| N "github.com/sagernet/sing/common/network" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/sagernet/sing/service" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -161,36 +167,68 @@ func (b *boundsRule) match(tag string) bool { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type readConn struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| net.Conn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx context.Context | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info *ClientInfo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info ClientInfo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger log.ContextLogger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| reader io.Reader | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| n int | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| readErr error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // newReadConn creates a readConn and reads client info from it. If successful, the info is stored | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // in the context. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func newReadConn(ctx context.Context, conn net.Conn, logger log.ContextLogger) net.Conn { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c := &readConn{Conn: conn, ctx: ctx} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info, err := c.readInfo() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.Error("reading client info ", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return conn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c := &readConn{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Conn: conn, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx: ctx, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| reader: conn, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger: logger, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err := c.readInfo(); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.Warn("reading client info: ", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| service.ContextWithPtr(ctx, info) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return c | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // readInfo reads and decodes client info, then sends an OK response. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (c *readConn) readInfo() (*ClientInfo, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (c *readConn) Read(b []byte) (n int, err error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if c.readErr != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return c.n, c.readErr | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return c.reader.Read(b) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // readInfo reads and decodes client info, then sends an HTTP 200 OK response. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (c *readConn) readInfo() error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var buf [32]byte | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| n, err := c.Conn.Read(buf[:]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c.readErr = err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c.n = n | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| reader := io.MultiReader(bytes.NewReader(buf[:n]), c.Conn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !bytes.HasPrefix(buf[:n], []byte("POST /clientinfo")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+202
to
+210
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var buf [32]byte | |
| n, err := c.Conn.Read(buf[:]) | |
| if err != nil { | |
| c.readErr = err | |
| c.n = n | |
| return err | |
| } | |
| reader := io.MultiReader(bytes.NewReader(buf[:n]), c.Conn) | |
| if !bytes.HasPrefix(buf[:n], []byte("POST /clientinfo")) { | |
| prefix := []byte("POST /clientinfo") | |
| buf := make([]byte, 0, 64) // start with a reasonable buffer size | |
| tmp := make([]byte, 32) | |
| var n int | |
| var err error | |
| for len(buf) < len(prefix) { | |
| n, err = c.Conn.Read(tmp) | |
| if n > 0 { | |
| buf = append(buf, tmp[:n]...) | |
| } | |
| if err != nil { | |
| if err != io.EOF || len(buf) < len(prefix) { | |
| c.readErr = err | |
| c.n = len(buf) | |
| return err | |
| } | |
| break | |
| } | |
| } | |
| reader := io.MultiReader(bytes.NewReader(buf), c.Conn) | |
| if len(buf) < len(prefix) || !bytes.HasPrefix(buf, prefix) { |
Uh oh!
There was an error while loading. Please reload this page.