Skip to content

Commit 79b1fab

Browse files
author
Pascal Benoit
committed
Add checker on auth and invite
1 parent 812cd1c commit 79b1fab

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

sip-to-webrtc/softphone/invite.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package softphone
22

33
import (
44
"fmt"
5+
"strings"
56
)
67

78
// Invite ...
@@ -22,14 +23,43 @@ func (softphone *Softphone) Invite(extension, offer string) {
2223
sipMessage.Body = offer
2324

2425
softphone.request(sipMessage, func(message string) bool {
25-
authenticateHeader := SIPMessage{}.FromString(message).headers["Proxy-Authenticate"]
26-
ai := GetAuthInfo(authenticateHeader)
27-
ai.AuthType = "Proxy-Authorization"
28-
ai.Uri = "sip:"+ extension + "@"+ softphone.sipInfo.Domain
29-
ai.Method = "INVITE"
26+
proxyAuthenticateHeader := SIPMessage{}.FromString(message).headers["Proxy-Authenticate"]
27+
authenticateHeader := SIPMessage{}.FromString(message).headers["WWW-Authenticate"]
28+
29+
var ai AuthInfo
30+
if len(authenticateHeader) > 0 { //WWW-Authenticate
31+
ai = GetAuthInfo(authenticateHeader)
32+
ai.AuthType = "Proxy-Authorization"
33+
ai.Uri = "sip:"+ extension + "@"+ softphone.sipInfo.Domain
34+
ai.Method = "INVITE"
35+
} else if len(proxyAuthenticateHeader) > 0 { //Proxy-Authenticate
36+
ai = GetAuthInfo(authenticateHeader)
37+
ai.AuthType = "Authorization"
38+
ai.Uri = "sip:"+ extension + "@"+ softphone.sipInfo.Domain
39+
ai.Method = "INVITE"
40+
} else {
41+
panic("FAIL TO SEND INVITE")
42+
}
43+
3044
sipMessage.addAuthorization(*softphone, ai).addCseq(softphone).newViaBranch()
3145
softphone.request(sipMessage, func(msg string) bool {
32-
return false
46+
responseStatus := strings.Split(strings.Split(msg, "\r\n")[0], " ")[1]
47+
textStatus := strings.Split(strings.Split(msg, "\r\n")[0], " ")[2]
48+
switch responseStatus {
49+
case "100":
50+
fmt.Println(textStatus)
51+
return false //Continue the handler
52+
case "183":
53+
fmt.Println(textStatus)
54+
return false //Continue the handler
55+
56+
case "200":
57+
fmt.Println("### INVITE SUCCESS ###")
58+
return true
59+
default: //480
60+
panic("INVITE FAILED : " + responseStatus + " " + textStatus)
61+
}
62+
return true
3363
})
3464

3565
return true

sip-to-webrtc/softphone/register.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package softphone
22

33
import (
4-
"context"
54
"crypto/tls"
65
"fmt"
76
"github.com/gorilla/websocket"
87
"log"
98
"net/url"
9+
"strings"
1010
)
1111

1212
func (softphone *Softphone) register() {
@@ -40,7 +40,6 @@ func (softphone *Softphone) register() {
4040
}
4141
}
4242
}()
43-
4443
sipMessage := SIPMessage{}
4544
sipMessage.method = "REGISTER"
4645
sipMessage.address = softphone.sipInfo.Domain
@@ -53,23 +52,29 @@ func (softphone *Softphone) register() {
5352
sipMessage.headers["Supported"] = "path,ice"
5453
sipMessage.addCseq(softphone).addCallID(*softphone).addUserAgent()
5554

56-
registered, registeredFunc := context.WithCancel(context.Background())
57-
5855
softphone.request(sipMessage, func(message string) bool {
5956
authenticateHeader := SIPMessage{}.FromString(message).headers["WWW-Authenticate"]
60-
ai := GetAuthInfo(authenticateHeader)
57+
ai := GetAuthInfo(authenticateHeader)
6158
ai.AuthType = "Authorization"
6259
ai.Uri = "sip:" + softphone.sipInfo.Domain
6360
ai.Method = "REGISTER"
64-
sipMessage.addAuthorization(*softphone,ai).addCseq(softphone).newViaBranch()
61+
sipMessage.addAuthorization(*softphone, ai).addCseq(softphone).newViaBranch()
6562
softphone.request(sipMessage, func(msg string) bool {
66-
registeredFunc()
6763

68-
return false
64+
responseStatus := strings.Split(strings.Split(msg, "\r\n")[0], " ")[1]
65+
textStatus := strings.Split(strings.Split(msg, "\r\n")[0], " ")[2]
66+
switch responseStatus {
67+
case "200":
68+
fmt.Println("### SOFTPHONE CONNECTED ###")
69+
break
70+
default:
71+
panic("AUTHENTICATION FAILED : " + responseStatus + " " + textStatus)
72+
break
73+
}
74+
75+
return true
6976
})
7077

7178
return true
7279
})
73-
74-
<-registered.Done()
7580
}

sip-to-webrtc/softphone/softphone.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func (softphone *Softphone) removeMessageListener(key string) {
5656

5757
func (softphone Softphone) request2(sipMessage SIPMessage, expectedResp string) string {
5858

59-
6059
if err := softphone.wsConn.WriteMessage(1, []byte(sipMessage.ToString())); err != nil {
6160
log.Panic(err)
6261
}
@@ -91,7 +90,7 @@ func (softphone *Softphone) request(sipMessage SIPMessage, responseHandler func(
9190
}
9291

9392
func (softphone *Softphone) response(message string) {
94-
// log.Print("↑↑↑\n", message)
93+
// log.Print("↑↑↑\n", message)
9594

9695
if err := softphone.wsConn.WriteMessage(1, []byte(message)); err != nil {
9796
log.Fatal(err)

0 commit comments

Comments
 (0)