Skip to content

Commit

Permalink
feat: token cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Tylone-zx authored and VaalaCat committed Dec 28, 2024
1 parent e9a59a7 commit e5772fa
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 53 deletions.
180 changes: 155 additions & 25 deletions cmd/frpp/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package main

import (
"context"
"errors"
"fmt"
"os"

"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/conf"
"github.com/VaalaCat/frp-panel/logger"
"github.com/VaalaCat/frp-panel/pb"
"github.com/VaalaCat/frp-panel/rpc"
"github.com/VaalaCat/frp-panel/utils"
"github.com/joho/godotenv"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand All @@ -20,6 +26,17 @@ var (
)

func initCommand() {
rootCmd = &cobra.Command{
Use: "frp-panel",
Short: "frp-panel is a frp panel QwQ",
}
CmdListWithFlag := initCmdWithFlag()
CmdListWithoutFlag := initCmdWithoutFlag()
rootCmd.AddCommand(CmdListWithFlag...)
rootCmd.AddCommand(CmdListWithoutFlag...)
}

func initCmdWithFlag() []*cobra.Command {
var (
clientSecret string
clientID string
Expand All @@ -28,6 +45,7 @@ func initCommand() {
rpcPort int
apiPort int
apiScheme string
joinToken string
)

clientCmd = &cobra.Command{
Expand All @@ -47,6 +65,7 @@ func initCommand() {
}
},
}

serverCmd = &cobra.Command{
Use: "server [-s client secret] [-i client id] [-a app secret] [-r rpc host] [-c rpc port] [-p api port]",
Short: "run managed frps",
Expand All @@ -64,6 +83,43 @@ func initCommand() {
}
},
}

joinCmd := &cobra.Command{
Use: "join [-j join token] [-r rpc host] [-p api port] [-e api scheme]",
Short: "join extra params",
Run: func(cmd *cobra.Command, args []string) {
pullRunConfig(joinToken, appSecret, rpcHost, apiScheme, rpcPort, apiPort)
},
}

clientCmd.Flags().StringVarP(&clientSecret, "secret", "s", "", "client secret")
serverCmd.Flags().StringVarP(&clientSecret, "secret", "s", "", "client secret")
clientCmd.Flags().StringVarP(&clientID, "id", "i", "", "client id")
serverCmd.Flags().StringVarP(&clientID, "id", "i", "", "client id")
clientCmd.Flags().StringVarP(&rpcHost, "rpc", "r", "", "rpc host, canbe ip or domain")
serverCmd.Flags().StringVarP(&rpcHost, "rpc", "r", "", "rpc host, canbe ip or domain")
clientCmd.Flags().StringVarP(&appSecret, "app", "a", "", "app secret")
serverCmd.Flags().StringVarP(&appSecret, "app", "a", "", "app secret")

clientCmd.Flags().IntVarP(&rpcPort, "rpc-port", "c", 0, "rpc port, master rpc port, scheme is grpc")
serverCmd.Flags().IntVarP(&rpcPort, "rpc-port", "c", 0, "rpc port, master rpc port, scheme is grpc")
clientCmd.Flags().IntVarP(&apiPort, "api-port", "p", 0, "api port, master api port, scheme is http/https")
serverCmd.Flags().IntVarP(&apiPort, "api-port", "p", 0, "api port, master api port, scheme is http/https")

clientCmd.Flags().StringVarP(&apiScheme, "api-scheme", "e", "", "api scheme, master api scheme, scheme is http/https")
serverCmd.Flags().StringVarP(&apiScheme, "api-scheme", "e", "", "api scheme, master api scheme, scheme is http/https")

joinCmd.Flags().IntVarP(&rpcPort, "rpc-port", "c", 0, "rpc port, master rpc port, scheme is grpc")
joinCmd.Flags().IntVarP(&apiPort, "api-port", "p", 0, "api port, master api port, scheme is http/https")
joinCmd.Flags().StringVarP(&appSecret, "app", "a", "", "app secret")
joinCmd.Flags().StringVarP(&joinToken, "join-token", "j", "", "join token")
joinCmd.Flags().StringVarP(&rpcHost, "rpc", "r", "", "rpc host, canbe ip or domain")
joinCmd.Flags().StringVarP(&apiScheme, "api-scheme", "e", "", "api scheme, master api scheme, scheme is http/https")

return []*cobra.Command{clientCmd, serverCmd, joinCmd}
}

