Files
free-turn-proxy-orig/internal/proxy/common/common.go
T
samosvalishe 242862397a chore: переименовать модуль в github.com/samosvalishe/btp; обновить зависимости
- путь модуля: github.com/cacggghp/vk-turn-proxy → github.com/samosvalishe/btp
- директории cmd: cmd/vk-turn-{client,server} → cmd/{client,server}
- зависимости: pion/turn v5.0.4, kcp-go v5.6.72, smux v1.5.57, x/crypto v0.51,
  klauspost/{compress,cpuid,reedsolomon}, x/{net,sys,text,tools,mod,time}
- README, workflows, GLOSSARY обновлены под новые пути
2026-05-20 12:39:26 +03:00

44 lines
1.6 KiB
Go

// Package common holds the few helpers shared by udprelay and tcpfwd
// (TURN dial + wrap codec construction). The two proxy modes layer DTLS and
// srtpmimicry differently, so a full Engine/Handler abstraction is intentionally
// avoided — this package only collects what is genuinely identical.
package common
import (
"context"
"fmt"
"net"
"github.com/samosvalishe/btp/internal/transport/turndial"
"github.com/samosvalishe/btp/internal/wire/srtpmimicry"
)
// GetCredsFunc resolves VK TURN credentials for a (link, streamID) pair.
// Matches vkauth.Client.GetCredentials.
type GetCredsFunc func(ctx context.Context, link string, streamID int) (user, pass, rawURL string, err error)
// DialTURN fetches credentials and opens a TURN stream. The caller is
// responsible for closing the returned stream and for any auth-error retry
// policy (udprelay) or session-restart policy (tcpfwd).
func DialTURN(ctx context.Context, host, port string, udp bool, peer *net.UDPAddr, link string, streamID int, getCreds GetCredsFunc) (*turndial.Stream, error) {
user, pass, rawURL, err := getCreds(ctx, link, streamID)
if err != nil {
return nil, fmt.Errorf("get TURN creds: %w", err)
}
return turndial.Open(ctx, turndial.Config{
HostOverride: host,
PortOverride: port,
UDP: udp,
}, peer, user, pass, rawURL)
}
// NewClientWrap returns a client-side srtpmimicry.Conn if key has the
// expected length, otherwise (nil, nil) — wrap disabled. NewConn errors are
// propagated so callers can surface them.
func NewClientWrap(key []byte) (*srtpmimicry.Conn, error) {
if len(key) != srtpmimicry.KeyLen {
return nil, nil
}
return srtpmimicry.NewConn(key, false)
}