mirror of
https://github.com/fumiama/terasu-cloudflared.git
synced 2026-06-05 00:50:24 +08:00
44 lines
1000 B
Go
44 lines
1000 B
Go
// Copyright 2015 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package websocket
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/fumiama/terasu"
|
|
)
|
|
|
|
func dialWithDialer(ctx context.Context, dialer *net.Dialer, config *Config) (conn net.Conn, err error) {
|
|
switch config.Location.Scheme {
|
|
case "ws":
|
|
conn, err = dialer.DialContext(ctx, "tcp", parseAuthority(config.Location))
|
|
|
|
case "wss":
|
|
var rawConn net.Conn
|
|
rawConn, err = dialer.DialContext(ctx, "tcp", parseAuthority(config.Location))
|
|
if err == nil {
|
|
tlsCn := tls.Client(terasu.NewConn(rawConn), config.TlsConfig)
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
rawConn.SetDeadline(deadline)
|
|
}
|
|
err = tlsCn.Handshake()
|
|
if _, ok := ctx.Deadline(); ok {
|
|
rawConn.SetDeadline(time.Time{})
|
|
}
|
|
if err != nil {
|
|
rawConn.Close()
|
|
} else {
|
|
conn = tlsCn
|
|
}
|
|
}
|
|
default:
|
|
err = ErrBadScheme
|
|
}
|
|
return
|
|
}
|