mirror of
https://github.com/v2rayA/v2rayA.git
synced 2026-11-20 23:02:25 +03:00
131 lines
3.4 KiB
Go
131 lines
3.4 KiB
Go
package v2ray
|
|
|
|
import (
|
|
"net"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/go-leo/slicex"
|
|
"github.com/v2rayA/v2rayA/db/configure"
|
|
"github.com/v2rayA/v2rayA/kernel/coreObj"
|
|
"github.com/v2rayA/v2rayA/kernel/v2ray/where"
|
|
"github.com/v2rayA/v2rayA/pkg/util/log"
|
|
)
|
|
|
|
func (t *Template) SetAPI(serverData *ServerData) (port int, err error) {
|
|
// find a valid port
|
|
config := configure.GetPortsNotNil()
|
|
if config.Api.Port != 0 {
|
|
port = config.Api.Port
|
|
} else {
|
|
for {
|
|
if l, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
|
|
port = l.Addr().(*net.TCPAddr).Port
|
|
_ = l.Close()
|
|
break
|
|
}
|
|
time.Sleep(30 * time.Millisecond)
|
|
}
|
|
}
|
|
services := []string{
|
|
"LoggerService",
|
|
"StatsService",
|
|
}
|
|
services = slicex.Uniq(append(services, config.Api.Services...))
|
|
// observatory
|
|
if serverData != nil {
|
|
outbounds := t.outNames()
|
|
for outbound, isGroup := range outbounds {
|
|
if !isGroup {
|
|
continue
|
|
}
|
|
|
|
strategy := serverData.OutboundName2Setting[outbound].Type
|
|
interval, err := time.ParseDuration(serverData.OutboundName2Setting[outbound].ProbeInterval)
|
|
if err != nil {
|
|
log.Warn("observatory: %v", err)
|
|
interval = 10 * time.Second
|
|
}
|
|
var selector []string
|
|
|
|
for _, vi := range serverData.OutboundName2ServerObjs[outbound] {
|
|
selector = append(selector, GroupWrapper(vi.GetName()))
|
|
}
|
|
// Keep fallback inside this group, never the global default or direct.
|
|
fallback := "block"
|
|
if len(selector) > 0 {
|
|
fallback = selector[0]
|
|
}
|
|
|
|
t.Routing.Balancers = append(t.Routing.Balancers, coreObj.Balancer{
|
|
Tag: outbound,
|
|
Selector: selector,
|
|
FallbackTag: fallback,
|
|
Strategy: coreObj.BalancerStrategy{
|
|
Type: strategy.String(),
|
|
},
|
|
})
|
|
|
|
probeUrl := serverData.OutboundName2Setting[outbound].ProbeURL
|
|
if _, err := url.Parse(probeUrl); err != nil {
|
|
log.Warn("observatory: %v", err)
|
|
probeUrl = "https://gstatic.com/generate_204"
|
|
}
|
|
|
|
// An explicit fallback enables health filtering for random and round-robin.
|
|
if t.MultiObservatory == nil {
|
|
t.MultiObservatory = &coreObj.MultiObservatory{}
|
|
}
|
|
t.MultiObservatory.Observers = append(t.MultiObservatory.Observers, coreObj.ObservatoryItem{
|
|
Tag: outbound,
|
|
Settings: coreObj.Observatory{
|
|
SubjectSelector: selector,
|
|
ProbeURL: probeUrl,
|
|
ProbeInterval: interval.String(),
|
|
},
|
|
})
|
|
}
|
|
if t.MultiObservatory != nil || t.Observatory != nil {
|
|
// v2raya_core supports ObservatoryService via the v2ray-compat gRPC path.
|
|
if t.Variant == where.V2rayaCore {
|
|
|
|
var observatoryTags []string
|
|
for name, isGroup := range t.outNames() {
|
|
if isGroup {
|
|
observatoryTags = append(observatoryTags, name)
|
|
}
|
|
}
|
|
t.ApiCloses = append(t.ApiCloses, ObservatoryProducer(port, observatoryTags))
|
|
}
|
|
}
|
|
}
|
|
t.API = &coreObj.APIObject{
|
|
Tag: "api-out",
|
|
Services: services,
|
|
}
|
|
t.Stats = &struct{}{}
|
|
if t.Policy == nil {
|
|
t.Policy = &coreObj.Policy{}
|
|
}
|
|
t.Policy.System.StatsOutboundUplink = true
|
|
t.Policy.System.StatsOutboundDownlink = true
|
|
t.ApiCloses = append(t.ApiCloses, TrafficProducer(port))
|
|
|
|
t.Inbounds = append(t.Inbounds, coreObj.Inbound{
|
|
Port: port,
|
|
Protocol: "dokodemo-door",
|
|
Listen: "127.0.0.1",
|
|
Settings: &coreObj.InboundSettings{
|
|
Address: "127.0.0.1",
|
|
},
|
|
Tag: "api-in",
|
|
})
|
|
t.Routing.Rules = append(t.Routing.Rules, coreObj.RoutingRule{
|
|
Type: "field",
|
|
InboundTag: []string{"api-in"},
|
|
OutboundTag: "api-out",
|
|
})
|
|
t.ApiPort = port
|
|
return port, nil
|
|
}
|