2017-03-08 19:03:47 +01:00
|
|
|
// Copyright 2017 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// 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 client
|
|
|
|
|
|
|
|
import (
|
2019-10-12 14:13:12 +02:00
|
|
|
"context"
|
|
|
|
"net"
|
2023-11-06 03:51:48 +01:00
|
|
|
"sync/atomic"
|
2017-03-08 19:03:47 +01:00
|
|
|
"time"
|
|
|
|
|
2023-09-06 04:18:02 +02:00
|
|
|
"github.com/samber/lo"
|
2022-08-28 19:02:53 +02:00
|
|
|
|
2018-12-09 15:06:22 +01:00
|
|
|
"github.com/fatedier/frp/client/proxy"
|
2023-05-28 10:50:43 +02:00
|
|
|
"github.com/fatedier/frp/client/visitor"
|
2020-09-23 07:49:14 +02:00
|
|
|
"github.com/fatedier/frp/pkg/auth"
|
2023-09-06 04:18:02 +02:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2020-09-23 07:49:14 +02:00
|
|
|
"github.com/fatedier/frp/pkg/msg"
|
2023-05-28 10:50:43 +02:00
|
|
|
"github.com/fatedier/frp/pkg/transport"
|
2023-11-27 08:47:49 +01:00
|
|
|
netpkg "github.com/fatedier/frp/pkg/util/net"
|
2023-11-06 03:51:48 +01:00
|
|
|
"github.com/fatedier/frp/pkg/util/wait"
|
2020-09-23 07:49:14 +02:00
|
|
|
"github.com/fatedier/frp/pkg/util/xlog"
|
2017-03-08 19:03:47 +01:00
|
|
|
)
|
|
|
|
|
2023-11-27 08:47:49 +01:00
|
|
|
type SessionContext struct {
|
|
|
|
// The client common configuration.
|
|
|
|
Common *v1.ClientCommonConfig
|
|
|
|
|
|
|
|
// Unique ID obtained from frps.
|
|
|
|
// It should be attached to the login message when reconnecting.
|
|
|
|
RunID string
|
|
|
|
// Underlying control connection. Once conn is closed, the msgDispatcher and the entire Control will exit.
|
|
|
|
Conn net.Conn
|
|
|
|
// Indicates whether the connection is encrypted.
|
|
|
|
ConnEncrypted bool
|
|
|
|
// Sets authentication based on selected method
|
|
|
|
AuthSetter auth.Setter
|
|
|
|
// Connector is used to create new connections, which could be real TCP connections or virtual streams.
|
|
|
|
Connector Connector
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:03:47 +01:00
|
|
|
type Control struct {
|
2023-05-28 10:50:43 +02:00
|
|
|
// service context
|
|
|
|
ctx context.Context
|
|
|
|
xl *xlog.Logger
|
|
|
|
|
2023-11-27 08:47:49 +01:00
|
|
|
// session context
|
|
|
|
sessionCtx *SessionContext
|
2017-03-08 19:03:47 +01:00
|
|
|
|
2018-06-25 12:22:35 +02:00
|
|
|
// manage all proxies
|
2023-11-27 08:47:49 +01:00
|
|
|
pm *proxy.Manager
|
2017-06-25 21:02:33 +02:00
|
|
|
|
2018-06-25 12:22:35 +02:00
|
|
|
// manage all visitors
|
2023-05-28 10:50:43 +02:00
|
|
|
vm *visitor.Manager
|
2018-06-25 12:22:35 +02:00
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
doneCh chan struct{}
|
2017-03-08 19:03:47 +01:00
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
// of time.Time, last time got the Pong message
|
|
|
|
lastPong atomic.Value
|
2023-05-28 10:50:43 +02:00
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
// The role of msgTransporter is similar to HTTP2.
|
|
|
|
// It allows multiple messages to be sent simultaneously on the same control connection.
|
|
|
|
// The server's response messages will be dispatched to the corresponding waiting goroutines based on the laneKey and message type.
|
2023-05-28 10:50:43 +02:00
|
|
|
msgTransporter transport.MessageTransporter
|
2023-11-06 03:51:48 +01:00
|
|
|
|
|
|
|
// msgDispatcher is a wrapper for control connection.
|
|
|
|
// It provides a channel for sending messages, and you can register handlers to process messages based on their respective types.
|
|
|
|
msgDispatcher *msg.Dispatcher
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
2023-11-27 08:47:49 +01:00
|
|
|
func NewControl(ctx context.Context, sessionCtx *SessionContext) (*Control, error) {
|
2019-10-12 14:13:12 +02:00
|
|
|
// new xlog instance
|
2018-01-16 18:09:33 +01:00
|
|
|
ctl := &Control{
|
2023-11-06 03:51:48 +01:00
|
|
|
ctx: ctx,
|
|
|
|
xl: xlog.FromContextSafe(ctx),
|
2023-11-27 08:47:49 +01:00
|
|
|
sessionCtx: sessionCtx,
|
2023-11-06 03:51:48 +01:00
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
ctl.lastPong.Store(time.Now())
|
|
|
|
|
2023-11-27 08:47:49 +01:00
|
|
|
if sessionCtx.ConnEncrypted {
|
|
|
|
cryptoRW, err := netpkg.NewCryptoReadWriter(sessionCtx.Conn, []byte(sessionCtx.Common.Auth.Token))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctl.msgDispatcher = msg.NewDispatcher(cryptoRW)
|
|
|
|
} else {
|
|
|
|
ctl.msgDispatcher = msg.NewDispatcher(sessionCtx.Conn)
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
2023-11-06 03:51:48 +01:00
|
|
|
ctl.registerMsgHandlers()
|
|
|
|
ctl.msgTransporter = transport.NewMessageTransporter(ctl.msgDispatcher.SendChannel())
|
|
|
|
|
2023-11-27 08:47:49 +01:00
|
|
|
ctl.pm = proxy.NewManager(ctl.ctx, sessionCtx.Common, ctl.msgTransporter)
|
|
|
|
ctl.vm = visitor.NewManager(ctl.ctx, sessionCtx.RunID, sessionCtx.Common, ctl.connectServer, ctl.msgTransporter)
|
2023-11-06 03:51:48 +01:00
|
|
|
return ctl, nil
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
2023-11-27 08:47:49 +01:00
|
|
|
func (ctl *Control) Run(proxyCfgs []v1.ProxyConfigurer, visitorCfgs []v1.VisitorConfigurer) {
|
2018-01-16 18:09:33 +01:00
|
|
|
go ctl.worker()
|
2017-06-27 17:30:09 +02:00
|
|
|
|
2018-12-07 10:05:36 +01:00
|
|
|
// start all proxies
|
2023-11-27 08:47:49 +01:00
|
|
|
ctl.pm.UpdateAll(proxyCfgs)
|
2018-06-25 12:22:35 +02:00
|
|
|
|
2018-12-07 10:05:36 +01:00
|
|
|
// start all visitors
|
2023-11-27 08:47:49 +01:00
|
|
|
ctl.vm.UpdateAll(visitorCfgs)
|
2017-06-25 21:02:33 +02:00
|
|
|
}
|
2017-05-17 11:47:20 +02:00
|
|
|
|
2023-11-21 04:19:35 +01:00
|
|
|
func (ctl *Control) SetInWorkConnCallback(cb func(*v1.ProxyBaseConfig, net.Conn, *msg.StartWorkConn) bool) {
|
|
|
|
ctl.pm.SetInWorkConnCallback(cb)
|
|
|
|
}
|
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
func (ctl *Control) handleReqWorkConn(_ msg.Message) {
|
2019-10-12 14:13:12 +02:00
|
|
|
xl := ctl.xl
|
2017-06-25 21:02:33 +02:00
|
|
|
workConn, err := ctl.connectServer()
|
|
|
|
if err != nil {
|
2022-12-12 04:04:10 +01:00
|
|
|
xl.Warn("start new connection to server error: %v", err)
|
2017-06-25 21:02:33 +02:00
|
|
|
return
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m := &msg.NewWorkConn{
|
2023-11-27 08:47:49 +01:00
|
|
|
RunID: ctl.sessionCtx.RunID,
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
2023-11-27 08:47:49 +01:00
|
|
|
if err = ctl.sessionCtx.AuthSetter.SetNewWorkConn(m); err != nil {
|
2020-03-01 03:57:01 +01:00
|
|
|
xl.Warn("error during NewWorkConn authentication: %v", err)
|
2023-12-22 08:47:59 +01:00
|
|
|
workConn.Close()
|
2020-03-01 03:57:01 +01:00
|
|
|
return
|
|
|
|
}
|
2017-03-08 19:03:47 +01:00
|
|
|
if err = msg.WriteMsg(workConn, m); err != nil {
|
2019-10-12 14:13:12 +02:00
|
|
|
xl.Warn("work connection write to server error: %v", err)
|
2017-03-08 19:03:47 +01:00
|
|
|
workConn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var startMsg msg.StartWorkConn
|
|
|
|
if err = msg.ReadMsgInto(workConn, &startMsg); err != nil {
|
2022-12-18 11:43:42 +01:00
|
|
|
xl.Trace("work connection closed before response StartWorkConn message: %v", err)
|
2017-03-08 19:03:47 +01:00
|
|
|
workConn.Close()
|
|
|
|
return
|
|
|
|
}
|
2020-03-01 03:57:01 +01:00
|
|
|
if startMsg.Error != "" {
|
|
|
|
xl.Error("StartWorkConn contains error: %s", startMsg.Error)
|
|
|
|
workConn.Close()
|
|
|
|
return
|
|
|
|
}
|
2017-03-08 19:03:47 +01:00
|
|
|
|
|
|
|
// dispatch this work connection to related proxy
|
2019-03-29 12:01:18 +01:00
|
|
|
ctl.pm.HandleWorkConn(startMsg.ProxyName, workConn, &startMsg)
|
2018-01-16 18:09:33 +01:00
|
|
|
}
|
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
func (ctl *Control) handleNewProxyResp(m msg.Message) {
|
2019-10-12 14:13:12 +02:00
|
|
|
xl := ctl.xl
|
2023-11-06 03:51:48 +01:00
|
|
|
inMsg := m.(*msg.NewProxyResp)
|
2018-01-16 18:09:33 +01:00
|
|
|
// Server will return NewProxyResp message to each NewProxy message.
|
|
|
|
// Start a new proxy handler if no error got
|
2018-01-17 07:40:08 +01:00
|
|
|
err := ctl.pm.StartProxy(inMsg.ProxyName, inMsg.RemoteAddr, inMsg.Error)
|
2018-01-16 18:09:33 +01:00
|
|
|
if err != nil {
|
2019-10-12 14:13:12 +02:00
|
|
|
xl.Warn("[%s] start error: %v", inMsg.ProxyName, err)
|
2017-03-12 19:44:47 +01:00
|
|
|
} else {
|
2019-10-12 14:13:12 +02:00
|
|
|
xl.Info("[%s] start proxy success", inMsg.ProxyName)
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
func (ctl *Control) handleNatHoleResp(m msg.Message) {
|
2023-05-28 10:50:43 +02:00
|
|
|
xl := ctl.xl
|
2023-11-06 03:51:48 +01:00
|
|
|
inMsg := m.(*msg.NatHoleResp)
|
2023-05-28 10:50:43 +02:00
|
|
|
|
|
|
|
// Dispatch the NatHoleResp message to the related proxy.
|
|
|
|
ok := ctl.msgTransporter.DispatchWithType(inMsg, msg.TypeNameNatHoleResp, inMsg.TransactionID)
|
|
|
|
if !ok {
|
|
|
|
xl.Trace("dispatch NatHoleResp message to related proxy error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
func (ctl *Control) handlePong(m msg.Message) {
|
|
|
|
xl := ctl.xl
|
|
|
|
inMsg := m.(*msg.Pong)
|
|
|
|
|
|
|
|
if inMsg.Error != "" {
|
|
|
|
xl.Error("Pong message contains error: %s", inMsg.Error)
|
2023-11-27 08:47:49 +01:00
|
|
|
ctl.closeSession()
|
2023-11-06 03:51:48 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctl.lastPong.Store(time.Now())
|
|
|
|
xl.Debug("receive heartbeat from server")
|
|
|
|
}
|
|
|
|
|
2023-11-27 08:47:49 +01:00
|
|
|
// closeSession closes the control connection.
|
|
|
|
func (ctl *Control) closeSession() {
|
|
|
|
ctl.sessionCtx.Conn.Close()
|
|
|
|
ctl.sessionCtx.Connector.Close()
|
|
|
|
}
|
|
|
|
|
2017-06-26 19:59:30 +02:00
|
|
|
func (ctl *Control) Close() error {
|
2021-10-19 08:57:26 +02:00
|
|
|
return ctl.GracefulClose(0)
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:02:45 +02:00
|
|
|
func (ctl *Control) GracefulClose(d time.Duration) error {
|
2018-12-11 08:06:54 +01:00
|
|
|
ctl.pm.Close()
|
2020-04-02 04:58:37 +02:00
|
|
|
ctl.vm.Close()
|
2021-10-19 08:57:26 +02:00
|
|
|
|
2021-10-19 09:02:45 +02:00
|
|
|
time.Sleep(d)
|
2021-10-19 08:57:26 +02:00
|
|
|
|
2023-11-27 08:47:49 +01:00
|
|
|
ctl.closeSession()
|
2018-01-16 18:09:33 +01:00
|
|
|
return nil
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
// Done returns a channel that will be closed after all resources are released
|
|
|
|
func (ctl *Control) Done() <-chan struct{} {
|
|
|
|
return ctl.doneCh
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
2018-07-15 19:21:29 +02:00
|
|
|
// connectServer return a new connection to frps
|
2023-11-27 08:47:49 +01:00
|
|
|
func (ctl *Control) connectServer() (net.Conn, error) {
|
|
|
|
return ctl.sessionCtx.Connector.Connect()
|
2017-06-25 21:02:33 +02:00
|
|
|
}
|
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
func (ctl *Control) registerMsgHandlers() {
|
|
|
|
ctl.msgDispatcher.RegisterHandler(&msg.ReqWorkConn{}, msg.AsyncHandler(ctl.handleReqWorkConn))
|
|
|
|
ctl.msgDispatcher.RegisterHandler(&msg.NewProxyResp{}, ctl.handleNewProxyResp)
|
|
|
|
ctl.msgDispatcher.RegisterHandler(&msg.NatHoleResp{}, ctl.handleNatHoleResp)
|
|
|
|
ctl.msgDispatcher.RegisterHandler(&msg.Pong{}, ctl.handlePong)
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
// headerWorker sends heartbeat to server and check heartbeat timeout.
|
|
|
|
func (ctl *Control) heartbeatWorker() {
|
2019-10-12 14:13:12 +02:00
|
|
|
xl := ctl.xl
|
2020-05-24 11:48:37 +02:00
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
// TODO(fatedier): Change default value of HeartbeatInterval to -1 if tcpmux is enabled.
|
|
|
|
// Users can still enable heartbeat feature by setting HeartbeatInterval to a positive value.
|
2023-11-27 08:47:49 +01:00
|
|
|
if ctl.sessionCtx.Common.Transport.HeartbeatInterval > 0 {
|
2023-11-06 03:51:48 +01:00
|
|
|
// send heartbeat to server
|
2023-12-21 13:51:10 +01:00
|
|
|
sendHeartBeat := func() (bool, error) {
|
2023-11-06 03:51:48 +01:00
|
|
|
xl.Debug("send heartbeat to server")
|
|
|
|
pingMsg := &msg.Ping{}
|
2023-11-27 08:47:49 +01:00
|
|
|
if err := ctl.sessionCtx.AuthSetter.SetPing(pingMsg); err != nil {
|
2023-11-06 03:51:48 +01:00
|
|
|
xl.Warn("error during ping authentication: %v, skip sending ping message", err)
|
2023-12-21 13:51:10 +01:00
|
|
|
return false, err
|
2023-11-06 03:51:48 +01:00
|
|
|
}
|
|
|
|
_ = ctl.msgDispatcher.Send(pingMsg)
|
2023-12-21 13:51:10 +01:00
|
|
|
return false, nil
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
go wait.BackoffUntil(sendHeartBeat,
|
|
|
|
wait.NewFastBackoffManager(wait.FastBackoffOptions{
|
2023-11-27 08:47:49 +01:00
|
|
|
Duration: time.Duration(ctl.sessionCtx.Common.Transport.HeartbeatInterval) * time.Second,
|
2023-11-06 03:51:48 +01:00
|
|
|
InitDurationIfFail: time.Second,
|
|
|
|
Factor: 2.0,
|
|
|
|
Jitter: 0.1,
|
2023-11-27 08:47:49 +01:00
|
|
|
MaxDuration: time.Duration(ctl.sessionCtx.Common.Transport.HeartbeatInterval) * time.Second,
|
2023-11-06 03:51:48 +01:00
|
|
|
}),
|
|
|
|
true, ctl.doneCh,
|
|
|
|
)
|
2022-01-13 07:26:07 +01:00
|
|
|
}
|
2017-03-08 19:03:47 +01:00
|
|
|
|
2022-01-13 07:26:07 +01:00
|
|
|
// Check heartbeat timeout only if TCPMux is not enabled and users don't disable heartbeat feature.
|
2023-11-27 08:47:49 +01:00
|
|
|
if ctl.sessionCtx.Common.Transport.HeartbeatInterval > 0 && ctl.sessionCtx.Common.Transport.HeartbeatTimeout > 0 &&
|
|
|
|
!lo.FromPtr(ctl.sessionCtx.Common.Transport.TCPMux) {
|
2018-01-16 18:09:33 +01:00
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
go wait.Until(func() {
|
2023-11-27 08:47:49 +01:00
|
|
|
if time.Since(ctl.lastPong.Load().(time.Time)) > time.Duration(ctl.sessionCtx.Common.Transport.HeartbeatTimeout)*time.Second {
|
2019-10-12 14:13:12 +02:00
|
|
|
xl.Warn("heartbeat timeout")
|
2023-11-27 08:47:49 +01:00
|
|
|
ctl.closeSession()
|
2017-03-08 19:03:47 +01:00
|
|
|
return
|
|
|
|
}
|
2023-11-06 03:51:48 +01:00
|
|
|
}, time.Second, ctl.doneCh)
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 18:09:33 +01:00
|
|
|
func (ctl *Control) worker() {
|
2023-11-06 03:51:48 +01:00
|
|
|
go ctl.heartbeatWorker()
|
|
|
|
go ctl.msgDispatcher.Run()
|
2018-01-16 18:09:33 +01:00
|
|
|
|
2023-11-06 03:51:48 +01:00
|
|
|
<-ctl.msgDispatcher.Done()
|
2023-11-27 08:47:49 +01:00
|
|
|
ctl.closeSession()
|
2018-11-06 11:35:05 +01:00
|
|
|
|
2022-08-28 19:02:53 +02:00
|
|
|
ctl.pm.Close()
|
|
|
|
ctl.vm.Close()
|
2023-11-06 03:51:48 +01:00
|
|
|
close(ctl.doneCh)
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
2017-06-27 17:30:09 +02:00
|
|
|
|
2023-11-27 08:47:49 +01:00
|
|
|
func (ctl *Control) UpdateAllConfigurer(proxyCfgs []v1.ProxyConfigurer, visitorCfgs []v1.VisitorConfigurer) error {
|
|
|
|
ctl.vm.UpdateAll(visitorCfgs)
|
|
|
|
ctl.pm.UpdateAll(proxyCfgs)
|
2018-12-07 10:05:36 +01:00
|
|
|
return nil
|
2017-07-12 20:20:49 +02:00
|
|
|
}
|