Compare commits
No commits in common. 'b371c74e9a419e75afc99b7cd1f832434672b4ad' and '7c4b0eda8b12be9eb63bc282bb92fa36e91b6c84' have entirely different histories.
b371c74e9a
...
7c4b0eda8b
7 changed files with 60 additions and 186 deletions
@ -1,104 +0,0 @@ |
|||||||
package auth |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
"os/exec" |
|
||||||
"regexp" |
|
||||||
"strconv" |
|
||||||
"time" |
|
||||||
|
|
||||||
"github.com/Riyyi/declpac/pkg/log" |
|
||||||
) |
|
||||||
|
|
||||||
var tool string |
|
||||||
var timeout time.Duration = 5 * time.Minute |
|
||||||
var refreshCommand []string = []string{"-n", "true"} |
|
||||||
|
|
||||||
// -----------------------------------------
|
|
||||||
// public
|
|
||||||
|
|
||||||
func Command(name string, args ...string) *exec.Cmd { |
|
||||||
if tool == "" { |
|
||||||
return log.Command(name, args...) |
|
||||||
} |
|
||||||
args = append([]string{name}, args...) |
|
||||||
return log.Command(tool, args...) |
|
||||||
} |
|
||||||
|
|
||||||
func Run() { |
|
||||||
exec.Command(tool, refreshCommand...).Run() |
|
||||||
} |
|
||||||
|
|
||||||
func Start() error { |
|
||||||
err := detect() |
|
||||||
if err != nil { |
|
||||||
return err |
|
||||||
} |
|
||||||
|
|
||||||
// Automatically refresh privilege elevation to prevent user prompts
|
|
||||||
go func() { |
|
||||||
for { |
|
||||||
Run() |
|
||||||
time.Sleep(timeout) |
|
||||||
} |
|
||||||
}() |
|
||||||
|
|
||||||
return nil |
|
||||||
} |
|
||||||
|
|
||||||
// -----------------------------------------
|
|
||||||
// private
|
|
||||||
|
|
||||||
func detect() error { |
|
||||||
tool = getTool() |
|
||||||
if tool == "" { |
|
||||||
return fmt.Errorf("no privilege elevation tool detected in PATH") |
|
||||||
} |
|
||||||
|
|
||||||
parseTimeout() |
|
||||||
// We have to be a little faster than the actual timeout
|
|
||||||
timeout -= 30 * time.Second |
|
||||||
|
|
||||||
return nil |
|
||||||
} |
|
||||||
|
|
||||||
func execLookPath(name string) string { |
|
||||||
path, err := exec.LookPath(name) |
|
||||||
if err != nil { |
|
||||||
return "" |
|
||||||
} |
|
||||||
return path |
|
||||||
} |
|
||||||
|
|
||||||
func getTool() string { |
|
||||||
sudo := execLookPath("sudo") |
|
||||||
doas := execLookPath("doas") |
|
||||||
|
|
||||||
if sudo != "" { |
|
||||||
return "sudo" |
|
||||||
} |
|
||||||
if doas != "" { |
|
||||||
return "doas" |
|
||||||
} |
|
||||||
|
|
||||||
return "" |
|
||||||
} |
|
||||||
|
|
||||||
func parseTimeout() { |
|
||||||
switch tool { |
|
||||||
case "sudo": |
|
||||||
out, err := exec.Command("sudo", "sudo", "-V").CombinedOutput() |
|
||||||
if err != nil { |
|
||||||
return |
|
||||||
} |
|
||||||
re := regexp.MustCompile(`Authentication timestamp timeout: (\d+)\..*`) |
|
||||||
matches := re.FindStringSubmatch(string(out)) |
|
||||||
if len(matches) == 2 { |
|
||||||
if minutes, err := strconv.Atoi(matches[1]); err == nil { |
|
||||||
timeout = time.Duration(minutes) * time.Minute |
|
||||||
} |
|
||||||
} |
|
||||||
case "doas": |
|
||||||
exec.Command("doas", "true").Run() |
|
||||||
} |
|
||||||
} |
|
||||||
@ -1,21 +0,0 @@ |
|||||||
package lib |
|
||||||
|
|
||||||
import ( |
|
||||||
"os" |
|
||||||
"path/filepath" |
|
||||||
"strings" |
|
||||||
) |
|
||||||
|
|
||||||
// -----------------------------------------
|
|
||||||
// public
|
|
||||||
|
|
||||||
func ExpandPath(path string) string { |
|
||||||
if strings.HasPrefix(path, "~/") { |
|
||||||
home, err := os.UserHomeDir() |
|
||||||
if err != nil { |
|
||||||
return path |
|
||||||
} |
|
||||||
return filepath.Join(home, path[2:]) |
|
||||||
} |
|
||||||
return path |
|
||||||
} |
|
||||||
Loading…
Reference in new issue