func initCmdWithoutFlag() []*cobra.Command {
masterCmd = &cobra.Command{
Use: "master",
Short: "run frp-panel manager",
Expand All @@ -75,10 +131,6 @@ func initCommand() {
}
},
}
rootCmd = &cobra.Command{
Use: "frp-panel",
Short: "frp-panel is a frp panel QwQ",
}

installServiceCmd := &cobra.Command{
Use: "install",
Expand Down Expand Up @@ -138,27 +190,15 @@ func initCommand() {
fmt.Println(conf.GetVersion().String())
},
}

rootCmd.AddCommand(clientCmd, serverCmd, masterCmd, versionCmd,
installServiceCmd, uninstallServiceCmd,
startServiceCmd, stopServiceCmd, restartServiceCmd)

clientCmd.Flags().StringVarP(&clientSecret, "secret", "s", "", "client secret")
serverCmd.Flags().StringVarP(&clientSecret, "secret", "s", "", "client secret")
clientCmd.Flags().StringVarP(&clientID, "id", "i", "", "client id")
serverCmd.Flags().StringVarP(&clientID, "id", "i", "", "client id")
clientCmd.Flags().StringVarP(&rpcHost, "rpc", "r", "", "rpc host, canbe ip or domain")
serverCmd.Flags().StringVarP(&rpcHost, "rpc", "r", "", "rpc host, canbe ip or domain")
clientCmd.Flags().StringVarP(&appSecret, "app", "a", "", "app secret")
serverCmd.Flags().StringVarP(&appSecret, "app", "a", "", "app secret")

clientCmd.Flags().IntVarP(&rpcPort, "rpc-port", "c", 0, "rpc port, master rpc port, scheme is grpc")
serverCmd.Flags().IntVarP(&rpcPort, "rpc-port", "c", 0, "rpc port, master rpc port, scheme is grpc")
clientCmd.Flags().IntVarP(&apiPort, "api-port", "p", 0, "api port, master api port, scheme is http/https")
serverCmd.Flags().IntVarP(&apiPort, "api-port", "p", 0, "api port, master api port, scheme is http/https")

clientCmd.Flags().StringVarP(&apiScheme, "api-scheme", "e", "", "api scheme, master api scheme, scheme is http/https")
serverCmd.Flags().StringVarP(&apiScheme, "api-scheme", "e", "", "api scheme, master api scheme, scheme is http/https")
return []*cobra.Command{
masterCmd,
installServiceCmd,
uninstallServiceCmd,
startServiceCmd,
stopServiceCmd,
restartServiceCmd,
versionCmd,
}
}

func initLogger() {
Expand Down Expand Up @@ -202,3 +242,93 @@ func setMasterCommandIfNonePresent() {
rootCmd.SetArgs(args)
}
}

func pullRunConfig(joinToken, appSecret, rpcHost, apiScheme string, rpcPort, apiPort int) {
c := context.Background()
if err := checkPullParams(joinToken, rpcHost, apiScheme, apiPort); err != nil {
logger.Logger(c).Errorf("check pull params failed: %s", err.Error())
return
}

if err := utils.EnsureDirectoryExists(common.SysEnvPath); err != nil {
logger.Logger(c).Errorf("ensure directory failed: %s", err.Error())
return
}

// 设置一下调接口需要的参数
clientID := utils.GetHostnameWithIP()
clientID = utils.MakeClientIDPermited(clientID)
patchConfig(rpcHost, appSecret, "", "", apiScheme, rpcPort, apiPort)

initResp, err := rpc.InitClient(clientID, joinToken)
if err != nil {
logger.Logger(c).Errorf("init client failed: %s", err.Error())
return
}
if initResp == nil {
logger.Logger(c).Errorf("init resp is nil")
return
}
if initResp.GetStatus().GetCode() != pb.RespCode_RESP_CODE_SUCCESS {
logger.Logger(c).Errorf("init resp code is not success: %s", initResp.GetStatus().GetMessage())
return
}

clientID = initResp.GetClientId()
clientResp, err := rpc.GetClient(clientID, joinToken)
if err != nil {
logger.Logger(c).Errorf("get client failed: %s", err.Error())
return
}
if clientResp == nil {
logger.Logger(c).Errorf("client resp is nil")
return
}
if clientResp.GetStatus().GetCode() != pb.RespCode_RESP_CODE_SUCCESS {
logger.Logger(c).Errorf("client resp code is not success: %s", clientResp.GetStatus().GetMessage())
return
}

client := clientResp.GetClient()
if client == nil {
logger.Logger(c).Errorf("client is nil")
return
}

envMap, err := godotenv.Read(common.SysEnvPath)
if err != nil {
envMap = make(map[string]string)
logger.Logger(c).Errorf("read env file failed, try to create: %s", err.Error())
}

envMap[common.EnvAppSecret] = appSecret
envMap[common.EnvClientID] = clientID
envMap[common.EnvClientSecret] = client.GetSecret()
envMap[common.EnvMasterRPCHost] = rpcHost
envMap[common.EnvMasterRPCPort] = cast.ToString(rpcPort)
envMap[common.EnvMasterAPIHost] = rpcHost
envMap[common.EnvMasterAPIPort] = cast.ToString(apiPort)
envMap[common.EnvMasterAPIScheme] = apiScheme

if err = godotenv.Write(envMap, common.SysEnvPath); err != nil {
logger.Logger(c).Errorf("write env file failed: %s", err.Error())
return
}
logger.Logger(c).Info("run config saved to env file")
}

