Skip to content

Commit 69ae2b0

Browse files
authored
optimize some code (#3801)
1 parent d5b41f1 commit 69ae2b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+880
-600
lines changed

Release.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
### Features
22

3-
* New command line parameter `--strict_config` is added to enable strict configuration validation mode. It will throw an error for non-existent fields instead of ignoring them.
3+
* New command line parameter `--strict_config` is added to enable strict configuration validation mode. It will throw an error for non-existent fields instead of ignoring them. In future versions, we may set the default value of this parameter to true.
4+
* Support `SSH reverse tunneling`. With this feature, you can expose your local service without running frpc, only using SSH. The SSH reverse tunnel agent has many functional limitations compared to the frpc agent. The currently supported proxy types are tcp, http, https, tcpmux, and stcp.
5+
* The frpc tcpmux command line parameters have been updated to support configuring `http_user` and `http_pwd`.
6+
* The frpc stcp/sudp/xtcp command line parameters have been updated to support configuring `allow_users`.
47

58
### Fixes
69

client/admin.go

Lines changed: 0 additions & 85 deletions
This file was deleted.

client/admin_api.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,39 @@ import (
3131
"github.com/fatedier/frp/client/proxy"
3232
"github.com/fatedier/frp/pkg/config"
3333
"github.com/fatedier/frp/pkg/config/v1/validation"
34+
httppkg "github.com/fatedier/frp/pkg/util/http"
3435
"github.com/fatedier/frp/pkg/util/log"
36+
netpkg "github.com/fatedier/frp/pkg/util/net"
3537
)
3638

3739
type GeneralResponse struct {
3840
Code int
3941
Msg string
4042
}
4143

44+
func (svr *Service) registerRouteHandlers(helper *httppkg.RouterRegisterHelper) {
45+
helper.Router.HandleFunc("/healthz", svr.healthz)
46+
subRouter := helper.Router.NewRoute().Subrouter()
47+
48+
subRouter.Use(helper.AuthMiddleware.Middleware)
49+
50+
// api, see admin_api.go
51+
subRouter.HandleFunc("/api/reload", svr.apiReload).Methods("GET")
52+
subRouter.HandleFunc("/api/stop", svr.apiStop).Methods("POST")
53+
subRouter.HandleFunc("/api/status", svr.apiStatus).Methods("GET")
54+
subRouter.HandleFunc("/api/config", svr.apiGetConfig).Methods("GET")
55+
subRouter.HandleFunc("/api/config", svr.apiPutConfig).Methods("PUT")
56+
57+
// view
58+
subRouter.Handle("/favicon.ico", http.FileServer(helper.AssetsFS)).Methods("GET")
59+
subRouter.PathPrefix("/static/").Handler(
60+
netpkg.MakeHTTPGzipHandler(http.StripPrefix("/static/", http.FileServer(helper.AssetsFS))),
61+
).Methods("GET")
62+
subRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
63+
http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
64+
})
65+
}
66+
4267
// /healthz
4368
func (svr *Service) healthz(w http.ResponseWriter, _ *http.Request) {
4469
w.WriteHeader(200)
@@ -62,21 +87,21 @@ func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request) {
6287
}
6388
}()
6489

65-
cliCfg, pxyCfgs, visitorCfgs, _, err := config.LoadClientConfig(svr.cfgFile, strictConfigMode)
90+
cliCfg, proxyCfgs, visitorCfgs, _, err := config.LoadClientConfig(svr.configFilePath, strictConfigMode)
6691
if err != nil {
6792
res.Code = 400
6893
res.Msg = err.Error()
6994
log.Warn("reload frpc proxy config error: %s", res.Msg)
7095
return
7196
}
72-
if _, err := validation.ValidateAllClientConfig(cliCfg, pxyCfgs, visitorCfgs); err != nil {
97+
if _, err := validation.ValidateAllClientConfig(cliCfg, proxyCfgs, visitorCfgs); err != nil {
7398
res.Code = 400
7499
res.Msg = err.Error()
75100
log.Warn("reload frpc proxy config error: %s", res.Msg)
76101
return
77102
}
78103

79-
if err := svr.ReloadConf(pxyCfgs, visitorCfgs); err != nil {
104+
if err := svr.UpdateAllConfigurer(proxyCfgs, visitorCfgs); err != nil {
80105
res.Code = 500
81106
res.Msg = err.Error()
82107
log.Warn("reload frpc proxy config error: %s", res.Msg)
@@ -158,7 +183,7 @@ func (svr *Service) apiStatus(w http.ResponseWriter, _ *http.Request) {
158183

159184
ps := ctl.pm.GetAllProxyStatus()
160185
for _, status := range ps {
161-
res[status.Type] = append(res[status.Type], NewProxyStatusResp(status, svr.cfg.ServerAddr))
186+
res[status.Type] = append(res[status.Type], NewProxyStatusResp(status, svr.common.ServerAddr))
162187
}
163188

164189
for _, arrs := range res {
@@ -184,14 +209,14 @@ func (svr *Service) apiGetConfig(w http.ResponseWriter, _ *http.Request) {
184209
}
185210
}()
186211

187-
if svr.cfgFile == "" {
212+
if svr.configFilePath == "" {
188213
res.Code = 400
189214
res.Msg = "frpc has no config file path"
190215
log.Warn("%s", res.Msg)
191216
return
192217
}
193218

194-
content, err := os.ReadFile(svr.cfgFile)
219+
content, err := os.ReadFile(svr.configFilePath)
195220
if err != nil {
196221
res.Code = 400
197222
res.Msg = err.Error()
@@ -230,7 +255,7 @@ func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
230255
return
231256
}
232257

233-
if err := os.WriteFile(svr.cfgFile, body, 0o644); err != nil {
258+
if err := os.WriteFile(svr.configFilePath, body, 0o644); err != nil {
234259
res.Code = 500
235260
res.Msg = fmt.Sprintf("write content to frpc config file error: %v", err)
236261
log.Warn("%s", res.Msg)

client/connector.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net"
2222
"strconv"
2323
"strings"
24+
"sync"
2425
"time"
2526

2627
libdial "github.com/fatedier/golib/net/dial"
@@ -30,7 +31,7 @@ import (
3031

3132
v1 "github.com/fatedier/frp/pkg/config/v1"
3233
"github.com/fatedier/frp/pkg/transport"
33-
utilnet "github.com/fatedier/frp/pkg/util/net"
34+
netpkg "github.com/fatedier/frp/pkg/util/net"
3435
"github.com/fatedier/frp/pkg/util/xlog"
3536
)
3637

@@ -48,6 +49,7 @@ type defaultConnectorImpl struct {
4849

4950
muxSession *fmux.Session
5051
quicConn quic.Connection
52+
closeOnce sync.Once
5153
}
5254

5355
func NewConnector(ctx context.Context, cfg *v1.ClientCommonConfig) Connector {
@@ -130,7 +132,7 @@ func (c *defaultConnectorImpl) Connect() (net.Conn, error) {
130132
if err != nil {
131133
return nil, err
132134
}
133-
return utilnet.QuicStreamToNetConn(stream, c.quicConn), nil
135+
return netpkg.QuicStreamToNetConn(stream, c.quicConn), nil
134136
} else if c.muxSession != nil {
135137
stream, err := c.muxSession.OpenStream()
136138
if err != nil {
@@ -177,19 +179,19 @@ func (c *defaultConnectorImpl) realConnect() (net.Conn, error) {
177179
switch protocol {
178180
case "websocket":
179181
protocol = "tcp"
180-
dialOptions = append(dialOptions, libdial.WithAfterHook(libdial.AfterHook{Hook: utilnet.DialHookWebsocket(protocol, "")}))
182+
dialOptions = append(dialOptions, libdial.WithAfterHook(libdial.AfterHook{Hook: netpkg.DialHookWebsocket(protocol, "")}))
181183
dialOptions = append(dialOptions, libdial.WithAfterHook(libdial.AfterHook{
182-
Hook: utilnet.DialHookCustomTLSHeadByte(tlsConfig != nil, lo.FromPtr(c.cfg.Transport.TLS.DisableCustomTLSFirstByte)),
184+
Hook: netpkg.DialHookCustomTLSHeadByte(tlsConfig != nil, lo.FromPtr(c.cfg.Transport.TLS.DisableCustomTLSFirstByte)),
183185
}))
184186
dialOptions = append(dialOptions, libdial.WithTLSConfig(tlsConfig))
185187
case "wss":
186188
protocol = "tcp"
187189
dialOptions = append(dialOptions, libdial.WithTLSConfigAndPriority(100, tlsConfig))
188190
// Make sure that if it is wss, the websocket hook is executed after the tls hook.
189-
dialOptions = append(dialOptions, libdial.WithAfterHook(libdial.AfterHook{Hook: utilnet.DialHookWebsocket(protocol, tlsConfig.ServerName), Priority: 110}))
191+
dialOptions = append(dialOptions, libdial.WithAfterHook(libdial.AfterHook{Hook: netpkg.DialHookWebsocket(protocol, tlsConfig.ServerName), Priority: 110}))
190192
default:
191193
dialOptions = append(dialOptions, libdial.WithAfterHook(libdial.AfterHook{
192-
Hook: utilnet.DialHookCustomTLSHeadByte(tlsConfig != nil, lo.FromPtr(c.cfg.Transport.TLS.DisableCustomTLSFirstByte)),
194+
Hook: netpkg.DialHookCustomTLSHeadByte(tlsConfig != nil, lo.FromPtr(c.cfg.Transport.TLS.DisableCustomTLSFirstByte)),
193195
}))
194196
dialOptions = append(dialOptions, libdial.WithTLSConfig(tlsConfig))
195197
}
@@ -213,11 +215,13 @@ func (c *defaultConnectorImpl) realConnect() (net.Conn, error) {
213215
}
214216

215217
func (c *defaultConnectorImpl) Close() error {
216-
if c.quicConn != nil {
217-
_ = c.quicConn.CloseWithError(0, "")
218-
}
219-
if c.muxSession != nil {
220-
_ = c.muxSession.Close()
221-
}
218+
c.closeOnce.Do(func() {
219+
if c.quicConn != nil {
220+
_ = c.quicConn.CloseWithError(0, "")
221+
}
222+
if c.muxSession != nil {
223+
_ = c.muxSession.Close()
224+
}
225+
})
222226
return nil
223227
}

0 commit comments

Comments
 (0)