Skip to content

Commit

Permalink
feat: add client visit info
Browse files Browse the repository at this point in the history
  • Loading branch information
VaalaCat committed Jan 22, 2024
1 parent dcf0ccb commit 0e03c93
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 46 deletions.
7 changes: 4 additions & 3 deletions biz/master/client/get_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ func GetClientHandler(ctx context.Context, req *pb.GetClientRequest) (*pb.GetCli
return &pb.GetClientResponse{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
Client: &pb.Client{
Id: lo.ToPtr(client.ClientID),
Secret: lo.ToPtr(client.ConnectSecret),
Config: lo.ToPtr(string(client.ConfigContent)),
Id: lo.ToPtr(client.ClientID),
Secret: lo.ToPtr(client.ConnectSecret),
Config: lo.ToPtr(string(client.ConfigContent)),
ServerId: lo.ToPtr(client.ServerID),
},
}, nil
}
7 changes: 4 additions & 3 deletions biz/master/client/list_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ func ListClientsHandler(ctx context.Context, req *pb.ListClientsRequest) (*pb.Li

respClients := lo.Map(clients, func(c *models.ClientEntity, _ int) *pb.Client {
return &pb.Client{
Id: lo.ToPtr(c.ClientID),
Secret: lo.ToPtr(c.ConnectSecret),
Config: lo.ToPtr(string(c.ConfigContent)),
Id: lo.ToPtr(c.ClientID),
Secret: lo.ToPtr(c.ConnectSecret),
Config: lo.ToPtr(string(c.ConfigContent)),
ServerId: lo.ToPtr(c.ServerID),
}
})

Expand Down
1 change: 1 addition & 0 deletions biz/master/client/update_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func UpdateFrpcHander(c context.Context, req *pb.UpdateFRPCRequest) (*pb.UpdateF
cliCfg.Auth = v1.AuthClientConfig{}
cliCfg.Metadatas = map[string]string{
common.FRPAuthTokenKey: userInfo.GetToken(),
common.FRPClientIDKey: clientID,
}

newCfg := struct {
Expand Down
28 changes: 21 additions & 7 deletions biz/master/server/rpc_frp_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package server

import (
"context"
"fmt"

"github.com/VaalaCat/frp-panel/models"
"github.com/VaalaCat/frp-panel/cache"
"github.com/VaalaCat/frp-panel/dao"
"github.com/VaalaCat/frp-panel/pb"
"github.com/sirupsen/logrus"
)
Expand All @@ -12,18 +14,30 @@ func FRPAuth(ctx context.Context, req *pb.FRPAuthRequest) (*pb.FRPAuthResponse,
logrus.Infof("frpc auth, req: [%+v]", req)
var (
err error
cli *models.ServerEntity
)

if cli, err = ValidateServerRequest(req.GetBase()); err != nil {
userToken, err := cache.Get().Get([]byte(req.User))
if err != nil {
u, err := dao.GetUserByUserName(req.User)
if err != nil || u == nil {
logrus.WithError(err).Errorf("invalid user: %s", req.User)
return &pb.FRPAuthResponse{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_INVALID, Message: err.Error()},
Ok: false,
}, fmt.Errorf("invalid user: %s", req.User)
}
cache.Get().Set([]byte(u.GetUserName()), []byte(u.GetToken()), 0)
userToken = []byte(u.GetToken())
}

if string(userToken) != req.GetToken() {
logrus.Error("invalid token")
return &pb.FRPAuthResponse{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_INVALID, Message: err.Error()},
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_INVALID, Message: "invalid token"},
Ok: false,
}, err
}, fmt.Errorf("invalid token")
}

logrus.Infof("frpc auth success, server: [%+v]", cli.ServerID)

return &pb.FRPAuthResponse{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
Ok: true,
Expand Down
4 changes: 4 additions & 0 deletions biz/master/server/update_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"fmt"

"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/conf"
"github.com/VaalaCat/frp-panel/dao"
"github.com/VaalaCat/frp-panel/pb"
"github.com/VaalaCat/frp-panel/rpc"
"github.com/VaalaCat/frp-panel/utils"
v1 "github.com/fatedier/frp/pkg/config/v1"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -36,6 +38,8 @@ func UpdateFrpsHander(c context.Context, req *pb.UpdateFRPSRequest) (*pb.UpdateF
return nil, err
}

srvCfg.HTTPPlugins = []v1.HTTPPluginOptions{conf.FRPsAuthOption(common.DefaultServerID == serverID)}

if err := srv.SetConfigContent(srvCfg); err != nil {
logrus.WithError(err).Errorf("cannot set server config")
return nil, err
Expand Down
7 changes: 6 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ bash ./codegen.sh
mkdir -p dist
rm -rf dist/*
cd www && pnpm install && pnpm build && cd ..
echo "Building frp-panel windows binaries..."
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o dist/frp-panel-amd64.exe cmd/*.go
CGO_ENABLED=0 GOOS=windows GOARCH=arm64 go build -o dist/frp-panel-arm64.exe cmd/*.go
echo "Building frp-panel linux binaries..."
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o dist/frp-panel-linux-amd64 cmd/*.go
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o dist/frp-panel-linux-arm64 cmd/*.go
echo "Building frp-panel darwin binaries..."
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o dist/frp-panel-darwin-amd64 cmd/*.go
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o dist/frp-panel-darwin-arm64 cmd/*.go
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o dist/frp-panel-darwin-arm64 cmd/*.go

echo "Build Done!"
1 change: 1 addition & 0 deletions common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
FRPAuthTokenKey = "token"
ErrKey = "err"
UserInfoKey = "x-vaala-userinfo"
FRPClientIDKey = "x-vaala-frp-client-id"
)

const (
Expand Down
13 changes: 10 additions & 3 deletions conf/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,17 @@ func ServerAPIListenAddr() string {
return fmt.Sprintf(":%d", cfg.Server.APIPort)
}

func FRPsAuthOption() v1.HTTPPluginOptions {
func FRPsAuthOption(isDefault bool) v1.HTTPPluginOptions {
cfg := Get()
authUrl, err := url.Parse(fmt.Sprintf("http://%s:%d%s", cfg.Master.InternalFRPAuthServerHost,
cfg.Master.InternalFRPAuthServerPort,
var port int
if isDefault {
port = cfg.Master.APIPort
} else {
port = cfg.Master.InternalFRPAuthServerPort
}
authUrl, err := url.Parse(fmt.Sprintf("http://%s:%d%s",
cfg.Master.InternalFRPAuthServerHost,
port,
cfg.Master.InternalFRPAuthServerPath))
if err != nil {
logrus.WithError(err).Fatalf("parse auth url error")
Expand Down
2 changes: 1 addition & 1 deletion conf/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Config struct {
InternalFRPServerHost string `env:"INTERNAL_FRP_SERVER_HOST" env-description:"internal frp server host, used for client connection"`
InternalFRPServerPort int `env:"INTERNAL_FRP_SERVER_PORT" env-default:"9002" env-description:"internal frp server port, used for client connection"`
InternalFRPAuthServerHost string `env:"INTERNAL_FRP_AUTH_SERVER_HOST" env-default:"127.0.0.1" env-description:"internal frp auth server host"`
InternalFRPAuthServerPort int `env:"INTERNAL_FRP_AUTH_SERVER_PORT" env-default:"9000" env-description:"internal frp auth server port"`
InternalFRPAuthServerPort int `env:"INTERNAL_FRP_AUTH_SERVER_PORT" env-default:"8999" env-description:"internal frp auth server port"`
InternalFRPAuthServerPath string `env:"INTERNAL_FRP_AUTH_SERVER_PATH" env-default:"/auth" env-description:"internal frp auth server path"`
} `env-prefix:"MASTER_"`
Server struct {
Expand Down
1 change: 1 addition & 0 deletions idl/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ message Client {
optional string secret = 2;
optional string config = 3;
optional string comment = 5; // 用户自定义的备注
optional string server_id = 6;
}

message Server {
Expand Down
27 changes: 19 additions & 8 deletions pb/common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions services/server/frps_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"context"
"time"

v1 "github.com/fatedier/frp/pkg/config/v1"
"github.com/fatedier/frp/pkg/config/v1/validation"
Expand Down Expand Up @@ -64,13 +63,8 @@ func NewServerHandler(svrCfg *v1.ServerConfig) *Server {

var svr *server.Service

for svr, err = server.NewService(svrCfg); err != nil; svr, err = server.NewService(svrCfg) {
if err != nil {
logrus.WithError(err).Errorf("cannot create server, wait 5 sec for retry")
time.Sleep(5 * time.Second)
continue
}
break
if svr, err = server.NewService(svrCfg); err != nil {
logrus.WithError(err).Panic("cannot create server, exit and restart")
}

return &Server{
Expand Down
22 changes: 15 additions & 7 deletions www/components/frpc_card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { Label } from "@radix-ui/react-label"
import { useQuery } from '@tanstack/react-query'
import { listServer } from "@/api/server"
import { listClient } from "@/api/client"
import { getClient, listClient } from "@/api/client"
import {
Card,
CardContent,
Expand Down Expand Up @@ -50,12 +50,6 @@ export const FRPCFormCard: React.FC<FRPCFormCardProps> = ({ clientID: defaultCli
}
}, [defaultClientID, defaultServerID])

useEffect(() => {
if (paramClientID) {
setClientID(paramClientID)
}
}, [paramClientID])

const { data: serverList, refetch: refetchServers } = useQuery({
queryKey: ["listServer"], queryFn: () => {
return listServer({ page: 1, pageSize: 500 })
Expand All @@ -68,6 +62,19 @@ export const FRPCFormCard: React.FC<FRPCFormCardProps> = ({ clientID: defaultCli
}
})

const { data: client, refetch: refetchClient } = useQuery({
queryKey: ["getClient", clientID], queryFn: () => {
return getClient({ clientId: clientID })
}
})

useEffect(() => {
if (paramClientID) {
setClientID(paramClientID)
setServerID(clientList?.clients?.find((client) => client.id == paramClientID)?.serverId)
}
}, [paramClientID, clientList])

return (
<Card className="w-full">
<CardHeader>
Expand All @@ -90,6 +97,7 @@ export const FRPCFormCard: React.FC<FRPCFormCardProps> = ({ clientID: defaultCli

<Label className="text-sm font-medium">服务端</Label>
<Select onValueChange={handleServerChange}
value={serverID}
onOpenChange={() => {
refetchServers()
refetchClients()
Expand Down
2 changes: 1 addition & 1 deletion www/components/frps_form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const FRPSForm: React.FC<FRPSFormProps> = ({ serverID, server }) => {
<Label className="text-sm font-medium">节点 {serverID} 的备注</Label>
<p className="text-sm text-muted-foreground">可以到高级模式修改备注哦!</p>
<p className="text-sm border rounded p-2 my-2">
{server?.comment == undefined ? "空空如也" : server?.comment}
{server?.comment == undefined || server?.comment === "" ? "空空如也" : server?.comment}
</p>
{serverID && <Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
Expand Down
2 changes: 1 addition & 1 deletion www/components/platforminfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const PlatformInfo = () => {
</div>
</CardHeader>
<CardContent>
<div className='text-2xl font-bold'>{platformInfo.data?.configuredServerCount}</div>
<div className='text-2xl font-bold'>{platformInfo.data?.configuredClientCount}</div>
<p className="text-xs text-muted-foreground">请前往左侧🫲菜单修改</p>
</CardContent>
</Card>
Expand Down
Loading

0 comments on commit 0e03c93

Please sign in to comment.