Remove WebRTCPeer.activeTransportMode.

Make "u" mode the assumed default. The WebRTC data channel protocol
contains just the hex clientID.
This commit is contained in:
David Fifield 2024-08-02 02:58:59 +00:00 committed by WofWca
parent 568a0999d2
commit 490502691d
3 changed files with 13 additions and 82 deletions

View file

@ -331,11 +331,6 @@ func parseIceServers(addresses []string) []webrtc.ICEServer {
// over. The net.PacketConn successively connects through Snowflake proxies // over. The net.PacketConn successively connects through Snowflake proxies
// pulled from snowflakes. // pulled from snowflakes.
func newSession(snowflakes SnowflakeCollector, clientIDCandid turbotunnel.ClientID) (net.PacketConn, *smux.Session, error) { func newSession(snowflakes SnowflakeCollector, clientIDCandid turbotunnel.ClientID) (net.PacketConn, *smux.Session, error) {
clientID := turbotunnel.NewClientID()
if clientIDCandid != (turbotunnel.ClientID{}) {
clientID = clientIDCandid
}
// We build a persistent KCP session on a sequence of ephemeral WebRTC // We build a persistent KCP session on a sequence of ephemeral WebRTC
// connections. This dialContext tells RedialPacketConn how to get a new // connections. This dialContext tells RedialPacketConn how to get a new
// WebRTC connection when the previous one dies. Inside each WebRTC // WebRTC connection when the previous one dies. Inside each WebRTC
@ -349,8 +344,6 @@ func newSession(snowflakes SnowflakeCollector, clientIDCandid turbotunnel.Client
return nil, errors.New("handler: Received invalid Snowflake") return nil, errors.New("handler: Received invalid Snowflake")
} }
log.Println("---- Handler: snowflake assigned ----") log.Println("---- Handler: snowflake assigned ----")
log.Printf("activeTransportMode = %c \n", conn.activeTransportMode)
if conn.activeTransportMode == 'u' {
packetConnWrapper := &packetConnWrapper{ packetConnWrapper := &packetConnWrapper{
ReadWriter: conn, ReadWriter: conn,
remoteAddr: dummyAddr{}, remoteAddr: dummyAddr{},
@ -358,18 +351,6 @@ func newSession(snowflakes SnowflakeCollector, clientIDCandid turbotunnel.Client
} }
return packetConnWrapper, nil return packetConnWrapper, nil
} }
// Send the magic Turbo Tunnel token.
_, err := conn.Write(turbotunnel.Token[:])
if err != nil {
return nil, err
}
// Send ClientID prefix.
_, err = conn.Write(clientID[:])
if err != nil {
return nil, err
}
return newEncapsulationPacketConn(dummyAddr{}, dummyAddr{}, conn), nil
}
pconn := turbotunnel.NewRedialPacketConn(dummyAddr{}, dummyAddr{}, dialContext) pconn := turbotunnel.NewRedialPacketConn(dummyAddr{}, dummyAddr{}, dialContext)
// conn is built on the underlying RedialPacketConn—when one WebRTC // conn is built on the underlying RedialPacketConn—when one WebRTC

View file

@ -46,7 +46,6 @@ type WebRTCPeer struct {
eventsLogger event.SnowflakeEventReceiver eventsLogger event.SnowflakeEventReceiver
proxy *url.URL proxy *url.URL
activeTransportMode byte
clientID turbotunnel.ClientID clientID turbotunnel.ClientID
} }
@ -212,7 +211,6 @@ func (c *WebRTCPeer) connect(
) error { ) error {
log.Println(c.id, " connecting...") log.Println(c.id, " connecting...")
c.activeTransportMode = 'u'
err := c.preparePeerConnection(config, broker.keepLocalAddresses) err := c.preparePeerConnection(config, broker.keepLocalAddresses)
localDescription := c.pc.LocalDescription() localDescription := c.pc.LocalDescription()
c.eventsLogger.OnNewSnowflakeEvent(event.EventOnOfferCreated{ c.eventsLogger.OnNewSnowflakeEvent(event.EventOnOfferCreated{
@ -318,18 +316,13 @@ func (c *WebRTCPeer) preparePeerConnection(
log.Printf("NewPeerConnection ERROR: %s", err) log.Printf("NewPeerConnection ERROR: %s", err)
return err return err
} }
ordered := true ordered := false
var maxRetransmission *uint16 var maxRetransmission uint16 = 0
if c.activeTransportMode == 'u' { protocol := fmt.Sprintf("%s", c.clientID.String())
ordered = false
maxRetransmissionVal := uint16(0)
maxRetransmission = &maxRetransmissionVal
}
protocol := fmt.Sprintf("%c %s", c.activeTransportMode, c.clientID.String())
dataChannelOptions := &webrtc.DataChannelInit{ dataChannelOptions := &webrtc.DataChannelInit{
Ordered: &ordered, Ordered: &ordered,
Protocol: &protocol, Protocol: &protocol,
MaxRetransmits: maxRetransmission, MaxRetransmits: &maxRetransmission,
} }
// We must create the data channel before creating an offer // We must create the data channel before creating an offer
// https://github.com/pion/webrtc/wiki/Release-WebRTC@v3.0.0#a-data-channel-is-no-longer-implicitly-created-with-a-peerconnection // https://github.com/pion/webrtc/wiki/Release-WebRTC@v3.0.0#a-data-channel-is-no-longer-implicitly-created-with-a-peerconnection

View file

@ -2,7 +2,6 @@ package snowflake_server
import ( import (
"bufio" "bufio"
"bytes"
"crypto/hmac" "crypto/hmac"
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
@ -13,7 +12,6 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"strings"
"sync" "sync"
"time" "time"
@ -112,48 +110,11 @@ func (handler *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
addr := clientAddr(clientIPParam) addr := clientAddr(clientIPParam)
protocol := r.URL.Query().Get("protocol") protocol := r.URL.Query().Get("protocol")
clientTransport := "t"
if protocol != "" {
clientTransport = fmt.Sprintf("%c", protocol[0])
}
if clientTransport == "u" {
err = handler.turboTunnelUDPLikeMode(conn, addr, protocol) err = handler.turboTunnelUDPLikeMode(conn, addr, protocol)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
log.Println(err) log.Println(err)
return return
} }
return
}
var token [len(turbotunnel.Token)]byte
_, err = io.ReadFull(conn, token[:])
if err != nil {
// Don't bother logging EOF: that happens with an unused
// connection, which clients make frequently as they maintain a
// pool of proxies.
if err != io.EOF {
log.Printf("reading token: %v", err)
}
return
}
switch {
case bytes.Equal(token[:], turbotunnel.Token[:]):
err = handler.turbotunnelMode(conn, addr)
default:
// We didn't find a matching token, which means that we are
// dealing with a client that doesn't know about such things.
// Close the conn as we no longer support the old
// one-session-per-WebSocket mode.
log.Println("Received unsupported oneshot connection")
return
}
if err != nil {
log.Println(err)
return
}
} }
// turbotunnelMode handles clients that sent turbotunnel.Token at the start of // turbotunnelMode handles clients that sent turbotunnel.Token at the start of
@ -243,11 +204,7 @@ func (handler *httpHandler) turboTunnelUDPLikeMode(conn net.Conn, addr net.Addr,
var packet [1600]byte var packet [1600]byte
clientID := turbotunnel.ClientID{} clientID := turbotunnel.ClientID{}
compoments := strings.Split(protocol, " ") _, err := hex.Decode(clientID[:], []byte(protocol))
if len(compoments) != 2 {
return fmt.Errorf("invalid protocol: %s", protocol)
}
_, err := hex.Decode(clientID[:], []byte(compoments[1]))
if err != nil { if err != nil {
return fmt.Errorf("reading ClientID: %v", err) return fmt.Errorf("reading ClientID: %v", err)
} }