Skip to content

Commit

Permalink
feat: spilit client
Browse files Browse the repository at this point in the history
  • Loading branch information
VaalaCat committed Feb 6, 2024
1 parent f4ba526 commit bf3e99e
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .drone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ steps:
- name: build - amd64
image: golang:1.21-alpine
commands:
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY=https://goproxy.cn,https://proxy.golang.org,direct go build -ldflags="-s -w" -o frp-panel-amd64 cmd/*.go
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY=https://goproxy.cn,https://proxy.golang.org,direct go build -ldflags="-s -w" -o frp-panel-amd64 cmd/frpp/*.go
volumes:
- name: gocache
path: /go/pkg/mod
Expand All @@ -59,7 +59,7 @@ steps:
- name: build - arm64
image: golang:1.21-alpine
commands:
- CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GOPROXY=https://goproxy.cn,https://proxy.golang.org,direct go build -ldflags="-s -w" -o frp-panel-arm64 cmd/*.go
- CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GOPROXY=https://goproxy.cn,https://proxy.golang.org,direct go build -ldflags="-s -w" -o frp-panel-arm64 cmd/frpp/*.go
volumes:
- name: gocache
path: /go/pkg/mod
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile.standalone
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ RUN apk update --no-cache && apk add --no-cache tzdata git
COPY go.mod go.sum ./
RUN CGO_ENABLED=0 GOPROXY=https://goproxy.cn,https://proxy.golang.org,direct go mod download
COPY . .
RUN rm -rf /app/cmd/out
COPY --from=frontend /app/out ./cmd/out
RUN CGO_ENABLED=0 GOPROXY=https://goproxy.cn,https://proxy.golang.org,direct go build -ldflags="-s -w" -o frp-panel cmd/*.go
RUN rm -rf /app/cmd/frpp/out
COPY --from=frontend /app/out ./cmd/frpp/out
RUN CGO_ENABLED=0 GOPROXY=https://goproxy.cn,https://proxy.golang.org,direct go build -ldflags="-s -w" -o frp-panel cmd/frpp/*.go

