mirror of
https://github.com/v2rayA/v2rayA.git
synced 2026-11-20 23:02:25 +03:00
feat: add websocket api for observatory messages
This commit is contained in:
1 parent
6d8681dc7a
commit
bd05c0e90c
8 files changed
+150
-11
No files matched your search
@@ -3,7 +3,10 @@ package v2ray
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/devfeel/mapper"
|
||||
"github.com/v2fly/v2ray-core/v4/app/observatory"
|
||||
pb "github.com/v2fly/v2ray-core/v4/app/observatory/command"
|
||||
"github.com/v2rayA/v2rayA/db/configure"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
@@ -14,7 +17,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
Products = []string{
|
||||
ApiProducts = []string{
|
||||
"observatory",
|
||||
}
|
||||
ApiFeed *Feed
|
||||
@@ -22,9 +25,21 @@ var (
|
||||
|
||||
const ApiFeedBoxSize = 10
|
||||
|
||||
type OutboundStatus struct {
|
||||
Alive bool `json:"alive,omitempty"`
|
||||
Delay int64 `json:"delay,omitempty"`
|
||||
LastErrorReason string `json:"last_error_reason,omitempty"`
|
||||
OutboundTag string `json:"outbound_tag,omitempty"`
|
||||
Which *configure.Which `json:"which,omitempty"`
|
||||
LastSeenTime int64 `json:"last_seen_time,omitempty"`
|
||||
LastTryTime int64 `json:"last_try_time,omitempty"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
mapper.Register(&observatory.OutboundStatus{})
|
||||
|
||||
ApiFeed = NewSubscriptions(ApiFeedBoxSize)
|
||||
for _, product := range Products {
|
||||
for _, product := range ApiProducts {
|
||||
ApiFeed.RegisterProduct(product)
|
||||
}
|
||||
go observatoryProducer()
|
||||
@@ -69,7 +84,13 @@ func observatoryProducer() {
|
||||
log.Printf("[Warning] observatoryProducer: %v", err)
|
||||
} else {
|
||||
outboundStatus := r.GetStatus().GetStatus()
|
||||
ApiFeed.ProductMessage(product, outboundStatus)
|
||||
os := make([]OutboundStatus, len(outboundStatus))
|
||||
css := configure.GetConnectedServers()
|
||||
for i := range outboundStatus {
|
||||
_ = mapper.AutoMapper(outboundStatus[i], &os[i])
|
||||
os[i].Which = css.Get()[tag2WhichIndex[os[i].OutboundTag]]
|
||||
}
|
||||
ApiFeed.ProductMessage(product, os)
|
||||
}
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
|
||||
@@ -12,8 +12,13 @@ type Feed struct {
|
||||
}
|
||||
type Message struct {
|
||||
ProduceTime time.Time
|
||||
Product string
|
||||
Body interface{}
|
||||
}
|
||||
type Box struct {
|
||||
Messages chan Message
|
||||
Cancel func()
|
||||
}
|
||||
|
||||
func NewSubscriptions(boxSize int) *Feed {
|
||||
if boxSize <= 0 {
|
||||
@@ -35,20 +40,20 @@ func (s *Feed) RegisterProduct(product string) {
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Feed) SubscribeMessage(product string) (box *chan Message, cancel func()) {
|
||||
func (s *Feed) SubscribeMessage(product string) (box *Box) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if _, ok := s.boxes[product]; !ok {
|
||||
return nil, nil
|
||||
return nil
|
||||
}
|
||||
b := make(chan Message, s.boxSize)
|
||||
s.boxes[product] = append(s.boxes[product], &b)
|
||||
messages := make(chan Message, s.boxSize)
|
||||
s.boxes[product] = append(s.boxes[product], &messages)
|
||||
index := len(s.boxes[product]) - 1
|
||||
cancel = func() {
|
||||
if index >= len(s.boxes[product]) || s.boxes[product][index] != &b {
|
||||
cancel := func() {
|
||||
if index >= len(s.boxes[product]) || s.boxes[product][index] != &messages {
|
||||
index = -1
|
||||
for i := range s.boxes[product] {
|
||||
if s.boxes[product][i] == &b {
|
||||
if s.boxes[product][i] == &messages {
|
||||
index = i
|
||||
break
|
||||
}
|
||||
@@ -58,15 +63,20 @@ func (s *Feed) SubscribeMessage(product string) (box *chan Message, cancel func(
|
||||
// the cancel function is invoked more than once
|
||||
return
|
||||
}
|
||||
close(*s.boxes[product][index])
|
||||
s.boxes[product] = append(s.boxes[product][:index], s.boxes[product][index+1:]...)
|
||||
}
|
||||
return &b, cancel
|
||||
return &Box{
|
||||
Messages: messages,
|
||||
Cancel: cancel,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Feed) ProductMessage(product string, message interface{}) (numConsumer int) {
|
||||
msg := Message{
|
||||
ProduceTime: time.Now(),
|
||||
Body: message,
|
||||
Product: product,
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
@@ -337,6 +337,7 @@ func AddConnect(wt Which) (err error) {
|
||||
return db.Set(bucket, "connectedServers", wcs)
|
||||
}
|
||||
|
||||
// OverwriteConnects will replace each outbounds contained in given ws with whiches in the ws
|
||||
func OverwriteConnects(ws *Whiches) (err error) {
|
||||
outWs := make(map[string][]*Which)
|
||||
for _, w := range ws.Get() {
|
||||
|
||||
@@ -4,6 +4,7 @@ go 1.16
|
||||
|
||||
require (
|
||||
github.com/beevik/ntp v0.3.0
|
||||
github.com/devfeel/mapper v0.7.5
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||
github.com/gin-contrib/cors v1.3.1
|
||||
github.com/gin-gonic/gin v1.7.1
|
||||
|
||||
@@ -61,6 +61,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/devfeel/mapper v0.7.5 h1:YZISGXQbYkdh5Gn3uJlL++UqCzMJDxZoYKAtS58ZdGo=
|
||||
github.com/devfeel/mapper v0.7.5/go.mod h1:foz4u16jrssGoDfnWYQGFcthjlU6uBV5UV8uYJfKneA=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/dgryski/go-camellia v0.0.0-20191119043421-69a8a13fb23d h1:CPqTNIigGweVPT4CYb+OO2E6XyRKFOmvTHwWRLgCAlE=
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/v2rayA/v2rayA/server/service"
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
}
|
||||
|
||||
func WsMessage(ctx *gin.Context) {
|
||||
conn, err := upgrader.Upgrade(ctx.Writer, ctx.Request, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
h := service.NewMessageHandler(conn)
|
||||
go h.Write()
|
||||
go h.Read()
|
||||
}
|
||||
@@ -124,6 +124,7 @@ func Run() error {
|
||||
auth.GET("outbounds", controller.GetOutbounds)
|
||||
auth.POST("outbound", controller.PostOutbound)
|
||||
auth.DELETE("outbound", controller.DeleteOutbound)
|
||||
auth.GET("message", controller.WsMessage)
|
||||
}
|
||||
|
||||
ServeGUI(engine)
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/gorilla/websocket"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/v2rayA/v2rayA/core/v2ray"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
writeWait = 5 * time.Second
|
||||
)
|
||||
|
||||
type MessageHandler struct {
|
||||
conn *websocket.Conn
|
||||
boxes map[string]*v2ray.Box
|
||||
}
|
||||
type Message struct {
|
||||
ProductTime time.Time `json:"product_time"`
|
||||
Type string `json:"type"`
|
||||
Body interface{} `json:"body"`
|
||||
}
|
||||
|
||||
func NewMessageHandler(conn *websocket.Conn) *MessageHandler {
|
||||
h := &MessageHandler{
|
||||
conn: conn,
|
||||
boxes: make(map[string]*v2ray.Box),
|
||||
}
|
||||
for _, product := range v2ray.ApiProducts {
|
||||
box := v2ray.ApiFeed.SubscribeMessage(product)
|
||||
h.boxes[product] = box
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// Read should be invoked only once
|
||||
func (h *MessageHandler) Read() {
|
||||
defer func() {
|
||||
for product := range h.boxes {
|
||||
h.boxes[product].Cancel()
|
||||
}
|
||||
h.conn.Close()
|
||||
}()
|
||||
for {
|
||||
if _, _, err := h.conn.ReadMessage(); err != nil {
|
||||
log.Println(err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Write should be invoked only once
|
||||
func (h *MessageHandler) Write() {
|
||||
defer func() {
|
||||
h.conn.Close()
|
||||
}()
|
||||
var wg sync.WaitGroup
|
||||
for _, box := range h.boxes {
|
||||
wg.Add(1)
|
||||
go func(box *v2ray.Box) {
|
||||
defer wg.Done()
|
||||
for msg := range box.Messages {
|
||||
b, err := jsoniter.Marshal(Message{
|
||||
ProductTime: msg.ProduceTime,
|
||||
Type: msg.Product,
|
||||
Body: msg.Body,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("[Warning] %v", err)
|
||||
}
|
||||
_ = h.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
if err := h.conn.WriteMessage(websocket.TextMessage, b); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}(box)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
Reference in new issue
Block a user