func checkPullParams(joinToken, rpcHost, apiScheme string, apiPort int) error {
if len(joinToken) == 0 {
return errors.New("join token is empty")
}
if len(rpcHost) == 0 {
return errors.New("rpc host is empty")
}
if len(apiScheme) == 0 {
return errors.New("api scheme is empty")
}
if apiPort == 0 {
return errors.New("api port is empty")
}
return nil
}
17 changes: 16 additions & 1 deletion common/const.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package common

import "time"
import (
"time"
)

const (
AuthorizationKey = "authorization"
Expand Down Expand Up @@ -61,3 +63,16 @@ const (
PullConfigDuration = 30 * time.Second
PushProxyInfoDuration = 30 * time.Second
)

const (
CurEnvPath = ".env"
SysEnvPath = "/etc/frpp/.env"
EnvAppSecret = "APP_SECRET"
EnvClientID = "CLIENT_ID"
EnvClientSecret = "CLIENT_SECRET"
EnvMasterRPCHost = "MASTER_RPC_HOST"
EnvMasterAPIHost = "MASTER_API_HOST"
EnvMasterRPCPort = "MASTER_RPC_PORT"
EnvMasterAPIPort = "MASTER_API_PORT"
EnvMasterAPIScheme = "MASTER_API_SCHEME"
)
13 changes: 5 additions & 8 deletions conf/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"

"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/logger"
"github.com/ilyakaznacheev/cleanenv"
"github.com/joho/godotenv"
Expand Down Expand Up @@ -69,20 +70,16 @@ func InitConfig() {
ctx = context.Background()
)

// 越前面优先级越高,后面的不会覆盖前面的
envFiles := []string{
".env",
"/etc/frpp/.env",
common.CurEnvPath,
common.SysEnvPath,
}

for i, envFile := range envFiles {
for _, envFile := range envFiles {
if err = godotenv.Load(envFile); err == nil {
logger.Logger(ctx).Infof("load env file success: %s", envFile)
useEnvFile = true
break
}
if i == len(envFiles)-1 {
useEnvFile = false
break
}
}

Expand Down
16 changes: 10 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ require (
github.com/iamacarpet/go-winpty v1.0.4
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/imroc/req/v3 v3.42.3
github.com/jackpal/gateway v1.0.16
github.com/joho/godotenv v1.5.1
github.com/kardianos/service v1.2.2
github.com/samber/lo v1.39.0
github.com/shirou/gopsutil/v4 v4.24.11
github.com/sirupsen/logrus v1.9.3
github.com/sourcegraph/conc v0.3.0
github.com/spf13/cast v1.7.1
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/tiendc/go-deepcopy v1.2.0
golang.org/x/crypto v0.25.0
golang.org/x/crypto v0.31.0
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.35.2
gorm.io/driver/mysql v1.5.2
Expand Down Expand Up @@ -102,7 +105,8 @@ require (
github.com/refraction-networking/utls v1.6.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/templexxx/cpu v0.1.1 // indirect
github.com/templexxx/xorsimd v0.4.3 // indirect
github.com/tjfoc/gmsm v1.4.1 // indirect
Expand All @@ -118,11 +122,11 @@ require (
golang.org/x/arch v0.3.0 // indirect
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240711142825-46eb208f015d // indirect
Expand Down
Loading

0 comments on commit e5772fa

Please sign in to comment.