Skip to content

Commit 192f7c1

Browse files
committed
feat: support change svc name and rolllback frp-auth
1 parent aefb72f commit 192f7c1

File tree

8 files changed

+57
-24
lines changed

8 files changed

+57
-24
lines changed

biz/master/server/update_tunnel.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66

77
"github.com/VaalaCat/frp-panel/common"
88
"github.com/VaalaCat/frp-panel/conf"
9+
"github.com/VaalaCat/frp-panel/defs"
910
"github.com/VaalaCat/frp-panel/pb"
1011
"github.com/VaalaCat/frp-panel/services/app"
1112
"github.com/VaalaCat/frp-panel/services/dao"
1213
"github.com/VaalaCat/frp-panel/services/rpc"
1314
"github.com/VaalaCat/frp-panel/utils"
1415
"github.com/VaalaCat/frp-panel/utils/logger"
1516
v1 "github.com/fatedier/frp/pkg/config/v1"
17+
"github.com/samber/lo"
1618
)
1719

1820
func UpdateFrpsHander(c *app.Context, req *pb.UpdateFRPSRequest) (*pb.UpdateFRPSResponse, error) {
@@ -40,7 +42,10 @@ func UpdateFrpsHander(c *app.Context, req *pb.UpdateFRPSRequest) (*pb.UpdateFRPS
4042
return nil, err
4143
}
4244

43-
srvCfg.HTTPPlugins = []v1.HTTPPluginOptions{conf.FRPsAuthOption(c.GetApp().GetConfig())}
45+
lo.Filter(srvCfg.HTTPPlugins, func(item v1.HTTPPluginOptions, _ int) bool {
46+
return item.Name != defs.FRP_Plugin_Multiuser
47+
})
48+
srvCfg.HTTPPlugins = append(srvCfg.HTTPPlugins, conf.FRPsAuthOption(c.GetApp().GetConfig()))
4449

4550
if err := srv.SetConfigContent(srvCfg); err != nil {
4651
logger.Logger(context.Background()).WithError(err).Errorf("cannot set server config")

biz/server/rpc_pull_config.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ import (
44
"context"
55
"reflect"
66

7+
"github.com/VaalaCat/frp-panel/conf"
8+
"github.com/VaalaCat/frp-panel/defs"
79
"github.com/VaalaCat/frp-panel/pb"
810
"github.com/VaalaCat/frp-panel/services/app"
911
"github.com/VaalaCat/frp-panel/services/server"
1012
"github.com/VaalaCat/frp-panel/utils"
1113
"github.com/VaalaCat/frp-panel/utils/logger"
14+
v1 "github.com/fatedier/frp/pkg/config/v1"
15+
"github.com/samber/lo"
1216
)
1317

1418
func PullConfig(appInstance app.Application, serverID, serverSecret string) error {
15-
ctx := context.Background()
19+
ctx := app.NewContext(context.Background(), appInstance)
20+
1621
logger.Logger(ctx).Infof("start to pull server config, serverID: [%s]", serverID)
1722

1823
cli := appInstance.GetMasterCli()
@@ -50,9 +55,22 @@ func PullConfig(appInstance app.Application, serverID, serverSecret string) erro
5055
return nil
5156
}
5257
}
58+
59+
InjectAuthPlugin(ctx, s)
60+
5361
ctrl.Add(serverID, server.NewServerHandler(s))
5462
ctrl.Run(serverID)
5563

5664
logger.Logger(ctx).Infof("pull server config success, serverID: [%s]", serverID)
5765
return nil
5866
}
67+
68+
func InjectAuthPlugin(ctx *app.Context, cfg *v1.ServerConfig) {
69+
cfg.HTTPPlugins = lo.Filter(cfg.HTTPPlugins, func(item v1.HTTPPluginOptions, _ int) bool {
70+
return item.Name != defs.FRP_Plugin_Multiuser
71+
})
72+
cfg.HTTPPlugins = append(
73+
cfg.HTTPPlugins,
74+
conf.FRPsAuthOption(ctx.GetApp().GetConfig()),
75+
)
76+
}

