mirror of
https://github.com/InazumaV/Ratte.git
synced 2026-02-04 04:30:09 +00:00
update
add hook for handler change map to cmap for Trigger.hashs
This commit is contained in:
@@ -25,6 +25,7 @@ type Options struct {
|
||||
Panel string `json:"Panel"`
|
||||
Acme string `json:"Acme"`
|
||||
Cert Cert `json:"Cert"`
|
||||
Hook Hook `json:"Hook"`
|
||||
Expand map[string]interface{} `json:"Other"`
|
||||
}
|
||||
|
||||
@@ -41,6 +42,13 @@ type Cert struct {
|
||||
KeyPath string `json:"Key"`
|
||||
}
|
||||
|
||||
type Hook struct {
|
||||
BeforeAddNode string `json:"BeforeAddNode"`
|
||||
AfterAddNode string `json:"AfterAddNode"`
|
||||
BeforeDelNode string `json:"BeforeDelNode"`
|
||||
AfterDelNode string `json:"AfterDelNode"`
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
Name string `json:"Name"`
|
||||
Remote Remote `json:"-"`
|
||||
|
||||
3
go.mod
3
go.mod
@@ -5,10 +5,11 @@ go 1.22.0
|
||||
toolchain go1.23.1
|
||||
|
||||
require (
|
||||
github.com/Yuzuki616/Ratte-Interface v0.0.0-20240911204230-d6204ff1f3e0
|
||||
github.com/Yuzuki616/Ratte-Interface v0.0.0-20241025203935-a64517afc32b
|
||||
github.com/fsnotify/fsnotify v1.7.0
|
||||
github.com/go-acme/lego/v4 v4.18.0
|
||||
github.com/goccy/go-json v0.10.3
|
||||
github.com/orcaman/concurrent-map/v2 v2.0.1
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
github.com/spf13/cobra v1.8.0
|
||||
|
||||
8
go.sum
8
go.sum
@@ -64,10 +64,8 @@ github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXY
|
||||
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
|
||||
github.com/OpenDNS/vegadns2client v0.0.0-20180418235048-a3fa4a771d87 h1:xPMsUicZ3iosVPSIP7bW5EcGUzjiiMl1OYTe14y/R24=
|
||||
github.com/OpenDNS/vegadns2client v0.0.0-20180418235048-a3fa4a771d87/go.mod h1:iGLljf5n9GjT6kc0HBvyI1nOKnGQbNB66VzSNbK5iks=
|
||||
github.com/Yuzuki616/Ratte-Interface v0.0.0-20240911195516-cdd49f9593d6 h1:vHQZO/S9b6idTO9JDY74ISwHgw8NskrXJWe4BOxsppQ=
|
||||
github.com/Yuzuki616/Ratte-Interface v0.0.0-20240911195516-cdd49f9593d6/go.mod h1:oW6wbZfawdudu9VbTy2YIWoMNpfttYX8qcsM19oXCCI=
|
||||
github.com/Yuzuki616/Ratte-Interface v0.0.0-20240911204230-d6204ff1f3e0 h1:rsxhN/GRu3BJKsMckQOP3e+oRrRrAwILFtMEoNeQ34A=
|
||||
github.com/Yuzuki616/Ratte-Interface v0.0.0-20240911204230-d6204ff1f3e0/go.mod h1:oW6wbZfawdudu9VbTy2YIWoMNpfttYX8qcsM19oXCCI=
|
||||
github.com/Yuzuki616/Ratte-Interface v0.0.0-20241025203935-a64517afc32b h1:ipw6740pMlWCAxhoINURmXw+JwIOI+MhgRVx2eiReas=
|
||||
github.com/Yuzuki616/Ratte-Interface v0.0.0-20241025203935-a64517afc32b/go.mod h1:oW6wbZfawdudu9VbTy2YIWoMNpfttYX8qcsM19oXCCI=
|
||||
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
|
||||
github.com/akamai/AkamaiOPEN-edgegrid-golang v1.2.2 h1:F1j7z+/DKEsYqZNoxC6wvfmaiDneLsQOFQmuq9NADSY=
|
||||
github.com/akamai/AkamaiOPEN-edgegrid-golang v1.2.2/go.mod h1:QlXr/TrICfQ/ANa76sLeQyhAJyNR9sEcfNuZBkY9jgY=
|
||||
@@ -540,6 +538,8 @@ github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b h1:Ff
|
||||
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b/go.mod h1:AC62GU6hc0BrNm+9RK9VSiwa/EUe1bkIeFORAMcHvJU=
|
||||
github.com/oracle/oci-go-sdk/v65 v65.63.1 h1:dYL7sk9L1+C9LCmoq+zjPMNteuJJfk54YExq/4pV9xQ=
|
||||
github.com/oracle/oci-go-sdk/v65 v65.63.1/go.mod h1:IBEV9l1qBzUpo7zgGaRUhbB05BVfcDGYRFBCPlTcPp0=
|
||||
github.com/orcaman/concurrent-map/v2 v2.0.1 h1:jOJ5Pg2w1oeB6PeDurIYf6k9PQ+aTITr/6lP/L/zp6c=
|
||||
github.com/orcaman/concurrent-map/v2 v2.0.1/go.mod h1:9Eq3TG2oBe5FirmYWQfYO5iH1q0Jv47PLaNK++uCdOM=
|
||||
github.com/ovh/go-ovh v1.5.1 h1:P8O+7H+NQuFK9P/j4sFW5C0fvSS2DnHYGPwdVCp45wI=
|
||||
github.com/ovh/go-ovh v1.5.1/go.mod h1:cTVDnl94z4tl8pP1uZ/8jlVxntjSIf09bNcQ5TJSC7c=
|
||||
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
||||
|
||||
@@ -42,6 +42,16 @@ func New(
|
||||
|
||||
func (h *Handler) Close() error {
|
||||
if h.nodeAdded.Load() {
|
||||
err := h.execHookCmd(h.Hook.BeforeDelNode, h.nodeName)
|
||||
if err != nil {
|
||||
h.l.WithError(err).Warn("Exec before del node hook failed")
|
||||
}
|
||||
defer func() {
|
||||
err = h.execHookCmd(h.Hook.AfterDelNode, h.nodeName)
|
||||
if err != nil {
|
||||
h.l.WithError(err).Warn("Exec after del node hook failed")
|
||||
}
|
||||
}()
|
||||
return h.c.DelNode(h.nodeName)
|
||||
}
|
||||
return nil
|
||||
|
||||
18
handler/hook.go
Normal file
18
handler/hook.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func (h *Handler) execHookCmd(cmd string, ares ...string) error {
|
||||
c := exec.Command(cmd, ares...)
|
||||
c.Stdin = os.Stdin
|
||||
c.Stdout = os.Stdout
|
||||
c.Stderr = os.Stderr
|
||||
err := c.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -20,11 +20,34 @@ func (h *Handler) PullNodeHandle(n *panel.NodeInfo) error {
|
||||
return fmt.Errorf("create cert error: %w", err)
|
||||
}
|
||||
}
|
||||
err := h.c.AddNode(&core.AddNodeParams{
|
||||
var protocol, port string
|
||||
switch n.Type {
|
||||
case "vmess":
|
||||
protocol = "vmess"
|
||||
port = n.VMess.Port
|
||||
case "vless":
|
||||
protocol = "vless"
|
||||
port = n.VLess.Port
|
||||
case "shadowsocks":
|
||||
protocol = "shadowsocks"
|
||||
port = n.Shadowsocks.Port
|
||||
case "trojan":
|
||||
protocol = "trojana"
|
||||
port = n.Trojan.Port
|
||||
case "other":
|
||||
protocol = "other"
|
||||
port = n.Other.Port
|
||||
}
|
||||
err := h.execHookCmd(h.Hook.BeforeAddNode, h.nodeName, protocol, port)
|
||||
if err != nil {
|
||||
h.l.WithError(err).Error("Exec before add node hook failed")
|
||||
}
|
||||
err = h.c.AddNode(&core.AddNodeParams{
|
||||
NodeInfo: core.NodeInfo{
|
||||
CommonNodeInfo: params.CommonNodeInfo{
|
||||
Type: n.Type,
|
||||
VMess: n.VMess,
|
||||
VLess: n.VLess,
|
||||
Shadowsocks: n.Shadowsocks,
|
||||
Trojan: n.Trojan,
|
||||
Hysteria: n.Hysteria,
|
||||
@@ -43,6 +66,10 @@ func (h *Handler) PullNodeHandle(n *panel.NodeInfo) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("add node error: %w", err)
|
||||
}
|
||||
err = h.execHookCmd(h.Hook.AfterAddNode, h.nodeName, protocol, port)
|
||||
if err != nil {
|
||||
h.l.WithError(err).Warn("Exec after add node hook failed")
|
||||
}
|
||||
if h.nodeAdded.Load() {
|
||||
h.nodeAdded.Store(true)
|
||||
}
|
||||
|
||||
@@ -17,13 +17,13 @@ func (t *Trigger) addCronHandle(cron any, job cron.FuncJob) (cron.EntryID, error
|
||||
}
|
||||
|
||||
func (t *Trigger) hashEqualsOrStore(name, hash string) bool {
|
||||
if h, ok := t.hashs[name]; ok {
|
||||
if h, ok := t.hashs.Get(name); ok {
|
||||
if h == hash {
|
||||
return true
|
||||
}
|
||||
t.hashs[name] = hash
|
||||
t.hashs.Set(name, hash)
|
||||
} else {
|
||||
t.hashs[name] = hash
|
||||
t.hashs.Set(name, hash)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"Ratte/handler"
|
||||
"fmt"
|
||||
"github.com/Yuzuki616/Ratte-Interface/panel"
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -16,7 +17,7 @@ type Trigger struct {
|
||||
p panel.Panel
|
||||
remoteId int
|
||||
remoteC *conf.Remote
|
||||
hashs map[string]string
|
||||
hashs cmap.ConcurrentMap[string, string]
|
||||
}
|
||||
|
||||
func NewTrigger(
|
||||
|
||||
Reference in New Issue
Block a user