2017-03-22 19:01:25 +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.
|
|
|
|
|
2017-03-08 19:03:47 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2018-08-10 05:43:08 +02:00
|
|
|
"bytes"
|
2019-03-11 07:14:31 +01:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2017-03-08 19:03:47 +01:00
|
|
|
"fmt"
|
2018-05-04 18:09:39 +02:00
|
|
|
"io/ioutil"
|
2019-03-11 07:14:31 +01:00
|
|
|
"math/big"
|
2017-12-12 20:27:43 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2017-03-08 19:03:47 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/assets"
|
2018-04-10 11:46:49 +02:00
|
|
|
"github.com/fatedier/frp/g"
|
2017-03-08 19:03:47 +01:00
|
|
|
"github.com/fatedier/frp/models/msg"
|
2019-01-14 17:11:08 +01:00
|
|
|
"github.com/fatedier/frp/models/nathole"
|
|
|
|
"github.com/fatedier/frp/server/controller"
|
2018-05-23 08:39:12 +02:00
|
|
|
"github.com/fatedier/frp/server/group"
|
|
|
|
"github.com/fatedier/frp/server/ports"
|
2019-01-14 17:11:08 +01:00
|
|
|
"github.com/fatedier/frp/server/proxy"
|
|
|
|
"github.com/fatedier/frp/server/stats"
|
2017-03-08 19:03:47 +01:00
|
|
|
"github.com/fatedier/frp/utils/log"
|
2017-05-17 11:47:20 +02:00
|
|
|
frpNet "github.com/fatedier/frp/utils/net"
|
2017-03-08 19:03:47 +01:00
|
|
|
"github.com/fatedier/frp/utils/util"
|
|
|
|
"github.com/fatedier/frp/utils/version"
|
|
|
|
"github.com/fatedier/frp/utils/vhost"
|
2017-05-17 11:47:20 +02:00
|
|
|
|
2018-05-08 18:23:42 +02:00
|
|
|
"github.com/fatedier/golib/net/mux"
|
2018-04-24 20:34:07 +02:00
|
|
|
fmux "github.com/hashicorp/yamux"
|
2017-03-08 19:03:47 +01:00
|
|
|
)
|
|
|
|
|
2017-05-09 18:46:42 +02:00
|
|
|
const (
|
|
|
|
connReadTimeout time.Duration = 10 * time.Second
|
|
|
|
)
|
|
|
|
|
2017-03-22 19:01:25 +01:00
|
|
|
var ServerService *Service
|
|
|
|
|
2018-05-22 17:59:35 +02:00
|
|
|
// Server service
|
2017-03-08 19:03:47 +01:00
|
|
|
type Service struct {
|
2018-05-22 17:59:35 +02:00
|
|
|
// Dispatch connections to different handlers listen on same port
|
2018-05-05 22:27:30 +02:00
|
|
|
muxer *mux.Mux
|
|
|
|
|
2018-05-22 17:59:35 +02:00
|
|
|
// Accept connections from client
|
2017-05-17 11:47:20 +02:00
|
|
|
listener frpNet.Listener
|
2017-03-08 19:03:47 +01:00
|
|
|
|
2018-05-22 17:59:35 +02:00
|
|
|
// Accept connections using kcp
|
2017-06-04 13:56:21 +02:00
|
|
|
kcpListener frpNet.Listener
|
|
|
|
|
2018-08-03 13:41:54 +02:00
|
|
|
// Accept connections using websocket
|
|
|
|
websocketListener frpNet.Listener
|
|
|
|
|
2019-03-11 07:14:31 +01:00
|
|
|
// Accept frp tls connections
|
|
|
|
tlsListener frpNet.Listener
|
|
|
|
|
2019-01-14 17:11:08 +01:00
|
|
|
// Manage all controllers
|
|
|
|
ctlManager *ControlManager
|
|
|
|
|
|
|
|
// Manage all proxies
|
|
|
|
pxyManager *proxy.ProxyManager
|
|
|
|
|
2019-01-10 13:53:06 +01:00
|
|
|
// All resource managers and controllers
|
2019-01-14 17:11:08 +01:00
|
|
|
rc *controller.ResourceController
|
|
|
|
|
|
|
|
// stats collector to store server and proxies stats info
|
|
|
|
statsCollector stats.Collector
|
2019-03-11 07:14:31 +01:00
|
|
|
|
|
|
|
tlsConfig *tls.Config
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewService() (svr *Service, err error) {
|
2018-04-10 11:46:49 +02:00
|
|
|
cfg := &g.GlbServerCfg.ServerCommonConf
|
2017-03-08 19:03:47 +01:00
|
|
|
svr = &Service{
|
2019-01-14 17:11:08 +01:00
|
|
|
ctlManager: NewControlManager(),
|
|
|
|
pxyManager: proxy.NewProxyManager(),
|
|
|
|
rc: &controller.ResourceController{
|
|
|
|
VisitorManager: controller.NewVisitorManager(),
|
2019-01-10 13:53:06 +01:00
|
|
|
TcpPortManager: ports.NewPortManager("tcp", cfg.ProxyBindAddr, cfg.AllowPorts),
|
|
|
|
UdpPortManager: ports.NewPortManager("udp", cfg.ProxyBindAddr, cfg.AllowPorts),
|
|
|
|
},
|
2019-03-11 07:14:31 +01:00
|
|
|
tlsConfig: generateTLSConfig(),
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
2019-01-14 17:11:08 +01:00
|
|
|
|
|
|
|
// Init group controller
|
2019-01-10 13:53:06 +01:00
|
|
|
svr.rc.TcpGroupCtl = group.NewTcpGroupCtl(svr.rc.TcpPortManager)
|
2017-03-08 19:03:47 +01:00
|
|
|
|
2019-02-01 12:26:10 +01:00
|
|
|
// Init assets
|
2017-10-24 12:20:07 +02:00
|
|
|
err = assets.Load(cfg.AssetsDir)
|
2017-03-08 19:03:47 +01:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Load assets error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-05 22:27:30 +02:00
|
|
|
var (
|
|
|
|
httpMuxOn bool
|
|
|
|
httpsMuxOn bool
|
|
|
|
)
|
|
|
|
if cfg.BindAddr == cfg.ProxyBindAddr {
|
|
|
|
if cfg.BindPort == cfg.VhostHttpPort {
|
|
|
|
httpMuxOn = true
|
|
|
|
}
|
|
|
|
if cfg.BindPort == cfg.VhostHttpsPort {
|
|
|
|
httpsMuxOn = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:03:47 +01:00
|
|
|
// Listen for accepting connections from client.
|
2018-05-05 22:27:30 +02:00
|
|
|
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.BindPort))
|
2017-03-08 19:03:47 +01:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Create server listener error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
2018-08-03 13:41:54 +02:00
|
|
|
|
2018-08-10 06:02:38 +02:00
|
|
|
svr.muxer = mux.NewMux(ln)
|
|
|
|
go svr.muxer.Serve()
|
2018-08-03 13:41:54 +02:00
|
|
|
ln = svr.muxer.DefaultListener()
|
|
|
|
|
2018-05-05 22:27:30 +02:00
|
|
|
svr.listener = frpNet.WrapLogListener(ln)
|
2017-10-24 12:20:07 +02:00
|
|
|
log.Info("frps tcp listen on %s:%d", cfg.BindAddr, cfg.BindPort)
|
2017-06-04 13:56:21 +02:00
|
|
|
|
|
|
|
// Listen for accepting connections from client using kcp protocol.
|
2017-10-24 12:20:07 +02:00
|
|
|
if cfg.KcpBindPort > 0 {
|
|
|
|
svr.kcpListener, err = frpNet.ListenKcp(cfg.BindAddr, cfg.KcpBindPort)
|
2017-06-04 13:56:21 +02:00
|
|
|
if err != nil {
|
2017-10-24 12:20:07 +02:00
|
|
|
err = fmt.Errorf("Listen on kcp address udp [%s:%d] error: %v", cfg.BindAddr, cfg.KcpBindPort, err)
|
2017-06-04 13:56:21 +02:00
|
|
|
return
|
|
|
|
}
|
2017-12-10 18:36:47 +01:00
|
|
|
log.Info("frps kcp listen on udp %s:%d", cfg.BindAddr, cfg.KcpBindPort)
|
2017-06-04 13:56:21 +02:00
|
|
|
}
|
2017-03-08 19:03:47 +01:00
|
|
|
|
2018-08-10 05:43:08 +02:00
|
|
|
// Listen for accepting connections from client using websocket protocol.
|
2018-08-10 08:44:14 +02:00
|
|
|
websocketPrefix := []byte("GET " + frpNet.FrpWebsocketPath)
|
2018-08-10 05:43:08 +02:00
|
|
|
websocketLn := svr.muxer.Listen(0, uint32(len(websocketPrefix)), func(data []byte) bool {
|
|
|
|
return bytes.Equal(data, websocketPrefix)
|
|
|
|
})
|
|
|
|
svr.websocketListener = frpNet.NewWebsocketListener(websocketLn)
|
|
|
|
|
2017-03-08 19:03:47 +01:00
|
|
|
// Create http vhost muxer.
|
2017-10-24 12:20:07 +02:00
|
|
|
if cfg.VhostHttpPort > 0 {
|
2018-08-08 05:18:38 +02:00
|
|
|
rp := vhost.NewHttpReverseProxy(vhost.HttpReverseProxyOptions{
|
|
|
|
ResponseHeaderTimeoutS: cfg.VhostHttpTimeout,
|
|
|
|
})
|
2019-01-10 13:53:06 +01:00
|
|
|
svr.rc.HttpReverseProxy = rp
|
2017-12-12 20:27:43 +01:00
|
|
|
|
|
|
|
address := fmt.Sprintf("%s:%d", cfg.ProxyBindAddr, cfg.VhostHttpPort)
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: address,
|
|
|
|
Handler: rp,
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
2017-12-12 20:27:43 +01:00
|
|
|
var l net.Listener
|
2018-05-05 22:27:30 +02:00
|
|
|
if httpMuxOn {
|
2018-08-10 05:43:08 +02:00
|
|
|
l = svr.muxer.ListenHttp(1)
|
2018-05-05 22:27:30 +02:00
|
|
|
} else {
|
|
|
|
l, err = net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Create vhost http listener error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
2017-12-12 20:27:43 +01:00
|
|
|
go server.Serve(l)
|
2017-10-24 12:20:07 +02:00
|
|
|
log.Info("http service listen on %s:%d", cfg.ProxyBindAddr, cfg.VhostHttpPort)
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create https vhost muxer.
|
2017-10-24 12:20:07 +02:00
|
|
|
if cfg.VhostHttpsPort > 0 {
|
2018-05-05 22:27:30 +02:00
|
|
|
var l net.Listener
|
|
|
|
if httpsMuxOn {
|
2018-08-10 05:43:08 +02:00
|
|
|
l = svr.muxer.ListenHttps(1)
|
2018-05-05 22:27:30 +02:00
|
|
|
} else {
|
|
|
|
l, err = net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.ProxyBindAddr, cfg.VhostHttpsPort))
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Create server listener error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
2018-05-05 22:27:30 +02:00
|
|
|
|
2019-01-10 13:53:06 +01:00
|
|
|
svr.rc.VhostHttpsMuxer, err = vhost.NewHttpsMuxer(frpNet.WrapLogListener(l), 30*time.Second)
|
2017-03-08 19:03:47 +01:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Create vhost httpsMuxer error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-10-24 12:20:07 +02:00
|
|
|
log.Info("https service listen on %s:%d", cfg.ProxyBindAddr, cfg.VhostHttpsPort)
|
|
|
|
}
|
|
|
|
|
2019-03-11 07:14:31 +01:00
|
|
|
// frp tls listener
|
|
|
|
tlsListener := svr.muxer.Listen(1, 1, func(data []byte) bool {
|
|
|
|
return int(data[0]) == frpNet.FRP_TLS_HEAD_BYTE
|
|
|
|
})
|
|
|
|
svr.tlsListener = frpNet.WrapLogListener(tlsListener)
|
|
|
|
|
2017-10-24 12:20:07 +02:00
|
|
|
// Create nat hole controller.
|
|
|
|
if cfg.BindUdpPort > 0 {
|
2019-01-14 17:11:08 +01:00
|
|
|
var nc *nathole.NatHoleController
|
2017-10-24 12:20:07 +02:00
|
|
|
addr := fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.BindUdpPort)
|
2019-01-14 17:11:08 +01:00
|
|
|
nc, err = nathole.NewNatHoleController(addr)
|
2017-10-24 12:20:07 +02:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Create nat hole controller error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-01-10 13:53:06 +01:00
|
|
|
svr.rc.NatHoleController = nc
|
2017-10-24 12:20:07 +02:00
|
|
|
log.Info("nat hole udp service listen on %s:%d", cfg.BindAddr, cfg.BindUdpPort)
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
2019-01-14 17:11:08 +01:00
|
|
|
var statsEnable bool
|
2017-03-08 19:03:47 +01:00
|
|
|
// Create dashboard web server.
|
2017-10-24 12:20:07 +02:00
|
|
|
if cfg.DashboardPort > 0 {
|
2019-01-10 13:53:06 +01:00
|
|
|
err = svr.RunDashboardServer(cfg.DashboardAddr, cfg.DashboardPort)
|
2017-03-08 19:03:47 +01:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Create dashboard web server error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-11-28 08:56:34 +01:00
|
|
|
log.Info("Dashboard listen on %s:%d", cfg.DashboardAddr, cfg.DashboardPort)
|
2019-01-14 17:11:08 +01:00
|
|
|
statsEnable = true
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
2018-08-03 13:41:54 +02:00
|
|
|
|
2019-01-14 17:11:08 +01:00
|
|
|
svr.statsCollector = stats.NewInternalCollector(statsEnable)
|
2017-03-08 19:03:47 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svr *Service) Run() {
|
2019-01-10 13:53:06 +01:00
|
|
|
if svr.rc.NatHoleController != nil {
|
|
|
|
go svr.rc.NatHoleController.Run()
|
2017-10-24 12:20:07 +02:00
|
|
|
}
|
2018-04-10 11:46:49 +02:00
|
|
|
if g.GlbServerCfg.KcpBindPort > 0 {
|
2017-06-04 13:56:21 +02:00
|
|
|
go svr.HandleListener(svr.kcpListener)
|
|
|
|
}
|
|
|
|
|
2018-08-10 05:43:08 +02:00
|
|
|
go svr.HandleListener(svr.websocketListener)
|
2019-03-11 07:14:31 +01:00
|
|
|
go svr.HandleListener(svr.tlsListener)
|
2018-08-10 05:43:08 +02:00
|
|
|
|
2017-06-04 13:56:21 +02:00
|
|
|
svr.HandleListener(svr.listener)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svr *Service) HandleListener(l frpNet.Listener) {
|
2017-03-08 19:03:47 +01:00
|
|
|
// Listen for incoming connections from client.
|
|
|
|
for {
|
2017-06-04 13:56:21 +02:00
|
|
|
c, err := l.Accept()
|
2017-03-08 19:03:47 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("Listener for incoming connections from client closed")
|
|
|
|
return
|
|
|
|
}
|
2019-03-11 07:14:31 +01:00
|
|
|
c = frpNet.CheckAndEnableTLSServerConn(c, svr.tlsConfig)
|
2017-03-08 19:03:47 +01:00
|
|
|
|
|
|
|
// Start a new goroutine for dealing connections.
|
2017-05-17 11:47:20 +02:00
|
|
|
go func(frpConn frpNet.Conn) {
|
|
|
|
dealFn := func(conn frpNet.Conn) {
|
|
|
|
var rawMsg msg.Message
|
|
|
|
conn.SetReadDeadline(time.Now().Add(connReadTimeout))
|
|
|
|
if rawMsg, err = msg.ReadMsg(conn); err != nil {
|
2017-06-04 13:56:21 +02:00
|
|
|
log.Trace("Failed to read message: %v", err)
|
2017-05-17 11:47:20 +02:00
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conn.SetReadDeadline(time.Time{})
|
|
|
|
|
|
|
|
switch m := rawMsg.(type) {
|
|
|
|
case *msg.Login:
|
|
|
|
err = svr.RegisterControl(conn, m)
|
|
|
|
// If login failed, send error message there.
|
|
|
|
// Otherwise send success message in control's work goroutine.
|
|
|
|
if err != nil {
|
|
|
|
conn.Warn("%v", err)
|
|
|
|
msg.WriteMsg(conn, &msg.LoginResp{
|
|
|
|
Version: version.Full(),
|
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
case *msg.NewWorkConn:
|
|
|
|
svr.RegisterWorkConn(conn, m)
|
2017-12-04 18:34:33 +01:00
|
|
|
case *msg.NewVisitorConn:
|
|
|
|
if err = svr.RegisterVisitorConn(conn, m); err != nil {
|
2017-06-25 21:02:33 +02:00
|
|
|
conn.Warn("%v", err)
|
2017-12-04 18:34:33 +01:00
|
|
|
msg.WriteMsg(conn, &msg.NewVisitorConnResp{
|
2017-06-25 21:02:33 +02:00
|
|
|
ProxyName: m.ProxyName,
|
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
conn.Close()
|
|
|
|
} else {
|
2017-12-04 18:34:33 +01:00
|
|
|
msg.WriteMsg(conn, &msg.NewVisitorConnResp{
|
2017-06-25 21:02:33 +02:00
|
|
|
ProxyName: m.ProxyName,
|
|
|
|
Error: "",
|
|
|
|
})
|
|
|
|
}
|
2017-05-17 11:47:20 +02:00
|
|
|
default:
|
|
|
|
log.Warn("Error message type for the new connection [%s]", conn.RemoteAddr().String())
|
|
|
|
conn.Close()
|
|
|
|
}
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
2018-04-10 11:46:49 +02:00
|
|
|
if g.GlbServerCfg.TcpMux {
|
2018-05-04 18:09:39 +02:00
|
|
|
fmuxCfg := fmux.DefaultConfig()
|
2018-12-11 08:06:54 +01:00
|
|
|
fmuxCfg.KeepAliveInterval = 20 * time.Second
|
2018-05-04 18:09:39 +02:00
|
|
|
fmuxCfg.LogOutput = ioutil.Discard
|
|
|
|
session, err := fmux.Server(frpConn, fmuxCfg)
|
2017-03-08 19:03:47 +01:00
|
|
|
if err != nil {
|
2017-05-17 11:47:20 +02:00
|
|
|
log.Warn("Failed to create mux connection: %v", err)
|
2017-03-08 19:03:47 +01:00
|
|
|
frpConn.Close()
|
2017-05-17 11:47:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
stream, err := session.AcceptStream()
|
|
|
|
if err != nil {
|
2018-05-11 04:35:16 +02:00
|
|
|
log.Debug("Accept new mux stream error: %v", err)
|
2017-05-17 19:51:32 +02:00
|
|
|
session.Close()
|
2017-05-17 11:47:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
wrapConn := frpNet.WrapConn(stream)
|
|
|
|
go dealFn(wrapConn)
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
2017-05-17 11:47:20 +02:00
|
|
|
} else {
|
|
|
|
dealFn(frpConn)
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
}(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:47:20 +02:00
|
|
|
func (svr *Service) RegisterControl(ctlConn frpNet.Conn, loginMsg *msg.Login) (err error) {
|
2017-03-08 19:03:47 +01:00
|
|
|
ctlConn.Info("client login info: ip [%s] version [%s] hostname [%s] os [%s] arch [%s]",
|
|
|
|
ctlConn.RemoteAddr().String(), loginMsg.Version, loginMsg.Hostname, loginMsg.Os, loginMsg.Arch)
|
|
|
|
|
|
|
|
// Check client version.
|
|
|
|
if ok, msg := version.Compat(loginMsg.Version); !ok {
|
|
|
|
err = fmt.Errorf("%s", msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check auth.
|
2018-04-10 11:46:49 +02:00
|
|
|
if util.GetAuthKey(g.GlbServerCfg.Token, loginMsg.Timestamp) != loginMsg.PrivilegeKey {
|
2017-03-08 19:03:47 +01:00
|
|
|
err = fmt.Errorf("authorization failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If client's RunId is empty, it's a new client, we just create a new controller.
|
|
|
|
// Otherwise, we check if there is one controller has the same run id. If so, we release previous controller and start new one.
|
|
|
|
if loginMsg.RunId == "" {
|
|
|
|
loginMsg.RunId, err = util.RandId()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 17:11:08 +01:00
|
|
|
ctl := NewControl(svr.rc, svr.pxyManager, svr.statsCollector, ctlConn, loginMsg)
|
2017-03-08 19:03:47 +01:00
|
|
|
|
2019-01-14 17:11:08 +01:00
|
|
|
if oldCtl := svr.ctlManager.Add(loginMsg.RunId, ctl); oldCtl != nil {
|
2018-01-16 18:09:33 +01:00
|
|
|
oldCtl.allShutdown.WaitDone()
|
2017-03-08 19:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctlConn.AddLogPrefix(loginMsg.RunId)
|
|
|
|
ctl.Start()
|
2017-03-22 19:01:25 +01:00
|
|
|
|
|
|
|
// for statistics
|
2019-01-14 17:11:08 +01:00
|
|
|
svr.statsCollector.Mark(stats.TypeNewClient, &stats.NewClientPayload{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// block until control closed
|
|
|
|
ctl.WaitClosed()
|
2019-01-26 14:36:24 +01:00
|
|
|
svr.ctlManager.Del(loginMsg.RunId, ctl)
|
2019-01-14 17:11:08 +01:00
|
|
|
}()
|
2017-03-08 19:03:47 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterWorkConn register a new work connection to control and proxies need it.
|
2017-05-17 11:47:20 +02:00
|
|
|
func (svr *Service) RegisterWorkConn(workConn frpNet.Conn, newMsg *msg.NewWorkConn) {
|
2019-01-14 17:11:08 +01:00
|
|
|
ctl, exist := svr.ctlManager.GetById(newMsg.RunId)
|
2017-03-08 19:03:47 +01:00
|
|
|
if !exist {
|
|
|
|
workConn.Warn("No client control found for run id [%s]", newMsg.RunId)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctl.RegisterWorkConn(workConn)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-04 18:34:33 +01:00
|
|
|
func (svr *Service) RegisterVisitorConn(visitorConn frpNet.Conn, newMsg *msg.NewVisitorConn) error {
|
2019-01-10 13:53:06 +01:00
|
|
|
return svr.rc.VisitorManager.NewConn(newMsg.ProxyName, visitorConn, newMsg.Timestamp, newMsg.SignKey,
|
2017-06-25 21:02:33 +02:00
|
|
|
newMsg.UseEncryption, newMsg.UseCompression)
|
|
|
|
}
|
2019-03-11 07:14:31 +01:00
|
|
|
|
|
|
|
// Setup a bare-bones TLS config for the server
|
|
|
|
func generateTLSConfig() *tls.Config {
|
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
template := x509.Certificate{SerialNumber: big.NewInt(1)}
|
|
|
|
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
|
|
|
|
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
|
|
|
|
|
|
|
|
tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &tls.Config{Certificates: []tls.Certificate{tlsCert}}
|
|
|
|
}
|