// ALR - Any Linux Repository // Copyright (C) 2025 Евгений Храмов // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . package build import ( "log/slog" "net/rpc" "os" "os/exec" "syscall" "github.com/hashicorp/go-plugin" "gitea.plemya-x.ru/Plemya-x/ALR/internal/logger" "gitea.plemya-x.ru/Plemya-x/ALR/internal/utils" ) type InstallerPlugin struct { Impl InstallerExecutor } type InstallerRPC struct { client *rpc.Client } type InstallerRPCServer struct { Impl InstallerExecutor } func (r *InstallerRPC) InstallLocal(paths []string) error { return r.client.Call("Plugin.InstallLocal", paths, nil) } func (s *InstallerRPCServer) InstallLocal(paths []string, reply *struct{}) error { slog.Warn("install", "paths", paths) return s.Impl.InstallLocal(paths) } func (r *InstallerRPC) Install(pkgs []string) error { return r.client.Call("Plugin.Install", pkgs, nil) } func (s *InstallerRPCServer) Install(pkgs []string, reply *struct{}) error { slog.Debug("install", "pkgs", pkgs) return s.Impl.Install(pkgs) } func (r *InstallerRPC) RemoveAlreadyInstalled(paths []string) ([]string, error) { err := r.client.Call("Plugin.RemoveAlreadyInstalled", paths, nil) return nil, err } func (s *InstallerRPCServer) RemoveAlreadyInstalled(pkgs []string, res *[]string) error { vars, err := s.Impl.RemoveAlreadyInstalled(pkgs) if err != nil { return err } *res = vars return nil } func (p *InstallerPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) { return &InstallerRPC{client: c}, nil } func (p *InstallerPlugin) Server(*plugin.MuxBroker) (interface{}, error) { return &InstallerRPCServer{Impl: p.Impl}, nil } func GetSafeInstaller() (InstallerExecutor, error) { executable, err := os.Executable() if err != nil { return nil, err } cmd := exec.Command(executable, "_internal-installer") cmd.Env = append(os.Environ(), "HOME=/var/cache/alr", "LOGNAME=alr", "USER=alr", "PATH=/usr/bin:/bin:/usr/local/bin", "ALR_LOG_LEVEL=DEBUG", "XDG_SESSION_CLASS=user", ) uid, gid, err := utils.GetUidGidAlrUser() if err != nil { return nil, err } cmd.SysProcAttr = &syscall.SysProcAttr{ Credential: &syscall.Credential{ Uid: uint32(uid), Gid: uint32(gid), }, } client := plugin.NewClient(&plugin.ClientConfig{ HandshakeConfig: HandshakeConfig, Plugins: pluginMap, Cmd: cmd, Logger: logger.GetHCLoggerAdapter(), SkipHostEnv: true, UnixSocketConfig: &plugin.UnixSocketConfig{ Group: "alr", }, SyncStderr: os.Stderr, }) rpcClient, err := client.Client() if err != nil { slog.Info("1") return nil, err } raw1, err := rpcClient.Dispense("installer") if err != nil { return nil, err } return raw1.(InstallerExecutor), nil }