2017-03-08 19:03:47 +01:00
|
|
|
// Copyright 2017 fatedier, fatedier@gmail.com
|
2016-12-18 18:22:21 +01:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-03-08 19:03:47 +01:00
|
|
|
package client
|
2016-12-18 18:22:21 +01:00
|
|
|
|
2017-07-12 20:20:49 +02:00
|
|
|
import (
|
2019-10-12 14:13:12 +02:00
|
|
|
"context"
|
2019-03-11 07:14:31 +01:00
|
|
|
"crypto/tls"
|
2018-11-06 11:35:05 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-10-12 14:13:12 +02:00
|
|
|
"net"
|
2018-11-06 11:35:05 +01:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2019-02-01 12:26:10 +01:00
|
|
|
"github.com/fatedier/frp/assets"
|
2020-03-01 03:57:01 +01:00
|
|
|
"github.com/fatedier/frp/models/auth"
|
2017-07-12 20:20:49 +02:00
|
|
|
"github.com/fatedier/frp/models/config"
|
2018-11-06 11:35:05 +01:00
|
|
|
"github.com/fatedier/frp/models/msg"
|
2017-07-12 20:20:49 +02:00
|
|
|
"github.com/fatedier/frp/utils/log"
|
2018-11-06 11:35:05 +01:00
|
|
|
frpNet "github.com/fatedier/frp/utils/net"
|
|
|
|
"github.com/fatedier/frp/utils/version"
|
2019-10-12 14:13:12 +02:00
|
|
|
"github.com/fatedier/frp/utils/xlog"
|
2018-11-06 11:35:05 +01:00
|
|
|
|
|
|
|
fmux "github.com/hashicorp/yamux"
|
2017-07-12 20:20:49 +02:00
|
|
|
)
|
2016-12-18 18:22:21 +01:00
|
|
|
|
2019-08-25 00:20:34 +02:00
|
|
|
// Service is a client service.
|
2017-03-08 19:03:47 +01:00
|
|
|
type Service struct {
|
2018-11-06 11:35:05 +01:00
|
|
|
// uniq id got from frps, attach it in loginMsg
|
|
|
|
runId string
|
|
|
|
|
2017-03-08 19:03:47 +01:00
|
|
|
// manager control connection with server
|
2018-11-06 11:35:05 +01:00
|
|
|
ctl *Control
|
|
|
|
ctlMu sync.RWMutex
|
2017-03-08 19:03:47 +01:00
|
|
|
|
2020-03-01 03:57:01 +01:00
|
|
|
// Sets authentication based on selected method
|
|
|
|
authSetter auth.Setter
|
|
|
|
|
2019-08-20 22:53:27 +02:00
|
|
|
cfg config.ClientCommonConf
|
2018-11-06 11:35:05 +01:00
|
|
|
pxyCfgs map[string]config.ProxyConf
|
|
|
|
visitorCfgs map[string]config.VisitorConf
|
|
|
|
cfgMu sync.RWMutex
|
|
|
|
|
2019-08-20 22:53:27 +02:00
|
|
|
// The configuration file used to initialize this client, or an empty
|
|
|
|
// string if no configuration file was used.
|
|
|
|
cfgFile string
|
|
|
|
|
|
|
|
// This is configured by the login response from frps
|
|
|
|
serverUDPPort int
|
|
|
|
|
2019-10-12 14:13:12 +02:00
|
|
|
exit uint32 // 0 means not exit
|
|
|
|
|
|
|
|
// service context
|
|
|
|
ctx context.Context
|
|
|
|
// call cancel to stop service
|
|
|
|
cancel context.CancelFunc
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
2019-08-20 22:53:27 +02:00
|
|
|
func NewService(cfg config.ClientCommonConf, pxyCfgs map[string]config.ProxyConf, visitorCfgs map[string]config.VisitorConf, cfgFile string) (svr *Service, err error) {
|
2019-10-12 14:13:12 +02:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-03-08 19:03:47 +01:00
|
|
|
svr = &Service{
|
2020-03-01 03:57:01 +01:00
|
|
|
authSetter: auth.NewAuthSetter(cfg.AuthClientConfig),
|
2019-08-20 22:53:27 +02:00
|
|
|
cfg: cfg,
|
|
|
|
cfgFile: cfgFile,
|
2018-11-06 11:35:05 +01:00
|
|
|
pxyCfgs: pxyCfgs,
|
|
|
|
visitorCfgs: visitorCfgs,
|
|
|
|
exit: 0,
|
2019-10-12 14:13:12 +02:00
|
|
|
ctx: xlog.NewContext(ctx, xlog.New()),
|
|
|
|
cancel: cancel,
|
2016-12-18 18:22:21 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-03-08 19:03:47 +01:00
|
|
|
|
2018-11-06 11:35:05 +01:00
|
|
|
func (svr *Service) GetController() *Control {
|
|
|
|
svr.ctlMu.RLock()
|
|
|
|
defer svr.ctlMu.RUnlock()
|
|
|
|
return svr.ctl
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:03:47 +01:00
|
|
|
func (svr *Service) Run() error {
|
2019-10-12 14:13:12 +02:00
|
|
|
xl := xlog.FromContextSafe(svr.ctx)
|
|
|
|
|
|
|
|
// login to frps
|
2018-11-06 11:35:05 +01:00
|
|
|
for {
|
|
|
|
conn, session, err := svr.login()
|
|
|
|
if err != nil {
|
2019-10-12 14:13:12 +02:00
|
|
|
xl.Warn("login to server failed: %v", err)
|
2018-11-06 11:35:05 +01:00
|
|
|
|
|
|
|
// if login_fail_exit is true, just exit this program
|
|
|
|
// otherwise sleep a while and try again to connect to server
|
2019-08-20 22:53:27 +02:00
|
|
|
if svr.cfg.LoginFailExit {
|
2018-11-06 11:35:05 +01:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// login success
|
2020-03-01 03:57:01 +01:00
|
|
|
ctl := NewControl(svr.ctx, svr.runId, conn, session, svr.cfg, svr.pxyCfgs, svr.visitorCfgs, svr.serverUDPPort, svr.authSetter)
|
2018-11-06 11:35:05 +01:00
|
|
|
ctl.Run()
|
|
|
|
svr.ctlMu.Lock()
|
|
|
|
svr.ctl = ctl
|
|
|
|
svr.ctlMu.Unlock()
|
|
|
|
break
|
|
|
|
}
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 11:35:05 +01:00
|
|
|
go svr.keepControllerWorking()
|
|
|
|
|
2019-08-20 22:53:27 +02:00
|
|
|
if svr.cfg.AdminPort != 0 {
|
2019-08-16 20:02:00 +02:00
|
|
|
// Init admin server assets
|
2019-08-20 22:53:27 +02:00
|
|
|
err := assets.Load(svr.cfg.AssetsDir)
|
2019-08-16 20:02:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Load assets error: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-08-20 22:53:27 +02:00
|
|
|
err = svr.RunAdminServer(svr.cfg.AdminAddr, svr.cfg.AdminPort)
|
2017-07-12 20:20:49 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("run admin server error: %v", err)
|
|
|
|
}
|
2019-08-20 22:53:27 +02:00
|
|
|
log.Info("admin server listen on %s:%d", svr.cfg.AdminAddr, svr.cfg.AdminPort)
|
2017-07-12 20:20:49 +02:00
|
|
|
}
|
2019-10-12 14:13:12 +02:00
|
|
|
<-svr.ctx.Done()
|
2017-03-08 19:03:47 +01:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-26 19:59:30 +02:00
|
|
|
|
2018-11-06 11:35:05 +01:00
|
|
|
func (svr *Service) keepControllerWorking() {
|
2019-10-12 14:13:12 +02:00
|
|
|
xl := xlog.FromContextSafe(svr.ctx)
|
2018-11-06 11:35:05 +01:00
|
|
|
maxDelayTime := 20 * time.Second
|
|
|
|
delayTime := time.Second
|
|
|
|
|
|
|
|
for {
|
|
|
|
<-svr.ctl.ClosedDoneCh()
|
|
|
|
if atomic.LoadUint32(&svr.exit) != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2019-10-12 14:13:12 +02:00
|
|
|
xl.Info("try to reconnect to server...")
|
2018-11-06 11:35:05 +01:00
|
|
|
conn, session, err := svr.login()
|
|
|
|
if err != nil {
|
2019-10-12 14:13:12 +02:00
|
|
|
xl.Warn("reconnect to server error: %v", err)
|
2018-11-06 11:35:05 +01:00
|
|
|
time.Sleep(delayTime)
|
|
|
|
delayTime = delayTime * 2
|
|
|
|
if delayTime > maxDelayTime {
|
|
|
|
delayTime = maxDelayTime
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// reconnect success, init delayTime
|
|
|
|
delayTime = time.Second
|
|
|
|
|
2020-03-01 03:57:01 +01:00
|
|
|
ctl := NewControl(svr.ctx, svr.runId, conn, session, svr.cfg, svr.pxyCfgs, svr.visitorCfgs, svr.serverUDPPort, svr.authSetter)
|
2018-11-06 11:35:05 +01:00
|
|
|
ctl.Run()
|
|
|
|
svr.ctlMu.Lock()
|
|
|
|
svr.ctl = ctl
|
|
|
|
svr.ctlMu.Unlock()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// login creates a connection to frps and registers it self as a client
|
|
|
|
// conn: control connection
|
|
|
|
// session: if it's not nil, using tcp mux
|
2019-10-12 14:13:12 +02:00
|
|
|
func (svr *Service) login() (conn net.Conn, session *fmux.Session, err error) {
|
|
|
|
xl := xlog.FromContextSafe(svr.ctx)
|
2019-03-11 07:14:31 +01:00
|
|
|
var tlsConfig *tls.Config
|
2019-08-20 22:53:27 +02:00
|
|
|
if svr.cfg.TLSEnable {
|
2019-03-11 07:14:31 +01:00
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 22:53:27 +02:00
|
|
|
conn, err = frpNet.ConnectServerByProxyWithTLS(svr.cfg.HttpProxy, svr.cfg.Protocol,
|
|
|
|
fmt.Sprintf("%s:%d", svr.cfg.ServerAddr, svr.cfg.ServerPort), tlsConfig)
|
2018-11-06 11:35:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
2019-08-03 10:43:21 +02:00
|
|
|
if session != nil {
|
|
|
|
session.Close()
|
|
|
|
}
|
2018-11-06 11:35:05 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-08-20 22:53:27 +02:00
|
|
|
if svr.cfg.TcpMux {
|
2018-11-06 11:35:05 +01:00
|
|
|
fmuxCfg := fmux.DefaultConfig()
|
2018-12-11 08:06:54 +01:00
|
|
|
fmuxCfg.KeepAliveInterval = 20 * time.Second
|
2018-11-06 11:35:05 +01:00
|
|
|
fmuxCfg.LogOutput = ioutil.Discard
|
|
|
|
session, err = fmux.Client(conn, fmuxCfg)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stream, errRet := session.OpenStream()
|
|
|
|
if errRet != nil {
|
|
|
|
session.Close()
|
|
|
|
err = errRet
|
|
|
|
return
|
|
|
|
}
|
2019-10-12 14:13:12 +02:00
|
|
|
conn = stream
|
2018-11-06 11:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
loginMsg := &msg.Login{
|
2020-03-01 03:57:01 +01:00
|
|
|
Arch: runtime.GOARCH,
|
|
|
|
Os: runtime.GOOS,
|
|
|
|
PoolCount: svr.cfg.PoolCount,
|
|
|
|
User: svr.cfg.User,
|
|
|
|
Version: version.Full(),
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
RunId: svr.runId,
|
|
|
|
Metas: svr.cfg.Metas,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add auth
|
|
|
|
if err = svr.authSetter.SetLogin(loginMsg); err != nil {
|
|
|
|
return
|
2018-11-06 11:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = msg.WriteMsg(conn, loginMsg); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var loginRespMsg msg.LoginResp
|
|
|
|
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
|
|
|
|
if err = msg.ReadMsgInto(conn, &loginRespMsg); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conn.SetReadDeadline(time.Time{})
|
|
|
|
|
|
|
|
if loginRespMsg.Error != "" {
|
|
|
|
err = fmt.Errorf("%s", loginRespMsg.Error)
|
2019-10-12 14:13:12 +02:00
|
|
|
xl.Error("%s", loginRespMsg.Error)
|
2018-11-06 11:35:05 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
svr.runId = loginRespMsg.RunId
|
2019-10-12 14:13:12 +02:00
|
|
|
xl.ResetPrefixes()
|
|
|
|
xl.AppendPrefix(svr.runId)
|
|
|
|
|
2019-08-20 22:53:27 +02:00
|
|
|
svr.serverUDPPort = loginRespMsg.ServerUdpPort
|
2019-10-12 14:13:12 +02:00
|
|
|
xl.Info("login to server success, get run id [%s], server udp port [%d]", loginRespMsg.RunId, loginRespMsg.ServerUdpPort)
|
2018-11-06 11:35:05 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svr *Service) ReloadConf(pxyCfgs map[string]config.ProxyConf, visitorCfgs map[string]config.VisitorConf) error {
|
|
|
|
svr.cfgMu.Lock()
|
|
|
|
svr.pxyCfgs = pxyCfgs
|
|
|
|
svr.visitorCfgs = visitorCfgs
|
|
|
|
svr.cfgMu.Unlock()
|
|
|
|
|
|
|
|
return svr.ctl.ReloadConf(pxyCfgs, visitorCfgs)
|
|
|
|
}
|
|
|
|
|
2018-01-16 18:09:33 +01:00
|
|
|
func (svr *Service) Close() {
|
2018-11-06 11:35:05 +01:00
|
|
|
atomic.StoreUint32(&svr.exit, 1)
|
2018-01-16 18:09:33 +01:00
|
|
|
svr.ctl.Close()
|
2019-10-12 14:13:12 +02:00
|
|
|
svr.cancel()
|
2017-06-26 19:59:30 +02:00
|
|
|
}
|