sing内核支持面板rule规则

This commit is contained in:
wyx2685
2025-01-10 16:33:05 +09:00
parent c755e9800b
commit 2f1362067b
10 changed files with 74 additions and 93 deletions

View File

@@ -2,7 +2,7 @@ package sing
import (
"context"
"io"
"fmt"
"net"
"sync"
@@ -20,47 +20,16 @@ import (
var _ adapter.ConnectionTracker = (*HookServer)(nil)
type HookServer struct {
EnableConnClear bool
counter sync.Map
connClears sync.Map
}
type ConnClear struct {
lock sync.RWMutex
conns map[int]io.Closer
}
func (c *ConnClear) AddConn(cn io.Closer) (key int) {
c.lock.Lock()
defer c.lock.Unlock()
key = len(c.conns)
c.conns[key] = cn
return
}
func (c *ConnClear) DelConn(key int) {
c.lock.Lock()
defer c.lock.Unlock()
delete(c.conns, key)
}
func (c *ConnClear) ClearConn() {
c.lock.Lock()
defer c.lock.Unlock()
for _, c := range c.conns {
c.Close()
}
counter sync.Map
}
func (h *HookServer) ModeList() []string {
return nil
}
func NewHookServer(enableClear bool) *HookServer {
func NewHookServer() *HookServer {
server := &HookServer{
EnableConnClear: enableClear,
counter: sync.Map{},
connClears: sync.Map{},
counter: sync.Map{},
}
return server
}
@@ -79,14 +48,26 @@ func (h *HookServer) RoutedConnection(_ context.Context, conn net.Conn, m adapte
} else if b != nil {
conn = rate.NewConnRateLimiter(conn, b)
}
if h.EnableConnClear {
cc := &ConnClear{
conns: map[int]io.Closer{
0: conn,
},
if l != nil {
destStr := m.Destination.AddrString()
protocol := m.Destination.Network()
if l.CheckDomainRule(destStr) {
log.Error(fmt.Sprintf(
"User %s access domain %s reject by rule",
m.User,
destStr))
conn.Close()
return conn
}
if v, ok := h.connClears.LoadOrStore(m.Inbound+m.User, cc); ok {
cc = v.(*ConnClear)
if len(protocol) != 0 {
if l.CheckProtocolRule(protocol) {
log.Error(fmt.Sprintf(
"User %s access protocol %s reject by rule",
m.User,
protocol))
conn.Close()
return conn
}
}
}
if c, ok := h.counter.Load(m.Inbound); ok {
@@ -112,14 +93,26 @@ func (h *HookServer) RoutedPacketConnection(_ context.Context, conn N.PacketConn
} else if b != nil {
//conn = rate.NewPacketConnCounter(conn, b)
}
if h.EnableConnClear {
cc := &ConnClear{
conns: map[int]io.Closer{
0: conn,
},
if l != nil {
destStr := m.Destination.AddrString()
protocol := m.Destination.Network()
if l.CheckDomainRule(destStr) {
log.Error(fmt.Sprintf(
"User %s access domain %s reject by rule",
m.User,
destStr))
conn.Close()
return conn
}
if v, ok := h.connClears.LoadOrStore(m.Inbound+m.User, cc); ok {
cc = v.(*ConnClear)
if len(protocol) != 0 {
if l.CheckProtocolRule(protocol) {
log.Error(fmt.Sprintf(
"User %s access protocol %s reject by rule",
m.User,
protocol))
conn.Close()
return conn
}
}
}
if c, ok := h.counter.Load(m.Inbound); ok {
@@ -130,10 +123,3 @@ func (h *HookServer) RoutedPacketConnection(_ context.Context, conn N.PacketConn
return counter.NewPacketConnCounter(conn, c.GetCounter(m.User))
}
}
func (h *HookServer) ClearConn(inbound string, user string) {
if v, ok := h.connClears.Load(inbound + user); ok {
v.(*ConnClear).ClearConn()
h.connClears.Delete(inbound + user)
}
}

View File

@@ -71,7 +71,7 @@ func New(c *conf.CoreConfig) (vCore.Core, error) {
if err != nil {
return nil, err
}
hs := NewHookServer(c.SingConfig.EnableConnClear)
hs := NewHookServer()
b.Router().SetTracker(hs)
return &Sing{
ctx: b.Router().GetCtx(),

View File

@@ -22,27 +22,25 @@ func (b *Sing) AddUsers(p *core.AddUsersParams) (added int, err error) {
return 0, errors.New("the inbound not found")
}
switch p.NodeInfo.Type {
case "vmess", "vless":
if p.NodeInfo.Type == "vless" {
us := make([]option.VLESSUser, len(p.Users))
for i := range p.Users {
us[i] = option.VLESSUser{
Name: p.Users[i].Uuid,
Flow: p.VAllss.Flow,
UUID: p.Users[i].Uuid,
}
case "vless":
us := make([]option.VLESSUser, len(p.Users))
for i := range p.Users {
us[i] = option.VLESSUser{
Name: p.Users[i].Uuid,
Flow: p.VAllss.Flow,
UUID: p.Users[i].Uuid,
}
err = in.(*vless.Inbound).AddUsers(us)
} else {
us := make([]option.VMessUser, len(p.Users))
for i := range p.Users {
us[i] = option.VMessUser{
Name: p.Users[i].Uuid,
UUID: p.Users[i].Uuid,
}
}
err = in.(*vmess.Inbound).AddUsers(us)
}
err = in.(*vless.Inbound).AddUsers(us)
case "vmess":
us := make([]option.VMessUser, len(p.Users))
for i := range p.Users {
us[i] = option.VMessUser{
Name: p.Users[i].Uuid,
UUID: p.Users[i].Uuid,
}
}
err = in.(*vmess.Inbound).AddUsers(us)
case "shadowsocks":
us := make([]option.ShadowsocksUser, len(p.Users))
for i := range p.Users {
@@ -112,10 +110,10 @@ type UserDeleter interface {
DelUsers(uuid []string) error
}
func (b *Sing) DelUsers(users []panel.UserInfo, tag string) error {
func (b *Sing) DelUsers(users []panel.UserInfo, tag string, info *panel.NodeInfo) error {
var del UserDeleter
if i, found := b.box.Inbound().Get(tag); found {
switch i.Type() {
switch info.Type {
case "vmess":
del = i.(*vmess.Inbound)
case "vless":
@@ -134,7 +132,6 @@ func (b *Sing) DelUsers(users []panel.UserInfo, tag string) error {
}
uuids := make([]string, len(users))
for i := range users {
b.hookServer.ClearConn(tag, users[i].Uuid)
uuids[i] = users[i].Uuid
}
err := del.DelUsers(uuids)