# Stage 3: Build image
FROM alpine:latest
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
## 项目使用说明
frp-panel可选docker和直接运行模式部署,直接部署请到release下载文件:[release](https://github.com/VaalaCat/frp-panel/releases)

注意:二进制有两种,一种是仅客户端,一种是全功能可执行文件,客户端版只能执行client命令

启动过后默认访问地址为 http://IP:9000

### docker
Expand Down
28 changes: 19 additions & 9 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ 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
echo "Building frp-panel full windows binaries..."
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o dist/frp-panel-amd64.exe cmd/frpp/*.go
CGO_ENABLED=0 GOOS=windows GOARCH=arm64 go build -o dist/frp-panel-arm64.exe cmd/frpp/*.go
echo "Building frp-panel full linux binaries..."
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o dist/frp-panel-linux-amd64 cmd/frpp/*.go
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o dist/frp-panel-linux-arm64 cmd/frpp/*.go
echo "Building frp-panel full darwin binaries..."
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o dist/frp-panel-darwin-amd64 cmd/frpp/*.go
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o dist/frp-panel-darwin-arm64 cmd/frpp/*.go

echo "Building frp-panel client only windows binaries..."
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o dist/frp-panel-client-amd64.exe cmd/frppc/*.go
CGO_ENABLED=0 GOOS=windows GOARCH=arm64 go build -o dist/frp-panel-client-arm64.exe cmd/frppc/*.go
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=arm64 go build -o dist/frp-panel-client-darwin-arm64 cmd/frppc/*.go

echo "Build Done!"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 58 additions & 0 deletions cmd/frppc/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
bizclient "github.com/VaalaCat/frp-panel/biz/client"
"github.com/VaalaCat/frp-panel/conf"
"github.com/VaalaCat/frp-panel/pb"
"github.com/VaalaCat/frp-panel/rpc"
"github.com/VaalaCat/frp-panel/services/rpcclient"
"github.com/VaalaCat/frp-panel/utils"
"github.com/VaalaCat/frp-panel/watcher"
"github.com/fatedier/golib/crypto"
"github.com/sirupsen/logrus"
"github.com/sourcegraph/conc"
)

func runClient(clientID, clientSecret string) {
crypto.DefaultSalt = conf.Get().App.Secret
logrus.Infof("start to run client")
if len(clientSecret) == 0 {
logrus.Fatal("client secret cannot be empty")
}

if len(clientID) == 0 {
logrus.Fatal("client id cannot be empty")
}

cred, err := utils.TLSClientCertNoValidate(rpc.GetClientCert(clientID, clientSecret, pb.ClientType_CLIENT_TYPE_FRPC))
if err != nil {
logrus.Fatal(err)
}
conf.ClientCred = cred

rpcclient.MustInitClientRPCSerivce(
clientID,
clientSecret,
pb.Event_EVENT_REGISTER_CLIENT,
bizclient.HandleServerMessage,
)
r := rpcclient.GetClientRPCSerivce()
defer r.Stop()

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

initClientOnce(clientID, clientSecret)

var wg conc.WaitGroup
wg.Go(r.Run)
wg.Go(w.Run)
wg.Wait()
}

func initClientOnce(clientID, clientSecret string) {
err := bizclient.PullConfig(clientID, clientSecret)
if err != nil {
logrus.WithError(err).Errorf("cannot pull client config, wait for retry")
}
}
75 changes: 75 additions & 0 deletions cmd/frppc/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"fmt"
"os"

"github.com/VaalaCat/frp-panel/conf"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
clientCmd *cobra.Command
rootCmd *cobra.Command
)

func initCommand() {
var (
clientSecret string
clientID string
rpcHost string
appSecret string
rpcPort int
apiPort int
apiScheme string
)
hostname, err := os.Hostname()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

clientCmd = &cobra.Command{
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)
},
}
rootCmd = &cobra.Command{
Use: "frp-panel-client",
Short: "frp-panel-client is a frp panel client QwQ",
}
rootCmd.AddCommand(clientCmd)
clientCmd.Flags().StringVarP(&clientSecret, "secret", "s", "", "client secret")
clientCmd.Flags().StringVarP(&clientID, "id", "i", hostname, "client id")
clientCmd.Flags().StringVarP(&rpcHost, "rpc", "r", "", "rpc host")
clientCmd.Flags().StringVarP(&appSecret, "app", "a", "", "app secret")

clientCmd.Flags().IntVarP(&rpcPort, "rpc-port", "c", 0, "rpc port")
clientCmd.Flags().IntVarP(&apiPort, "api-port", "p", 0, "api port")

clientCmd.Flags().StringVarP(&apiScheme, "api-scheme", "e", "", "api scheme")
}

func initLogger() {
logrus.SetReportCaller(true)
}

func patchConfig(host, secret string, rpcPort, apiPort int) {
if len(host) != 0 {
conf.Get().Master.RPCHost = host
conf.Get().Master.APIHost = host
}
if len(secret) != 0 {
conf.Get().App.Secret = secret
}
if rpcPort != 0 {
conf.Get().Master.RPCPort = rpcPort
}
if apiPort != 0 {
conf.Get().Master.APIPort = apiPort
}
}
15 changes: 15 additions & 0 deletions cmd/frppc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"github.com/VaalaCat/frp-panel/conf"
"github.com/VaalaCat/frp-panel/rpc"
)

func main() {
initLogger()
initCommand()
conf.InitConfig()
rpc.InitRPCClients()

rootCmd.Execute()
}
4 changes: 2 additions & 2 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build && rm -rf ../cmd/out && cp -r out ../cmd",
"build": "next build && rm -rf ../cmd/frpp/out && cp -r out ../cmd/frpp",
"start": "next start",
"lint": "next lint"
},
Expand Down Expand Up @@ -64,4 +64,4 @@
"tailwindcss": "^3.3.0",
"typescript": "^5"
}
}
}

0 comments on commit bf3e99e

Please sign in to comment.