arblant
firefox-vpn-client / core / cmd / firefox-vpn-tun / commands.go
commands.go72 lines · 1.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
//go:build windows || linux || darwin

package main

import (
	"errors"
	"flag"
	"os"

	"github.com/armanlife/firefox-vpn-client/core/internal/tunservice"
)

// run runs a command. The init system (the service manager, systemd,
// launchd) starts "service"; the elevated copy of an install or uninstall
// runs "install-as-admin" or "uninstall-as-admin".
func run(command string, args []string) int {
	switch command {
	case "install", "uninstall":
		fs := flag.NewFlagSet(command, flag.ContinueOnError)
		prompt := fs.String("prompt", "", "the reason the administrator prompt shows (macOS)")
		if fs.Parse(args) != nil {
			return exitUsage
		}
		var err error
		if command == "install" {
			err = tunservice.InstallForUser(*prompt)
		} else {
			err = tunservice.UninstallForUser(*prompt)
		}
		if errors.Is(err, tunservice.ErrCancelled) {
			return exitCancelled
		}
		if err != nil {
			return fail(err)
		}
	case "update":
		self, err := os.Executable()
		if err == nil {
			err = tunservice.UpdateService(self)
		}
		if err != nil {
			return fail(err)
		}
	case "service":
		if err := tunservice.RunService(); err != nil {
			return fail(err)
		}
	case "install-as-admin", "uninstall-as-admin":
		fs := flag.NewFlagSet(command, flag.ContinueOnError)
		user := fs.String("user", "", "the user who may use the service: a SID on Windows, a user id elsewhere")
		report := fs.String("report", "", "where to write why it failed")
		if fs.Parse(args) != nil {
			return exitUsage
		}
		var err error
		if command == "install-as-admin" {
			if *user == "" {
				return exitUsage
			}
			err = tunservice.Install(*user)
		} else {
			err = tunservice.Uninstall()
		}
		if err != nil {
			tunservice.WriteReport(*report, err)
			return fail(err)
		}
	default:
		return runPlatform(command, args)
	}
	return 0
}