Skip to content

Commit 78e751d

Browse files
committed
refactor: rewrite some code for future wireguard p2p mesh
1 parent 86fb44a commit 78e751d

File tree

18 files changed

+350
-145
lines changed

18 files changed

+350
-145
lines changed

biz/master/shell/mgr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package shell
22

33
import (
44
"github.com/VaalaCat/frp-panel/pb"
5+
"github.com/VaalaCat/frp-panel/services/app"
56
"github.com/VaalaCat/frp-panel/utils"
67
)
78

@@ -31,10 +32,9 @@ func (m *PTYMgr) Add(sessionID string, conn pb.Master_PTYConnectServer) {
3132
m.doneMap.Store(sessionID, make(chan bool))
3233
}
3334

34-
func NewPTYMgr() *PTYMgr {
35+
func NewPTYMgr() app.ShellPTYMgr {
3536
return &PTYMgr{
3637
SyncMap: &utils.SyncMap[string, pb.Master_PTYConnectServer]{},
3738
doneMap: &utils.SyncMap[string, chan bool]{},
3839
}
3940
}
40-

biz/master/streamlog/collect_log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (c *ClientLogManager) GetClientLock(clientId string) *sync.Mutex {
2828
return lock
2929
}
3030

31-
func NewClientLogManager() *ClientLogManager {
31+
func NewClientLogManager() app.ClientLogManager {
3232
return &ClientLogManager{
3333
SyncMap: &utils.SyncMap[string, chan string]{},
3434
clientLocksMap: &utils.SyncMap[string, *sync.Mutex]{},

cmd/frpp/shared/client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/VaalaCat/frp-panel/defs"
99
"github.com/VaalaCat/frp-panel/pb"
1010
"github.com/VaalaCat/frp-panel/services/app"
11+
"github.com/VaalaCat/frp-panel/services/rpc"
1112
"github.com/VaalaCat/frp-panel/services/rpcclient"
1213
"github.com/VaalaCat/frp-panel/services/tunnel"
1314
"github.com/VaalaCat/frp-panel/services/watcher"
@@ -25,7 +26,8 @@ type runClientParam struct {
2526
AppInstance app.Application
2627
TaskManager watcher.Client `name:"clientTaskManager"`
2728
WorkersManager app.WorkersManager
28-
Cfg conf.Config
29+
30+
Cfg conf.Config
2931
}
3032

3133
func runClient(param runClientParam) {
@@ -53,7 +55,7 @@ func runClient(param runClientParam) {
5355
param.Lc.Append(fx.Hook{
5456
OnStart: func(ctx context.Context) error {
5557
appInstance.SetRPCCred(NewClientCred(appInstance))
56-
appInstance.SetMasterCli(NewClientMasterCli(appInstance))
58+
appInstance.SetMasterCli(rpc.NewMasterCli(appInstance))
5759
appInstance.SetClientController(tunnel.NewClientController())
5860

5961
cliRpcHandler := rpcclient.NewClientRPCHandler(

cmd/frpp/shared/modules.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package shared
22

33
import (
4+
bizmaster "github.com/VaalaCat/frp-panel/biz/master"
5+
"github.com/VaalaCat/frp-panel/biz/master/shell"
6+
"github.com/VaalaCat/frp-panel/biz/master/streamlog"
7+
bizserver "github.com/VaalaCat/frp-panel/biz/server"
8+
"github.com/VaalaCat/frp-panel/conf"
9+
"github.com/VaalaCat/frp-panel/services/rpc"
10+
"github.com/VaalaCat/frp-panel/utils/logger"
411
"go.uber.org/fx"
512
)
613

@@ -14,21 +21,22 @@ var (
1421

1522
serverMod = fx.Module("cmd.server", fx.Provide(
1623
fx.Annotate(NewServerAPI, fx.ResultTags(`name:"serverApiService"`)),
17-
fx.Annotate(NewServerRouter, fx.ResultTags(`name:"serverRouter"`)),
24+
fx.Annotate(bizserver.NewRouter, fx.ResultTags(`name:"serverRouter"`)),
1825
fx.Annotate(NewWatcher, fx.ResultTags(`name:"serverTaskManager"`)),
1926
))
2027

2128
masterMod = fx.Module("cmd.master", fx.Provide(
2229
NewPermissionManager,
2330
NewEnforcer,
24-
NewListenerOptions,
31+
conf.GetListener,
2532
NewDBManager,
2633
NewWSListener,
2734
NewMasterTLSConfig,
2835
NewWSUpgrader,
29-
NewClientLogManager,
36+
streamlog.NewClientLogManager,
37+
// wireguard.NewWireGuardManager,
3038
fx.Annotate(NewWatcher, fx.ResultTags(`name:"masterTaskManager"`)),
31-
fx.Annotate(NewMasterRouter, fx.ResultTags(`name:"masterRouter"`)),
39+
fx.Annotate(bizmaster.NewRouter, fx.ResultTags(`name:"masterRouter"`)),
3240
fx.Annotate(NewHTTPMasterService, fx.ResultTags(`name:"httpMasterService"`)),
3341
fx.Annotate(NewHTTPMasterService, fx.ResultTags(`name:"wsMasterService"`)),
3442
fx.Annotate(NewTLSMasterService, fx.ResultTags(`name:"tlsMasterService"`)),
@@ -38,12 +46,15 @@ var (
3846
))
3947

4048
commonMod = fx.Module("common", fx.Provide(
49+
logger.Logger,
50+
logger.Instance,
4151
NewLogHookManager,
42-
NewPTYManager,
52+
shell.NewPTYMgr,
4353
NewBaseApp,
4454
NewContext,
45-
NewClientsManager,
46-
NewAutoJoin,
55+
NewAndFinishNormalContext,
56+
rpc.NewClientsManager,
57+
NewAutoJoin, // provide final config
4758
fx.Annotate(NewPatchedConfig, fx.ResultTags(`name:"argsPatchedConfig"`)),
4859
))
4960
)

cmd/frpp/shared/providers.go

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@ package shared
33
import (
44
"context"
55
"crypto/tls"
6-
"embed"
76
"net"
87
"net/http"
98
"os"
109
"path/filepath"
1110
"sync"
1211

1312
bizcommon "github.com/VaalaCat/frp-panel/biz/common"
14-
bizmaster "github.com/VaalaCat/frp-panel/biz/master"
15-
"github.com/VaalaCat/frp-panel/biz/master/shell"
16-
"github.com/VaalaCat/frp-panel/biz/master/streamlog"
17-
bizserver "github.com/VaalaCat/frp-panel/biz/server"
1813
"github.com/VaalaCat/frp-panel/conf"
1914
"github.com/VaalaCat/frp-panel/defs"
2015
"github.com/VaalaCat/frp-panel/models"
@@ -43,12 +38,14 @@ import (
4338
"gorm.io/gorm"
4439
)
4540

46-
func NewLogHookManager() app.StreamLogHookMgr {
47-
return &bizcommon.HookMgr{}
41+
type Finish struct {
42+
fx.Out
43+
44+
Context context.Context
4845
}
4946

50-
func NewPTYManager() app.ShellPTYMgr {
51-
return shell.NewPTYMgr()
47+
func NewLogHookManager() app.StreamLogHookMgr {
48+
return &bizcommon.HookMgr{}
5249
}
5350

5451
func NewBaseApp(param struct {
@@ -68,10 +65,6 @@ func NewBaseApp(param struct {
6865
return appInstance
6966
}
7067

71-
func NewClientsManager() app.ClientsManager {
72-
return rpc.NewClientsManager()
73-
}
74-
7568
func NewPatchedConfig(param struct {
7669
fx.In
7770

@@ -88,8 +81,16 @@ func NewContext(appInstance app.Application) *app.Context {
8881
return app.NewContext(context.Background(), appInstance)
8982
}
9083

91-
func NewClientLogManager() app.ClientLogManager {
92-
return streamlog.NewClientLogManager()
84+
func NewAndFinishNormalContext(param struct {
85+
fx.In
86+
87+
Ctx *app.Context
88+
Cfg conf.Config
89+
}) Finish {
90+
91+
return Finish{
92+
Context: param.Ctx,
93+
}
9394
}
9495

9596
func NewDBManager(ctx *app.Context, appInstance app.Application) app.DBManager {
@@ -151,14 +152,6 @@ func NewMasterTLSConfig(ctx *app.Context) *tls.Config {
151152
return dao.NewQuery(ctx).InitCert(conf.GetCertTemplate(ctx.GetApp().GetConfig()))
152153
}
153154

154-
func NewMasterRouter(fs embed.FS, appInstance app.Application) *gin.Engine {
155-
return bizmaster.NewRouter(fs, appInstance)
156-
}
157-
158-
func NewListenerOptions(ctx *app.Context, cfg conf.Config) conf.LisOpt {
159-
return conf.GetListener(ctx, cfg)
160-
}
161-
162155
func NewTLSMasterService(appInstance app.Application, masterTLSConfig *tls.Config) master.MasterService {
163156
return master.NewMasterService(appInstance, credentials.NewTLS(masterTLSConfig))
164157
}
@@ -167,14 +160,6 @@ func NewHTTPMasterService(appInstance app.Application) master.MasterService {
167160
return master.NewMasterService(appInstance, insecure.NewCredentials())
168161
}
169162

170-
func NewServerMasterCli(appInstance app.Application) app.MasterClient {
171-
return rpc.NewMasterCli(appInstance)
172-
}
173-
174-
func NewClientMasterCli(appInstance app.Application) app.MasterClient {
175-
return rpc.NewMasterCli(appInstance)
176-
}
177-
178163
func NewMux(param struct {
179164
fx.In
180165

@@ -214,10 +199,6 @@ func NewWSUpgrader(ctx *app.Context, cfg conf.Config) *websocket.Upgrader {
214199
}
215200
}
216201

217-
func NewServerRouter(appInstance app.Application) *gin.Engine {
218-
return bizserver.NewRouter(appInstance)
219-
}
220-
221202
func NewServerAPI(param struct {
222203
fx.In
223204
Ctx *app.Context
@@ -305,7 +286,7 @@ func NewAutoJoin(param struct {
305286
Ctx *app.Context
306287
Cfg conf.Config `name:"argsPatchedConfig"`
307288
CommonArgs CommonArgs
308-
}) conf.Config {
289+
}) conf.Config { // provide final config
309290
var (
310291
ctx = param.Ctx
311292
clientID = param.Cfg.Client.ID

cmd/frpp/shared/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/VaalaCat/frp-panel/defs"
99
"github.com/VaalaCat/frp-panel/pb"
1010
"github.com/VaalaCat/frp-panel/services/app"
11+
"github.com/VaalaCat/frp-panel/services/rpc"
1112
"github.com/VaalaCat/frp-panel/services/rpcclient"
1213
"github.com/VaalaCat/frp-panel/services/tunnel"
1314
"github.com/VaalaCat/frp-panel/services/watcher"
@@ -52,7 +53,7 @@ func runServer(param runServerParam) {
5253
OnStart: func(ctx context.Context) error {
5354
logger.Logger(ctx).Infof("start to run server, serverID: [%s]", clientID)
5455
appInstance.SetRPCCred(NewServerCred(appInstance))
55-
appInstance.SetMasterCli(NewServerMasterCli(appInstance))
56+
appInstance.SetMasterCli(rpc.NewMasterCli(appInstance))
5657

5758
cliHandler := rpcclient.NewClientRPCHandler(
5859
appInstance,

defs/const.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,12 @@ const (
175175
FrpProxyAnnotationsKey_WorkerId = "worker_id"
176176
FrpProxyAnnotationsKey_LoadBalancerGroup = "load_balancer_group"
177177
)
178+
179+
const (
180+
PlaceholderPrivateKey = "<REPLACE_WITH_YOUR_PRIVATE_KEY>"
181+
PlaceholderPeerVPNAddressCIDR = "<PEER_VPN_IP_ADDRESS/PREFIX>"
182+
)
183+
184+
var VaalaMagicBytes = []byte("vaala-ping")
185+
186+
const VaalaMagicBytesCookie = uint32(1630367849)

defs/types_rpc.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package defs
2+
3+
import "github.com/VaalaCat/frp-panel/pb"
4+
5+
type Connector struct {
6+
CliID string
7+
Conn pb.Master_ServerSendServer
8+
CliType string
9+
}

services/app/app_impl.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package app
22

33
import (
4+
"context"
45
"sync"
56

67
"github.com/VaalaCat/frp-panel/conf"
8+
"github.com/VaalaCat/frp-panel/pb"
9+
"github.com/VaalaCat/frp-panel/utils/logger"
710
"github.com/casbin/casbin/v2"
11+
"github.com/sirupsen/logrus"
812
"google.golang.org/grpc/credentials"
913
)
1014

@@ -28,6 +32,33 @@ type application struct {
2832
enforcer *casbin.Enforcer
2933
workerExecManager WorkerExecManager
3034
workersManager WorkersManager
35+
36+
loggerInstance *logrus.Logger
37+
}
38+
39+
func (a *application) GetClientBase() *pb.ClientBase {
40+
return &pb.ClientBase{
41+
ClientId: a.GetConfig().Client.ID,
42+
ClientSecret: a.GetConfig().Client.Secret,
43+
}
44+
}
45+
46+
func (a *application) GetServerBase() *pb.ServerBase {
47+
return &pb.ServerBase{
48+
ServerId: a.GetConfig().Client.ID,
49+
ServerSecret: a.GetConfig().Client.Secret,
50+
}
51+
}
52+
53+
func (a *application) SetLogger(l *logrus.Logger) {
54+
a.loggerInstance = l
55+
}
56+
57+
func (a *application) Logger(ctx context.Context) *logrus.Entry {
58+
if a.loggerInstance == nil {
59+
return logger.Logger(ctx)
60+
}
61+
return a.loggerInstance.WithContext(ctx)
3162
}
3263

3364
// GetWorkersManager implements Application.

services/app/application.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"sync"
66

77
"github.com/VaalaCat/frp-panel/conf"
8+
"github.com/VaalaCat/frp-panel/pb"
89
"github.com/casbin/casbin/v2"
910
"github.com/gin-gonic/gin"
11+
"github.com/sirupsen/logrus"
1012
"google.golang.org/grpc/credentials"
1113
)
1214

@@ -47,11 +49,16 @@ type Application interface {
4749
SetWorkerExecManager(WorkerExecManager)
4850
GetWorkersManager() WorkersManager
4951
SetWorkersManager(WorkersManager)
52+
SetLogger(*logrus.Logger)
53+
Logger(ctx context.Context) *logrus.Entry
54+
GetClientBase() *pb.ClientBase
55+
GetServerBase() *pb.ServerBase
5056
}
5157

5258
type Context struct {
5359
context.Context
54-
appInstance Application
60+
appInstance Application
61+
loggerInstance *logrus.Logger
5562
}
5663

5764
func (c *Context) GetApp() Application {
@@ -70,6 +77,31 @@ func (c *Context) Background() *Context {
7077
return NewContext(context.Background(), c.appInstance)
7178
}
7279

80+
func (c *Context) Copy() *Context {
81+
return NewContext(c.Context, c.appInstance)
82+
}
83+
84+
func (c *Context) CopyWithCancel() (*Context, context.CancelFunc) {
85+
ctx, cancel := context.WithCancel(c.Context)
86+
return NewContext(ctx, c.appInstance), cancel
87+
}
88+
89+
func (c *Context) BackgroundWithCancel() (*Context, context.CancelFunc) {
90+
ctx, cancel := context.WithCancel(context.Background())
91+
return NewContext(ctx, c.appInstance), cancel
92+
}
93+
94+
func (c *Context) Logger() *logrus.Entry {
95+
if c.loggerInstance != nil {
96+
return c.loggerInstance.WithContext(c)
97+
}
98+
return c.GetApp().Logger(c)
99+
}
100+
101+
func (c *Context) SetLogger(logger *logrus.Logger) {
102+
c.loggerInstance = logger
103+
}
104+
73105
func NewContext(c context.Context, appInstance Application) *Context {
74106
return &Context{
75107
Context: c,

0 commit comments

Comments
 (0)