Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add proxy summary #48

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/latest.workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
- name: Install Protoc
uses: arduino/setup-protoc@v2
uses: arduino/setup-protoc@v3
- name: Compile server
run: bash ./build.sh
- uses: "marvinpinto/action-automatic-releases@latest"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tag.workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
- name: Install Protoc
uses: arduino/setup-protoc@v2
uses: arduino/setup-protoc@v3
- name: Compile server
run: bash ./build.sh
- uses: "marvinpinto/action-automatic-releases@latest"
Expand Down
6 changes: 6 additions & 0 deletions biz/master/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/VaalaCat/frp-panel/biz/master/auth"
"github.com/VaalaCat/frp-panel/biz/master/client"
"github.com/VaalaCat/frp-panel/biz/master/platform"
"github.com/VaalaCat/frp-panel/biz/master/proxy"
"github.com/VaalaCat/frp-panel/biz/master/server"
"github.com/VaalaCat/frp-panel/biz/master/user"
"github.com/VaalaCat/frp-panel/common"
Expand Down Expand Up @@ -69,5 +70,10 @@ func ConfigureRouter(router *gin.Engine) {
frpsRouter.POST("/update", common.Wrapper(server.UpdateFrpsHander))
frpsRouter.POST("/delete", common.Wrapper(server.RemoveFrpsHandler))
}
proxyRouter := v1.Group("/proxy", middleware.JWTAuth, middleware.AuthCtx)
{
proxyRouter.POST("/get_by_cid", common.Wrapper(proxy.GetProxyByCID))
proxyRouter.POST("/get_by_sid", common.Wrapper(proxy.GetProxyBySID))
}
}
}
34 changes: 34 additions & 0 deletions biz/master/proxy/get_by_cid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package proxy

import (
"context"
"fmt"

"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/dao"
"github.com/VaalaCat/frp-panel/pb"
"github.com/sirupsen/logrus"
)

// GetProxyByCID get proxy info by client id
func GetProxyByCID(c context.Context, req *pb.GetProxyByCIDRequest) (*pb.GetProxyByCIDResponse, error) {
logrus.Infof("get proxy by client id, req: [%+v]", req)
var (
clientID = req.GetClientId()
userInfo = common.GetUserInfo(c)
)

if len(clientID) == 0 {
return nil, fmt.Errorf("request invalid")
}

proxyList, err := dao.GetProxyByClientID(userInfo, clientID)
if proxyList == nil || err != nil {
logrus.WithError(err).Errorf("cannot get proxy, client id: [%s]", clientID)
return nil, err
}
return &pb.GetProxyByCIDResponse{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
ProxyInfos: convertProxyList(proxyList),
}, nil
}
34 changes: 34 additions & 0 deletions biz/master/proxy/get_by_sid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package proxy

import (
"context"
"fmt"

"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/dao"
"github.com/VaalaCat/frp-panel/pb"
"github.com/sirupsen/logrus"
)

// GetProxyBySID get proxy info by server id
func GetProxyBySID(c context.Context, req *pb.GetProxyBySIDRequest) (*pb.GetProxyBySIDResponse, error) {
logrus.Infof("get proxy by server id, req: [%+v]", req)
var (
serverID = req.GetServerId()
userInfo = common.GetUserInfo(c)
)

if len(serverID) == 0 {
return nil, fmt.Errorf("request invalid")
}

proxyList, err := dao.GetProxyByServerID(userInfo, serverID)
if proxyList == nil || err != nil {
logrus.WithError(err).Errorf("cannot get proxy, server id: [%s]", serverID)
return nil, err
}
return &pb.GetProxyBySIDResponse{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
ProxyInfos: convertProxyList(proxyList),
}, nil
}
22 changes: 22 additions & 0 deletions biz/master/proxy/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package proxy

import (
"github.com/VaalaCat/frp-panel/models"
"github.com/VaalaCat/frp-panel/pb"
"github.com/samber/lo"
)

