Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/pkg/gowebdav/netrc.go
1560 views
1
package gowebdav
2
3
import (
4
"bufio"
5
"fmt"
6
"net/url"
7
"os"
8
"regexp"
9
"strings"
10
)
11
12
func parseLine(s string) (login, pass string) {
13
fields := strings.Fields(s)
14
for i, f := range fields {
15
if f == "login" {
16
login = fields[i+1]
17
}
18
if f == "password" {
19
pass = fields[i+1]
20
}
21
}
22
return login, pass
23
}
24
25
// ReadConfig reads login and password configuration from ~/.netrc
26
// machine foo.com login username password 123456
27
func ReadConfig(uri, netrc string) (string, string) {
28
u, err := url.Parse(uri)
29
if err != nil {
30
return "", ""
31
}
32
33
file, err := os.Open(netrc)
34
if err != nil {
35
return "", ""
36
}
37
defer file.Close()
38
39
re := fmt.Sprintf(`^.*machine %s.*$`, u.Host)
40
scanner := bufio.NewScanner(file)
41
for scanner.Scan() {
42
s := scanner.Text()
43
44
matched, err := regexp.MatchString(re, s)
45
if err != nil {
46
return "", ""
47
}
48
if matched {
49
return parseLine(s)
50
}
51
}
52
53
return "", ""
54
}
55
56