2 tofuproxy -- flexible HTTP proxy, TLS terminator, X.509 certificates
3 manager, WARC/Gemini browser
4 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 3 of the License.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 ttls "go.stargrave.org/tofuproxy/tls"
35 func findInNetrc(host string) (string, string) {
36 netrcPath, ok := os.LookupEnv("NETRC")
38 netrcPath = filepath.Join(os.Getenv("HOME"), ".netrc")
40 data, err := ioutil.ReadFile(netrcPath)
42 if os.IsNotExist(err) {
49 for _, line := range strings.Split(string(data), "\n") {
50 if i := strings.Index(line, "#"); i >= 0 {
53 f := strings.Fields(line)
55 f[0] == "machine" && f[1] == host &&
56 f[2] == "login" && f[4] == "password" {
57 login, password = f[3], f[5]
61 return login, password
64 func authDialog(host, realm string) (string, string, error) {
66 userInit, passInit := findInNetrc(host)
67 b.WriteString(fmt.Sprintf(`
69 wm title . "Unauthorized: %s"
71 label .luser -text "User"
73 set u [entry .user -textvariable userinit]
76 label .lpass -text "Password"
78 set p [entry .pass -show "*" -textvariable passinit]
88 button .login -text "Login" -command login
91 bind . <KeyPress> {switch -exact %%K {
92 q {exit 0} ; # reject once
95 `, realm, userInit, passInit))
96 cmd := exec.Command(ttls.CmdWish)
98 out, err := cmd.Output()
102 lines := strings.Split(string(out), "\n")
104 return "", "", errors.New("invalid output from authorization form")
106 return lines[0], lines[1], nil