func convertProxyList(proxyList []*models.ProxyEntity) []*pb.ProxyInfo {
return lo.Map(proxyList, func(item *models.ProxyEntity, index int) *pb.ProxyInfo {
return &pb.ProxyInfo{
Name: lo.ToPtr(item.Name),
Type: lo.ToPtr(item.Type),
ClientId: lo.ToPtr(item.ClientID),
ServerId: lo.ToPtr(item.ServerID),
TodayTrafficIn: lo.ToPtr(item.TodayTrafficIn),
TodayTrafficOut: lo.ToPtr(item.TodayTrafficOut),
HistoryTrafficIn: lo.ToPtr(item.HistoryTrafficIn),
HistoryTrafficOut: lo.ToPtr(item.HistoryTrafficOut),
}
})
}
25 changes: 25 additions & 0 deletions biz/master/server/rpc_push_proxy_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package server

import (
"context"

"github.com/VaalaCat/frp-panel/dao"
"github.com/VaalaCat/frp-panel/models"
"github.com/VaalaCat/frp-panel/pb"
)

func PushProxyInfo(ctx context.Context, req *pb.PushProxyInfoReq) (*pb.PushProxyInfoResp, error) {
var srv *models.ServerEntity
var err error

if srv, err = ValidateServerRequest(req.GetBase()); err != nil {
return nil, err
}

if err = dao.AdminUpdateProxy(srv, req.GetProxyInfos()); err != nil {
return nil, err
}
return &pb.PushProxyInfoResp{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
}, nil
}
64 changes: 64 additions & 0 deletions biz/server/rpc_push_proxy_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package server

import (
"context"

"github.com/VaalaCat/frp-panel/pb"
"github.com/VaalaCat/frp-panel/rpc"
"github.com/VaalaCat/frp-panel/tunnel"
v1 "github.com/fatedier/frp/pkg/config/v1"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
)

var proxyTypeList = []v1.ProxyType{
v1.ProxyTypeTCP,
v1.ProxyTypeUDP,
v1.ProxyTypeTCPMUX,
v1.ProxyTypeHTTP,
v1.ProxyTypeHTTPS,
v1.ProxyTypeSTCP,
v1.ProxyTypeXTCP,
v1.ProxyTypeSUDP,
}

