add api checkOutbound #761

This commit is contained in:
Alireza Ahmadi
2026-02-11 21:52:31 +01:00
parent d996e7171b
commit 688e0c3e23
5 changed files with 61 additions and 0 deletions
+2
View File
@@ -97,6 +97,8 @@ func (a *APIHandler) getHandler(c *gin.Context) {
a.ApiService.GetTokens(c) a.ApiService.GetTokens(c)
case "singbox-config": case "singbox-config":
a.ApiService.GetSingboxConfig(c) a.ApiService.GetSingboxConfig(c)
case "checkOutbound":
a.ApiService.GetCheckOutbound(c)
default: default:
jsonMsg(c, "failed", common.NewError("unknown action: ", action)) jsonMsg(c, "failed", common.NewError("unknown action: ", action))
} }
+7
View File
@@ -396,3 +396,10 @@ func (a *ApiService) GetSingboxConfig(c *gin.Context) {
c.Header("Content-Disposition", "attachment; filename=config_"+time.Now().Format("20060102-150405")+".json") c.Header("Content-Disposition", "attachment; filename=config_"+time.Now().Format("20060102-150405")+".json")
c.Writer.Write(rawConfig) c.Writer.Write(rawConfig)
} }
func (a *ApiService) GetCheckOutbound(c *gin.Context) {
tag := c.Query("tag")
link := c.Query("link")
result := a.ConfigService.CheckOutbound(tag, link)
jsonObj(c, result, nil)
}
+2
View File
@@ -86,6 +86,8 @@ func (a *APIv2Handler) getHandler(c *gin.Context) {
a.ApiService.GetKeypairs(c) a.ApiService.GetKeypairs(c)
case "getdb": case "getdb":
a.ApiService.GetDb(c) a.ApiService.GetDb(c)
case "checkOutbound":
a.ApiService.GetCheckOutbound(c)
default: default:
jsonMsg(c, "failed", common.NewError("unknown action: ", action)) jsonMsg(c, "failed", common.NewError("unknown action: ", action))
} }
+40
View File
@@ -0,0 +1,40 @@
package core
import (
"context"
"time"
urltest "github.com/sagernet/sing-box/common/urltest"
)
const checkTimeout = 15 * time.Second
type CheckOutboundResult struct {
OK bool
Delay uint16
Error string
}
func CheckOutbound(ctx context.Context, tag string, link string) (result CheckOutboundResult) {
if outbound_manager == nil {
result.Error = "core not running"
return result
}
ob, ok := outbound_manager.Outbound(tag)
if !ok {
result.Error = "outbound not found"
return result
}
ctx, cancel := context.WithTimeout(ctx, checkTimeout)
defer cancel()
delay, err := urltest.URLTest(ctx, link, ob)
if err != nil {
result.Error = err.Error()
return result
}
result.OK = true
result.Delay = delay
return result
}
+10
View File
@@ -123,6 +123,16 @@ func (s *ConfigService) StopCore() error {
return nil return nil
} }
func (s *ConfigService) CheckOutbound(tag string, link string) core.CheckOutboundResult {
if tag == "" {
return core.CheckOutboundResult{Error: "missing query parameter: tag"}
}
if corePtr == nil || !corePtr.IsRunning() {
return core.CheckOutboundResult{Error: "core not running"}
}
return core.CheckOutbound(corePtr.GetCtx(), tag, link)
}
func (s *ConfigService) Save(obj string, act string, data json.RawMessage, initUsers string, loginUser string, hostname string) ([]string, error) { func (s *ConfigService) Save(obj string, act string, data json.RawMessage, initUsers string, loginUser string, hostname string) ([]string, error) {
var err error var err error
var objs []string = []string{obj} var objs []string = []string{obj}