Use permanent credentials

This commit is contained in:
Zoltán Papp 2024-06-21 15:02:54 +02:00
parent 4a08f1a1e9
commit 4d67d72785
2 changed files with 7 additions and 38 deletions

View File

@ -19,8 +19,6 @@ var (
ErrSignalIsNotReady = errors.New("signal is not ready")
)
type DoHandshake func() (*OfferAnswer, error)
// IceCredentials ICE protocol credentials struct
type IceCredentials struct {
UFrag string
@ -54,20 +52,6 @@ type HandshakeArgs struct {
RelayAddr string
}
func (a HandshakeArgs) Equal(args HandshakeArgs) bool {
if a.IceUFrag != args.IceUFrag {
return false
}
if a.IcePwd != args.IcePwd {
return false
}
if a.RelayAddr != args.RelayAddr {
return false
}
return true
}
type Handshaker struct {
mu sync.Mutex
ctx context.Context
@ -84,7 +68,6 @@ type Handshaker struct {
remoteOfferAnswer *OfferAnswer
remoteOfferAnswerCreated time.Time
lastSentOffer time.Time
lastOfferArgs HandshakeArgs
}
@ -105,6 +88,7 @@ func (h *Handshaker) Listen() {
remoteOfferAnswer, err := h.waitForRemoteOfferConfirmation()
if err != nil {
if _, ok := err.(*ConnectionClosedError); ok {
log.Tracef("stop handshaker")
return
}
log.Errorf("failed to received remote offer confirmation: %s", err)
@ -120,17 +104,12 @@ func (h *Handshaker) SendOffer(args HandshakeArgs) error {
h.mu.Lock()
defer h.mu.Unlock()
if h.lastOfferArgs.Equal(args) && h.lastSentOffer.After(time.Now().Add(-time.Second)) {
return nil
}
err := h.sendOffer(args)
if err != nil {
return err
}
h.lastOfferArgs = args
h.lastSentOffer = time.Now()
return nil
}
@ -187,6 +166,7 @@ func (h *Handshaker) waitForRemoteOfferConfirmation() (*OfferAnswer, error) {
// sendOffer prepares local user credentials and signals them to the remote peer
func (h *Handshaker) sendOffer(args HandshakeArgs) error {
log.Debugf("SEND OFFER: %s", args.IceUFrag)
offer := OfferAnswer{
IceCredentials: IceCredentials{args.IceUFrag, args.IcePwd},
WgListenPort: h.config.LocalWgPort,
@ -201,6 +181,7 @@ func (h *Handshaker) sendOffer(args HandshakeArgs) error {
func (h *Handshaker) sendAnswer() error {
h.log.Debugf("sending answer")
log.Debugf("SEND ANSWER: %s", h.lastOfferArgs.IceUFrag)
answer := OfferAnswer{
IceCredentials: IceCredentials{h.lastOfferArgs.IceUFrag, h.lastOfferArgs.IcePwd},
WgListenPort: h.config.LocalWgPort,

View File

@ -94,8 +94,6 @@ type WorkerICE struct {
localPwd string
creadantialHasUsed bool
hasRelayOnLocally bool
onDisconnected context.CancelFunc
onOfferReceived context.CancelFunc
tickerCancel context.CancelFunc
ticker *time.Ticker
}
@ -274,7 +272,6 @@ func (w *WorkerICE) GetLocalUserCredentials() (frag string, pwd string) {
}
func (w *WorkerICE) reCreateAgent(agentCancel context.CancelFunc, relaySupport []ice.CandidateType) (*ice.Agent, error) {
log.Debugf("--RECREATE AGENT-----")
transportNet, err := w.newStdNet()
if err != nil {
w.log.Errorf("failed to create pion's stdnet: %s", err)
@ -285,9 +282,9 @@ func (w *WorkerICE) reCreateAgent(agentCancel context.CancelFunc, relaySupport [
iceRelayAcceptanceMinWait := iceRelayAcceptanceMinWait()
agentConfig := &ice.AgentConfig{
MulticastDNSMode: ice.MulticastDNSModeDisabled,
NetworkTypes: []ice.NetworkType{ice.NetworkTypeUDP4, ice.NetworkTypeUDP6},
//Urls: w.configICE.StunTurn.Load().([]*stun.URI),
MulticastDNSMode: ice.MulticastDNSModeDisabled,
NetworkTypes: []ice.NetworkType{ice.NetworkTypeUDP4, ice.NetworkTypeUDP6},
Urls: w.configICE.StunTurn.Load().([]*stun.URI),
CandidateTypes: relaySupport,
InterfaceFilter: stdnet.InterfaceFilter(w.configICE.InterfaceBlackList),
UDPMux: w.configICE.UDPMux,
@ -327,14 +324,6 @@ func (w *WorkerICE) reCreateAgent(agentCancel context.CancelFunc, relaySupport [
_ = agent.Close()
w.agent = nil
// generate credentials for the next agent creation loop
localUfrag, localPwd, err := generateICECredentials()
if err != nil {
log.Errorf("failed to generate new ICE credentials: %s", err)
}
w.localUfrag = localUfrag
w.localPwd = localPwd
w.muxAgent.Unlock()
go w.sendOffer()
}
@ -510,7 +499,7 @@ func candidateTypes() []ice.CandidateType {
}
func candidateTypesP2P() []ice.CandidateType {
return []ice.CandidateType{ice.CandidateTypeHost}
return []ice.CandidateType{ice.CandidateTypeHost, ice.CandidateTypeServerReflexive}
}
func isRelayCandidate(candidate ice.Candidate) bool {
@ -525,7 +514,6 @@ func isRelayed(pair *ice.CandidatePair) bool {
}
func generateICECredentials() (string, string, error) {
log.Debugf("-----GENERATE CREDENTIALS------")
ufrag, err := randutil.GenerateCryptoRandomString(lenUFrag, runesAlpha)
if err != nil {
return "", "", err