2017-03-09 02:03:47 +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.
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2017-10-24 22:53:20 +08:00
|
|
|
"bytes"
|
2017-03-09 02:03:47 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-03-13 02:44:47 +08:00
|
|
|
"net"
|
2017-04-25 00:34:14 +08:00
|
|
|
"sync"
|
2017-05-15 00:08:21 +08:00
|
|
|
"time"
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
"github.com/fatedier/frp/g"
|
2017-03-09 02:03:47 +08:00
|
|
|
"github.com/fatedier/frp/models/config"
|
2017-03-13 02:44:47 +08:00
|
|
|
"github.com/fatedier/frp/models/msg"
|
2017-05-21 22:42:42 +08:00
|
|
|
"github.com/fatedier/frp/models/plugin"
|
2017-03-13 02:44:47 +08:00
|
|
|
"github.com/fatedier/frp/models/proto/udp"
|
|
|
|
"github.com/fatedier/frp/utils/log"
|
|
|
|
frpNet "github.com/fatedier/frp/utils/net"
|
2018-05-08 02:13:30 +08:00
|
|
|
|
|
|
|
"github.com/fatedier/golib/errors"
|
2018-05-08 23:42:04 +08:00
|
|
|
frpIo "github.com/fatedier/golib/io"
|
2018-05-08 02:13:30 +08:00
|
|
|
"github.com/fatedier/golib/pool"
|
2017-03-09 02:03:47 +08:00
|
|
|
)
|
|
|
|
|
2018-11-06 18:35:05 +08:00
|
|
|
// Proxy defines how to handle work connections for different proxy type.
|
2017-03-09 02:03:47 +08:00
|
|
|
type Proxy interface {
|
2017-03-12 02:03:24 +08:00
|
|
|
Run() error
|
2017-03-10 01:42:06 +08:00
|
|
|
|
|
|
|
// InWorkConn accept work connections registered to server.
|
2017-03-13 02:44:47 +08:00
|
|
|
InWorkConn(conn frpNet.Conn)
|
2018-01-17 01:09:33 +08:00
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
Close()
|
2017-03-13 02:44:47 +08:00
|
|
|
log.Logger
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-01-17 01:09:33 +08:00
|
|
|
func NewProxy(pxyConf config.ProxyConf) (pxy Proxy) {
|
2017-03-13 02:44:47 +08:00
|
|
|
baseProxy := BaseProxy{
|
2018-04-10 17:46:49 +08:00
|
|
|
Logger: log.NewPrefixLogger(pxyConf.GetBaseInfo().ProxyName),
|
2017-03-13 02:44:47 +08:00
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
switch cfg := pxyConf.(type) {
|
|
|
|
case *config.TcpProxyConf:
|
|
|
|
pxy = &TcpProxy{
|
2017-03-13 02:44:47 +08:00
|
|
|
BaseProxy: baseProxy,
|
|
|
|
cfg: cfg,
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
case *config.UdpProxyConf:
|
|
|
|
pxy = &UdpProxy{
|
2017-03-13 02:44:47 +08:00
|
|
|
BaseProxy: baseProxy,
|
|
|
|
cfg: cfg,
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
case *config.HttpProxyConf:
|
|
|
|
pxy = &HttpProxy{
|
2017-03-13 02:44:47 +08:00
|
|
|
BaseProxy: baseProxy,
|
|
|
|
cfg: cfg,
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
case *config.HttpsProxyConf:
|
|
|
|
pxy = &HttpsProxy{
|
2017-03-13 02:44:47 +08:00
|
|
|
BaseProxy: baseProxy,
|
|
|
|
cfg: cfg,
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
2017-06-26 03:02:33 +08:00
|
|
|
case *config.StcpProxyConf:
|
|
|
|
pxy = &StcpProxy{
|
|
|
|
BaseProxy: baseProxy,
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
2017-10-24 18:20:07 +08:00
|
|
|
case *config.XtcpProxyConf:
|
|
|
|
pxy = &XtcpProxy{
|
|
|
|
BaseProxy: baseProxy,
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-13 02:44:47 +08:00
|
|
|
type BaseProxy struct {
|
2017-04-25 00:34:14 +08:00
|
|
|
closed bool
|
|
|
|
mu sync.RWMutex
|
2017-03-13 02:44:47 +08:00
|
|
|
log.Logger
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
// TCP
|
|
|
|
type TcpProxy struct {
|
2017-03-13 02:44:47 +08:00
|
|
|
BaseProxy
|
|
|
|
|
2017-05-21 22:42:42 +08:00
|
|
|
cfg *config.TcpProxyConf
|
|
|
|
proxyPlugin plugin.Plugin
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2017-03-12 02:03:24 +08:00
|
|
|
func (pxy *TcpProxy) Run() (err error) {
|
2017-05-21 22:42:42 +08:00
|
|
|
if pxy.cfg.Plugin != "" {
|
|
|
|
pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-03-12 02:03:24 +08:00
|
|
|
return
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *TcpProxy) Close() {
|
2017-05-21 22:42:42 +08:00
|
|
|
if pxy.proxyPlugin != nil {
|
|
|
|
pxy.proxyPlugin.Close()
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2017-03-13 02:44:47 +08:00
|
|
|
func (pxy *TcpProxy) InWorkConn(conn frpNet.Conn) {
|
2017-10-25 02:49:56 +08:00
|
|
|
HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, pxy.proxyPlugin, &pxy.cfg.BaseProxyConf, conn,
|
2018-04-10 17:46:49 +08:00
|
|
|
[]byte(g.GlbClientCfg.Token))
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP
|
|
|
|
type HttpProxy struct {
|
2017-03-13 02:44:47 +08:00
|
|
|
BaseProxy
|
|
|
|
|
2017-05-21 22:42:42 +08:00
|
|
|
cfg *config.HttpProxyConf
|
|
|
|
proxyPlugin plugin.Plugin
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2017-03-12 02:03:24 +08:00
|
|
|
func (pxy *HttpProxy) Run() (err error) {
|
2017-05-21 22:42:42 +08:00
|
|
|
if pxy.cfg.Plugin != "" {
|
|
|
|
pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-03-12 02:03:24 +08:00
|
|
|
return
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *HttpProxy) Close() {
|
2017-05-21 22:42:42 +08:00
|
|
|
if pxy.proxyPlugin != nil {
|
|
|
|
pxy.proxyPlugin.Close()
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2017-03-13 02:44:47 +08:00
|
|
|
func (pxy *HttpProxy) InWorkConn(conn frpNet.Conn) {
|
2017-10-25 02:49:56 +08:00
|
|
|
HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, pxy.proxyPlugin, &pxy.cfg.BaseProxyConf, conn,
|
2018-04-10 17:46:49 +08:00
|
|
|
[]byte(g.GlbClientCfg.Token))
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPS
|
|
|
|
type HttpsProxy struct {
|
2017-03-13 02:44:47 +08:00
|
|
|
BaseProxy
|
|
|
|
|
2017-05-21 22:42:42 +08:00
|
|
|
cfg *config.HttpsProxyConf
|
|
|
|
proxyPlugin plugin.Plugin
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2017-03-12 02:03:24 +08:00
|
|
|
func (pxy *HttpsProxy) Run() (err error) {
|
2017-05-21 22:42:42 +08:00
|
|
|
if pxy.cfg.Plugin != "" {
|
|
|
|
pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-03-12 02:03:24 +08:00
|
|
|
return
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *HttpsProxy) Close() {
|
2017-05-21 22:42:42 +08:00
|
|
|
if pxy.proxyPlugin != nil {
|
|
|
|
pxy.proxyPlugin.Close()
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2017-03-13 02:44:47 +08:00
|
|
|
func (pxy *HttpsProxy) InWorkConn(conn frpNet.Conn) {
|
2017-10-25 02:49:56 +08:00
|
|
|
HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, pxy.proxyPlugin, &pxy.cfg.BaseProxyConf, conn,
|
2018-04-10 17:46:49 +08:00
|
|
|
[]byte(g.GlbClientCfg.Token))
|
2017-03-10 02:01:17 +08:00
|
|
|
}
|
|
|
|
|
2017-06-26 03:02:33 +08:00
|
|
|
// STCP
|
|
|
|
type StcpProxy struct {
|
|
|
|
BaseProxy
|
|
|
|
|
|
|
|
cfg *config.StcpProxyConf
|
|
|
|
proxyPlugin plugin.Plugin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *StcpProxy) Run() (err error) {
|
|
|
|
if pxy.cfg.Plugin != "" {
|
|
|
|
pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *StcpProxy) Close() {
|
|
|
|
if pxy.proxyPlugin != nil {
|
|
|
|
pxy.proxyPlugin.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *StcpProxy) InWorkConn(conn frpNet.Conn) {
|
2017-10-25 02:49:56 +08:00
|
|
|
HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, pxy.proxyPlugin, &pxy.cfg.BaseProxyConf, conn,
|
2018-04-10 17:46:49 +08:00
|
|
|
[]byte(g.GlbClientCfg.Token))
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|
|
|
|
|
2017-10-24 18:20:07 +08:00
|
|
|
// XTCP
|
|
|
|
type XtcpProxy struct {
|
|
|
|
BaseProxy
|
|
|
|
|
|
|
|
cfg *config.XtcpProxyConf
|
|
|
|
proxyPlugin plugin.Plugin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *XtcpProxy) Run() (err error) {
|
|
|
|
if pxy.cfg.Plugin != "" {
|
|
|
|
pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *XtcpProxy) Close() {
|
|
|
|
if pxy.proxyPlugin != nil {
|
|
|
|
pxy.proxyPlugin.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *XtcpProxy) InWorkConn(conn frpNet.Conn) {
|
|
|
|
defer conn.Close()
|
|
|
|
var natHoleSidMsg msg.NatHoleSid
|
|
|
|
err := msg.ReadMsgInto(conn, &natHoleSidMsg)
|
|
|
|
if err != nil {
|
|
|
|
pxy.Error("xtcp read from workConn error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
natHoleClientMsg := &msg.NatHoleClient{
|
|
|
|
ProxyName: pxy.cfg.ProxyName,
|
|
|
|
Sid: natHoleSidMsg.Sid,
|
|
|
|
}
|
|
|
|
raddr, _ := net.ResolveUDPAddr("udp",
|
2018-04-10 17:46:49 +08:00
|
|
|
fmt.Sprintf("%s:%d", g.GlbClientCfg.ServerAddr, g.GlbClientCfg.ServerUdpPort))
|
2017-10-24 18:20:07 +08:00
|
|
|
clientConn, err := net.DialUDP("udp", nil, raddr)
|
|
|
|
defer clientConn.Close()
|
|
|
|
|
|
|
|
err = msg.WriteMsg(clientConn, natHoleClientMsg)
|
|
|
|
if err != nil {
|
|
|
|
pxy.Error("send natHoleClientMsg to server error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-24 22:53:20 +08:00
|
|
|
// Wait for client address at most 5 seconds.
|
2017-10-24 18:20:07 +08:00
|
|
|
var natHoleRespMsg msg.NatHoleResp
|
2017-10-24 22:53:20 +08:00
|
|
|
clientConn.SetReadDeadline(time.Now().Add(5 * time.Second))
|
|
|
|
|
|
|
|
buf := pool.GetBuf(1024)
|
|
|
|
n, err := clientConn.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
pxy.Error("get natHoleRespMsg error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = msg.ReadMsgInto(bytes.NewReader(buf[:n]), &natHoleRespMsg)
|
2017-10-24 18:20:07 +08:00
|
|
|
if err != nil {
|
|
|
|
pxy.Error("get natHoleRespMsg error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
clientConn.SetReadDeadline(time.Time{})
|
|
|
|
clientConn.Close()
|
2018-05-17 00:07:56 +08:00
|
|
|
|
|
|
|
if natHoleRespMsg.Error != "" {
|
|
|
|
pxy.Error("natHoleRespMsg get error info: %s", natHoleRespMsg.Error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-24 22:53:20 +08:00
|
|
|
pxy.Trace("get natHoleRespMsg, sid [%s], client address [%s]", natHoleRespMsg.Sid, natHoleRespMsg.ClientAddr)
|
2017-10-24 18:20:07 +08:00
|
|
|
|
2017-12-05 01:34:33 +08:00
|
|
|
// Send sid to visitor udp address.
|
2017-10-24 18:20:07 +08:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
laddr, _ := net.ResolveUDPAddr("udp", clientConn.LocalAddr().String())
|
2017-12-05 01:34:33 +08:00
|
|
|
daddr, err := net.ResolveUDPAddr("udp", natHoleRespMsg.VisitorAddr)
|
2017-10-24 18:20:07 +08:00
|
|
|
if err != nil {
|
2017-12-05 01:34:33 +08:00
|
|
|
pxy.Error("resolve visitor udp address error: %v", err)
|
2017-10-24 18:20:07 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lConn, err := net.DialUDP("udp", laddr, daddr)
|
|
|
|
if err != nil {
|
2017-12-05 01:34:33 +08:00
|
|
|
pxy.Error("dial visitor udp address error: %v", err)
|
2017-10-24 18:20:07 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
lConn.Write([]byte(natHoleRespMsg.Sid))
|
|
|
|
|
2017-12-05 01:34:33 +08:00
|
|
|
kcpConn, err := frpNet.NewKcpConnFromUdp(lConn, true, natHoleRespMsg.VisitorAddr)
|
2017-10-24 18:20:07 +08:00
|
|
|
if err != nil {
|
|
|
|
pxy.Error("create kcp connection from udp connection error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-25 02:49:56 +08:00
|
|
|
HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, pxy.proxyPlugin, &pxy.cfg.BaseProxyConf,
|
|
|
|
frpNet.WrapConn(kcpConn), []byte(pxy.cfg.Sk))
|
2017-10-24 18:20:07 +08:00
|
|
|
}
|
|
|
|
|
2017-03-10 02:01:17 +08:00
|
|
|
// UDP
|
|
|
|
type UdpProxy struct {
|
2017-03-13 02:44:47 +08:00
|
|
|
BaseProxy
|
|
|
|
|
2017-03-10 02:01:17 +08:00
|
|
|
cfg *config.UdpProxyConf
|
2017-03-13 02:44:47 +08:00
|
|
|
|
|
|
|
localAddr *net.UDPAddr
|
|
|
|
readCh chan *msg.UdpPacket
|
2017-05-15 00:08:21 +08:00
|
|
|
|
|
|
|
// include msg.UdpPacket and msg.Ping
|
|
|
|
sendCh chan msg.Message
|
|
|
|
workConn frpNet.Conn
|
2017-03-10 02:01:17 +08:00
|
|
|
}
|
|
|
|
|
2017-03-12 02:03:24 +08:00
|
|
|
func (pxy *UdpProxy) Run() (err error) {
|
2017-03-13 02:44:47 +08:00
|
|
|
pxy.localAddr, err = net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", pxy.cfg.LocalIp, pxy.cfg.LocalPort))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-03-12 02:03:24 +08:00
|
|
|
return
|
2017-03-10 02:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *UdpProxy) Close() {
|
2017-04-25 00:34:14 +08:00
|
|
|
pxy.mu.Lock()
|
|
|
|
defer pxy.mu.Unlock()
|
|
|
|
|
|
|
|
if !pxy.closed {
|
|
|
|
pxy.closed = true
|
|
|
|
if pxy.workConn != nil {
|
|
|
|
pxy.workConn.Close()
|
|
|
|
}
|
|
|
|
if pxy.readCh != nil {
|
|
|
|
close(pxy.readCh)
|
|
|
|
}
|
|
|
|
if pxy.sendCh != nil {
|
|
|
|
close(pxy.sendCh)
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 02:01:17 +08:00
|
|
|
}
|
|
|
|
|
2017-03-13 02:44:47 +08:00
|
|
|
func (pxy *UdpProxy) InWorkConn(conn frpNet.Conn) {
|
2017-05-15 00:08:21 +08:00
|
|
|
pxy.Info("incoming a new work connection for udp proxy, %s", conn.RemoteAddr().String())
|
2017-04-25 00:34:14 +08:00
|
|
|
// close resources releated with old workConn
|
|
|
|
pxy.Close()
|
|
|
|
|
|
|
|
pxy.mu.Lock()
|
2017-03-13 02:44:47 +08:00
|
|
|
pxy.workConn = conn
|
2017-05-15 00:08:21 +08:00
|
|
|
pxy.readCh = make(chan *msg.UdpPacket, 1024)
|
|
|
|
pxy.sendCh = make(chan msg.Message, 1024)
|
2017-04-25 00:34:14 +08:00
|
|
|
pxy.closed = false
|
|
|
|
pxy.mu.Unlock()
|
2017-03-13 02:44:47 +08:00
|
|
|
|
2017-05-15 00:08:21 +08:00
|
|
|
workConnReaderFn := func(conn net.Conn, readCh chan *msg.UdpPacket) {
|
2017-03-13 02:44:47 +08:00
|
|
|
for {
|
|
|
|
var udpMsg msg.UdpPacket
|
|
|
|
if errRet := msg.ReadMsgInto(conn, &udpMsg); errRet != nil {
|
|
|
|
pxy.Warn("read from workConn for udp error: %v", errRet)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if errRet := errors.PanicToError(func() {
|
2017-04-25 00:34:14 +08:00
|
|
|
pxy.Trace("get udp package from workConn: %s", udpMsg.Content)
|
2017-05-15 00:08:21 +08:00
|
|
|
readCh <- &udpMsg
|
2017-03-13 02:44:47 +08:00
|
|
|
}); errRet != nil {
|
2017-04-25 00:34:14 +08:00
|
|
|
pxy.Info("reader goroutine for udp work connection closed: %v", errRet)
|
2017-03-13 02:44:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 00:08:21 +08:00
|
|
|
workConnSenderFn := func(conn net.Conn, sendCh chan msg.Message) {
|
|
|
|
defer func() {
|
|
|
|
pxy.Info("writer goroutine for udp work connection closed")
|
|
|
|
}()
|
2017-03-13 02:44:47 +08:00
|
|
|
var errRet error
|
2017-05-15 00:08:21 +08:00
|
|
|
for rawMsg := range sendCh {
|
|
|
|
switch m := rawMsg.(type) {
|
|
|
|
case *msg.UdpPacket:
|
|
|
|
pxy.Trace("send udp package to workConn: %s", m.Content)
|
|
|
|
case *msg.Ping:
|
|
|
|
pxy.Trace("send ping message to udp workConn")
|
|
|
|
}
|
|
|
|
if errRet = msg.WriteMsg(conn, rawMsg); errRet != nil {
|
|
|
|
pxy.Error("udp work write error: %v", errRet)
|
2017-03-13 02:44:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 00:08:21 +08:00
|
|
|
heartbeatFn := func(conn net.Conn, sendCh chan msg.Message) {
|
|
|
|
var errRet error
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Duration(30) * time.Second)
|
|
|
|
if errRet = errors.PanicToError(func() {
|
|
|
|
sendCh <- &msg.Ping{}
|
|
|
|
}); errRet != nil {
|
|
|
|
pxy.Trace("heartbeat goroutine for udp work connection closed")
|
2017-05-17 16:02:31 +08:00
|
|
|
break
|
2017-05-15 00:08:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-13 02:44:47 +08:00
|
|
|
|
2017-05-15 00:08:21 +08:00
|
|
|
go workConnSenderFn(pxy.workConn, pxy.sendCh)
|
|
|
|
go workConnReaderFn(pxy.workConn, pxy.readCh)
|
|
|
|
go heartbeatFn(pxy.workConn, pxy.sendCh)
|
2017-03-13 02:44:47 +08:00
|
|
|
udp.Forwarder(pxy.localAddr, pxy.readCh, pxy.sendCh)
|
2017-03-10 02:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Common handler for tcp work connections.
|
2017-05-21 22:42:42 +08:00
|
|
|
func HandleTcpWorkConnection(localInfo *config.LocalSvrConf, proxyPlugin plugin.Plugin,
|
2017-10-25 02:49:56 +08:00
|
|
|
baseInfo *config.BaseProxyConf, workConn frpNet.Conn, encKey []byte) {
|
2017-03-10 02:01:17 +08:00
|
|
|
|
2017-05-21 22:42:42 +08:00
|
|
|
var (
|
|
|
|
remote io.ReadWriteCloser
|
|
|
|
err error
|
|
|
|
)
|
2017-03-10 02:01:17 +08:00
|
|
|
remote = workConn
|
2018-01-23 16:31:59 +08:00
|
|
|
|
2017-03-10 02:01:17 +08:00
|
|
|
if baseInfo.UseEncryption {
|
2017-10-25 02:49:56 +08:00
|
|
|
remote, err = frpIo.WithEncryption(remote, encKey)
|
2017-03-10 02:01:17 +08:00
|
|
|
if err != nil {
|
2018-01-23 16:31:59 +08:00
|
|
|
workConn.Close()
|
2017-03-10 02:01:17 +08:00
|
|
|
workConn.Error("create encryption stream error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if baseInfo.UseCompression {
|
2017-06-06 18:48:40 +08:00
|
|
|
remote = frpIo.WithCompression(remote)
|
2017-03-10 02:01:17 +08:00
|
|
|
}
|
2017-05-21 22:42:42 +08:00
|
|
|
|
|
|
|
if proxyPlugin != nil {
|
|
|
|
// if plugin is set, let plugin handle connections first
|
|
|
|
workConn.Debug("handle by plugin: %s", proxyPlugin.Name())
|
2018-01-18 00:45:11 +08:00
|
|
|
proxyPlugin.Handle(remote, workConn)
|
2017-05-21 22:42:42 +08:00
|
|
|
workConn.Debug("handle by plugin finished")
|
|
|
|
return
|
|
|
|
} else {
|
2017-06-04 19:56:21 +08:00
|
|
|
localConn, err := frpNet.ConnectServer("tcp", fmt.Sprintf("%s:%d", localInfo.LocalIp, localInfo.LocalPort))
|
2017-05-21 22:42:42 +08:00
|
|
|
if err != nil {
|
2018-01-23 16:31:59 +08:00
|
|
|
workConn.Close()
|
2017-05-21 22:42:42 +08:00
|
|
|
workConn.Error("connect to local service [%s:%d] error: %v", localInfo.LocalIp, localInfo.LocalPort, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
workConn.Debug("join connections, localConn(l[%s] r[%s]) workConn(l[%s] r[%s])", localConn.LocalAddr().String(),
|
|
|
|
localConn.RemoteAddr().String(), workConn.LocalAddr().String(), workConn.RemoteAddr().String())
|
2017-06-06 18:48:40 +08:00
|
|
|
frpIo.Join(localConn, remote)
|
2017-05-21 22:42:42 +08:00
|
|
|
workConn.Debug("join connections closed")
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|