Skip to content

Commit 24898e9

Browse files
committed
Dahua: digest auth
1 parent 44f2150 commit 24898e9

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

servers/dahua/server.go

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package dahua
22

33
import (
44
"fmt"
5+
"github.com/icholy/digest"
56
"io"
6-
"io/ioutil"
77
"mime"
88
"mime/multipart"
99
"net/http"
@@ -50,7 +50,9 @@ func (camera *DhCamera) readEvents(channel chan<- DhEvent, callback func()) {
5050
callback()
5151
return
5252
}
53-
request.SetBasicAuth(camera.Username, camera.Password)
53+
if camera.client.Transport == nil { // BASIC AUTH
54+
request.SetBasicAuth(camera.Username, camera.Password)
55+
}
5456

5557
response, err := camera.client.Do(request)
5658
if err != nil {
@@ -89,7 +91,7 @@ func (camera *DhCamera) readEvents(channel chan<- DhEvent, callback func()) {
8991
fmt.Println(err)
9092
continue
9193
}
92-
body, err := ioutil.ReadAll(part)
94+
body, err := io.ReadAll(part)
9395
if err != nil {
9496
fmt.Println(err)
9597
continue
@@ -151,6 +153,55 @@ func (server *Server) addCamera(waitGroup *sync.WaitGroup, cam *DhCamera, channe
151153
cam.client = &http.Client{}
152154
}
153155

156+
// PROBE AUTH
157+
request, err := http.NewRequest("GET", cam.Url+"/cgi-bin/eventManager.cgi?action=getConfig&name=General", nil)
158+
if err != nil {
159+
fmt.Printf("DAHUA: Error probing auth method for camera %s\n", cam.Name)
160+
fmt.Println(err)
161+
return
162+
}
163+
request.SetBasicAuth(cam.Username, cam.Password)
164+
response, err := cam.client.Do(request)
165+
if err != nil {
166+
fmt.Printf("DAHUA: Error probing HTTP Auth method for camera %s\n", cam.Name)
167+
fmt.Println(err)
168+
return
169+
}
170+
if response.StatusCode == 401 {
171+
if response.Header.Get("WWW-Authenticate") == "" {
172+
// BAD PASSWORD
173+
fmt.Printf("DAHUA: UNKNOWN AUTH METHOD FOR CAMERA %s! SKIPPING...", cam.Name)
174+
return
175+
}
176+
authMethod := strings.Split(response.Header.Get("WWW-Authenticate"), " ")[0]
177+
if authMethod == "Basic" {
178+
// BAD PASSWORD
179+
fmt.Printf("DAHUA: BAD PASSWORD FOR CAMERA %s! SKIPPING...", cam.Name)
180+
return
181+
}
182+
183+
// TRY ANOTHER TIME WITH DIGEST TRANSPORT
184+
cam.client.Transport = &digest.Transport{
185+
Username: cam.Username,
186+
Password: cam.Password,
187+
}
188+
response, err := cam.client.Do(request)
189+
if err != nil || response.StatusCode == 401 {
190+
if err != nil {
191+
fmt.Println(err)
192+
}
193+
// BAD PASSWORD
194+
fmt.Printf("DAHUA: BAD PASSWORD FOR CAMERA %s! SKIPPING...", cam.Name)
195+
return
196+
}
197+
198+
if server.Debug {
199+
fmt.Println("DAHUA: USING DIGEST AUTH")
200+
}
201+
} else if server.Debug {
202+
fmt.Println("DAHUA: USING BASIC AUTH")
203+
}
204+
154205
go func() {
155206
defer waitGroup.Done()
156207
done := false

0 commit comments

Comments
 (0)