func PushProxyInfo(serverID, serverSecret string) error {
proxyInfos := []*pb.ProxyInfo{}

if cli := tunnel.GetServerController().Get(serverID); cli != nil {
for _, proxyType := range proxyTypeList {
proxyStatsList := cli.GetProxyStatsByType(proxyType)
for _, proxyStats := range proxyStatsList {
if proxyStats != nil {
proxyInfos = append(proxyInfos, &pb.ProxyInfo{
Name: lo.ToPtr(proxyStats.Name),
Type: lo.ToPtr(proxyStats.Type),
TodayTrafficIn: lo.ToPtr(proxyStats.TodayTrafficIn),
TodayTrafficOut: lo.ToPtr(proxyStats.TodayTrafficOut),
})
}
}
}
}

if len(proxyInfos) > 0 {
ctx := context.Background()
cli, err := rpc.MasterCli(ctx)
if err != nil {
logrus.WithError(err).Error("cannot get master server")
return err
}
_, err = cli.PushProxyInfo(ctx, &pb.PushProxyInfoReq{
Base: &pb.ServerBase{
ServerId: serverID,
ServerSecret: serverSecret,
},
ProxyInfos: proxyInfos,
})
if err != nil {
logrus.WithError(err).Error("cannot push proxy info")
return err
}
}
return nil
}
4 changes: 3 additions & 1 deletion cmd/frpp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
bizclient "github.com/VaalaCat/frp-panel/biz/client"
"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/conf"
"github.com/VaalaCat/frp-panel/pb"
"github.com/VaalaCat/frp-panel/rpc"
Expand Down Expand Up @@ -43,7 +44,8 @@ func runClient() {
r := rpcclient.GetClientRPCSerivce()
defer r.Stop()

w := watcher.NewClient(bizclient.PullConfig, clientID, clientSecret)
w := watcher.NewClient()
w.AddTask(common.PullConfigDuration, bizclient.PullConfig, clientID, clientSecret)
defer w.Stop()

initClientOnce(clientID, clientSecret)
Expand Down
5 changes: 4 additions & 1 deletion cmd/frpp/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/VaalaCat/frp-panel/biz/master/auth"
bizserver "github.com/VaalaCat/frp-panel/biz/server"
"github.com/VaalaCat/frp-panel/cache"
"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/conf"
"github.com/VaalaCat/frp-panel/dao"
"github.com/VaalaCat/frp-panel/models"
Expand Down Expand Up @@ -111,7 +112,9 @@ func initDefaultInternalServer() (rpcclient.ClientRPCHandler, watcher.Client) {

r := rpcclient.GetClientRPCSerivce()

w := watcher.NewClient(bizserver.PullConfig, defaultServer.ServerID, defaultServer.ConnectSecret)
w := watcher.NewClient()
w.AddTask(common.PullConfigDuration, bizserver.PullConfig, defaultServer.ServerID, defaultServer.ConnectSecret)
w.AddTask(common.PushProxyInfoDuration, bizserver.PushProxyInfo, defaultServer.ServerID, defaultServer.ConnectSecret)

go initServerOnce(defaultServer.ServerID, defaultServer.ConnectSecret)
return r, w
Expand Down
5 changes: 4 additions & 1 deletion cmd/frpp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
bizserver "github.com/VaalaCat/frp-panel/biz/server"
"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/conf"
"github.com/VaalaCat/frp-panel/pb"
"github.com/VaalaCat/frp-panel/rpc"
Expand Down Expand Up @@ -47,7 +48,9 @@ func runServer() {
r := rpcclient.GetClientRPCSerivce()
defer r.Stop()

w := watcher.NewClient(bizserver.PullConfig, clientID, clientSecret)
w := watcher.NewClient()
w.AddTask(common.PullConfigDuration, bizserver.PullConfig, clientID, clientSecret)
w.AddTask(common.PushProxyInfoDuration, bizserver.PushProxyInfo, clientID, clientSecret)
defer w.Stop()

initServerOnce(clientID, clientSecret)
Expand Down
4 changes: 3 additions & 1 deletion cmd/frppc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
bizclient "github.com/VaalaCat/frp-panel/biz/client"
"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/conf"
"github.com/VaalaCat/frp-panel/pb"
"github.com/VaalaCat/frp-panel/rpc"
Expand Down Expand Up @@ -43,7 +44,8 @@ func runClient() {
r := rpcclient.GetClientRPCSerivce()
defer r.Stop()

w := watcher.NewClient(bizclient.PullConfig, clientID, clientSecret)
w := watcher.NewClient()
w.AddTask(common.PullConfigDuration, bizclient.PullConfig, clientID, clientSecret)
defer w.Stop()

initClientOnce(clientID, clientSecret)
Expand Down
5 changes: 5 additions & 0 deletions common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ const (
DefaultServerID = "default"
DefaultAdminUserID = 1
)

const (
PullConfigDuration = 30 * time.Second
PushProxyInfoDuration = 30 * time.Second
)
3 changes: 2 additions & 1 deletion common/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type ReqType interface {
pb.GetUserInfoRequest | pb.UpdateUserInfoRequest |
pb.GetPlatformInfoRequest | pb.GetClientsStatusRequest |
pb.GetClientCertRequest |
pb.StartFRPCRequest | pb.StopFRPCRequest | pb.StartFRPSRequest | pb.StopFRPSRequest
pb.StartFRPCRequest | pb.StopFRPCRequest | pb.StartFRPSRequest | pb.StopFRPSRequest |
pb.GetProxyByCIDRequest | pb.GetProxyBySIDRequest
}

func GetProtoRequest[T ReqType](c *gin.Context) (r *T, err error) {
Expand Down
3 changes: 2 additions & 1 deletion common/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ type RespType interface {
pb.GetUserInfoResponse | pb.UpdateUserInfoResponse |
pb.GetPlatformInfoResponse | pb.GetClientsStatusResponse |
pb.GetClientCertResponse |
pb.StartFRPCResponse | pb.StopFRPCResponse | pb.StartFRPSResponse | pb.StopFRPSResponse
pb.StartFRPCResponse | pb.StopFRPCResponse | pb.StartFRPSResponse | pb.StopFRPSResponse |
pb.GetProxyByCIDResponse | pb.GetProxyBySIDResponse
}

func OKResp[T RespType](c *gin.Context, origin *T) {
Expand Down
Loading
Loading