forked from martensson/vaban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
39 lines (37 loc) · 1 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"crypto/sha256"
"encoding/hex"
"errors"
"net"
"regexp"
)
func varnishAuth(server string, secret string, conn net.Conn) error {
// I want to allocate 512 bytes, enough to read the varnish help output.
reply := make([]byte, 512)
_, err := conn.Read(reply)
if err != nil {
return errors.New(server + " " + err.Error())
}
rp := regexp.MustCompile("[a-z]{32}") //find challenge string
challenge := rp.FindString(string(reply))
if challenge != "" {
// time to authenticate
hash := sha256.New()
hash.Write([]byte(challenge + "\n" + secret + "\n" + challenge + "\n"))
md := hash.Sum(nil)
mdStr := hex.EncodeToString(md)
_, err := conn.Write([]byte("auth " + mdStr + "\n"))
if err != nil {
return errors.New(server + " " + err.Error())
}
auth_reply := make([]byte, 512)
_, err = conn.Read(auth_reply)
if err != nil {
return errors.New(server + " " + err.Error())
}
return nil
} else {
return errors.New(server + " no challenge code, secret-file disabled.")
}
}