增加自动扫描

This commit is contained in:
iluobei
2026-04-07 16:35:45 +08:00
parent bd3c03d51d
commit 1415cc7fe0
5 changed files with 323 additions and 2 deletions
+6
View File
@@ -0,0 +1,6 @@
package handler
import _ "embed"
//go:embed xray_default_config.json
var defaultXrayConfig []byte
+31
View File
@@ -297,6 +297,9 @@ func (h *ManageHandler) HandleXrayInstall(w http.ResponseWriter, r *http.Request
log.Printf("[Manage] Xray installed successfully")
// Deploy default config if no config exists
h.deployDefaultXrayConfig()
writeJSON(w, http.StatusOK, map[string]interface{}{
"success": true,
"message": "Xray installed successfully",
@@ -2511,6 +2514,31 @@ func runCommand(name string, args ...string) error {
return nil
}
// deployDefaultXrayConfig deploys the embedded default xray config if no config exists.
func (h *ManageHandler) deployDefaultXrayConfig() {
configPath := "/usr/local/etc/xray/config.json"
if _, err := os.Stat(configPath); err == nil {
// Config already exists — run EnsureXrayConfig to add missing sections
result := h.EnsureXrayConfig()
if result.Modified {
log.Printf("[Manage] Xray config updated after install: added %v", result.AddedSections)
exec.Command("systemctl", "restart", "xray").Run()
}
return
}
if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
log.Printf("[Manage] Failed to create xray config dir: %v", err)
return
}
if err := os.WriteFile(configPath, defaultXrayConfig, 0644); err != nil {
log.Printf("[Manage] Failed to write default xray config: %v", err)
return
}
log.Printf("[Manage] Deployed default xray config to %s", configPath)
exec.Command("systemctl", "restart", "xray").Run()
}
// ================== SSE Streaming Install/Remove ==================
func sseStreamCmd(w http.ResponseWriter, r *http.Request, cmd *exec.Cmd, completeMsg string) {
@@ -2600,6 +2628,9 @@ func (h *ManageHandler) HandleXrayInstallStream(w http.ResponseWriter, r *http.R
`bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install`)
cmd.Env = os.Environ()
sseStreamCmd(w, r, cmd, "Xray installed successfully")
// Deploy default config after install
h.deployDefaultXrayConfig()
}
func (h *ManageHandler) HandleXrayRemoveStream(w http.ResponseWriter, r *http.Request) {
+104
View File
@@ -0,0 +1,104 @@
{
"log": {
"access": "/var/log/xray/access.log",
"error": "/var/log/xray/error.log",
"loglevel": "error"
},
"dns": {},
"api": {
"tag": "api",
"services": [
"HandlerService",
"LoggerService",
"StatsService",
"RoutingService"
]
},
"stats": {},
"policy": {
"levels": {
"0": {
"handshake": 5,
"connIdle": 300,
"uplinkOnly": 2,
"downlinkOnly": 2,
"statsUserUplink": true,
"statsUserDownlink": true
}
},
"system": {
"statsInboundUplink": true,
"statsInboundDownlink": true,
"statsOutboundUplink": true,
"statsOutboundDownlink": true
}
},
"routing": {
"domainStrategy": "IPIfNonMatch",
"rules": [
{
"type": "field",
"inboundTag": [
"api"
],
"outboundTag": "api"
},
{
"type": "field",
"protocol": [
"bittorrent"
],
"marktag": "ban_bt",
"outboundTag": "block"
},
{
"type": "field",
"ip": [
"geoip:cn"
],
"marktag": "ban_geoip_cn",
"outboundTag": "block"
},
{
"type": "field",
"domain": [
"geosite:openai"
],
"marktag": "fix_openai",
"outboundTag": "direct"
},
{
"type": "field",
"ip": [
"geoip:private"
],
"outboundTag": "block"
}
]
},
"inbounds": [
{
"tag": "api",
"port": 46736,
"listen": "127.0.0.1",
"protocol": "dokodemo-door",
"settings": {
"address": "127.0.0.1"
}
}
],
"outbounds": [
{
"tag": "direct",
"protocol": "freedom"
},
{
"tag": "block",
"protocol": "blackhole"
}
],
"metrics": {
"tag": "Metrics",
"listen": "127.0.0.1:38889"
}
}