mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
fix: Release closed inbound connections
This commit is contained in:
@@ -273,7 +273,7 @@ replace github.com/sagernet/tailscale => github.com/shtorm-7/tailscale v1.102.1-
|
||||
|
||||
replace github.com/ameshkov/dnscrypt/v2 => github.com/shtorm-7/dnscrypt/v2 v2.4.0-extended-1.0.0
|
||||
|
||||
replace github.com/sagernet/sing-vmess => github.com/shtorm-7/sing-vmess v0.2.9-extended-1.0.1
|
||||
replace github.com/sagernet/sing-vmess => github.com/shtorm-7/sing-vmess v0.2.9-extended-1.0.2
|
||||
|
||||
replace github.com/dolonet/mtg-multi => github.com/shtorm-7/mtg-multi v1.12.0-extended-1.0.0
|
||||
|
||||
|
||||
@@ -509,8 +509,8 @@ github.com/shtorm-7/sing v0.9.7-extended-1.2.2 h1:Dc07BEGPMTPhxSDG0oubA14leinlu0
|
||||
github.com/shtorm-7/sing v0.9.7-extended-1.2.2/go.mod h1:K3Owt3xPhHugvlnlPPxZJ/exXdaJfEPOTNorGk4AXjo=
|
||||
github.com/shtorm-7/sing-mux v0.3.10-extended-1.1.1 h1:pOF6muUagOsK5kSLfUeqwPAmUVaGzY5BcpSYPOtAsrA=
|
||||
github.com/shtorm-7/sing-mux v0.3.10-extended-1.1.1/go.mod h1:8q3NgdR0tasq2fKqoul6jfK9UmxnFWLW8fIunD93Bdo=
|
||||
github.com/shtorm-7/sing-vmess v0.2.9-extended-1.0.1 h1:2EwuK5ZCTUtrO8KCxLfq62Rke3PApzSK8m6yazFA9Y0=
|
||||
github.com/shtorm-7/sing-vmess v0.2.9-extended-1.0.1/go.mod h1:P11scgTxMxVVQ8dlM27yNm3Cro40mD0+gHbnqrNGDuY=
|
||||
github.com/shtorm-7/sing-vmess v0.2.9-extended-1.0.2 h1:U7DMFfw3Dq9HJ1ulbJeifUsbiEwtqDc6Bf3GLGjl/i4=
|
||||
github.com/shtorm-7/sing-vmess v0.2.9-extended-1.0.2/go.mod h1:P11scgTxMxVVQ8dlM27yNm3Cro40mD0+gHbnqrNGDuY=
|
||||
github.com/shtorm-7/tailscale v1.102.1-sing-box-1.14-mod.5-extended-1.0.4 h1:aCKjNnACWCCWg693T5JQYVbzN/QwE7hzwscsZTXD9ZE=
|
||||
github.com/shtorm-7/tailscale v1.102.1-sing-box-1.14-mod.5-extended-1.0.4/go.mod h1:+1R9TnmCDbxb7h7ZOBKxi5UI6aWf+polOO/fc0WB/4o=
|
||||
github.com/shtorm-7/wireguard-go v0.0.8-extended-1.6.3 h1:mR6EP0tatNN793A5xzuNFH8fIuGnX8jFx4ZRCoE3EaM=
|
||||
|
||||
+1
-1
@@ -279,7 +279,7 @@ replace github.com/sagernet/tailscale => github.com/shtorm-7/tailscale v1.102.1-
|
||||
|
||||
replace github.com/ameshkov/dnscrypt/v2 => github.com/shtorm-7/dnscrypt/v2 v2.4.0-extended-1.0.0
|
||||
|
||||
replace github.com/sagernet/sing-vmess => github.com/shtorm-7/sing-vmess v0.2.9-extended-1.0.1
|
||||
replace github.com/sagernet/sing-vmess => github.com/shtorm-7/sing-vmess v0.2.9-extended-1.0.2
|
||||
|
||||
replace github.com/dolonet/mtg-multi => github.com/shtorm-7/mtg-multi v1.12.0-extended-1.0.0
|
||||
|
||||
|
||||
+2
-2
@@ -490,8 +490,8 @@ github.com/shtorm-7/sing v0.9.7-extended-1.2.2 h1:Dc07BEGPMTPhxSDG0oubA14leinlu0
|
||||
github.com/shtorm-7/sing v0.9.7-extended-1.2.2/go.mod h1:K3Owt3xPhHugvlnlPPxZJ/exXdaJfEPOTNorGk4AXjo=
|
||||
github.com/shtorm-7/sing-mux v0.3.10-extended-1.1.1 h1:pOF6muUagOsK5kSLfUeqwPAmUVaGzY5BcpSYPOtAsrA=
|
||||
github.com/shtorm-7/sing-mux v0.3.10-extended-1.1.1/go.mod h1:8q3NgdR0tasq2fKqoul6jfK9UmxnFWLW8fIunD93Bdo=
|
||||
github.com/shtorm-7/sing-vmess v0.2.9-extended-1.0.1 h1:2EwuK5ZCTUtrO8KCxLfq62Rke3PApzSK8m6yazFA9Y0=
|
||||
github.com/shtorm-7/sing-vmess v0.2.9-extended-1.0.1/go.mod h1:P11scgTxMxVVQ8dlM27yNm3Cro40mD0+gHbnqrNGDuY=
|
||||
github.com/shtorm-7/sing-vmess v0.2.9-extended-1.0.2 h1:U7DMFfw3Dq9HJ1ulbJeifUsbiEwtqDc6Bf3GLGjl/i4=
|
||||
github.com/shtorm-7/sing-vmess v0.2.9-extended-1.0.2/go.mod h1:P11scgTxMxVVQ8dlM27yNm3Cro40mD0+gHbnqrNGDuY=
|
||||
github.com/shtorm-7/tailscale v1.102.1-sing-box-1.14-mod.5-extended-1.0.4 h1:aCKjNnACWCCWg693T5JQYVbzN/QwE7hzwscsZTXD9ZE=
|
||||
github.com/shtorm-7/tailscale v1.102.1-sing-box-1.14-mod.5-extended-1.0.4/go.mod h1:+1R9TnmCDbxb7h7ZOBKxi5UI6aWf+polOO/fc0WB/4o=
|
||||
github.com/shtorm-7/wireguard-go v0.0.8-extended-1.6.3 h1:mR6EP0tatNN793A5xzuNFH8fIuGnX8jFx4ZRCoE3EaM=
|
||||
|
||||
+32
-34
@@ -76,7 +76,7 @@ func (s *Service[K]) UpdateUsers(userList []K, passwordList []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service[K]) NewConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, onClose N.CloseHandlerFunc) error {
|
||||
func (s *Service[K]) NewConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, onClose N.CloseHandlerFunc) (err error) {
|
||||
var key [KeyLength]byte
|
||||
n, err := conn.Read(key[:])
|
||||
if err != nil {
|
||||
@@ -84,69 +84,67 @@ func (s *Service[K]) NewConnection(ctx context.Context, conn net.Conn, source M.
|
||||
} else if n != KeyLength {
|
||||
return s.fallback(ctx, conn, source, key[:n], E.New("bad request size"), onClose)
|
||||
}
|
||||
|
||||
s.mu.RLock()
|
||||
if user, loaded := s.keys[key]; loaded {
|
||||
s.mu.RUnlock()
|
||||
ctx = auth.ContextWithUser(ctx, user)
|
||||
s.mu.Lock()
|
||||
s.conns[user] = append(s.conns[user], conn)
|
||||
s.mu.Lock()
|
||||
user, loaded := s.keys[key]
|
||||
if !loaded {
|
||||
s.mu.Unlock()
|
||||
originalOnClose := onClose
|
||||
onClose = N.OnceClose(func(it error) {
|
||||
s.mu.Lock()
|
||||
conns := s.conns[user]
|
||||
for i, c := range conns {
|
||||
if c == conn {
|
||||
s.conns[user] = append(conns[:i], conns[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
if originalOnClose != nil {
|
||||
originalOnClose(it)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
s.mu.RUnlock()
|
||||
return s.fallback(ctx, conn, source, key[:], E.New("bad request"), onClose)
|
||||
}
|
||||
|
||||
s.conns[user] = append(s.conns[user], conn)
|
||||
s.mu.Unlock()
|
||||
ctx = auth.ContextWithUser(ctx, user)
|
||||
removeConnection := N.OnceClose(func(error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
connections := s.conns[user]
|
||||
for index, connection := range connections {
|
||||
if connection != conn {
|
||||
continue
|
||||
}
|
||||
remaining := append(connections[:index], connections[index+1:]...)
|
||||
connections[len(remaining)] = nil
|
||||
if len(remaining) == 0 {
|
||||
delete(s.conns, user)
|
||||
} else {
|
||||
s.conns[user] = remaining
|
||||
}
|
||||
return
|
||||
}
|
||||
})
|
||||
defer func() {
|
||||
if err != nil {
|
||||
removeConnection(err)
|
||||
}
|
||||
}()
|
||||
onClose = N.OnceClose(N.AppendClose(onClose, removeConnection))
|
||||
err = rw.SkipN(conn, 2)
|
||||
if err != nil {
|
||||
return E.Cause(err, "skip crlf")
|
||||
}
|
||||
|
||||
var command byte
|
||||
err = binary.Read(conn, binary.BigEndian, &command)
|
||||
if err != nil {
|
||||
return E.Cause(err, "read command")
|
||||
}
|
||||
|
||||
switch command {
|
||||
case CommandTCP, CommandUDP, CommandMux:
|
||||
default:
|
||||
return E.New("unknown command ", command)
|
||||
}
|
||||
|
||||
// var destination M.Socksaddr
|
||||
destination, err := M.SocksaddrSerializer.ReadAddrPort(conn)
|
||||
if err != nil {
|
||||
return E.Cause(err, "read destination")
|
||||
}
|
||||
|
||||
err = rw.SkipN(conn, 2)
|
||||
if err != nil {
|
||||
return E.Cause(err, "skip crlf")
|
||||
}
|
||||
|
||||
switch command {
|
||||
case CommandTCP:
|
||||
s.handler.NewConnectionEx(ctx, conn, source, destination, onClose)
|
||||
case CommandUDP:
|
||||
s.handler.NewPacketConnectionEx(ctx, &PacketConn{Conn: conn}, source, destination, onClose)
|
||||
// case CommandMux:
|
||||
default:
|
||||
case CommandMux:
|
||||
return HandleMuxConnection(ctx, conn, source, s.handler, s.logger, onClose)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -62,24 +62,6 @@ func (s *Service) UpdateUsers(users map[string]string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) trackConn(username string, conn io.Closer) {
|
||||
s.mu.Lock()
|
||||
s.conns[username] = append(s.conns[username], conn)
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Service) untrackConn(username string, conn io.Closer) {
|
||||
s.mu.Lock()
|
||||
conns := s.conns[username]
|
||||
for i, c := range conns {
|
||||
if c == conn {
|
||||
s.conns[username] = append(conns[:i], conns[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Service) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
authorization := request.Header.Get("Proxy-Authorization")
|
||||
username, loaded := s.verify(authorization)
|
||||
@@ -167,6 +149,31 @@ func (s *Service) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) trackConn(username string, conn io.Closer) {
|
||||
s.mu.Lock()
|
||||
s.conns[username] = append(s.conns[username], conn)
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Service) untrackConn(username string, conn io.Closer) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
connections := s.conns[username]
|
||||
for index, connection := range connections {
|
||||
if connection != conn {
|
||||
continue
|
||||
}
|
||||
remaining := append(connections[:index], connections[index+1:]...)
|
||||
connections[len(remaining)] = nil
|
||||
if len(remaining) == 0 {
|
||||
delete(s.conns, username)
|
||||
} else {
|
||||
s.conns[username] = remaining
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) verify(authorization string) (username string, loaded bool) {
|
||||
username, password, loaded := parseBasicAuth(authorization)
|
||||
if !loaded {
|
||||
|
||||
Reference in New Issue
Block a user