2 tofuproxy -- HTTP proxy with TLS certificates management
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 authCache = make(map[string][2]string)
38 func findInNetrc(host string) (string, string) {
39 netrcPath, ok := os.LookupEnv("NETRC")
41 netrcPath = filepath.Join(os.Getenv("HOME"), ".netrc")
43 data, err := ioutil.ReadFile(netrcPath)
45 if os.IsNotExist(err) {
52 for _, line := range strings.Split(string(data), "\n") {
53 if i := strings.Index(line, "#"); i >= 0 {
56 f := strings.Fields(line)
58 f[0] == "machine" && f[1] == host &&
59 f[2] == "login" && f[4] == "password" {
60 login, password = f[3], f[5]
64 return login, password
67 func authDialog(host, realm string) (string, string, error) {
69 userInit, passInit := findInNetrc(host)
70 b.WriteString(fmt.Sprintf(`
71 wm title . "Unauthorized: %s"
73 label .luser -text "User"
75 set u [entry .user -textvariable userinit]
78 label .lpass -text "Password"
80 set p [entry .pass -show "*" -textvariable passinit]
90 button .submit -text "Submit" -command submit
92 `, realm, userInit, passInit))
93 cmd := exec.Command(CmdWish)
95 out, err := cmd.Output()
99 lines := strings.Split(string(out), "\n")
101 return "", "", errors.New("invalid output from authorization form")
103 return lines[0], lines[1], nil