]> Sergey Matveev's repositories - tofuproxy.git/commitdiff
External netrc module
authorSergey Matveev <stargrave@stargrave.org>
Sat, 12 Aug 2023 12:06:45 +0000 (15:06 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sat, 12 Aug 2023 12:10:44 +0000 (15:10 +0300)
go.mod
go.sum
httpauth.go
netrc.go [deleted file]

diff --git a/go.mod b/go.mod
index d29cc78ba5c91841425bd803ee626b37851f7d27..49fdb6f7b85d71d158fb7c621d3825a022200b0f 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,7 @@ go 1.20
 require (
        github.com/dustin/go-humanize v1.0.1
        github.com/miekg/dns v1.1.55
+       go.cypherpunks.ru/netrc v0.1.0
        go.cypherpunks.ru/tai64n/v2 v2.0.1
        go.cypherpunks.ru/ucspi v0.0.0-20230808200029-ef2a022b68db
 )
diff --git a/go.sum b/go.sum
index 8cd4f096197b678cd0a8b29749b47cf660172d73..241c3dbea513bb86b8cb0a69aa4b8727d75bffda 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -2,6 +2,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
 github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
 github.com/miekg/dns v1.1.55 h1:GoQ4hpsj0nFLYe+bWiCToyrBEJXkQfOOIvFGFy0lEgo=
 github.com/miekg/dns v1.1.55/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY=
+go.cypherpunks.ru/netrc v0.1.0 h1:dzaSh4lgpPXvLzUME/hTSegg+Uw1aPEmC79fxbPLFGk=
+go.cypherpunks.ru/netrc v0.1.0/go.mod h1:ZmQaL9ENEII++WQooelccr33XZcBhKd8VgtcICf4SIE=
 go.cypherpunks.ru/tai64n/v2 v2.0.1 h1:AnwUUgi0EixZcr6nzjUaOy2DFInFwbNRkBkgon4oJfU=
 go.cypherpunks.ru/tai64n/v2 v2.0.1/go.mod h1:Jlva0YVJBpaRFAJ4jfY4BXVPuav0GMyGXzYPSKCCCL0=
 go.cypherpunks.ru/ucspi v0.0.0-20230808200029-ef2a022b68db h1:Iwf6evW+CPPYMC0n6bikgreRLifuZQJaNNv24AIx0f4=
index 92eff6e699dc81fa0261438a804ab713525d8499..96f23d48a5b31bebae0b4defa4eecc19f6c9185a 100644 (file)
@@ -25,12 +25,13 @@ import (
        "os/exec"
        "strings"
 
+       "go.cypherpunks.ru/netrc"
        ttls "go.stargrave.org/tofuproxy/tls"
 )
 
 func authDialog(host, realm string) (string, string, error) {
        var b bytes.Buffer
-       userInit, passInit := findInNetrc(host)
+       userInit, passInit := netrc.Find(host)
        fmt.Fprintf(&b, `
 tk_setPalette grey
 wm title . "Unauthorized: %s"
diff --git a/netrc.go b/netrc.go
deleted file mode 100644 (file)
index bdc61bb..0000000
--- a/netrc.go
+++ /dev/null
@@ -1,68 +0,0 @@
-// Nearly all code is taken from src/cmd/go/internal/auth/netrc.go
-
-package tofuproxy
-
-import (
-       "errors"
-       "io/fs"
-       "log"
-       "os"
-       "path/filepath"
-       "strings"
-)
-
-func findInNetrc(host string) (string, string) {
-       netrcPath, ok := os.LookupEnv("NETRC")
-       if !ok {
-               homeDir, err := os.UserHomeDir()
-               if err != nil {
-                       log.Fatalln(err)
-               }
-               netrcPath = filepath.Join(homeDir, ".netrc")
-       }
-       data, err := os.ReadFile(netrcPath)
-       if err != nil {
-               if errors.Is(err, fs.ErrNotExist) {
-                       return "", ""
-               }
-               log.Fatalln(err)
-       }
-       inMacro := false
-       var machine, login, password string
-       for _, line := range strings.Split(string(data), "\n") {
-               if inMacro {
-                       if line == "" {
-                               inMacro = false
-                       }
-                       continue
-               }
-               fields := strings.Fields(line)
-               i := 0
-               for ; i < len(fields)-1; i += 2 {
-                       switch fields[i] {
-                       case "machine":
-                               machine = fields[i+1]
-                               login = ""
-                               password = ""
-                       case "default":
-                               break
-                       case "login":
-                               login = fields[i+1]
-                       case "password":
-                               password = fields[i+1]
-                       case "macdef":
-                               inMacro = true
-                       }
-                       if machine != "" && login != "" && password != "" {
-                               if machine == host {
-                                       return login, password
-                               }
-                               machine, login, password = "", "", ""
-                       }
-               }
-               if i < len(fields) && fields[i] == "default" {
-                       break
-               }
-       }
-       return "", ""
-}