2021-08-21 01:20:40 -04:00
|
|
|
//go:build !confonly
|
2021-03-06 12:48:39 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
2021-03-06 08:33:20 -05:00
|
|
|
package observatory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-08 15:56:04 -04:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-08-25 08:16:10 -04:00
|
|
|
"sort"
|
2021-04-08 15:56:04 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-03-06 11:26:15 -05:00
|
|
|
"github.com/golang/protobuf/proto"
|
2021-04-08 15:56:04 -04:00
|
|
|
|
2021-03-06 11:26:15 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
v2net "github.com/v2fly/v2ray-core/v4/common/net"
|
2021-03-07 07:14:58 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/session"
|
2021-03-06 08:33:20 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/signal/done"
|
2021-03-06 11:26:15 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/task"
|
2021-03-06 08:33:20 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/features/extension"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/outbound"
|
2021-03-06 11:26:15 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet/tagged"
|
2021-03-06 08:33:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Observer struct {
|
|
|
|
config *Config
|
|
|
|
ctx context.Context
|
|
|
|
|
|
|
|
statusLock sync.Mutex
|
2021-03-06 11:26:15 -05:00
|
|
|
status []*OutboundStatus
|
2021-03-06 08:33:20 -05:00
|
|
|
|
|
|
|
finished *done.Instance
|
|
|
|
|
|
|
|
ohm outbound.Manager
|
|
|
|
}
|
|
|
|
|
2021-03-06 11:26:15 -05:00
|
|
|
func (o *Observer) GetObservation(ctx context.Context) (proto.Message, error) {
|
|
|
|
return &ObservationResult{Status: o.status}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-06 08:33:20 -05:00
|
|
|
func (o *Observer) Type() interface{} {
|
|
|
|
return extension.ObservatoryType()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Observer) Start() error {
|
2021-05-01 10:28:23 -04:00
|
|
|
if o.config != nil && len(o.config.SubjectSelector) != 0 {
|
|
|
|
o.finished = done.New()
|
|
|
|
go o.background()
|
|
|
|
}
|
2021-03-06 08:33:20 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Observer) Close() error {
|
2021-05-01 10:28:23 -04:00
|
|
|
if o.finished != nil {
|
|
|
|
return o.finished.Close()
|
|
|
|
}
|
|
|
|
return nil
|
2021-03-06 08:33:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Observer) background() {
|
|
|
|
for !o.finished.Done() {
|
|
|
|
hs, ok := o.ohm.(outbound.HandlerSelector)
|
|
|
|
if !ok {
|
|
|
|
newError("outbound.Manager is not a HandlerSelector").WriteToLog()
|
|
|
|
return
|
|
|
|
}
|
2021-03-06 11:26:15 -05:00
|
|
|
|
2021-03-06 08:33:20 -05:00
|
|
|
outbounds := hs.Select(o.config.SubjectSelector)
|
2021-08-25 08:16:10 -04:00
|
|
|
sort.Strings(outbounds)
|
2021-03-06 08:33:20 -05:00
|
|
|
|
2021-03-06 11:26:15 -05:00
|
|
|
o.updateStatus(outbounds)
|
|
|
|
|
|
|
|
for _, v := range outbounds {
|
|
|
|
result := o.probe(v)
|
|
|
|
o.updateStatusForResult(v, &result)
|
|
|
|
if o.finished.Done() {
|
|
|
|
return
|
|
|
|
}
|
2021-06-30 14:33:37 -04:00
|
|
|
sleepTime := time.Second * 10
|
|
|
|
if o.config.ProbeInterval != 0 {
|
|
|
|
sleepTime = time.Duration(o.config.ProbeInterval)
|
|
|
|
}
|
|
|
|
time.Sleep(sleepTime)
|
2021-03-06 11:26:15 -05:00
|
|
|
}
|
2021-03-06 08:33:20 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-06 11:26:15 -05:00
|
|
|
|
2021-03-06 08:33:20 -05:00
|
|
|
func (o *Observer) updateStatus(outbounds []string) {
|
|
|
|
o.statusLock.Lock()
|
|
|
|
defer o.statusLock.Unlock()
|
2021-04-13 11:06:48 -04:00
|
|
|
// TODO should remove old inbound that is removed
|
|
|
|
_ = outbounds
|
2021-03-06 11:26:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Observer) probe(outbound string) ProbeResult {
|
2021-03-07 07:14:58 -05:00
|
|
|
errorCollectorForRequest := newErrorCollector()
|
|
|
|
|
2021-03-06 11:26:15 -05:00
|
|
|
httpTransport := http.Transport{
|
|
|
|
Proxy: func(*http.Request) (*url.URL, error) {
|
|
|
|
return nil, nil
|
|
|
|
},
|
|
|
|
DialContext: func(ctx context.Context, network string, addr string) (net.Conn, error) {
|
|
|
|
var connection net.Conn
|
|
|
|
taskErr := task.Run(ctx, func() error {
|
2021-04-13 11:06:48 -04:00
|
|
|
// MUST use V2Fly's built in context system
|
2021-03-06 11:26:15 -05:00
|
|
|
dest, err := v2net.ParseDestination(network + ":" + addr)
|
|
|
|
if err != nil {
|
|
|
|
return newError("cannot understand address").Base(err)
|
|
|
|
}
|
2021-03-07 07:14:58 -05:00
|
|
|
trackedCtx := session.TrackedConnectionError(o.ctx, errorCollectorForRequest)
|
|
|
|
conn, err := tagged.Dialer(trackedCtx, dest, outbound)
|
2021-03-06 11:26:15 -05:00
|
|
|
if err != nil {
|
2021-08-25 08:16:10 -04:00
|
|
|
return newError("cannot dial remote address ", dest).Base(err)
|
2021-03-06 11:26:15 -05:00
|
|
|
}
|
|
|
|
connection = conn
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if taskErr != nil {
|
|
|
|
return nil, newError("cannot finish connection").Base(taskErr)
|
|
|
|
}
|
|
|
|
return connection, nil
|
|
|
|
},
|
2021-04-13 11:06:48 -04:00
|
|
|
TLSHandshakeTimeout: time.Second * 5,
|
2021-03-06 11:26:15 -05:00
|
|
|
}
|
|
|
|
httpClient := &http.Client{
|
|
|
|
Transport: &httpTransport,
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
Jar: nil,
|
2021-04-13 11:06:48 -04:00
|
|
|
Timeout: time.Second * 5,
|
2021-03-06 11:26:15 -05:00
|
|
|
}
|
|
|
|
var GETTime time.Duration
|
|
|
|
err := task.Run(o.ctx, func() error {
|
|
|
|
startTime := time.Now()
|
2021-06-30 12:47:29 -04:00
|
|
|
probeURL := "https://api.v2fly.org/checkConnection.svgz"
|
|
|
|
if o.config.ProbeUrl != "" {
|
|
|
|
probeURL = o.config.ProbeUrl
|
|
|
|
}
|
|
|
|
response, err := httpClient.Get(probeURL)
|
2021-03-06 11:26:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return newError("outbound failed to relay connection").Base(err)
|
|
|
|
}
|
|
|
|
if response.Body != nil {
|
|
|
|
response.Body.Close()
|
|
|
|
}
|
|
|
|
endTime := time.Now()
|
|
|
|
GETTime = endTime.Sub(startTime)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-07 07:14:58 -05:00
|
|
|
fullerr := newError("underlying connection failed").Base(errorCollectorForRequest.UnderlyingError())
|
|
|
|
fullerr = newError("with outbound handler report").Base(fullerr)
|
|
|
|
fullerr = newError("GET request failed:", err).Base(fullerr)
|
2021-08-19 01:40:47 -04:00
|
|
|
fullerr = newError("the outbound ", outbound, " is dead:").Base(fullerr)
|
2021-03-07 07:14:58 -05:00
|
|
|
fullerr = fullerr.AtInfo()
|
|
|
|
fullerr.WriteToLog()
|
|
|
|
return ProbeResult{Alive: false, LastErrorReason: fullerr.Error()}
|
2021-03-06 11:26:15 -05:00
|
|
|
}
|
2021-08-25 08:16:10 -04:00
|
|
|
newError("the outbound ", outbound, " is alive:", GETTime.Seconds()).AtInfo().WriteToLog()
|
2021-03-06 11:26:15 -05:00
|
|
|
return ProbeResult{Alive: true, Delay: GETTime.Milliseconds()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Observer) updateStatusForResult(outbound string, result *ProbeResult) {
|
|
|
|
o.statusLock.Lock()
|
|
|
|
defer o.statusLock.Unlock()
|
|
|
|
var status *OutboundStatus
|
|
|
|
if location := o.findStatusLocationLockHolderOnly(outbound); location != -1 {
|
|
|
|
status = o.status[location]
|
|
|
|
} else {
|
|
|
|
status = &OutboundStatus{}
|
|
|
|
o.status = append(o.status, status)
|
|
|
|
}
|
|
|
|
|
|
|
|
status.LastTryTime = time.Now().Unix()
|
|
|
|
status.OutboundTag = outbound
|
|
|
|
status.Alive = result.Alive
|
|
|
|
if result.Alive {
|
|
|
|
status.Delay = result.Delay
|
|
|
|
status.LastSeenTime = status.LastTryTime
|
|
|
|
status.LastErrorReason = ""
|
|
|
|
} else {
|
|
|
|
status.LastErrorReason = result.LastErrorReason
|
|
|
|
status.Delay = 99999999
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Observer) findStatusLocationLockHolderOnly(outbound string) int {
|
|
|
|
for i, v := range o.status {
|
|
|
|
if v.OutboundTag == outbound {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(ctx context.Context, config *Config) (*Observer, error) {
|
|
|
|
var outboundManager outbound.Manager
|
|
|
|
err := core.RequireFeatures(ctx, func(om outbound.Manager) {
|
|
|
|
outboundManager = om
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("Cannot get depended features").Base(err)
|
|
|
|
}
|
|
|
|
return &Observer{
|
|
|
|
config: config,
|
|
|
|
ctx: ctx,
|
|
|
|
ohm: outboundManager,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-03-06 08:33:20 -05:00
|
|
|
|
2021-03-06 11:26:15 -05:00
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*Config))
|
|
|
|
}))
|
2021-03-06 08:33:20 -05:00
|
|
|
}
|