biz/server/update_tunnel.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ func UpdateFrpsHander(ctx *app.Context, req *pb.UpdateFRPSRequest) (*pb.UpdateFR
3737
}, nil
3838
}
3939
}
40+
41+
InjectAuthPlugin(ctx, s)
42+
4043
ctx.GetApp().GetServerController().Add(serverID, server.NewServerHandler(s))
4144
ctx.GetApp().GetServerController().Run(serverID)
4245

cmd/frpp/shared/cmd.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func NewMasterCmd(cfg conf.Config, fs embed.FS) *cobra.Command {
183183
}
184184
}
185185

186-
if srv, err := utils.CreateSystemService(args, run); err != nil {
186+
if srv, err := utils.CreateSystemService(defs.DefaultServiceName, args, run); err != nil {
187187
run()
188188
} else {
189189
srv.Run()
@@ -225,7 +225,7 @@ func NewClientCmd(cfg conf.Config) *cobra.Command {
225225
logger.Logger(context.Background()).Fatalf("clientApp FX Application Error: %v", err)
226226
}
227227
}
228-
if srv, err := utils.CreateSystemService(args, run); err != nil {
228+
if srv, err := utils.CreateSystemService(defs.DefaultServiceName, args, run); err != nil {
229229
run()
230230
} else {
231231
srv.Run()
@@ -270,7 +270,7 @@ func NewServerCmd(cfg conf.Config) *cobra.Command {
270270
logger.Logger(context.Background()).Fatalf("serverApp FX Application Error: %v", err)
271271
}
272272
}
273-
if srv, err := utils.CreateSystemService(args, run); err != nil {
273+
if srv, err := utils.CreateSystemService(defs.DefaultServiceName, args, run); err != nil {
274274
run()
275275
} else {
276276
srv.Run()
@@ -290,7 +290,7 @@ func NewInstallServiceCmd() *cobra.Command {
290290
DisableFlagParsing: true,
291291
DisableFlagsInUseLine: true,
292292
Run: func(cmd *cobra.Command, args []string) {
293-
utils.ControlSystemService(args, "install", func() {})
293+
utils.ControlSystemService(defs.DefaultServiceName, args, "install", func() {})
294294
},
295295
}
296296
}
@@ -302,7 +302,7 @@ func NewUninstallServiceCmd() *cobra.Command {
302302
DisableFlagParsing: true,
303303
DisableFlagsInUseLine: true,
304304
Run: func(cmd *cobra.Command, args []string) {
305-
utils.ControlSystemService(args, "uninstall", func() {})
305+
utils.ControlSystemService(defs.DefaultServiceName, args, "uninstall", func() {})
306306
},
307307
}
308308
}
@@ -314,7 +314,7 @@ func NewStartServiceCmd() *cobra.Command {
314314
DisableFlagParsing: true,
315315
DisableFlagsInUseLine: true,
316316
Run: func(cmd *cobra.Command, args []string) {
317-
utils.ControlSystemService(args, "start", func() {})
317+
utils.ControlSystemService(defs.DefaultServiceName, args, "start", func() {})
318318
},
319319
}
320320
}
@@ -326,7 +326,7 @@ func NewStopServiceCmd() *cobra.Command {
326326
DisableFlagParsing: true,
327327
DisableFlagsInUseLine: true,
328328
Run: func(cmd *cobra.Command, args []string) {
329-
utils.ControlSystemService(args, "stop", func() {})
329+
utils.ControlSystemService(defs.DefaultServiceName, args, "stop", func() {})
330330
},
331331
}
332332
}
@@ -338,7 +338,7 @@ func NewRestartServiceCmd() *cobra.Command {
338338
DisableFlagParsing: true,
339339
DisableFlagsInUseLine: true,
340340
Run: func(cmd *cobra.Command, args []string) {
341-
utils.ControlSystemService(args, "restart", func() {})
341+
utils.ControlSystemService(defs.DefaultServiceName, args, "restart", func() {})
342342
},
343343
}
344344
}

