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 (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-03-13 02:44:47 +08:00
|
|
|
"net"
|
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-03-09 02:03:47 +08:00
|
|
|
"github.com/fatedier/frp/models/proto/tcp"
|
2017-03-13 02:44:47 +08:00
|
|
|
"github.com/fatedier/frp/models/proto/udp"
|
|
|
|
"github.com/fatedier/frp/utils/errors"
|
|
|
|
"github.com/fatedier/frp/utils/log"
|
|
|
|
frpNet "github.com/fatedier/frp/utils/net"
|
2017-03-09 02:03:47 +08:00
|
|
|
)
|
|
|
|
|
2017-03-10 01:42:06 +08:00
|
|
|
// Proxy defines how to work 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)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func NewProxy(ctl *Control, pxyConf config.ProxyConf) (pxy Proxy) {
|
2017-03-13 02:44:47 +08:00
|
|
|
baseProxy := BaseProxy{
|
|
|
|
ctl: ctl,
|
|
|
|
Logger: log.NewPrefixLogger(pxyConf.GetName()),
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-13 02:44:47 +08:00
|
|
|
type BaseProxy struct {
|
|
|
|
ctl *Control
|
|
|
|
log.Logger
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
// TCP
|
|
|
|
type TcpProxy struct {
|
2017-03-13 02:44:47 +08:00
|
|
|
BaseProxy
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg *config.TcpProxyConf
|
|
|
|
}
|
|
|
|
|
2017-03-12 02:03:24 +08:00
|
|
|
func (pxy *TcpProxy) Run() (err error) {
|
|
|
|
return
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *TcpProxy) Close() {
|
|
|
|
}
|
|
|
|
|
2017-03-13 02:44:47 +08:00
|
|
|
func (pxy *TcpProxy) InWorkConn(conn frpNet.Conn) {
|
2017-03-09 02:03:47 +08:00
|
|
|
defer conn.Close()
|
2017-03-10 02:01:17 +08:00
|
|
|
HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, &pxy.cfg.BaseProxyConf, conn)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP
|
|
|
|
type HttpProxy struct {
|
2017-03-13 02:44:47 +08:00
|
|
|
BaseProxy
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg *config.HttpProxyConf
|
|
|
|
}
|
|
|
|
|
2017-03-12 02:03:24 +08:00
|
|
|
func (pxy *HttpProxy) Run() (err error) {
|
|
|
|
return
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *HttpProxy) Close() {
|
|
|
|
}
|
|
|
|
|
2017-03-13 02:44:47 +08:00
|
|
|
func (pxy *HttpProxy) InWorkConn(conn frpNet.Conn) {
|
2017-03-09 02:03:47 +08:00
|
|
|
defer conn.Close()
|
2017-03-10 02:01:17 +08:00
|
|
|
HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, &pxy.cfg.BaseProxyConf, conn)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPS
|
|
|
|
type HttpsProxy struct {
|
2017-03-13 02:44:47 +08:00
|
|
|
BaseProxy
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg *config.HttpsProxyConf
|
|
|
|
}
|
|
|
|
|
2017-03-12 02:03:24 +08:00
|
|
|
func (pxy *HttpsProxy) Run() (err error) {
|
|
|
|
return
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *HttpsProxy) Close() {
|
|
|
|
}
|
|
|
|
|
2017-03-13 02:44:47 +08:00
|
|
|
func (pxy *HttpsProxy) InWorkConn(conn frpNet.Conn) {
|
2017-03-09 02:03:47 +08:00
|
|
|
defer conn.Close()
|
2017-03-10 02:01:17 +08:00
|
|
|
HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, &pxy.cfg.BaseProxyConf, conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
sendCh chan *msg.UdpPacket
|
|
|
|
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-03-13 02:44:47 +08:00
|
|
|
pxy.workConn.Close()
|
|
|
|
close(pxy.readCh)
|
|
|
|
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) {
|
|
|
|
if pxy.workConn != nil {
|
|
|
|
pxy.workConn.Close()
|
|
|
|
close(pxy.readCh)
|
|
|
|
close(pxy.sendCh)
|
|
|
|
}
|
|
|
|
pxy.workConn = conn
|
|
|
|
pxy.readCh = make(chan *msg.UdpPacket, 64)
|
|
|
|
pxy.sendCh = make(chan *msg.UdpPacket, 64)
|
|
|
|
|
|
|
|
workConnReaderFn := func(conn net.Conn) {
|
|
|
|
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() {
|
|
|
|
pxy.readCh <- &udpMsg
|
|
|
|
}); errRet != nil {
|
|
|
|
pxy.Info("reader goroutine for udp work connection closed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
workConnSenderFn := func(conn net.Conn) {
|
|
|
|
var errRet error
|
|
|
|
for udpMsg := range pxy.sendCh {
|
|
|
|
if errRet = msg.WriteMsg(conn, udpMsg); errRet != nil {
|
|
|
|
pxy.Info("sender goroutine for udp work connection closed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go workConnSenderFn(pxy.workConn)
|
|
|
|
go workConnReaderFn(pxy.workConn)
|
|
|
|
udp.Forwarder(pxy.localAddr, pxy.readCh, pxy.sendCh)
|
2017-03-10 02:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Common handler for tcp work connections.
|
2017-03-13 02:44:47 +08:00
|
|
|
func HandleTcpWorkConnection(localInfo *config.LocalSvrConf, baseInfo *config.BaseProxyConf, workConn frpNet.Conn) {
|
|
|
|
localConn, err := frpNet.ConnectTcpServer(fmt.Sprintf("%s:%d", localInfo.LocalIp, localInfo.LocalPort))
|
2017-03-10 02:01:17 +08:00
|
|
|
if err != nil {
|
|
|
|
workConn.Error("connect to local service [%s:%d] error: %v", localInfo.LocalIp, localInfo.LocalPort, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var remote io.ReadWriteCloser
|
|
|
|
remote = workConn
|
|
|
|
if baseInfo.UseEncryption {
|
|
|
|
remote, err = tcp.WithEncryption(remote, []byte(config.ClientCommonCfg.PrivilegeToken))
|
|
|
|
if err != nil {
|
|
|
|
workConn.Error("create encryption stream error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if baseInfo.UseCompression {
|
|
|
|
remote = tcp.WithCompression(remote)
|
|
|
|
}
|
|
|
|
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())
|
|
|
|
tcp.Join(localConn, remote)
|
|
|
|
workConn.Debug("join connections closed")
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|