2019-08-31 17:01:39 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-01-25 10:19:45 +00:00
|
|
|
"crypto/rand"
|
2019-08-31 17:01:39 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
2019-09-01 00:33:34 +00:00
|
|
|
"fmt"
|
2019-08-31 17:01:39 +00:00
|
|
|
"github.com/cbeuw/Cloak/internal/util"
|
2019-09-01 00:33:34 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2019-08-31 17:01:39 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-04-06 12:07:16 +00:00
|
|
|
"time"
|
2019-09-02 13:03:10 +00:00
|
|
|
|
|
|
|
utls "github.com/refraction-networking/utls"
|
2019-08-31 17:01:39 +00:00
|
|
|
)
|
|
|
|
|
2019-09-02 13:03:10 +00:00
|
|
|
type WSOverTLS struct {
|
2020-04-06 12:07:16 +00:00
|
|
|
cdnDomainPort string
|
2019-08-31 17:01:39 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 13:03:10 +00:00
|
|
|
func (WSOverTLS) HasRecordLayer() bool { return false }
|
|
|
|
func (WSOverTLS) UnitReadFunc() func(net.Conn, []byte) (int, error) { return util.ReadWebSocket }
|
|
|
|
|
2020-04-07 20:15:28 +00:00
|
|
|
func (ws WSOverTLS) PrepareConnection(authInfo *authInfo, cdnConn net.Conn) (preparedConn net.Conn, sessionKey [32]byte, err error) {
|
2019-09-02 13:03:10 +00:00
|
|
|
utlsConfig := &utls.Config{
|
2020-04-06 12:07:16 +00:00
|
|
|
ServerName: authInfo.MockDomain,
|
2019-09-02 13:03:10 +00:00
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
2020-04-06 12:07:16 +00:00
|
|
|
uconn := utls.UClient(cdnConn, utlsConfig, utls.HelloChrome_Auto)
|
2019-09-02 13:03:10 +00:00
|
|
|
err = uconn.Handshake()
|
|
|
|
preparedConn = uconn
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-09-01 19:23:45 +00:00
|
|
|
|
2020-04-06 12:07:16 +00:00
|
|
|
u, err := url.Parse("ws://" + ws.cdnDomainPort)
|
2019-08-31 17:01:39 +00:00
|
|
|
if err != nil {
|
2020-04-07 20:15:28 +00:00
|
|
|
return preparedConn, sessionKey, fmt.Errorf("failed to parse ws url: %v", err)
|
2019-08-31 17:01:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 12:07:16 +00:00
|
|
|
payload, sharedSecret := makeAuthenticationPayload(authInfo, rand.Reader, time.Now())
|
2019-08-31 17:01:39 +00:00
|
|
|
header := http.Header{}
|
2020-01-24 16:44:29 +00:00
|
|
|
header.Add("hidden", base64.StdEncoding.EncodeToString(append(payload.randPubKey[:], payload.ciphertextWithTag[:]...)))
|
2019-09-01 19:23:45 +00:00
|
|
|
c, _, err := websocket.NewClient(preparedConn, u, header, 16480, 16480)
|
2019-08-31 17:01:39 +00:00
|
|
|
if err != nil {
|
2020-04-07 20:15:28 +00:00
|
|
|
return preparedConn, sessionKey, fmt.Errorf("failed to handshake: %v", err)
|
2019-08-31 17:01:39 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 19:23:45 +00:00
|
|
|
preparedConn = &util.WebSocketConn{Conn: c}
|
2019-09-01 00:33:34 +00:00
|
|
|
|
|
|
|
buf := make([]byte, 128)
|
2019-09-01 19:23:45 +00:00
|
|
|
n, err := preparedConn.Read(buf)
|
2019-08-31 17:01:39 +00:00
|
|
|
if err != nil {
|
2020-04-07 20:15:28 +00:00
|
|
|
return preparedConn, sessionKey, fmt.Errorf("failed to read reply: %v", err)
|
2019-08-31 17:01:39 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 00:33:34 +00:00
|
|
|
if n != 60 {
|
2020-04-07 20:15:28 +00:00
|
|
|
return preparedConn, sessionKey, errors.New("reply must be 60 bytes")
|
2019-08-31 17:01:39 +00:00
|
|
|
}
|
2019-09-01 00:33:34 +00:00
|
|
|
|
|
|
|
reply := buf[:60]
|
2020-04-07 20:15:28 +00:00
|
|
|
sessionKeySlice, err := util.AESGCMDecrypt(reply[:12], sharedSecret[:], reply[12:])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
copy(sessionKey[:], sessionKeySlice)
|
2019-08-31 17:01:39 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|