conf/helper.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,17 @@ func ServerAPIListenAddr(cfg Config) string {
3737
}
3838

3939
func FRPsAuthOption(cfg Config) v1.HTTPPluginOptions {
40-
apiUrl := GetAPIURL(cfg)
41-
42-
parsedUrl, err := url.Parse(apiUrl)
40+
authUrl := fmt.Sprintf("http://%s:%d/auth", defs.LocalHost, cfg.Server.APIPort)
41+
parsedUrl, err := url.Parse(authUrl)
4342
if err != nil {
4443
logger.Logger(context.Background()).WithError(err).Fatalf("parse auth url error")
4544
}
45+
4646
return v1.HTTPPluginOptions{
47-
Name: "multiuser",
48-
Ops: []string{"Login"},
49-
Addr: parsedUrl.Host,
50-
Path: "/auth",
51-
TLSVerify: false,
47+
Name: defs.FRP_Plugin_Multiuser,
48+
Ops: []string{"Login"},
49+
Addr: parsedUrl.Host,
50+
Path: parsedUrl.Path,
5251
}
5352
}
5453

defs/const.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ const (
6565
const (
6666
DefaultServerID = "default"
6767
DefaultAdminUserID = 1
68+
DefaultServiceName = "frpp"
69+
)
70+
71+
const (
72+
LocalHost = "127.0.0.1"
73+
FRP_Plugin_Multiuser = "multiuser"
6874
)
6975

7076
const (

services/server/frps_service.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func NewServerHandler(svrCfg *v1.ServerConfig) app.ServerHandler {
2525

2626
warning, err := validation.ValidateServerConfig(svrCfg)
2727
if warning != nil {
28-
logger.Logger(context.Background()).WithError(err).Warnf("validate server config warning: %+v", warning)
28+
logger.Logger(ctx).WithError(err).Warnf("validate server config warning: %+v", warning)
2929
}
3030
if err != nil {
3131
logger.Logger(ctx).Panic(err)
@@ -34,9 +34,11 @@ func NewServerHandler(svrCfg *v1.ServerConfig) app.ServerHandler {
3434
var svr *server.Service
3535

3636
if svr, err = server.NewService(svrCfg); err != nil {
37-
logger.Logger(context.Background()).WithError(err).Panic("cannot create server, exit and restart")
37+
logger.Logger(ctx).WithError(err).Panic("cannot create server, exit and restart")
3838
}
3939

40+
logger.Logger(ctx).Debugf("create server, config is: [ %+v ]", svrCfg)
41+
4042
return &serverImpl{
4143
srv: svr,
4244
Common: svrCfg,

utils/system_service.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ func (ss *SystemService) iRun() {
3535
ss.run()
3636
}
3737

38-
func CreateSystemService(args []string, run func()) (service.Service, error) {
38+
func CreateSystemService(svcName string, args []string, run func()) (service.Service, error) {
3939
currentPath, err := os.Executable()
4040
if err != nil {
4141
return nil, fmt.Errorf("get current path failed, err: %v", err)
4242
}
4343

4444
svcConfig := &service.Config{
45-
Name: "frpp",
45+
Name: svcName,
4646
DisplayName: "frp-panel",
4747
Description: "this is frp-panel service, developed by [VaalaCat] - https://github.com/VaalaCat/frp-panel",
4848
Arguments: args,
@@ -60,11 +60,11 @@ func CreateSystemService(args []string, run func()) (service.Service, error) {
6060
return s, nil
6161
}
6262

63-
func ControlSystemService(args []string, action string, run func()) error {
63+
func ControlSystemService(svcName string, args []string, action string, run func()) error {
6464
ctx := context.Background()
6565

6666
logger.Logger(ctx).Info("try to ", action, " service, args:", args)
67-
s, err := CreateSystemService(args, run)
67+
s, err := CreateSystemService(svcName, args, run)
6868
if err != nil {
6969
logger.Logger(ctx).WithError(err).Error("create service controller failed")
7070
return err

0 commit comments

Comments
 (0)