2023-05-28 16:50:43 +08:00
|
|
|
// Copyright 2023 The frp Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2023-11-21 11:19:35 +08:00
|
|
|
//go:build !frps
|
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
2023-05-30 22:18:56 +08:00
|
|
|
"reflect"
|
2023-05-28 16:50:43 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
fmux "github.com/hashicorp/yamux"
|
|
|
|
"github.com/quic-go/quic-go"
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2023-05-28 16:50:43 +08:00
|
|
|
"github.com/fatedier/frp/pkg/msg"
|
|
|
|
"github.com/fatedier/frp/pkg/nathole"
|
|
|
|
"github.com/fatedier/frp/pkg/transport"
|
2023-11-27 15:47:49 +08:00
|
|
|
netpkg "github.com/fatedier/frp/pkg/util/net"
|
2023-05-28 16:50:43 +08:00
|
|
|
)
|
|
|
|
|
2023-05-30 22:18:56 +08:00
|
|
|
func init() {
|
2023-09-06 10:18:02 +08:00
|
|
|
RegisterProxyFactory(reflect.TypeOf(&v1.XTCPProxyConfig{}), NewXTCPProxy)
|
2023-05-30 22:18:56 +08:00
|
|
|
}
|
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
type XTCPProxy struct {
|
|
|
|
*BaseProxy
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
cfg *v1.XTCPProxyConfig
|
2023-05-28 16:50:43 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
func NewXTCPProxy(baseProxy *BaseProxy, cfg v1.ProxyConfigurer) Proxy {
|
|
|
|
unwrapped, ok := cfg.(*v1.XTCPProxyConfig)
|
2023-05-30 22:18:56 +08:00
|
|
|
if !ok {
|
|
|
|
return nil
|
2023-05-28 16:50:43 +08:00
|
|
|
}
|
2023-05-30 22:18:56 +08:00
|
|
|
return &XTCPProxy{
|
|
|
|
BaseProxy: baseProxy,
|
|
|
|
cfg: unwrapped,
|
2023-05-28 16:50:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *XTCPProxy) InWorkConn(conn net.Conn, startWorkConnMsg *msg.StartWorkConn) {
|
|
|
|
xl := pxy.xl
|
|
|
|
defer conn.Close()
|
|
|
|
var natHoleSidMsg msg.NatHoleSid
|
|
|
|
err := msg.ReadMsgInto(conn, &natHoleSidMsg)
|
|
|
|
if err != nil {
|
|
|
|
xl.Error("xtcp read from workConn error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-26 00:10:27 +08:00
|
|
|
xl.Trace("nathole prepare start")
|
2023-05-28 16:50:43 +08:00
|
|
|
prepareResult, err := nathole.Prepare([]string{pxy.clientCfg.NatHoleSTUNServer})
|
|
|
|
if err != nil {
|
|
|
|
xl.Warn("nathole prepare error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
xl.Info("nathole prepare success, nat type: %s, behavior: %s, addresses: %v, assistedAddresses: %v",
|
|
|
|
prepareResult.NatType, prepareResult.Behavior, prepareResult.Addrs, prepareResult.AssistedAddrs)
|
|
|
|
defer prepareResult.ListenConn.Close()
|
|
|
|
|
|
|
|
// send NatHoleClient msg to server
|
|
|
|
transactionID := nathole.NewTransactionID()
|
|
|
|
natHoleClientMsg := &msg.NatHoleClient{
|
|
|
|
TransactionID: transactionID,
|
2023-09-06 10:18:02 +08:00
|
|
|
ProxyName: pxy.cfg.Name,
|
2023-05-28 16:50:43 +08:00
|
|
|
Sid: natHoleSidMsg.Sid,
|
|
|
|
MappedAddrs: prepareResult.Addrs,
|
|
|
|
AssistedAddrs: prepareResult.AssistedAddrs,
|
|
|
|
}
|
|
|
|
|
2023-06-26 00:10:27 +08:00
|
|
|
xl.Trace("nathole exchange info start")
|
2023-05-28 16:50:43 +08:00
|
|
|
natHoleRespMsg, err := nathole.ExchangeInfo(pxy.ctx, pxy.msgTransporter, transactionID, natHoleClientMsg, 5*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
xl.Warn("nathole exchange info error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
xl.Info("get natHoleRespMsg, sid [%s], protocol [%s], candidate address %v, assisted address %v, detectBehavior: %+v",
|
|
|
|
natHoleRespMsg.Sid, natHoleRespMsg.Protocol, natHoleRespMsg.CandidateAddrs,
|
|
|
|
natHoleRespMsg.AssistedAddrs, natHoleRespMsg.DetectBehavior)
|
|
|
|
|
|
|
|
listenConn := prepareResult.ListenConn
|
2023-09-06 10:18:02 +08:00
|
|
|
newListenConn, raddr, err := nathole.MakeHole(pxy.ctx, listenConn, natHoleRespMsg, []byte(pxy.cfg.Secretkey))
|
2023-05-28 16:50:43 +08:00
|
|
|
if err != nil {
|
|
|
|
listenConn.Close()
|
|
|
|
xl.Warn("make hole error: %v", err)
|
|
|
|
_ = pxy.msgTransporter.Send(&msg.NatHoleReport{
|
|
|
|
Sid: natHoleRespMsg.Sid,
|
|
|
|
Success: false,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
listenConn = newListenConn
|
|
|
|
xl.Info("establishing nat hole connection successful, sid [%s], remoteAddr [%s]", natHoleRespMsg.Sid, raddr)
|
|
|
|
|
|
|
|
_ = pxy.msgTransporter.Send(&msg.NatHoleReport{
|
|
|
|
Sid: natHoleRespMsg.Sid,
|
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if natHoleRespMsg.Protocol == "kcp" {
|
|
|
|
pxy.listenByKCP(listenConn, raddr, startWorkConnMsg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// default is quic
|
|
|
|
pxy.listenByQUIC(listenConn, raddr, startWorkConnMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *XTCPProxy) listenByKCP(listenConn *net.UDPConn, raddr *net.UDPAddr, startWorkConnMsg *msg.StartWorkConn) {
|
|
|
|
xl := pxy.xl
|
|
|
|
listenConn.Close()
|
|
|
|
laddr, _ := net.ResolveUDPAddr("udp", listenConn.LocalAddr().String())
|
|
|
|
lConn, err := net.DialUDP("udp", laddr, raddr)
|
|
|
|
if err != nil {
|
|
|
|
xl.Warn("dial udp error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer lConn.Close()
|
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
remote, err := netpkg.NewKCPConnFromUDP(lConn, true, raddr.String())
|
2023-05-28 16:50:43 +08:00
|
|
|
if err != nil {
|
|
|
|
xl.Warn("create kcp connection from udp connection error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmuxCfg := fmux.DefaultConfig()
|
|
|
|
fmuxCfg.KeepAliveInterval = 10 * time.Second
|
2023-06-04 19:58:55 +08:00
|
|
|
fmuxCfg.MaxStreamWindowSize = 6 * 1024 * 1024
|
2023-05-28 16:50:43 +08:00
|
|
|
fmuxCfg.LogOutput = io.Discard
|
|
|
|
session, err := fmux.Server(remote, fmuxCfg)
|
|
|
|
if err != nil {
|
|
|
|
xl.Error("create mux session error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
muxConn, err := session.Accept()
|
|
|
|
if err != nil {
|
|
|
|
xl.Error("accept connection error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
go pxy.HandleTCPWorkConnection(muxConn, startWorkConnMsg, []byte(pxy.cfg.Secretkey))
|
2023-05-28 16:50:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *XTCPProxy) listenByQUIC(listenConn *net.UDPConn, _ *net.UDPAddr, startWorkConnMsg *msg.StartWorkConn) {
|
|
|
|
xl := pxy.xl
|
|
|
|
defer listenConn.Close()
|
|
|
|
|
|
|
|
tlsConfig, err := transport.NewServerTLSConfig("", "", "")
|
|
|
|
if err != nil {
|
|
|
|
xl.Warn("create tls config error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tlsConfig.NextProtos = []string{"frp"}
|
|
|
|
quicListener, err := quic.Listen(listenConn, tlsConfig,
|
|
|
|
&quic.Config{
|
2023-09-06 10:18:02 +08:00
|
|
|
MaxIdleTimeout: time.Duration(pxy.clientCfg.Transport.QUIC.MaxIdleTimeout) * time.Second,
|
|
|
|
MaxIncomingStreams: int64(pxy.clientCfg.Transport.QUIC.MaxIncomingStreams),
|
|
|
|
KeepAlivePeriod: time.Duration(pxy.clientCfg.Transport.QUIC.KeepalivePeriod) * time.Second,
|
2023-05-28 16:50:43 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
xl.Warn("dial quic error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// only accept one connection from raddr
|
|
|
|
c, err := quicListener.Accept(pxy.ctx)
|
|
|
|
if err != nil {
|
|
|
|
xl.Error("quic accept connection error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
stream, err := c.AcceptStream(pxy.ctx)
|
|
|
|
if err != nil {
|
|
|
|
xl.Debug("quic accept stream error: %v", err)
|
|
|
|
_ = c.CloseWithError(0, "")
|
|
|
|
return
|
|
|
|
}
|
2023-11-27 15:47:49 +08:00
|
|
|
go pxy.HandleTCPWorkConnection(netpkg.QuicStreamToNetConn(stream, c), startWorkConnMsg, []byte(pxy.cfg.Secretkey))
|
2023-05-28 16:50:43 +08:00
|
|
|
}
|
|
|
|
}
|