2017-03-23 02:01:25 +08: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-09 02:03:47 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/assets"
|
|
|
|
"github.com/fatedier/frp/models/config"
|
|
|
|
"github.com/fatedier/frp/models/msg"
|
|
|
|
"github.com/fatedier/frp/utils/log"
|
2017-05-17 17:47:20 +08:00
|
|
|
frpNet "github.com/fatedier/frp/utils/net"
|
2017-03-09 02:03:47 +08:00
|
|
|
"github.com/fatedier/frp/utils/util"
|
|
|
|
"github.com/fatedier/frp/utils/version"
|
|
|
|
"github.com/fatedier/frp/utils/vhost"
|
2017-05-17 17:47:20 +08:00
|
|
|
|
|
|
|
"github.com/xtaci/smux"
|
2017-03-09 02:03:47 +08:00
|
|
|
)
|
|
|
|
|
2017-05-10 00:46:42 +08:00
|
|
|
const (
|
|
|
|
connReadTimeout time.Duration = 10 * time.Second
|
|
|
|
)
|
|
|
|
|
2017-03-23 02:01:25 +08:00
|
|
|
var ServerService *Service
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
// Server service.
|
|
|
|
type Service struct {
|
|
|
|
// Accept connections from client.
|
2017-05-17 17:47:20 +08:00
|
|
|
listener frpNet.Listener
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2017-06-04 19:56:21 +08:00
|
|
|
// Accept connections using kcp.
|
|
|
|
kcpListener frpNet.Listener
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
// For http proxies, route requests to different clients by hostname and other infomation.
|
|
|
|
VhostHttpMuxer *vhost.HttpMuxer
|
|
|
|
|
|
|
|
// For https proxies, route requests to different clients by hostname and other infomation.
|
|
|
|
VhostHttpsMuxer *vhost.HttpsMuxer
|
|
|
|
|
|
|
|
// Manage all controllers.
|
|
|
|
ctlManager *ControlManager
|
|
|
|
|
|
|
|
// Manage all proxies.
|
|
|
|
pxyManager *ProxyManager
|
2017-06-26 03:02:33 +08:00
|
|
|
|
|
|
|
// Manage all vistor listeners.
|
|
|
|
vistorManager *VistorManager
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewService() (svr *Service, err error) {
|
|
|
|
svr = &Service{
|
2017-06-26 03:02:33 +08:00
|
|
|
ctlManager: NewControlManager(),
|
|
|
|
pxyManager: NewProxyManager(),
|
|
|
|
vistorManager: NewVistorManager(),
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init assets.
|
|
|
|
err = assets.Load(config.ServerCommonCfg.AssetsDir)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Load assets error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen for accepting connections from client.
|
2017-05-17 17:47:20 +08:00
|
|
|
svr.listener, err = frpNet.ListenTcp(config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.BindPort)
|
2017-03-09 02:03:47 +08:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Create server listener error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-06-04 19:56:21 +08:00
|
|
|
log.Info("frps tcp listen on %s:%d", config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.BindPort)
|
|
|
|
|
|
|
|
// Listen for accepting connections from client using kcp protocol.
|
2017-06-13 23:36:10 +08:00
|
|
|
if config.ServerCommonCfg.KcpBindPort > 0 {
|
|
|
|
svr.kcpListener, err = frpNet.ListenKcp(config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.KcpBindPort)
|
2017-06-04 19:56:21 +08:00
|
|
|
if err != nil {
|
2017-06-13 23:36:10 +08:00
|
|
|
err = fmt.Errorf("Listen on kcp address udp [%s:%d] error: %v", config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.KcpBindPort, err)
|
2017-06-04 19:56:21 +08:00
|
|
|
return
|
|
|
|
}
|
2017-06-13 23:36:10 +08:00
|
|
|
log.Info("frps kcp listen on udp %s:%d", config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.BindPort)
|
2017-06-04 19:56:21 +08:00
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
|
|
|
|
// Create http vhost muxer.
|
2017-06-13 23:36:10 +08:00
|
|
|
if config.ServerCommonCfg.VhostHttpPort > 0 {
|
2017-05-17 17:47:20 +08:00
|
|
|
var l frpNet.Listener
|
|
|
|
l, err = frpNet.ListenTcp(config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.VhostHttpPort)
|
2017-03-09 02:03:47 +08:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Create vhost http listener error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
svr.VhostHttpMuxer, err = vhost.NewHttpMuxer(l, 30*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Create vhost httpMuxer error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-06-04 19:56:21 +08:00
|
|
|
log.Info("http service listen on %s:%d", config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.VhostHttpPort)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create https vhost muxer.
|
2017-06-13 23:36:10 +08:00
|
|
|
if config.ServerCommonCfg.VhostHttpsPort > 0 {
|
2017-05-17 17:47:20 +08:00
|
|
|
var l frpNet.Listener
|
|
|
|
l, err = frpNet.ListenTcp(config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.VhostHttpsPort)
|
2017-03-09 02:03:47 +08:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Create vhost https listener error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
svr.VhostHttpsMuxer, err = vhost.NewHttpsMuxer(l, 30*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Create vhost httpsMuxer error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-06-04 19:56:21 +08:00
|
|
|
log.Info("https service listen on %s:%d", config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.VhostHttpsPort)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create dashboard web server.
|
2017-06-13 23:36:10 +08:00
|
|
|
if config.ServerCommonCfg.DashboardPort > 0 {
|
2017-03-09 02:03:47 +08:00
|
|
|
err = RunDashboardServer(config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.DashboardPort)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Create dashboard web server error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-19 18:38:31 +08:00
|
|
|
log.Info("Dashboard listen on %s:%d", config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.DashboardPort)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svr *Service) Run() {
|
2017-06-13 23:36:10 +08:00
|
|
|
if config.ServerCommonCfg.KcpBindPort > 0 {
|
2017-06-04 19:56:21 +08:00
|
|
|
go svr.HandleListener(svr.kcpListener)
|
|
|
|
}
|
|
|
|
svr.HandleListener(svr.listener)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svr *Service) HandleListener(l frpNet.Listener) {
|
2017-03-09 02:03:47 +08:00
|
|
|
// Listen for incoming connections from client.
|
|
|
|
for {
|
2017-06-04 19:56:21 +08:00
|
|
|
c, err := l.Accept()
|
2017-03-09 02:03:47 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("Listener for incoming connections from client closed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a new goroutine for dealing connections.
|
2017-05-17 17:47:20 +08: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 19:56:21 +08:00
|
|
|
log.Trace("Failed to read message: %v", err)
|
2017-05-17 17:47:20 +08: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-06-26 03:02:33 +08:00
|
|
|
case *msg.NewVistorConn:
|
|
|
|
if err = svr.RegisterVistorConn(conn, m); err != nil {
|
|
|
|
conn.Warn("%v", err)
|
|
|
|
msg.WriteMsg(conn, &msg.NewVistorConnResp{
|
|
|
|
ProxyName: m.ProxyName,
|
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
conn.Close()
|
|
|
|
} else {
|
|
|
|
msg.WriteMsg(conn, &msg.NewVistorConnResp{
|
|
|
|
ProxyName: m.ProxyName,
|
|
|
|
Error: "",
|
|
|
|
})
|
|
|
|
}
|
2017-05-17 17:47:20 +08:00
|
|
|
default:
|
|
|
|
log.Warn("Error message type for the new connection [%s]", conn.RemoteAddr().String())
|
|
|
|
conn.Close()
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 17:47:20 +08:00
|
|
|
if config.ServerCommonCfg.TcpMux {
|
|
|
|
session, err := smux.Server(frpConn, nil)
|
2017-03-09 02:03:47 +08:00
|
|
|
if err != nil {
|
2017-05-17 17:47:20 +08:00
|
|
|
log.Warn("Failed to create mux connection: %v", err)
|
2017-03-09 02:03:47 +08:00
|
|
|
frpConn.Close()
|
2017-05-17 17:47:20 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
stream, err := session.AcceptStream()
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Accept new mux stream error: %v", err)
|
2017-05-18 01:51:32 +08:00
|
|
|
session.Close()
|
2017-05-17 17:47:20 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
wrapConn := frpNet.WrapConn(stream)
|
|
|
|
go dealFn(wrapConn)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
2017-05-17 17:47:20 +08:00
|
|
|
} else {
|
|
|
|
dealFn(frpConn)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
}(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:47:20 +08:00
|
|
|
func (svr *Service) RegisterControl(ctlConn frpNet.Conn, loginMsg *msg.Login) (err error) {
|
2017-03-09 02:03:47 +08: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.
|
|
|
|
nowTime := time.Now().Unix()
|
|
|
|
if config.ServerCommonCfg.AuthTimeout != 0 && nowTime-loginMsg.Timestamp > config.ServerCommonCfg.AuthTimeout {
|
|
|
|
err = fmt.Errorf("authorization timeout")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if util.GetAuthKey(config.ServerCommonCfg.PrivilegeToken, loginMsg.Timestamp) != loginMsg.PrivilegeKey {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctl := NewControl(svr, ctlConn, loginMsg)
|
|
|
|
|
|
|
|
if oldCtl := svr.ctlManager.Add(loginMsg.RunId, ctl); oldCtl != nil {
|
|
|
|
oldCtl.allShutdown.WaitDown()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlConn.AddLogPrefix(loginMsg.RunId)
|
|
|
|
ctl.Start()
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
|
|
// for statistics
|
|
|
|
StatsNewClient()
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterWorkConn register a new work connection to control and proxies need it.
|
2017-05-17 17:47:20 +08:00
|
|
|
func (svr *Service) RegisterWorkConn(workConn frpNet.Conn, newMsg *msg.NewWorkConn) {
|
2017-03-09 02:03:47 +08:00
|
|
|
ctl, exist := svr.ctlManager.GetById(newMsg.RunId)
|
|
|
|
if !exist {
|
|
|
|
workConn.Warn("No client control found for run id [%s]", newMsg.RunId)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctl.RegisterWorkConn(workConn)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-26 03:02:33 +08:00
|
|
|
func (svr *Service) RegisterVistorConn(vistorConn frpNet.Conn, newMsg *msg.NewVistorConn) error {
|
|
|
|
return svr.vistorManager.NewConn(newMsg.ProxyName, vistorConn, newMsg.Timestamp, newMsg.SignKey,
|
|
|
|
newMsg.UseEncryption, newMsg.UseCompression)
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
func (svr *Service) RegisterProxy(name string, pxy Proxy) error {
|
2017-06-26 03:02:33 +08:00
|
|
|
return svr.pxyManager.Add(name, pxy)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (svr *Service) DelProxy(name string) {
|
|
|
|
svr.pxyManager.Del(name)
|
|
|
|
}
|