| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package client |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "crypto/tls" |
| 20 | "net" |
| 21 | "strconv" |
| 22 | "strings" |
| 23 | "sync" |
| 24 | "time" |
| 25 |
| 26 | libnet "github.com/fatedier/golib/net" |
| 27 | fmux "github.com/hashicorp/yamux" |
| 28 | quic "github.com/quic-go/quic-go" |
| 29 | "github.com/samber/lo" |
| 30 |
| 31 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 32 | "github.com/fatedier/frp/pkg/msg" |
| 33 | "github.com/fatedier/frp/pkg/proto/wire" |
| 34 | "github.com/fatedier/frp/pkg/transport" |
| 35 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 36 | "github.com/fatedier/frp/pkg/util/xlog" |
| 37 | ) |
| 38 |
| 39 | // Connector is an interface for establishing connections to the server. |
| 40 | type Connector interface { |
| 41 | Open() error |
| 42 | Connect() (net.Conn, error) |
| 43 | Close() error |
| 44 | } |
| 45 |
| 46 | type MessageConnector interface { |
| 47 | Connect() (*msg.Conn, error) |
| 48 | Close() error |
| 49 | } |
| 50 |
| 51 | type messageConnector struct { |
| 52 | connector Connector |
| 53 | wireProtocol string |
| 54 | } |
| 55 |
| 56 | func newMessageConnector(connector Connector, wireProtocol string) *messageConnector { |
| 57 | return &messageConnector{ |
| 58 | connector: connector, |
| 59 | wireProtocol: wireProtocol, |
| 60 | } |
| 61 | } |
| 62 |
| 63 | func (c *messageConnector) Connect() (*msg.Conn, error) { |
| 64 | conn, err := c.connector.Connect() |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | if err = wire.WriteMagicIfV2(conn, c.wireProtocol); err != nil { |
| 69 | conn.Close() |
| 70 | return nil, err |
| 71 | } |
| 72 | return msg.NewConn(conn, msg.NewReadWriter(conn, c.wireProtocol)), nil |
| 73 | } |
| 74 |
| 75 | func (c *messageConnector) Close() error { |
| 76 | return c.connector.Close() |
| 77 | } |
| 78 |
| 79 | // defaultConnectorImpl is the default implementation of Connector for normal frpc. |
| 80 | type defaultConnectorImpl struct { |
| 81 | ctx context.Context |
| 82 | cfg *v1.ClientCommonConfig |
| 83 |
| 84 | muxSession *fmux.Session |
| 85 | quicConn *quic.Conn |
| 86 | closeOnce sync.Once |
| 87 | } |
| 88 |
| 89 | func NewConnector(ctx context.Context, cfg *v1.ClientCommonConfig) Connector { |
| 90 | return &defaultConnectorImpl{ |
| 91 | ctx: ctx, |
| 92 | cfg: cfg, |
| 93 | } |
| 94 | } |
| 95 |
| 96 | // Open opens an underlying connection to the server. |
| 97 | // The underlying connection is either a TCP connection or a QUIC connection. |
| 98 | // After the underlying connection is established, you can call Connect() to get a stream. |
| 99 | // If TCPMux isn't enabled, the underlying connection is nil, you will get a new real TCP connection every time you call Connect(). |
| 100 | func (c *defaultConnectorImpl) Open() error { |
| 101 | xl := xlog.FromContextSafe(c.ctx) |
| 102 |
| 103 | // special for quic |
| 104 | if strings.EqualFold(c.cfg.Transport.Protocol, "quic") { |
| 105 | var tlsConfig *tls.Config |
| 106 | var err error |
| 107 | sn := c.cfg.Transport.TLS.ServerName |
| 108 | if sn == "" { |
| 109 | sn = c.cfg.ServerAddr |
| 110 | } |
| 111 | if lo.FromPtr(c.cfg.Transport.TLS.Enable) { |
| 112 | tlsConfig, err = transport.NewClientTLSConfig( |
| 113 | c.cfg.Transport.TLS.CertFile, |
| 114 | c.cfg.Transport.TLS.KeyFile, |
| 115 | c.cfg.Transport.TLS.TrustedCaFile, |
| 116 | sn) |
| 117 | } else { |
| 118 | tlsConfig, err = transport.NewClientTLSConfig("", "", "", sn) |
| 119 | } |
| 120 | if err != nil { |
| 121 | xl.Warnf("fail to build tls configuration, err: %v", err) |
| 122 | return err |
| 123 | } |
| 124 | tlsConfig.NextProtos = []string{"frp"} |
| 125 |
| 126 | conn, err := quic.DialAddr( |
| 127 | c.ctx, |
| 128 | net.JoinHostPort(c.cfg.ServerAddr, strconv.Itoa(c.cfg.ServerPort)), |
| 129 | tlsConfig, &quic.Config{ |
| 130 | MaxIdleTimeout: time.Duration(c.cfg.Transport.QUIC.MaxIdleTimeout) * time.Second, |
| 131 | MaxIncomingStreams: int64(c.cfg.Transport.QUIC.MaxIncomingStreams), |
| 132 | KeepAlivePeriod: time.Duration(c.cfg.Transport.QUIC.KeepalivePeriod) * time.Second, |
| 133 | }) |
| 134 | if err != nil { |
| 135 | return err |
| 136 | } |
| 137 | c.quicConn = conn |
| 138 | return nil |
| 139 | } |
| 140 |
| 141 | if !lo.FromPtr(c.cfg.Transport.TCPMux) { |
| 142 | return nil |
| 143 | } |
| 144 |
| 145 | conn, err := c.realConnect() |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 |
| 150 | fmuxCfg := fmux.DefaultConfig() |
| 151 | fmuxCfg.KeepAliveInterval = time.Duration(c.cfg.Transport.TCPMuxKeepaliveInterval) * time.Second |
| 152 | // Use trace level for yamux logs |
| 153 | fmuxCfg.LogOutput = xlog.NewTraceWriter(xl) |
| 154 | fmuxCfg.MaxStreamWindowSize = 6 * 1024 * 1024 |
| 155 | session, err := fmux.Client(conn, fmuxCfg) |
| 156 | if err != nil { |
| 157 | conn.Close() |
| 158 | return err |
| 159 | } |
| 160 | c.muxSession = session |
| 161 | return nil |
| 162 | } |
| 163 |
| 164 | // Connect returns a stream from the underlying connection, or a new TCP connection if TCPMux isn't enabled. |
| 165 | func (c *defaultConnectorImpl) Connect() (net.Conn, error) { |
| 166 | if c.quicConn != nil { |
| 167 | stream, err := c.quicConn.OpenStreamSync(context.Background()) |
| 168 | if err != nil { |
| 169 | return nil, err |
| 170 | } |
| 171 | return netpkg.QuicStreamToNetConn(stream, c.quicConn), nil |
| 172 | } else if c.muxSession != nil { |
| 173 | stream, err := c.muxSession.OpenStream() |
| 174 | if err != nil { |
| 175 | return nil, err |
| 176 | } |
| 177 | return stream, nil |
| 178 | } |
| 179 |
| 180 | return c.realConnect() |
| 181 | } |
| 182 |
| 183 | func (c *defaultConnectorImpl) realConnect() (net.Conn, error) { |
| 184 | xl := xlog.FromContextSafe(c.ctx) |
| 185 | var tlsConfig *tls.Config |
| 186 | var err error |
| 187 | tlsEnable := lo.FromPtr(c.cfg.Transport.TLS.Enable) |
| 188 | if c.cfg.Transport.Protocol == "wss" { |
| 189 | tlsEnable = true |
| 190 | } |
| 191 | if tlsEnable { |
| 192 | sn := c.cfg.Transport.TLS.ServerName |
| 193 | if sn == "" { |
| 194 | sn = c.cfg.ServerAddr |
| 195 | } |
| 196 |
| 197 | tlsConfig, err = transport.NewClientTLSConfig( |
| 198 | c.cfg.Transport.TLS.CertFile, |
| 199 | c.cfg.Transport.TLS.KeyFile, |
| 200 | c.cfg.Transport.TLS.TrustedCaFile, |
| 201 | sn) |
| 202 | if err != nil { |
| 203 | xl.Warnf("fail to build tls configuration, err: %v", err) |
| 204 | return nil, err |
| 205 | } |
| 206 | } |
| 207 |
| 208 | proxyType, addr, auth, err := libnet.ParseProxyURL(c.cfg.Transport.ProxyURL) |
| 209 | if err != nil { |
| 210 | xl.Errorf("fail to parse proxy url") |
| 211 | return nil, err |
| 212 | } |
| 213 | dialOptions := []libnet.DialOption{} |
| 214 | protocol := c.cfg.Transport.Protocol |
| 215 | switch protocol { |
| 216 | case "websocket": |
| 217 | protocol = "tcp" |
| 218 | dialOptions = append(dialOptions, libnet.WithAfterHook(libnet.AfterHook{Hook: netpkg.DialHookWebsocket(protocol, "")})) |
| 219 | dialOptions = append(dialOptions, libnet.WithAfterHook(libnet.AfterHook{ |
| 220 | Hook: netpkg.DialHookCustomTLSHeadByte(tlsConfig != nil, lo.FromPtr(c.cfg.Transport.TLS.DisableCustomTLSFirstByte)), |
| 221 | })) |
| 222 | dialOptions = append(dialOptions, libnet.WithTLSConfig(tlsConfig)) |
| 223 | case "wss": |
| 224 | protocol = "tcp" |
| 225 | dialOptions = append(dialOptions, libnet.WithTLSConfigAndPriority(100, tlsConfig)) |
| 226 | // Make sure that if it is wss, the websocket hook is executed after the tls hook. |
| 227 | dialOptions = append(dialOptions, libnet.WithAfterHook(libnet.AfterHook{Hook: netpkg.DialHookWebsocket(protocol, tlsConfig.ServerName), Priority: 110})) |
| 228 | default: |
| 229 | dialOptions = append(dialOptions, libnet.WithAfterHook(libnet.AfterHook{ |
| 230 | Hook: netpkg.DialHookCustomTLSHeadByte(tlsConfig != nil, lo.FromPtr(c.cfg.Transport.TLS.DisableCustomTLSFirstByte)), |
| 231 | })) |
| 232 | dialOptions = append(dialOptions, libnet.WithTLSConfig(tlsConfig)) |
| 233 | } |
| 234 |
| 235 | if c.cfg.Transport.ConnectServerLocalIP != "" { |
| 236 | dialOptions = append(dialOptions, libnet.WithLocalAddr(c.cfg.Transport.ConnectServerLocalIP)) |
| 237 | } |
| 238 | dialOptions = append(dialOptions, |
| 239 | libnet.WithProtocol(protocol), |
| 240 | libnet.WithTimeout(time.Duration(c.cfg.Transport.DialServerTimeout)*time.Second), |
| 241 | libnet.WithKeepAlive(time.Duration(c.cfg.Transport.DialServerKeepAlive)*time.Second), |
| 242 | libnet.WithProxy(proxyType, addr), |
| 243 | libnet.WithProxyAuth(auth), |
| 244 | ) |
| 245 | conn, err := libnet.DialContext( |
| 246 | c.ctx, |
| 247 | net.JoinHostPort(c.cfg.ServerAddr, strconv.Itoa(c.cfg.ServerPort)), |
| 248 | dialOptions..., |
| 249 | ) |
| 250 | return conn, err |
| 251 | } |
| 252 |
| 253 | func (c *defaultConnectorImpl) Close() error { |
| 254 | c.closeOnce.Do(func() { |
| 255 | if c.quicConn != nil { |
| 256 | _ = c.quicConn.CloseWithError(0, "") |
| 257 | } |
| 258 | if c.muxSession != nil { |
| 259 | _ = c.muxSession.Close() |
| 260 | } |
| 261 | }) |
| 262 | return nil |
| 263 | } |