mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-03 20:07:38 +03:00
More contetxt in connection
This commit is contained in:
parent
f16dd7a336
commit
5be6eb2d64
11 changed files with 33 additions and 27 deletions
|
@ -21,7 +21,7 @@ type Handler interface {
|
|||
tcp.Handler
|
||||
}
|
||||
|
||||
func HandleRequest(request *http.Request, conn net.Conn, authenticator auth.Authenticator, handler Handler, metadata M.Metadata) error {
|
||||
func HandleRequest(ctx context.Context, request *http.Request, conn net.Conn, authenticator auth.Authenticator, handler Handler, metadata M.Metadata) error {
|
||||
var httpClient *http.Client
|
||||
for {
|
||||
if authenticator != nil {
|
||||
|
@ -56,7 +56,7 @@ func HandleRequest(request *http.Request, conn net.Conn, authenticator auth.Auth
|
|||
return E.Cause(err, "write http response")
|
||||
}
|
||||
metadata.Destination = destination
|
||||
return handler.NewConnection(conn, metadata)
|
||||
return handler.NewConnection(ctx, conn, metadata)
|
||||
}
|
||||
|
||||
keepAlive := strings.TrimSpace(strings.ToLower(request.Header.Get("Proxy-Connection"))) == "keep-alive"
|
||||
|
@ -96,7 +96,7 @@ func HandleRequest(request *http.Request, conn net.Conn, authenticator auth.Auth
|
|||
go func() {
|
||||
metadata.Destination = destination
|
||||
metadata.Protocol = "http"
|
||||
err = handler.NewConnection(right, metadata)
|
||||
err = handler.NewConnection(ctx, right, metadata)
|
||||
if err != nil {
|
||||
handler.HandleError(&tcp.Error{Conn: right, Cause: err})
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package shadowsocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
|
@ -31,12 +32,12 @@ func NewNoneService(handler Handler) Service {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *NoneService) NewConnection(conn net.Conn, metadata M.Metadata) error {
|
||||
func (s *NoneService) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
||||
destination, err := socks.AddressSerializer.ReadAddrPort(conn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metadata.Protocol = "shadowsocks"
|
||||
metadata.Destination = destination
|
||||
return s.handler.NewConnection(conn, metadata)
|
||||
return s.handler.NewConnection(ctx, conn, metadata)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package shadowaead
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/cipher"
|
||||
"io"
|
||||
"net"
|
||||
|
@ -73,7 +74,7 @@ func NewService(method string, key []byte, password []byte, secureRNG io.Reader,
|
|||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Service) NewConnection(conn net.Conn, metadata M.Metadata) error {
|
||||
func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
||||
_salt := buf.Make(s.keySaltLength)
|
||||
salt := common.Dup(_salt)
|
||||
|
||||
|
@ -92,7 +93,7 @@ func (s *Service) NewConnection(conn net.Conn, metadata M.Metadata) error {
|
|||
metadata.Protocol = "shadowsocks"
|
||||
metadata.Destination = destination
|
||||
|
||||
return s.handler.NewConnection(&serverConn{
|
||||
return s.handler.NewConnection(ctx, &serverConn{
|
||||
Service: s,
|
||||
Conn: conn,
|
||||
reader: reader,
|
||||
|
@ -153,7 +154,7 @@ func (c *serverConn) Write(p []byte) (n int, err error) {
|
|||
}
|
||||
|
||||
func (c *serverConn) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
if c.writer != nil {
|
||||
if c.writer == nil {
|
||||
return rw.ReadFrom0(c, r)
|
||||
}
|
||||
return c.writer.ReadFrom(r)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package shadowaead_2022
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/cipher"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
@ -61,7 +62,7 @@ func NewService(method string, psk []byte, secureRNG io.Reader, handler shadowso
|
|||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Service) NewConnection(conn net.Conn, metadata M.Metadata) error {
|
||||
func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
||||
requestSalt := make([]byte, KeySaltSize)
|
||||
_, err := io.ReadFull(conn, requestSalt)
|
||||
if err != nil {
|
||||
|
@ -117,7 +118,7 @@ func (s *Service) NewConnection(conn net.Conn, metadata M.Metadata) error {
|
|||
|
||||
metadata.Protocol = "shadowsocks"
|
||||
metadata.Destination = destination
|
||||
return s.handler.NewConnection(&serverConn{
|
||||
return s.handler.NewConnection(ctx, &serverConn{
|
||||
Service: s,
|
||||
Conn: conn,
|
||||
reader: reader,
|
||||
|
@ -184,7 +185,7 @@ func (c *serverConn) Write(p []byte) (n int, err error) {
|
|||
}
|
||||
|
||||
func (c *serverConn) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
if c.writer != nil {
|
||||
if c.writer == nil {
|
||||
return rw.ReadFrom0(c, r)
|
||||
}
|
||||
return c.writer.ReadFrom(r)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package socks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"net/netip"
|
||||
|
@ -34,8 +35,8 @@ func NewListener(bind netip.AddrPort, authenticator auth.Authenticator, handler
|
|||
return listener
|
||||
}
|
||||
|
||||
func (l *Listener) NewConnection(conn net.Conn, metadata M.Metadata) error {
|
||||
return HandleConnection(conn, l.authenticator, l.bindAddr, l.handler, metadata)
|
||||
func (l *Listener) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
||||
return HandleConnection(ctx, conn, l.authenticator, l.bindAddr, l.handler, metadata)
|
||||
}
|
||||
|
||||
func (l *Listener) Start() error {
|
||||
|
@ -50,7 +51,7 @@ func (l *Listener) HandleError(err error) {
|
|||
l.handler.HandleError(err)
|
||||
}
|
||||
|
||||
func HandleConnection(conn net.Conn, authenticator auth.Authenticator, bind netip.Addr, handler Handler, metadata M.Metadata) error {
|
||||
func HandleConnection(ctx context.Context, conn net.Conn, authenticator auth.Authenticator, bind netip.Addr, handler Handler, metadata M.Metadata) error {
|
||||
authRequest, err := ReadAuthRequest(conn)
|
||||
if err != nil {
|
||||
return E.Cause(err, "read socks auth request")
|
||||
|
@ -111,7 +112,7 @@ func HandleConnection(conn net.Conn, authenticator auth.Authenticator, bind neti
|
|||
}
|
||||
metadata.Protocol = "socks"
|
||||
metadata.Destination = request.Destination
|
||||
return handler.NewConnection(conn, metadata)
|
||||
return handler.NewConnection(ctx, conn, metadata)
|
||||
case CommandUDPAssociate:
|
||||
network := "udp"
|
||||
if bind.Is4() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue