diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 13b47c6950b..d24eaf818de 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -4,7 +4,3 @@ copilot:summary ### WHY - -### Walkthrough - -copilot:walkthrough diff --git a/README.md b/README.md index 82fb87d01cf..0ae937496c8 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,11 @@ frp also offers a P2P connect mode. * [Development Status](#development-status) + * [About V2](#about-v2) * [Architecture](#architecture) * [Example Usage](#example-usage) * [Access your computer in a LAN network via SSH](#access-your-computer-in-a-lan-network-via-ssh) + * [Multiple SSH services sharing the same port](#multiple-ssh-services-sharing-the-same-port) * [Accessing Internal Web Services with Custom Domains in LAN](#accessing-internal-web-services-with-custom-domains-in-lan) * [Forward DNS query requests](#forward-dns-query-requests) * [Forward Unix Domain Socket](#forward-unix-domain-socket) @@ -89,6 +91,20 @@ We are currently working on version 2 and attempting to perform some code refact We will transition from version 0 to version 1 at the appropriate time and will only accept bug fixes and improvements, rather than big feature requests. +### About V2 + +The overall situation is currently unfavorable, and there is significant pressure in both personal and professional aspects. + +The complexity and difficulty of the v2 version are much higher than anticipated. I can only work on its development during fragmented time periods, and the constant interruptions disrupt productivity significantly. Given this situation, we will continue to optimize and iterate on the current version until we have more free time to proceed with the major version overhaul. + +The concept behind v2 is based on my years of experience and reflection in the cloud-native domain, particularly in K8s and ServiceMesh. Its core is a modernized four-layer and seven-layer proxy, similar to envoy. This proxy itself is highly scalable, not only capable of implementing the functionality of intranet penetration but also applicable to various other domains. Building upon this highly scalable core, we aim to implement all the capabilities of frp v1 while also addressing the functionalities that were previously unachievable or difficult to implement in an elegant manner. Furthermore, we will maintain efficient development and iteration capabilities. + +In addition, I envision frp itself becoming a highly extensible system and platform, similar to how we can provide a range of extension capabilities based on K8s. In K8s, we can customize development according to enterprise needs, utilizing features such as CRD, controller mode, webhook, CSI, and CNI. In frp v1, we introduced the concept of server plugins, which implemented some basic extensibility. However, it relies on a simple HTTP protocol and requires users to start independent processes and manage them on their own. This approach is far from flexible and convenient, and real-world demands vary greatly. It is unrealistic to expect a non-profit open-source project maintained by a few individuals to meet everyone's needs. + +Finally, we acknowledge that the current design of modules such as configuration management, permission verification, certificate management, and API management is not modern enough. While we may carry out some optimizations in the v1 version, ensuring compatibility remains a challenging issue that requires a considerable amount of effort to address. + +We sincerely appreciate your support for frp. + ## Architecture ![architecture](/doc/pic/architecture.png) @@ -140,6 +156,56 @@ Note that the `local_port` (listened on the client) and `remote_port` (exposed o `ssh -oPort=6000 test@x.x.x.x` +### Multiple SSH services sharing the same port + +This example implements multiple SSH services exposed through the same port using a proxy of type tcpmux. Similarly, as long as the client supports the HTTP Connect proxy connection method, port reuse can be achieved in this way. + +1. Deploy frps on a machine with a public IP and modify the frps.ini file. Here is a simplified configuration: + + ```ini + [common] + bind_port = 7000 + tcpmux_httpconnect_port = 5002 + ``` + +2. Deploy frpc on the internal machine A with the following configuration: + + ```ini + [common] + server_addr = x.x.x.x + server_port = 7000 + + [ssh1] + type = tcpmux + multiplexer = httpconnect + custom_domains = machine-a.example.com + local_ip = 127.0.0.1 + local_port = 22 + ``` + +3. Deploy another frpc on the internal machine B with the following configuration: + + ```ini + [common] + server_addr = x.x.x.x + server_port = 7000 + + [ssh2] + type = tcpmux + multiplexer = httpconnect + custom_domains = machine-b.example.com + local_ip = 127.0.0.1 + local_port = 22 + ``` + +4. To access internal machine A using SSH ProxyCommand, assuming the username is "test": + + `ssh -o 'proxycommand socat - PROXY:x.x.x.x:machine-a.example.com:22,proxyport=5002' test@machine-a` + +5. To access internal machine B, the only difference is the domain name, assuming the username is "test": + + `ssh -o 'proxycommand socat - PROXY:x.x.x.x:machine-b.example.com:22,proxyport=5002' test@machine-b` + ### Accessing Internal Web Services with Custom Domains in LAN Sometimes we need to expose a local web service behind a NAT network to others for testing purposes with our own domain name. @@ -155,6 +221,8 @@ Unfortunately, we cannot resolve a domain name to a local IP. However, we can us vhost_http_port = 8080 ``` + If you want to configure an https proxy, you need to set up the `vhost_https_port`. + 2. Start `frps`: `./frps -c ./frps.ini` @@ -280,6 +348,7 @@ You may substitute `https2https` for the plugin, and point the `plugin_local_add [common] server_addr = x.x.x.x server_port = 7000 + vhost_https_port = 443 [test_https2http] type = https diff --git a/README_zh.md b/README_zh.md index 74d5e97cdb4..7a4124b8284 100644 --- a/README_zh.md +++ b/README_zh.md @@ -5,7 +5,7 @@ [README](README.md) | [中文文档](README_zh.md) -frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议。可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转暴露到公网。 +frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议,且支持 P2P 通信。可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转暴露到公网。

Gold Sponsors

@@ -20,12 +20,13 @@ frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP 通过在具有公网 IP 的节点上部署 frp 服务端,可以轻松地将内网服务穿透到公网,同时提供诸多专业的功能特性,这包括: -* 客户端服务端通信支持 TCP、KCP 以及 Websocket 等多种协议。 -* 采用 TCP 连接流式复用,在单个连接间承载更多请求,节省连接建立时间。 +* 客户端服务端通信支持 TCP、QUIC、KCP 以及 Websocket 等多种协议。 +* 采用 TCP 连接流式复用,在单个连接间承载更多请求,节省连接建立时间,降低请求延迟。 * 代理组间的负载均衡。 * 端口复用,多个服务通过同一个服务端端口暴露。 -* 多个原生支持的客户端插件(静态文件查看,HTTP、SOCK5 代理等),便于独立使用 frp 客户端完成某些工作。 -* 高度扩展性的服务端插件系统,方便结合自身需求进行功能扩展。 +* 支持 P2P 通信,流量不经过服务器中转,充分利用带宽资源。 +* 多个原生支持的客户端插件(静态文件查看,HTTPS/HTTP 协议转换,HTTP、SOCK5 代理等),便于独立使用 frp 客户端完成某些工作。 +* 高度扩展性的服务端插件系统,易于结合自身需求进行功能扩展。 * 服务端和客户端 UI 页面。 ## 开发状态 @@ -34,10 +35,24 @@ frp 目前已被很多公司广泛用于测试、生产环境。 master 分支用于发布稳定版本,dev 分支用于开发,您可以尝试下载最新的 release 版本进行测试。 -我们正在进行 v2 大版本的开发,将会尝试在各个方面进行重构和升级,且不会与 v1 版本进行兼容,预计会持续一段时间。 +我们正在进行 v2 大版本的开发,将会尝试在各个方面进行重构和升级,且不会与 v1 版本进行兼容,预计会持续较长的一段时间。 现在的 v0 版本将会在合适的时间切换为 v1 版本并且保证兼容性,后续只做 bug 修复和优化,不再进行大的功能性更新。 +### 关于 v2 的一些说明 + +当前整体形势不佳,面临的生活工作压力很大。 + +v2 版本的复杂度和难度比我们预期的要高得多。我只能利用零散的时间进行开发,而且由于上下文经常被打断,效率极低。由于这种情况可能会持续一段时间,我们仍然会在当前版本上进行一些优化和迭代,直到我们有更多空闲时间来推进大版本的重构。 + +v2 的构想是基于我多年在云原生领域,特别是在 K8s 和 ServiceMesh 方面的工作经验和思考。它的核心是一个现代化的四层和七层代理,类似于 envoy。这个代理本身高度可扩展,不仅可以用于实现内网穿透的功能,还可以应用于更多领域。在这个高度可扩展的内核基础上,我们将实现 frp v1 中的所有功能,并且能够以一种更加优雅的方式实现原先架构中无法实现或不易实现的功能。同时,我们将保持高效的开发和迭代能力。 + +除此之外,我希望 frp 本身也成为一个高度可扩展的系统和平台,就像我们可以基于 K8s 提供一系列扩展能力一样。在 K8s 上,我们可以根据企业需求进行定制化开发,例如使用 CRD、controller 模式、webhook、CSI 和 CNI 等。在 frp v1 中,我们引入了服务端插件的概念,实现了一些简单的扩展性。但是,它实际上依赖于简单的 HTTP 协议,并且需要用户自己启动独立的进程和管理。这种方式远远不够灵活和方便,而且现实世界的需求千差万别,我们不能期望一个由少数人维护的非营利性开源项目能够满足所有人的需求。 + +最后,我们意识到像配置管理、权限验证、证书管理和管理 API 等模块的当前设计并不够现代化。尽管我们可能在 v1 版本中进行一些优化,但确保兼容性是一个令人头疼的问题,需要投入大量精力来解决。 + +非常感谢您对 frp 的支持。 + ## 文档 完整文档已经迁移至 [https://gofrp.org](https://gofrp.org/docs)。 @@ -67,7 +82,7 @@ frp 是一个免费且开源的项目,我们欢迎任何人为其开发和进 ### 知识星球 -如果您想学习 frp 相关的知识和技术,或者寻求任何帮助及咨询,都可以通过微信扫描下方的二维码付费加入知识星球的官方社群: +如果您想了解更多 frp 相关技术以及更新详解,或者寻求任何帮助及咨询,都可以通过微信扫描下方的二维码付费加入知识星球的官方社群: ![zsxq](/doc/pic/zsxq.jpg) diff --git a/Release.md b/Release.md index ab58d8dbcc4..49667df129e 100644 --- a/Release.md +++ b/Release.md @@ -1,18 +1,12 @@ -## Notes - -**For enhanced security, the default values for `tls_enable` and `disable_custom_tls_first_byte` have been set to true.** - -If you wish to revert to the previous default values, you need to manually set the values of these two parameters to false. - ### Features -* Added support for `allow_users` in stcp, sudp, xtcp. By default, only the same user is allowed to access. Use `*` to allow access from any user. The visitor configuration now supports `server_user` to connect to proxies of other users. -* Added fallback support to a specified alternative visitor when xtcp connection fails. +* frpc supports connecting to frps via the wss protocol by enabling the configuration `protocol = wss`. +* frpc supports stopping the service through the stop command. ### Improvements -* Increased the default value of `MaxStreamWindowSize` for yamux to 6MB, improving traffic forwarding rate in high-latency scenarios. +* service.Run supports passing in context. ### Fixes -* Fixed an issue where having proxies with the same name would cause previously working proxies to become ineffective in `xtcp`. +* Fix an issue caused by a bug in yamux that prevents wss from working properly in certain plugins. diff --git a/client/admin.go b/client/admin.go index 37959e7e466..29e86a6e121 100644 --- a/client/admin.go +++ b/client/admin.go @@ -52,6 +52,7 @@ func (svr *Service) RunAdminServer(address string) (err error) { // api, see admin_api.go subRouter.HandleFunc("/api/reload", svr.apiReload).Methods("GET") + subRouter.HandleFunc("/api/stop", svr.apiStop).Methods("POST") subRouter.HandleFunc("/api/status", svr.apiStatus).Methods("GET") subRouter.HandleFunc("/api/config", svr.apiGetConfig).Methods("GET") subRouter.HandleFunc("/api/config", svr.apiPutConfig).Methods("PUT") diff --git a/client/admin_api.go b/client/admin_api.go index 5d163de705e..cba2d9193d2 100644 --- a/client/admin_api.go +++ b/client/admin_api.go @@ -24,6 +24,7 @@ import ( "sort" "strconv" "strings" + "time" "github.com/samber/lo" @@ -42,7 +43,7 @@ func (svr *Service) healthz(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) } -// GET api/reload +// GET /api/reload func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request) { res := GeneralResponse{Code: 200} @@ -72,6 +73,22 @@ func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request) { log.Info("success reload conf") } +// POST /api/stop +func (svr *Service) apiStop(w http.ResponseWriter, r *http.Request) { + res := GeneralResponse{Code: 200} + + log.Info("api request [/api/stop]") + defer func() { + log.Info("api response [/api/stop], code [%d]", res.Code) + w.WriteHeader(res.Code) + if len(res.Msg) > 0 { + _, _ = w.Write([]byte(res.Msg)) + } + }() + + go svr.GracefulClose(100 * time.Millisecond) +} + type StatusResp map[string][]ProxyStatusResp type ProxyStatusResp struct { @@ -106,7 +123,7 @@ func NewProxyStatusResp(status *proxy.WorkingStatus, serverAddr string) ProxySta return psr } -// GET api/status +// GET /api/status func (svr *Service) apiStatus(w http.ResponseWriter, r *http.Request) { var ( buf []byte @@ -135,7 +152,7 @@ func (svr *Service) apiStatus(w http.ResponseWriter, r *http.Request) { } } -// GET api/config +// GET /api/config func (svr *Service) apiGetConfig(w http.ResponseWriter, r *http.Request) { res := GeneralResponse{Code: 200} @@ -175,7 +192,7 @@ func (svr *Service) apiGetConfig(w http.ResponseWriter, r *http.Request) { res.Msg = strings.Join(newRows, "\n") } -// PUT api/config +// PUT /api/config func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) { res := GeneralResponse{Code: 200} diff --git a/client/service.go b/client/service.go index df85c05f4b1..b5366b1e319 100644 --- a/client/service.go +++ b/client/service.go @@ -86,16 +86,14 @@ func NewService( visitorCfgs map[string]config.VisitorConf, cfgFile string, ) (svr *Service, err error) { - ctx, cancel := context.WithCancel(context.Background()) svr = &Service{ authSetter: auth.NewAuthSetter(cfg.ClientConfig), cfg: cfg, cfgFile: cfgFile, pxyCfgs: pxyCfgs, visitorCfgs: visitorCfgs, + ctx: context.Background(), exit: 0, - ctx: xlog.NewContext(ctx, xlog.New()), - cancel: cancel, } return } @@ -106,7 +104,11 @@ func (svr *Service) GetController() *Control { return svr.ctl } -func (svr *Service) Run() error { +func (svr *Service) Run(ctx context.Context) error { + ctx, cancel := context.WithCancel(ctx) + svr.ctx = xlog.NewContext(ctx, xlog.New()) + svr.cancel = cancel + xl := xlog.FromContextSafe(svr.ctx) // set custom DNSServer @@ -135,7 +137,7 @@ func (svr *Service) Run() error { if svr.cfg.LoginFailExit { return err } - util.RandomSleep(10*time.Second, 0.9, 1.1) + util.RandomSleep(5*time.Second, 0.9, 1.1) } else { // login success ctl := NewControl(svr.ctx, svr.runID, conn, cm, svr.cfg, svr.pxyCfgs, svr.visitorCfgs, svr.authSetter) @@ -161,6 +163,10 @@ func (svr *Service) Run() error { log.Info("admin server listen on %s:%d", svr.cfg.AdminAddr, svr.cfg.AdminPort) } <-svr.ctx.Done() + // service context may not be canceled by svr.Close(), we should call it here to release resources + if atomic.LoadUint32(&svr.exit) == 0 { + svr.Close() + } return nil } @@ -182,7 +188,7 @@ func (svr *Service) keepControllerWorking() { return } - // the first three retry with no delay + // the first three attempts with a low delay if reconnectCounts > 3 { util.RandomSleep(reconnectDelay, 0.9, 1.1) xl.Info("wait %v to reconnect", reconnectDelay) @@ -322,10 +328,13 @@ func (svr *Service) GracefulClose(d time.Duration) { svr.ctlMu.RLock() if svr.ctl != nil { svr.ctl.GracefulClose(d) + svr.ctl = nil } svr.ctlMu.RUnlock() - svr.cancel() + if svr.cancel != nil { + svr.cancel() + } } type ConnectionManager struct { @@ -427,7 +436,11 @@ func (cm *ConnectionManager) realConnect() (net.Conn, error) { xl := xlog.FromContextSafe(cm.ctx) var tlsConfig *tls.Config var err error - if cm.cfg.TLSEnable { + tlsEnable := cm.cfg.TLSEnable + if cm.cfg.Protocol == "wss" { + tlsEnable = true + } + if tlsEnable { sn := cm.cfg.TLSServerName if sn == "" { sn = cm.cfg.ServerAddr @@ -451,10 +464,23 @@ func (cm *ConnectionManager) realConnect() (net.Conn, error) { } dialOptions := []libdial.DialOption{} protocol := cm.cfg.Protocol - if protocol == "websocket" { + switch protocol { + case "websocket": protocol = "tcp" - dialOptions = append(dialOptions, libdial.WithAfterHook(libdial.AfterHook{Hook: utilnet.DialHookWebsocket()})) + dialOptions = append(dialOptions, libdial.WithAfterHook(libdial.AfterHook{Hook: utilnet.DialHookWebsocket(protocol, "")})) + dialOptions = append(dialOptions, libdial.WithAfterHook(libdial.AfterHook{ + Hook: utilnet.DialHookCustomTLSHeadByte(tlsConfig != nil, cm.cfg.DisableCustomTLSFirstByte), + })) + dialOptions = append(dialOptions, libdial.WithTLSConfig(tlsConfig)) + case "wss": + protocol = "tcp" + dialOptions = append(dialOptions, libdial.WithTLSConfigAndPriority(100, tlsConfig)) + // Make sure that if it is wss, the websocket hook is executed after the tls hook. + dialOptions = append(dialOptions, libdial.WithAfterHook(libdial.AfterHook{Hook: utilnet.DialHookWebsocket(protocol, tlsConfig.ServerName), Priority: 110})) + default: + dialOptions = append(dialOptions, libdial.WithTLSConfig(tlsConfig)) } + if cm.cfg.ConnectServerLocalIP != "" { dialOptions = append(dialOptions, libdial.WithLocalAddr(cm.cfg.ConnectServerLocalIP)) } @@ -464,10 +490,6 @@ func (cm *ConnectionManager) realConnect() (net.Conn, error) { libdial.WithKeepAlive(time.Duration(cm.cfg.DialServerKeepAlive)*time.Second), libdial.WithProxy(proxyType, addr), libdial.WithProxyAuth(auth), - libdial.WithTLSConfig(tlsConfig), - libdial.WithAfterHook(libdial.AfterHook{ - Hook: utilnet.DialHookCustomTLSHeadByte(tlsConfig != nil, cm.cfg.DisableCustomTLSFirstByte), - }), ) conn, err := libdial.DialContext( cm.ctx, diff --git a/cmd/frpc/sub/root.go b/cmd/frpc/sub/root.go index 515a1936586..783514b99d0 100644 --- a/cmd/frpc/sub/root.go +++ b/cmd/frpc/sub/root.go @@ -15,6 +15,7 @@ package sub import ( + "context" "fmt" "io/fs" "net" @@ -76,7 +77,8 @@ var ( bindAddr string bindPort int - tlsEnable bool + tlsEnable bool + tlsServerName string ) func init() { @@ -88,13 +90,14 @@ func init() { func RegisterCommonFlags(cmd *cobra.Command) { cmd.PersistentFlags().StringVarP(&serverAddr, "server_addr", "s", "127.0.0.1:7000", "frp server's address") cmd.PersistentFlags().StringVarP(&user, "user", "u", "", "user") - cmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", "tcp", "tcp or kcp or websocket") + cmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", "tcp", "tcp, kcp, quic, websocket, wss") cmd.PersistentFlags().StringVarP(&token, "token", "t", "", "auth token") cmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "info", "log level") cmd.PersistentFlags().StringVarP(&logFile, "log_file", "", "console", "console or file path") cmd.PersistentFlags().IntVarP(&logMaxDays, "log_max_days", "", 3, "log file reversed days") cmd.PersistentFlags().BoolVarP(&disableLogColor, "disable_log_color", "", false, "disable log color in console") cmd.PersistentFlags().BoolVarP(&tlsEnable, "tls_enable", "", true, "enable frpc tls") + cmd.PersistentFlags().StringVarP(&tlsServerName, "tls_server_name", "", "", "specify the custom server name of tls certificate") cmd.PersistentFlags().StringVarP(&dnsServer, "dns_server", "", "", "specify dns server instead of using system default one") } @@ -150,12 +153,11 @@ func Execute() { } } -func handleSignal(svr *client.Service, doneCh chan struct{}) { +func handleTermSignal(svr *client.Service) { ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) <-ch svr.GracefulClose(500 * time.Millisecond) - close(doneCh) } func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) { @@ -186,6 +188,7 @@ func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) { cfg.ClientConfig = auth.GetDefaultClientConf() cfg.Token = token cfg.TLSEnable = tlsEnable + cfg.TLSServerName = tlsServerName cfg.Complete() if err = cfg.Validate(); err != nil { @@ -223,16 +226,12 @@ func startService( return } - closedDoneCh := make(chan struct{}) shouldGracefulClose := cfg.Protocol == "kcp" || cfg.Protocol == "quic" // Capture the exit signal if we use kcp or quic. if shouldGracefulClose { - go handleSignal(svr, closedDoneCh) + go handleTermSignal(svr) } - err = svr.Run() - if err == nil && shouldGracefulClose { - <-closedDoneCh - } + _ = svr.Run(context.Background()) return } diff --git a/cmd/frpc/sub/stop.go b/cmd/frpc/sub/stop.go new file mode 100644 index 00000000000..6c8f5f0aea3 --- /dev/null +++ b/cmd/frpc/sub/stop.go @@ -0,0 +1,84 @@ +// Copyright 2023 The frp Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package sub + +import ( + "encoding/base64" + "fmt" + "io" + "net/http" + "os" + "strings" + + "github.com/spf13/cobra" + + "github.com/fatedier/frp/pkg/config" +) + +func init() { + rootCmd.AddCommand(stopCmd) +} + +var stopCmd = &cobra.Command{ + Use: "stop", + Short: "Stop the running frpc", + RunE: func(cmd *cobra.Command, args []string) error { + cfg, _, _, err := config.ParseClientConfig(cfgFile) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + err = stopClient(cfg) + if err != nil { + fmt.Printf("frpc stop error: %v\n", err) + os.Exit(1) + } + fmt.Printf("stop success\n") + return nil + }, +} + +func stopClient(clientCfg config.ClientCommonConf) error { + if clientCfg.AdminPort == 0 { + return fmt.Errorf("admin_port shoud be set if you want to use stop feature") + } + + req, err := http.NewRequest("POST", "http://"+ + clientCfg.AdminAddr+":"+fmt.Sprintf("%d", clientCfg.AdminPort)+"/api/stop", nil) + if err != nil { + return err + } + + authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(clientCfg.AdminUser+":"+ + clientCfg.AdminPwd)) + + req.Header.Add("Authorization", authStr) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode == 200 { + return nil + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return err + } + return fmt.Errorf("code [%d], %s", resp.StatusCode, strings.TrimSpace(string(body))) +} diff --git a/cmd/frps/root.go b/cmd/frps/root.go index 1ba1352f3a4..e1859376a9a 100644 --- a/cmd/frps/root.go +++ b/cmd/frps/root.go @@ -15,6 +15,7 @@ package main import ( + "context" "fmt" "os" @@ -210,6 +211,6 @@ func runServer(cfg config.ServerCommonConf) (err error) { return err } log.Info("frps started successfully") - svr.Run() + svr.Run(context.Background()) return } diff --git a/conf/frpc_full.ini b/conf/frpc_full.ini index 4982f4fbf17..f8eca6b774c 100644 --- a/conf/frpc_full.ini +++ b/conf/frpc_full.ini @@ -95,7 +95,7 @@ user = your_name login_fail_exit = true # communication protocol used to connect to server -# supports tcp, kcp, quic and websocket now, default is tcp +# supports tcp, kcp, quic, websocket and wss now, default is tcp protocol = tcp # set client binding ip when connect server, default is empty. diff --git a/go.mod b/go.mod index 86801843a12..b42d8801d2e 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 github.com/coreos/go-oidc/v3 v3.4.0 github.com/fatedier/beego v0.0.0-20171024143340-6c6a4f5bd5eb - github.com/fatedier/golib v0.1.1-0.20230320133937-a7edcc8c793d + github.com/fatedier/golib v0.1.1-0.20230628070619-a1a0c648236a github.com/fatedier/kcp-go v2.0.4-0.20190803094908-fe8645b0a904+incompatible github.com/go-playground/validator/v10 v10.11.0 github.com/google/uuid v1.3.0 @@ -75,3 +75,6 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect ) + +// TODO(fatedier): Temporary use the modified version, update to the official version after merging into the official repository. +replace github.com/hashicorp/yamux => github.com/fatedier/yamux v0.0.0-20230628132301-7aca4898904d diff --git a/go.sum b/go.sum index 7950ad621fa..c259697ed5c 100644 --- a/go.sum +++ b/go.sum @@ -121,10 +121,12 @@ github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go. github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fatedier/beego v0.0.0-20171024143340-6c6a4f5bd5eb h1:wCrNShQidLmvVWn/0PikGmpdP0vtQmnvyRg3ZBEhczw= github.com/fatedier/beego v0.0.0-20171024143340-6c6a4f5bd5eb/go.mod h1:wx3gB6dbIfBRcucp94PI9Bt3I0F2c/MyNEWuhzpWiwk= -github.com/fatedier/golib v0.1.1-0.20230320133937-a7edcc8c793d h1:/m9Atycn9uKRwwOkxv4c+zaugxRgkdSG/Eg3IJWOpNs= -github.com/fatedier/golib v0.1.1-0.20230320133937-a7edcc8c793d/go.mod h1:Wdn1pJ0dHB1lah6FPYwt4AO9NEmWI0OzW13dpzC9g4E= +github.com/fatedier/golib v0.1.1-0.20230628070619-a1a0c648236a h1:HiRTFdy3ary86Vi2nsoINy2/YgjDPQ+21j3ikwJSD2E= +github.com/fatedier/golib v0.1.1-0.20230628070619-a1a0c648236a/go.mod h1:Wdn1pJ0dHB1lah6FPYwt4AO9NEmWI0OzW13dpzC9g4E= github.com/fatedier/kcp-go v2.0.4-0.20190803094908-fe8645b0a904+incompatible h1:ssXat9YXFvigNge/IkkZvFMn8yeYKFX+uI6wn2mLJ74= github.com/fatedier/kcp-go v2.0.4-0.20190803094908-fe8645b0a904+incompatible/go.mod h1:YpCOaxj7vvMThhIQ9AfTOPW2sfztQR5WDfs7AflSy4s= +github.com/fatedier/yamux v0.0.0-20230628132301-7aca4898904d h1:ynk1ra0RUqDWQfvFi5KtMiSobkVQ3cNc0ODb8CfIETo= +github.com/fatedier/yamux v0.0.0-20230628132301-7aca4898904d/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= @@ -270,8 +272,6 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= -github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= -github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= diff --git a/pkg/config/client.go b/pkg/config/client.go index b60e480158b..029470dc1de 100644 --- a/pkg/config/client.go +++ b/pkg/config/client.go @@ -20,6 +20,7 @@ import ( "path/filepath" "strings" + "github.com/samber/lo" "gopkg.in/ini.v1" "github.com/fatedier/frp/pkg/auth" @@ -117,7 +118,7 @@ type ClientCommonConf struct { Start []string `ini:"start" json:"start"` // Start map[string]struct{} `json:"start"` // Protocol specifies the protocol to use when interacting with the server. - // Valid values are "tcp", "kcp", "quic" and "websocket". By default, this value + // Valid values are "tcp", "kcp", "quic", "websocket" and "wss". By default, this value // is "tcp". Protocol string `ini:"protocol" json:"protocol"` // QUIC protocol options @@ -230,7 +231,7 @@ func (cfg *ClientCommonConf) Validate() error { } } - if cfg.Protocol != "tcp" && cfg.Protocol != "kcp" && cfg.Protocol != "websocket" && cfg.Protocol != "quic" { + if !lo.Contains([]string{"tcp", "kcp", "quic", "websocket", "wss"}, cfg.Protocol) { return fmt.Errorf("invalid protocol") } diff --git a/pkg/util/net/dial.go b/pkg/util/net/dial.go index 251ebbff7ae..bc670643a0b 100644 --- a/pkg/util/net/dial.go +++ b/pkg/util/net/dial.go @@ -21,9 +21,15 @@ func DialHookCustomTLSHeadByte(enableTLS bool, disableCustomTLSHeadByte bool) li } } -func DialHookWebsocket() libdial.AfterHookFunc { +func DialHookWebsocket(protocol string, host string) libdial.AfterHookFunc { return func(ctx context.Context, c net.Conn, addr string) (context.Context, net.Conn, error) { - addr = "ws://" + addr + FrpWebsocketPath + if protocol != "wss" { + protocol = "ws" + } + if host == "" { + host = addr + } + addr = protocol + "://" + host + FrpWebsocketPath uri, err := url.Parse(addr) if err != nil { return nil, nil, err diff --git a/pkg/util/version/version.go b/pkg/util/version/version.go index bc7ea9f0ec6..1778fd8e4d5 100644 --- a/pkg/util/version/version.go +++ b/pkg/util/version/version.go @@ -19,7 +19,7 @@ import ( "strings" ) -var version = "0.50.0" +var version = "0.51.0" func Full() string { return version diff --git a/server/dashboard_api.go b/server/dashboard_api.go index aaf2b321069..ee758429e56 100644 --- a/server/dashboard_api.go +++ b/server/dashboard_api.go @@ -59,7 +59,7 @@ func (svr *Service) Healthz(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) } -// api/serverinfo +// /api/serverinfo func (svr *Service) APIServerInfo(w http.ResponseWriter, r *http.Request) { res := GeneralResponse{Code: 200} defer func() { @@ -176,7 +176,7 @@ type GetProxyInfoResp struct { Proxies []*ProxyStatsInfo `json:"proxies"` } -// api/proxy/:type +// /api/proxy/:type func (svr *Service) APIProxyByType(w http.ResponseWriter, r *http.Request) { res := GeneralResponse{Code: 200} params := mux.Vars(r) @@ -244,7 +244,7 @@ type GetProxyStatsResp struct { Status string `json:"status"` } -// api/proxy/:type/:name +// /api/proxy/:type/:name func (svr *Service) APIProxyByTypeAndName(w http.ResponseWriter, r *http.Request) { res := GeneralResponse{Code: 200} params := mux.Vars(r) @@ -307,7 +307,7 @@ func (svr *Service) getProxyStatsByTypeAndName(proxyType string, proxyName strin return } -// api/traffic/:name +// /api/traffic/:name type GetProxyTrafficResp struct { Name string `json:"name"` TrafficIn []int64 `json:"traffic_in"` diff --git a/server/service.go b/server/service.go index 3c4f5a81e5b..7fbcb364be4 100644 --- a/server/service.go +++ b/server/service.go @@ -115,7 +115,6 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { return } - ctx, cancel := context.WithCancel(context.Background()) svr = &Service{ ctlManager: NewControlManager(), pxyManager: proxy.NewManager(), @@ -129,8 +128,7 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { authVerifier: auth.NewAuthVerifier(cfg.ServerConfig), tlsConfig: tlsConfig, cfg: cfg, - ctx: ctx, - cancel: cancel, + ctx: context.Background(), } // Create tcpmux httpconnect multiplexer. @@ -329,7 +327,11 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { return } -func (svr *Service) Run() { +func (svr *Service) Run(ctx context.Context) { + ctx, cancel := context.WithCancel(ctx) + svr.ctx = ctx + svr.cancel = cancel + if svr.kcpListener != nil { go svr.HandleListener(svr.kcpListener) } @@ -343,27 +345,39 @@ func (svr *Service) Run() { go svr.rc.NatHoleController.CleanWorker(svr.ctx) } svr.HandleListener(svr.listener) + + <-svr.ctx.Done() + // service context may not be canceled by svr.Close(), we should call it here to release resources + if svr.listener != nil { + svr.Close() + } } func (svr *Service) Close() error { if svr.kcpListener != nil { svr.kcpListener.Close() + svr.kcpListener = nil } if svr.quicListener != nil { svr.quicListener.Close() + svr.quicListener = nil } if svr.websocketListener != nil { svr.websocketListener.Close() + svr.websocketListener = nil } if svr.tlsListener != nil { svr.tlsListener.Close() + svr.tlsConfig = nil } if svr.listener != nil { svr.listener.Close() + svr.listener = nil } - svr.cancel() - svr.ctlManager.Close() + if svr.cancel != nil { + svr.cancel() + } return nil } diff --git a/test/e2e/basic/basic.go b/test/e2e/basic/basic.go index d9deafb7c87..d032a0d9a4d 100644 --- a/test/e2e/basic/basic.go +++ b/test/e2e/basic/basic.go @@ -331,24 +331,29 @@ var _ = ginkgo.Describe("[Feature: Basic]", func() { proxyExtraConfig string visitorExtraConfig string expectError bool - user2 bool + deployUser2Client bool + // skipXTCP is used to skip xtcp test case + skipXTCP bool }{ { proxyName: "normal", bindPortName: port.GenName("Normal"), visitorSK: correctSK, + skipXTCP: true, }, { proxyName: "with-encryption", bindPortName: port.GenName("WithEncryption"), visitorSK: correctSK, commonExtraConfig: "use_encryption = true", + skipXTCP: true, }, { proxyName: "with-compression", bindPortName: port.GenName("WithCompression"), visitorSK: correctSK, commonExtraConfig: "use_compression = true", + skipXTCP: true, }, { proxyName: "with-encryption-and-compression", @@ -371,7 +376,7 @@ var _ = ginkgo.Describe("[Feature: Basic]", func() { visitorSK: correctSK, proxyExtraConfig: "allow_users = another, user2", visitorExtraConfig: "server_user = user1", - user2: true, + deployUser2Client: true, }, { proxyName: "not-allowed-user", @@ -387,7 +392,7 @@ var _ = ginkgo.Describe("[Feature: Basic]", func() { visitorSK: correctSK, proxyExtraConfig: "allow_users = *", visitorExtraConfig: "server_user = user1", - user2: true, + deployUser2Client: true, }, } @@ -399,7 +404,7 @@ var _ = ginkgo.Describe("[Feature: Basic]", func() { config := getProxyVisitorConf( test.proxyName, test.bindPortName, test.visitorSK, test.commonExtraConfig+"\n"+test.visitorExtraConfig, ) + "\n" - if test.user2 { + if test.deployUser2Client { clientUser2VisitorConf += config } else { clientVisitorConf += config @@ -411,7 +416,10 @@ var _ = ginkgo.Describe("[Feature: Basic]", func() { for _, test := range tests { timeout := time.Second if t == "xtcp" { - timeout = 4 * time.Second + if test.skipXTCP { + continue + } + timeout = 10 * time.Second } framework.NewRequestExpect(f). RequestModify(func(r *request.Request) { diff --git a/test/e2e/basic/client.go b/test/e2e/basic/client.go index 95561a348b8..fb9b16b573a 100644 --- a/test/e2e/basic/client.go +++ b/test/e2e/basic/client.go @@ -101,4 +101,32 @@ var _ = ginkgo.Describe("[Feature: ClientManage]", func() { }).Port(dashboardPort). Ensure(framework.ExpectResponseCode(401)) }) + + ginkgo.It("stop", func() { + serverConf := consts.DefaultServerConfig + + adminPort := f.AllocPort() + testPort := f.AllocPort() + clientConf := consts.DefaultClientConfig + fmt.Sprintf(` + admin_port = %d + + [test] + type = tcp + local_port = {{ .%s }} + remote_port = %d + `, adminPort, framework.TCPEchoServerPort, testPort) + + f.RunProcesses([]string{serverConf}, []string{clientConf}) + + framework.NewRequestExpect(f).Port(testPort).Ensure() + + client := clientsdk.New("127.0.0.1", adminPort) + err := client.Stop() + framework.ExpectNoError(err) + + time.Sleep(3 * time.Second) + + // frpc stopped so the port is not listened, expect error + framework.NewRequestExpect(f).Port(testPort).ExpectError(true).Ensure() + }) }) diff --git a/test/e2e/basic/client_server.go b/test/e2e/basic/client_server.go index a02e4544a5b..e7730f459ce 100644 --- a/test/e2e/basic/client_server.go +++ b/test/e2e/basic/client_server.go @@ -3,6 +3,7 @@ package basic import ( "fmt" "strings" + "time" "github.com/onsi/ginkgo/v2" @@ -13,9 +14,13 @@ import ( ) type generalTestConfigures struct { - server string - client string - expectError bool + server string + client string + clientPrefix string + client2 string + client2Prefix string + testDelay time.Duration + expectError bool } func renderBindPortConfig(protocol string) string { @@ -30,6 +35,9 @@ func renderBindPortConfig(protocol string) string { func runClientServerTest(f *framework.Framework, configures *generalTestConfigures) { serverConf := consts.DefaultServerConfig clientConf := consts.DefaultClientConfig + if configures.clientPrefix != "" { + clientConf = configures.clientPrefix + } serverConf += fmt.Sprintf(` %s @@ -54,7 +62,23 @@ func runClientServerTest(f *framework.Framework, configures *generalTestConfigur framework.UDPEchoServerPort, udpPortName, ) - f.RunProcesses([]string{serverConf}, []string{clientConf}) + clientConfs := []string{clientConf} + if configures.client2 != "" { + client2Conf := consts.DefaultClientConfig + if configures.client2Prefix != "" { + client2Conf = configures.client2Prefix + } + client2Conf += fmt.Sprintf(` + %s + `, configures.client2) + clientConfs = append(clientConfs, client2Conf) + } + + f.RunProcesses([]string{serverConf}, clientConfs) + + if configures.testDelay > 0 { + time.Sleep(configures.testDelay) + } framework.NewRequestExpect(f).PortName(tcpPortName).ExpectError(configures.expectError).Explain("tcp proxy").Ensure() framework.NewRequestExpect(f).Protocol("udp"). @@ -84,6 +108,33 @@ var _ = ginkgo.Describe("[Feature: Client-Server]", func() { } }) + // wss is special, it needs to be tested separately. + // frps only supports ws, so there should be a proxy to terminate TLS before frps. + ginkgo.Describe("Protocol wss", func() { + wssPort := f.AllocPort() + configures := &generalTestConfigures{ + clientPrefix: fmt.Sprintf(` + [common] + server_addr = 127.0.0.1 + server_port = %d + protocol = wss + log_level = trace + login_fail_exit = false + `, wssPort), + // Due to the fact that frps cannot directly accept wss connections, we use the https2http plugin of another frpc to terminate TLS. + client2: fmt.Sprintf(` + [wss2ws] + type = tcp + remote_port = %d + plugin = https2http + plugin_local_addr = 127.0.0.1:{{ .%s }} + `, wssPort, consts.PortServerName), + testDelay: 10 * time.Second, + } + + defineClientServerTest("wss", f, configures) + }) + ginkgo.Describe("Authentication", func() { defineClientServerTest("Token Correct", f, &generalTestConfigures{ server: "token = 123456", diff --git a/test/e2e/pkg/sdk/client/client.go b/test/e2e/pkg/sdk/client/client.go index 4fb580d1159..3d157073e77 100644 --- a/test/e2e/pkg/sdk/client/client.go +++ b/test/e2e/pkg/sdk/client/client.go @@ -62,6 +62,15 @@ func (c *Client) Reload() error { return err } +func (c *Client) Stop() error { + req, err := http.NewRequest("POST", "http://"+c.address+"/api/stop", nil) + if err != nil { + return err + } + _, err = c.do(req) + return err +} + func (c *Client) GetConfig() (string, error) { req, err := http.NewRequest("GET", "http://"+c.address+"/api/config", nil) if err != nil {