frp/server/proxy.go

667 lines
17 KiB
Go
Raw Normal View History

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 (
2017-03-12 19:44:47 +01:00
"context"
2017-03-08 19:03:47 +01:00
"fmt"
"io"
2017-03-12 19:44:47 +01:00
"net"
2018-01-17 07:40:08 +01:00
"strings"
2017-04-24 18:34:14 +02:00
"sync"
2017-03-12 19:44:47 +01:00
"time"
2017-03-08 19:03:47 +01:00
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/config"
"github.com/fatedier/frp/models/msg"
2017-03-12 19:44:47 +01:00
"github.com/fatedier/frp/models/proto/udp"
"github.com/fatedier/frp/utils/errors"
2017-06-06 12:48:40 +02:00
frpIo "github.com/fatedier/frp/utils/io"
2017-03-08 19:03:47 +01:00
"github.com/fatedier/frp/utils/log"
2017-03-12 19:44:47 +01:00
frpNet "github.com/fatedier/frp/utils/net"
2018-01-17 07:40:08 +01:00
"github.com/fatedier/frp/utils/util"
2017-03-09 19:01:17 +01:00
"github.com/fatedier/frp/utils/vhost"
2017-03-08 19:03:47 +01:00
)
type Proxy interface {
2018-01-17 07:40:08 +01:00
Run() (remoteAddr string, err error)
2017-03-08 19:03:47 +01:00
GetControl() *Control
GetName() string
GetConf() config.ProxyConf
2017-03-12 19:44:47 +01:00
GetWorkConnFromPool() (workConn frpNet.Conn, err error)
2018-01-26 07:56:55 +01:00
GetUsedPortsNum() int
2017-03-08 19:03:47 +01:00
Close()
log.Logger
}
type BaseProxy struct {
2018-01-26 07:56:55 +01:00
name string
ctl *Control
listeners []frpNet.Listener
usedPortsNum int
mu sync.RWMutex
2017-03-08 19:03:47 +01:00
log.Logger
}
func (pxy *BaseProxy) GetName() string {
return pxy.name
}
func (pxy *BaseProxy) GetControl() *Control {
return pxy.ctl
}
2018-01-26 07:56:55 +01:00
func (pxy *BaseProxy) GetUsedPortsNum() int {
return pxy.usedPortsNum
}
2017-03-08 19:03:47 +01:00
func (pxy *BaseProxy) Close() {
pxy.Info("proxy closing")
for _, l := range pxy.listeners {
l.Close()
}
}
2017-03-12 19:44:47 +01:00
func (pxy *BaseProxy) GetWorkConnFromPool() (workConn frpNet.Conn, err error) {
ctl := pxy.GetControl()
// try all connections from the pool
for i := 0; i < ctl.poolCount+1; i++ {
if workConn, err = ctl.GetWorkConn(); err != nil {
pxy.Warn("failed to get work connection: %v", err)
return
}
pxy.Info("get a new work connection: [%s]", workConn.RemoteAddr().String())
workConn.AddLogPrefix(pxy.GetName())
err := msg.WriteMsg(workConn, &msg.StartWorkConn{
ProxyName: pxy.GetName(),
})
if err != nil {
workConn.Warn("failed to send message to work connection from pool: %v, times: %d", err, i)
workConn.Close()
} else {
break
}
}
if err != nil {
pxy.Error("try to get work connection failed in the end")
return
}
return
}
2017-03-09 19:01:17 +01:00
// startListenHandler start a goroutine handler for each listener.
2017-03-12 19:44:47 +01:00
// p: p will just be passed to handler(Proxy, frpNet.Conn).
2017-03-09 19:01:17 +01:00
// handler: each proxy type can set different handler function to deal with connections accepted from listeners.
2017-03-12 19:44:47 +01:00
func (pxy *BaseProxy) startListenHandler(p Proxy, handler func(Proxy, frpNet.Conn)) {
2017-03-09 19:01:17 +01:00
for _, listener := range pxy.listeners {
2017-03-12 19:44:47 +01:00
go func(l frpNet.Listener) {
2017-03-09 19:01:17 +01:00
for {
// block
// if listener is closed, err returned
c, err := l.Accept()
if err != nil {
pxy.Info("listener is closed")
return
}
pxy.Debug("get a user connection [%s]", c.RemoteAddr().String())
go handler(p, c)
}
}(listener)
}
}
2017-03-08 19:03:47 +01:00
func NewProxy(ctl *Control, pxyConf config.ProxyConf) (pxy Proxy, err error) {
basePxy := BaseProxy{
2018-04-10 11:46:49 +02:00
name: pxyConf.GetBaseInfo().ProxyName,
2017-03-08 19:03:47 +01:00
ctl: ctl,
2017-03-12 19:44:47 +01:00
listeners: make([]frpNet.Listener, 0),
2017-03-08 19:03:47 +01:00
Logger: log.NewPrefixLogger(ctl.runId),
}
switch cfg := pxyConf.(type) {
case *config.TcpProxyConf:
2018-01-26 07:56:55 +01:00
basePxy.usedPortsNum = 1
2017-03-08 19:03:47 +01:00
pxy = &TcpProxy{
BaseProxy: basePxy,
cfg: cfg,
}
case *config.HttpProxyConf:
pxy = &HttpProxy{
BaseProxy: basePxy,
cfg: cfg,
}
case *config.HttpsProxyConf:
pxy = &HttpsProxy{
BaseProxy: basePxy,
cfg: cfg,
}
case *config.UdpProxyConf:
2018-01-26 07:56:55 +01:00
basePxy.usedPortsNum = 1
2017-03-08 19:03:47 +01:00
pxy = &UdpProxy{
BaseProxy: basePxy,
cfg: cfg,
}
2017-06-25 21:02:33 +02:00
case *config.StcpProxyConf:
pxy = &StcpProxy{
BaseProxy: basePxy,
cfg: cfg,
}
2017-10-24 12:20:07 +02:00
case *config.XtcpProxyConf:
pxy = &XtcpProxy{
BaseProxy: basePxy,
cfg: cfg,
}
2017-03-08 19:03:47 +01:00
default:
return pxy, fmt.Errorf("proxy type not support")
}
pxy.AddLogPrefix(pxy.GetName())
return
}
type TcpProxy struct {
BaseProxy
cfg *config.TcpProxyConf
realPort int
2017-03-08 19:03:47 +01:00
}
2018-01-17 07:40:08 +01:00
func (pxy *TcpProxy) Run() (remoteAddr string, err error) {
pxy.realPort, err = pxy.ctl.svr.tcpPortManager.Acquire(pxy.name, pxy.cfg.RemotePort)
if err != nil {
return
}
defer func() {
if err != nil {
pxy.ctl.svr.tcpPortManager.Release(pxy.realPort)
}
}()
remoteAddr = fmt.Sprintf(":%d", pxy.realPort)
pxy.cfg.RemotePort = pxy.realPort
2018-04-10 11:46:49 +02:00
listener, errRet := frpNet.ListenTcp(g.GlbServerCfg.ProxyBindAddr, pxy.realPort)
2018-01-17 07:40:08 +01:00
if errRet != nil {
err = errRet
return
2017-03-08 19:03:47 +01:00
}
2017-03-11 19:03:24 +01:00
listener.AddLogPrefix(pxy.name)
2017-03-08 19:03:47 +01:00
pxy.listeners = append(pxy.listeners, listener)
2017-03-09 19:01:17 +01:00
pxy.Info("tcp proxy listen port [%d]", pxy.cfg.RemotePort)
2017-03-08 19:03:47 +01:00
2017-03-09 19:01:17 +01:00
pxy.startListenHandler(pxy, HandleUserTcpConnection)
2018-01-17 07:40:08 +01:00
return
2017-03-08 19:03:47 +01:00
}
func (pxy *TcpProxy) GetConf() config.ProxyConf {
return pxy.cfg
}
func (pxy *TcpProxy) Close() {
pxy.BaseProxy.Close()
pxy.ctl.svr.tcpPortManager.Release(pxy.realPort)
2017-03-08 19:03:47 +01:00
}
type HttpProxy struct {
BaseProxy
cfg *config.HttpProxyConf
2017-12-12 20:27:43 +01:00
closeFuncs []func()
2017-03-08 19:03:47 +01:00
}
2018-01-17 07:40:08 +01:00
func (pxy *HttpProxy) Run() (remoteAddr string, err error) {
2017-12-13 16:44:27 +01:00
routeConfig := vhost.VhostRouteConfig{
RewriteHost: pxy.cfg.HostHeaderRewrite,
Username: pxy.cfg.HttpUser,
Password: pxy.cfg.HttpPwd,
CreateConnFn: pxy.GetRealConn,
2017-03-09 19:01:17 +01:00
}
locations := pxy.cfg.Locations
if len(locations) == 0 {
locations = []string{""}
}
2018-01-17 07:40:08 +01:00
addrs := make([]string, 0)
2017-03-09 19:01:17 +01:00
for _, domain := range pxy.cfg.CustomDomains {
routeConfig.Domain = domain
for _, location := range locations {
routeConfig.Location = location
2018-01-17 07:40:08 +01:00
err = pxy.ctl.svr.httpReverseProxy.Register(routeConfig)
2017-03-09 19:01:17 +01:00
if err != nil {
2018-01-17 07:40:08 +01:00
return
2017-03-09 19:01:17 +01:00
}
2017-12-12 20:27:43 +01:00
tmpDomain := routeConfig.Domain
tmpLocation := routeConfig.Location
2018-04-10 11:46:49 +02:00
addrs = append(addrs, util.CanonicalAddr(tmpDomain, int(g.GlbServerCfg.VhostHttpPort)))
2017-12-12 20:27:43 +01:00
pxy.closeFuncs = append(pxy.closeFuncs, func() {
pxy.ctl.svr.httpReverseProxy.UnRegister(tmpDomain, tmpLocation)
})
2017-03-09 19:01:17 +01:00
pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location)
}
}
if pxy.cfg.SubDomain != "" {
2018-04-10 11:46:49 +02:00
routeConfig.Domain = pxy.cfg.SubDomain + "." + g.GlbServerCfg.SubDomainHost
2017-03-09 19:01:17 +01:00
for _, location := range locations {
routeConfig.Location = location
2018-01-17 07:40:08 +01:00
err = pxy.ctl.svr.httpReverseProxy.Register(routeConfig)
2017-03-09 19:01:17 +01:00
if err != nil {
2018-01-17 07:40:08 +01:00
return
2017-03-09 19:01:17 +01:00
}
2017-12-12 20:27:43 +01:00
tmpDomain := routeConfig.Domain
tmpLocation := routeConfig.Location
2018-04-10 11:46:49 +02:00
addrs = append(addrs, util.CanonicalAddr(tmpDomain, g.GlbServerCfg.VhostHttpPort))
2017-12-12 20:27:43 +01:00
pxy.closeFuncs = append(pxy.closeFuncs, func() {
pxy.ctl.svr.httpReverseProxy.UnRegister(tmpDomain, tmpLocation)
})
2017-03-09 19:01:17 +01:00
pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location)
}
}
2018-01-17 07:40:08 +01:00
remoteAddr = strings.Join(addrs, ",")
2017-03-08 19:03:47 +01:00
return
}
func (pxy *HttpProxy) GetConf() config.ProxyConf {
return pxy.cfg
}
func (pxy *HttpProxy) GetRealConn() (workConn frpNet.Conn, err error) {
tmpConn, errRet := pxy.GetWorkConnFromPool()
if errRet != nil {
err = errRet
return
}
var rwc io.ReadWriteCloser = tmpConn
if pxy.cfg.UseEncryption {
2018-04-10 11:46:49 +02:00
rwc, err = frpIo.WithEncryption(rwc, []byte(g.GlbServerCfg.Token))
if err != nil {
pxy.Error("create encryption stream error: %v", err)
return
}
}
if pxy.cfg.UseCompression {
rwc = frpIo.WithCompression(rwc)
}
workConn = frpNet.WrapReadWriteCloserToConn(rwc, tmpConn)
workConn = frpNet.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn)
StatsOpenConnection(pxy.GetName())
return
}
func (pxy *HttpProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) {
name := pxy.GetName()
StatsCloseConnection(name)
StatsAddTrafficIn(name, totalWrite)
StatsAddTrafficOut(name, totalRead)
}
2017-03-08 19:03:47 +01:00
func (pxy *HttpProxy) Close() {
pxy.BaseProxy.Close()
2017-12-12 20:27:43 +01:00
for _, closeFn := range pxy.closeFuncs {
closeFn()
}
2017-03-08 19:03:47 +01:00
}
type HttpsProxy struct {
BaseProxy
cfg *config.HttpsProxyConf
}
2018-01-17 07:40:08 +01:00
func (pxy *HttpsProxy) Run() (remoteAddr string, err error) {
2017-03-09 19:01:17 +01:00
routeConfig := &vhost.VhostRouteConfig{}
2018-01-17 07:40:08 +01:00
addrs := make([]string, 0)
2017-03-09 19:01:17 +01:00
for _, domain := range pxy.cfg.CustomDomains {
routeConfig.Domain = domain
2018-01-17 07:40:08 +01:00
l, errRet := pxy.ctl.svr.VhostHttpsMuxer.Listen(routeConfig)
if errRet != nil {
err = errRet
return
2017-03-09 19:01:17 +01:00
}
2017-03-11 19:03:24 +01:00
l.AddLogPrefix(pxy.name)
2017-03-09 19:01:17 +01:00
pxy.Info("https proxy listen for host [%s]", routeConfig.Domain)
pxy.listeners = append(pxy.listeners, l)
2018-04-10 11:46:49 +02:00
addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, g.GlbServerCfg.VhostHttpsPort))
2017-03-09 19:01:17 +01:00
}
if pxy.cfg.SubDomain != "" {
2018-04-10 11:46:49 +02:00
routeConfig.Domain = pxy.cfg.SubDomain + "." + g.GlbServerCfg.SubDomainHost
2018-01-17 07:40:08 +01:00
l, errRet := pxy.ctl.svr.VhostHttpsMuxer.Listen(routeConfig)
if errRet != nil {
err = errRet
return
2017-03-09 19:01:17 +01:00
}
2017-03-11 19:03:24 +01:00
l.AddLogPrefix(pxy.name)
2017-03-09 19:01:17 +01:00
pxy.Info("https proxy listen for host [%s]", routeConfig.Domain)
pxy.listeners = append(pxy.listeners, l)
2018-04-10 11:46:49 +02:00
addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, int(g.GlbServerCfg.VhostHttpsPort)))
2017-03-09 19:01:17 +01:00
}
pxy.startListenHandler(pxy, HandleUserTcpConnection)
2018-01-17 07:40:08 +01:00
remoteAddr = strings.Join(addrs, ",")
2017-03-08 19:03:47 +01:00
return
}
func (pxy *HttpsProxy) GetConf() config.ProxyConf {
return pxy.cfg
}
func (pxy *HttpsProxy) Close() {
pxy.BaseProxy.Close()
}
2017-06-25 21:02:33 +02:00
type StcpProxy struct {
BaseProxy
cfg *config.StcpProxyConf
}
2018-01-17 07:40:08 +01:00
func (pxy *StcpProxy) Run() (remoteAddr string, err error) {
listener, errRet := pxy.ctl.svr.visitorManager.Listen(pxy.GetName(), pxy.cfg.Sk)
if errRet != nil {
err = errRet
return
2017-06-25 21:02:33 +02:00
}
listener.AddLogPrefix(pxy.name)
pxy.listeners = append(pxy.listeners, listener)
pxy.Info("stcp proxy custom listen success")
pxy.startListenHandler(pxy, HandleUserTcpConnection)
2018-01-17 07:40:08 +01:00
return
2017-06-25 21:02:33 +02:00
}
func (pxy *StcpProxy) GetConf() config.ProxyConf {
return pxy.cfg
}
func (pxy *StcpProxy) Close() {
pxy.BaseProxy.Close()
2017-12-04 18:34:33 +01:00
pxy.ctl.svr.visitorManager.CloseListener(pxy.GetName())
2017-06-25 21:02:33 +02:00
}
2017-10-24 12:20:07 +02:00
type XtcpProxy struct {
BaseProxy
cfg *config.XtcpProxyConf
closeCh chan struct{}
}
2018-01-17 07:40:08 +01:00
func (pxy *XtcpProxy) Run() (remoteAddr string, err error) {
2017-10-24 12:20:07 +02:00
if pxy.ctl.svr.natHoleController == nil {
pxy.Error("udp port for xtcp is not specified.")
2018-01-17 07:40:08 +01:00
err = fmt.Errorf("xtcp is not supported in frps")
return
2017-10-24 12:20:07 +02:00
}
sidCh := pxy.ctl.svr.natHoleController.ListenClient(pxy.GetName(), pxy.cfg.Sk)
go func() {
for {
select {
case <-pxy.closeCh:
break
case sid := <-sidCh:
2018-01-17 07:40:08 +01:00
workConn, errRet := pxy.GetWorkConnFromPool()
if errRet != nil {
2017-10-24 12:20:07 +02:00
continue
}
m := &msg.NatHoleSid{
Sid: sid,
}
2018-01-17 07:40:08 +01:00
errRet = msg.WriteMsg(workConn, m)
if errRet != nil {
pxy.Warn("write nat hole sid package error, %v", errRet)
2017-10-24 12:20:07 +02:00
}
}
}
}()
2018-01-17 07:40:08 +01:00
return
2017-10-24 12:20:07 +02:00
}
func (pxy *XtcpProxy) GetConf() config.ProxyConf {
return pxy.cfg
}
func (pxy *XtcpProxy) Close() {
pxy.BaseProxy.Close()
pxy.ctl.svr.natHoleController.CloseClient(pxy.GetName())
errors.PanicToError(func() {
close(pxy.closeCh)
})
}
2017-03-08 19:03:47 +01:00
type UdpProxy struct {
BaseProxy
cfg *config.UdpProxyConf
2017-03-12 19:44:47 +01:00
realPort int
2017-04-24 18:34:14 +02:00
// udpConn is the listener of udp packages
udpConn *net.UDPConn
// there are always only one workConn at the same time
// get another one if it closed
workConn net.Conn
// sendCh is used for sending packages to workConn
sendCh chan *msg.UdpPacket
// readCh is used for reading packages from workConn
readCh chan *msg.UdpPacket
// checkCloseCh is used for watching if workConn is closed
2017-03-12 19:44:47 +01:00
checkCloseCh chan int
2017-04-24 18:34:14 +02:00
isClosed bool
2017-03-08 19:03:47 +01:00
}
2018-01-17 07:40:08 +01:00
func (pxy *UdpProxy) Run() (remoteAddr string, err error) {
pxy.realPort, err = pxy.ctl.svr.udpPortManager.Acquire(pxy.name, pxy.cfg.RemotePort)
if err != nil {
return
}
defer func() {
if err != nil {
pxy.ctl.svr.udpPortManager.Release(pxy.realPort)
}
}()
remoteAddr = fmt.Sprintf(":%d", pxy.realPort)
pxy.cfg.RemotePort = pxy.realPort
2018-04-10 11:46:49 +02:00
addr, errRet := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", g.GlbServerCfg.ProxyBindAddr, pxy.realPort))
2018-01-17 07:40:08 +01:00
if errRet != nil {
err = errRet
return
2017-03-12 19:44:47 +01:00
}
2018-01-17 07:40:08 +01:00
udpConn, errRet := net.ListenUDP("udp", addr)
if errRet != nil {
err = errRet
2017-03-12 19:44:47 +01:00
pxy.Warn("listen udp port error: %v", err)
2018-01-17 07:40:08 +01:00
return
2017-03-12 19:44:47 +01:00
}
pxy.Info("udp proxy listen port [%d]", pxy.cfg.RemotePort)
pxy.udpConn = udpConn
pxy.sendCh = make(chan *msg.UdpPacket, 1024)
pxy.readCh = make(chan *msg.UdpPacket, 1024)
2017-03-12 19:44:47 +01:00
pxy.checkCloseCh = make(chan int)
2017-04-24 18:34:14 +02:00
// read message from workConn, if it returns any error, notify proxy to start a new workConn
2017-03-12 19:44:47 +01:00
workConnReaderFn := func(conn net.Conn) {
for {
var (
rawMsg msg.Message
errRet error
)
2017-04-24 18:34:14 +02:00
pxy.Trace("loop waiting message from udp workConn")
// client will send heartbeat in workConn for keeping alive
conn.SetReadDeadline(time.Now().Add(time.Duration(60) * time.Second))
if rawMsg, errRet = msg.ReadMsg(conn); errRet != nil {
2017-03-12 19:44:47 +01:00
pxy.Warn("read from workConn for udp error: %v", errRet)
conn.Close()
2017-03-26 19:39:05 +02:00
// notify proxy to start a new work connection
2017-04-24 18:34:14 +02:00
// ignore error here, it means the proxy is closed
2017-03-12 19:44:47 +01:00
errors.PanicToError(func() {
pxy.checkCloseCh <- 1
})
return
}
conn.SetReadDeadline(time.Time{})
switch m := rawMsg.(type) {
case *msg.Ping:
pxy.Trace("udp work conn get ping message")
continue
case *msg.UdpPacket:
if errRet := errors.PanicToError(func() {
pxy.Trace("get udp message from workConn: %s", m.Content)
pxy.readCh <- m
StatsAddTrafficOut(pxy.GetName(), int64(len(m.Content)))
}); errRet != nil {
conn.Close()
pxy.Info("reader goroutine for udp work connection closed")
return
}
2017-03-12 19:44:47 +01:00
}
}
}
2017-04-24 18:34:14 +02:00
// send message to workConn
2017-03-12 19:44:47 +01:00
workConnSenderFn := func(conn net.Conn, ctx context.Context) {
var errRet error
for {
select {
case udpMsg, ok := <-pxy.sendCh:
if !ok {
pxy.Info("sender goroutine for udp work connection closed")
2017-03-12 19:44:47 +01:00
return
}
if errRet = msg.WriteMsg(conn, udpMsg); errRet != nil {
pxy.Info("sender goroutine for udp work connection closed: %v", errRet)
2017-04-24 18:34:14 +02:00
conn.Close()
2017-03-12 19:44:47 +01:00
return
} else {
2017-04-24 18:34:14 +02:00
pxy.Trace("send message to udp workConn: %s", udpMsg.Content)
2017-03-26 19:39:05 +02:00
StatsAddTrafficIn(pxy.GetName(), int64(len(udpMsg.Content)))
2017-03-12 19:44:47 +01:00
continue
}
case <-ctx.Done():
pxy.Info("sender goroutine for udp work connection closed")
return
}
}
}
go func() {
2017-04-24 18:34:14 +02:00
// Sleep a while for waiting control send the NewProxyResp to client.
time.Sleep(500 * time.Millisecond)
2017-03-12 19:44:47 +01:00
for {
workConn, err := pxy.GetWorkConnFromPool()
if err != nil {
2017-04-24 18:34:14 +02:00
time.Sleep(1 * time.Second)
2017-03-12 19:44:47 +01:00
// check if proxy is closed
select {
case _, ok := <-pxy.checkCloseCh:
if !ok {
return
}
default:
}
continue
}
2017-04-24 18:34:14 +02:00
// close the old workConn and replac it with a new one
if pxy.workConn != nil {
pxy.workConn.Close()
}
2017-03-12 19:44:47 +01:00
pxy.workConn = workConn
ctx, cancel := context.WithCancel(context.Background())
go workConnReaderFn(workConn)
go workConnSenderFn(workConn, ctx)
_, ok := <-pxy.checkCloseCh
cancel()
if !ok {
return
}
}
}()
2017-04-24 18:34:14 +02:00
// Read from user connections and send wrapped udp message to sendCh (forwarded by workConn).
2017-03-12 19:44:47 +01:00
// Client will transfor udp message to local udp service and waiting for response for a while.
2017-04-24 18:34:14 +02:00
// Response will be wrapped to be forwarded by work connection to server.
// Close readCh and sendCh at the end.
go func() {
udp.ForwardUserConn(udpConn, pxy.readCh, pxy.sendCh)
pxy.Close()
}()
2018-01-17 07:40:08 +01:00
return remoteAddr, nil
2017-03-08 19:03:47 +01:00
}
func (pxy *UdpProxy) GetConf() config.ProxyConf {
return pxy.cfg
}
func (pxy *UdpProxy) Close() {
2017-04-24 18:34:14 +02:00
pxy.mu.Lock()
defer pxy.mu.Unlock()
if !pxy.isClosed {
pxy.isClosed = true
pxy.BaseProxy.Close()
if pxy.workConn != nil {
pxy.workConn.Close()
}
2017-04-24 18:34:14 +02:00
pxy.udpConn.Close()
// all channels only closed here
close(pxy.checkCloseCh)
close(pxy.readCh)
close(pxy.sendCh)
}
pxy.ctl.svr.udpPortManager.Release(pxy.realPort)
2017-03-08 19:03:47 +01:00
}
// HandleUserTcpConnection is used for incoming tcp user connections.
2017-03-09 18:42:06 +01:00
// It can be used for tcp, http, https type.
2017-03-12 19:44:47 +01:00
func HandleUserTcpConnection(pxy Proxy, userConn frpNet.Conn) {
2017-03-08 19:03:47 +01:00
defer userConn.Close()
2017-03-12 19:44:47 +01:00
// try all connections from the pool
workConn, err := pxy.GetWorkConnFromPool()
2017-03-08 19:03:47 +01:00
if err != nil {
return
}
2017-03-12 19:44:47 +01:00
defer workConn.Close()
2017-03-08 19:03:47 +01:00
2017-03-09 19:01:17 +01:00
var local io.ReadWriteCloser = workConn
2017-03-08 19:03:47 +01:00
cfg := pxy.GetConf().GetBaseInfo()
if cfg.UseEncryption {
2018-04-10 11:46:49 +02:00
local, err = frpIo.WithEncryption(local, []byte(g.GlbServerCfg.Token))
2017-03-08 19:03:47 +01:00
if err != nil {
pxy.Error("create encryption stream error: %v", err)
return
}
}
if cfg.UseCompression {
2017-06-06 12:48:40 +02:00
local = frpIo.WithCompression(local)
2017-03-08 19:03:47 +01:00
}
2017-03-09 19:01:17 +01:00
pxy.Debug("join connections, workConn(l[%s] r[%s]) userConn(l[%s] r[%s])", workConn.LocalAddr().String(),
workConn.RemoteAddr().String(), userConn.LocalAddr().String(), userConn.RemoteAddr().String())
2017-03-22 19:01:25 +01:00
StatsOpenConnection(pxy.GetName())
2017-06-06 12:48:40 +02:00
inCount, outCount := frpIo.Join(local, userConn)
2017-03-22 19:01:25 +01:00
StatsCloseConnection(pxy.GetName())
2017-03-26 19:39:05 +02:00
StatsAddTrafficIn(pxy.GetName(), inCount)
StatsAddTrafficOut(pxy.GetName(), outCount)
2017-03-09 19:01:17 +01:00
pxy.Debug("join connections closed")
2017-03-08 19:03:47 +01:00
}