Skip to content

Commit

Permalink
feat: fully support config file startup
Browse files Browse the repository at this point in the history
  • Loading branch information
VaalaCat committed Feb 24, 2024
1 parent a54d8c2 commit b1a148d
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ echo "Building frp-panel client only linux binaries..."
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o dist/frp-panel-client-linux-amd64 cmd/frppc/*.go
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o dist/frp-panel-client-linux-arm64 cmd/frppc/*.go
echo "Building frp-panel client only darwin binaries..."
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o dist/frp-panel-client-darwin-amd64 cmd/frpp/*.go
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o dist/frp-panel-client-darwin-amd64 cmd/frppc/*.go
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o dist/frp-panel-client-darwin-arm64 cmd/frppc/*.go

echo "Build Done!"
6 changes: 5 additions & 1 deletion cmd/frpp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import (
"github.com/sourcegraph/conc"
)

func runClient(clientID, clientSecret string) {
func runClient() {
var (
clientID = conf.Get().Client.ID
clientSecret = conf.Get().Client.Secret
)
crypto.DefaultSalt = conf.Get().App.Secret
logrus.Infof("start to run client")
if len(clientSecret) == 0 {
Expand Down
23 changes: 18 additions & 5 deletions cmd/frpp/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@ func initCommand() {
Use: "client [-s client secret] [-i client id] [-a app secret] [-r rpc host] [-c rpc port] [-p api port]",
Short: "run managed frpc",
Run: func(cmd *cobra.Command, args []string) {
patchConfig(rpcHost, appSecret, rpcPort, apiPort)
runClient(clientID, clientSecret)
patchConfig(rpcHost, appSecret,
clientID, clientSecret,
apiScheme, rpcPort, apiPort)
runClient()
},
}
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",
Run: func(cmd *cobra.Command, args []string) {
patchConfig(rpcHost, appSecret, rpcPort, apiPort)
runServer(clientID, clientSecret)
patchConfig(rpcHost, appSecret,
clientID, clientSecret,
apiScheme, rpcPort, apiPort)
runServer()
},
}
masterCmd = &cobra.Command{
Expand Down Expand Up @@ -82,7 +86,7 @@ func initLogger() {
logrus.SetReportCaller(true)
}

func patchConfig(host, secret string, rpcPort, apiPort int) {
func patchConfig(host, secret, clientID, clientSecret, apiScheme string, rpcPort, apiPort int) {
if len(host) != 0 {
conf.Get().Master.RPCHost = host
conf.Get().Master.APIHost = host
Expand All @@ -96,4 +100,13 @@ func patchConfig(host, secret string, rpcPort, apiPort int) {
if apiPort != 0 {
conf.Get().Master.APIPort = apiPort
}
if len(apiScheme) != 0 {
conf.Get().Master.APIScheme = apiScheme
}
if len(clientID) != 0 {
conf.Get().Client.ID = clientID
}
if len(clientSecret) != 0 {
conf.Get().Client.Secret = clientSecret
}
}
6 changes: 5 additions & 1 deletion cmd/frpp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import (
"github.com/sourcegraph/conc"
)

func runServer(clientID, clientSecret string) {
func runServer() {
var (
clientID = conf.Get().Client.ID
clientSecret = conf.Get().Client.Secret
)
crypto.DefaultSalt = conf.Get().App.Secret
logrus.Infof("start to run server")

Expand Down
6 changes: 5 additions & 1 deletion cmd/frppc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import (
"github.com/sourcegraph/conc"
)

func runClient(clientID, clientSecret string) {
func runClient() {
var (
clientID = conf.Get().Client.ID
clientSecret = conf.Get().Client.Secret
)
crypto.DefaultSalt = conf.Get().App.Secret
logrus.Infof("start to run client")
if len(clientSecret) == 0 {
Expand Down
17 changes: 14 additions & 3 deletions cmd/frppc/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ func initCommand() {
Use: "client [-s client secret] [-i client id] [-a app secret] [-r rpc host] [-c rpc port] [-p api port]",
Short: "run managed frpc",
Run: func(cmd *cobra.Command, args []string) {
patchConfig(rpcHost, appSecret, rpcPort, apiPort)
runClient(clientID, clientSecret)
patchConfig(rpcHost, appSecret,
clientID, clientSecret,
apiScheme, rpcPort, apiPort)
runClient()
},
}
rootCmd = &cobra.Command{
Expand All @@ -58,7 +60,7 @@ func initLogger() {
logrus.SetReportCaller(true)
}

func patchConfig(host, secret string, rpcPort, apiPort int) {
func patchConfig(host, secret, clientID, clientSecret, apiScheme string, rpcPort, apiPort int) {
if len(host) != 0 {
conf.Get().Master.RPCHost = host
conf.Get().Master.APIHost = host
Expand All @@ -72,4 +74,13 @@ func patchConfig(host, secret string, rpcPort, apiPort int) {
if apiPort != 0 {
conf.Get().Master.APIPort = apiPort
}
if len(apiScheme) != 0 {
conf.Get().Master.APIScheme = apiScheme
}
if len(clientID) != 0 {
conf.Get().Client.ID = clientID
}
if len(clientSecret) != 0 {
conf.Get().Client.Secret = clientSecret
}
}
6 changes: 5 additions & 1 deletion conf/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ type Config struct {
Type string `env:"TYPE" env-default:"sqlite3" env-description:"db type, mysql or sqlite3 and so on"`
DSN string `env:"DSN" env-default:"data.db" env-description:"db dsn, for sqlite is path, other is dsn, look at https://github.com/go-sql-driver/mysql#dsn-data-source-name"`
} `env-prefix:"DB_"`
Client struct {
ID string `env:"ID" env-description:"client id"`
Secret string `env:"SECRET" env-description:"client secret"`
} `env-prefix:"CLIENT_"`
}

var (
Expand All @@ -56,7 +60,7 @@ func InitConfig() {
)

if err = godotenv.Load(); err != nil {
logrus.Infof("Error loading .env file, will use runtime env")
logrus.WithError(err).Infof("Error loading .env file, will use runtime env")
}

cfg := Config{}
Expand Down
27 changes: 25 additions & 2 deletions www/components/client_item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from '@/components/ui/dropdown-menu'
import { useToast } from './ui/use-toast'
import React, { useState } from 'react'
import { ExecCommandStr, LinuxInstallCommand, WindowsInstallCommand } from '@/lib/consts'
import { ClientEnvFile, ExecCommandStr, LinuxInstallCommand, WindowsInstallCommand } from '@/lib/consts'
import { useMutation, useQuery } from '@tanstack/react-query'
import { deleteClient, listClient } from '@/api/client'
import { useRouter } from 'next/router'
Expand Down Expand Up @@ -211,6 +211,15 @@ export const ClientActions: React.FC<ClientItemProps> = ({ client, table }) => {
},
})

const createAndDownloadFile = (fileName: string, content: string) => {
var aTag = document.createElement('a');
var blob = new Blob([content]);
aTag.download = fileName;
aTag.href = URL.createObjectURL(blob);
aTag.click();
URL.revokeObjectURL(aTag.href);
}

return (
<Dialog>
<DropdownMenu>
Expand Down Expand Up @@ -246,6 +255,20 @@ export const ClientActions: React.FC<ClientItemProps> = ({ client, table }) => {
>
修改客户端配置
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => {
try {
if (platformInfo) {
createAndDownloadFile(`.env`, ClientEnvFile(client, platformInfo))
}
}
catch (error) {
toast({ description: '获取平台信息失败' })
}
}}
>
下载配置文件
</DropdownMenuItem>
<DialogTrigger asChild>
<DropdownMenuItem className="text-destructive">删除</DropdownMenuItem>
</DialogTrigger>
Expand All @@ -269,6 +292,6 @@ export const ClientActions: React.FC<ClientItemProps> = ({ client, table }) => {
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
</Dialog >
)
}
26 changes: 25 additions & 1 deletion www/components/server_item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from '@/components/ui/dropdown-menu'
import { useToast } from './ui/use-toast'
import React, { useState } from 'react'
import { ExecCommandStr, LinuxInstallCommand, WindowsInstallCommand } from '@/lib/consts'
import { ClientEnvFile, ExecCommandStr, LinuxInstallCommand, WindowsInstallCommand } from '@/lib/consts'
import { useMutation, useQuery } from '@tanstack/react-query'
import { deleteServer, listServer } from '@/api/server'
import { useRouter } from 'next/router'
Expand Down Expand Up @@ -221,6 +221,16 @@ export const ServerActions: React.FC<ServerItemProps> = ({ server, table }) => {
toast({ description: '删除失败' })
},
})

const createAndDownloadFile = (fileName: string, content: string) => {
var aTag = document.createElement('a');
var blob = new Blob([content]);
aTag.download = fileName;
aTag.href = URL.createObjectURL(blob);
aTag.click();
URL.revokeObjectURL(aTag.href);
}

return (
<Dialog>
<DropdownMenu>
Expand Down Expand Up @@ -261,6 +271,20 @@ export const ServerActions: React.FC<ServerItemProps> = ({ server, table }) => {
>
修改服务端配置
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => {
try {
if (platformInfo) {
createAndDownloadFile(`.env`, ClientEnvFile(server, platformInfo))
}
}
catch (error) {
toast({ description: '获取平台信息失败' })
}
}}
>
下载配置文件
</DropdownMenuItem>
<DialogTrigger asChild>
<DropdownMenuItem className="text-destructive">删除</DropdownMenuItem>
</DialogTrigger>
Expand Down
19 changes: 16 additions & 3 deletions www/lib/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ export const ExecCommandStr = <T extends Client | Server>(
info: GetPlatformInfoResponse,
fileName?: string,
) => {
return `${fileName || 'frp-panel'} ${type} -s ${item.secret} -i ${item.id} -a ${info.globalSecret} -r ${
info.masterRpcHost
} -c ${info.masterRpcPort} -p ${info.masterApiPort} -e ${info.masterApiScheme}`
return `${fileName || 'frp-panel'} ${type} -s ${item.secret} -i ${item.id} -a ${info.globalSecret} -r ${info.masterRpcHost
} -c ${info.masterRpcPort} -p ${info.masterApiPort} -e ${info.masterApiScheme}`
}

export const WindowsInstallCommand = <T extends Client | Server>(
Expand All @@ -47,3 +46,17 @@ export const LinuxInstallCommand = <T extends Client | Server>(
) => {
return `curl -sSL https://raw.githubusercontent.com/VaalaCat/frp-panel/main/install.sh | bash -s --${ExecCommandStr(type, item, info, ' ')}`
}

export const ClientEnvFile = <T extends Client | Server>(
item: T,
info: GetPlatformInfoResponse,
) => {
return `CLIENT_ID=${item.id}
CLIENT_SECRET=${item.secret}
APP_SECRET=${info.globalSecret}
MASTER_RPC_HOST=${info.masterRpcHost}
MASTER_RPC_PORT=${info.masterRpcPort}
MASTER_API_HOST=${info.masterRpcHost}
MASTER_API_PORT=${info.masterApiPort}
MASTER_API_SCHEME=${info.masterApiScheme}`
}

0 comments on commit b1a148d

Please sign in to comment.