proxy

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ProxyAddCommand = &cobra.Command{
	Use:   "add",
	Short: "Add a new proxy",
	Long:  "Command which allows to create new proxy server to be used in the PAC file",
	Run: func(cmd *cobra.Command, args []string) {
		configuration, err := config.Initialize()
		if err != nil {
			logger.Fatalf("proxy:add", "failed to initialize configuration: %s", err.Error())
		}

		db := database.NewDatabase(
			storage.NewStorage(configuration.GetDatabase().GetPath(), 0755),
			configuration.GetDatabase().GetName(),
		)

		if err := db.Start(); err != nil {
			logger.Fatalf("proxy:add", "failed to start database: %s", err.Error())
		}
		defer db.Stop()

		proxyType := prompt.Select("Proxy Type", "Selected proxy type is: ", []string{"HTTP", "SOCKS4", "SOCKS5"})
		proxyHost := prompt.Text("Proxy Host")
		proxyPortString := prompt.Text("Proxy Port")

		proxyPort, err := strconv.Atoi(proxyPortString)
		if err != nil {
			logger.Fatalf("proxy:add", "failed to convert proxy port to integer: %s", err.Error())
		}

		if db.ProxyCreate(proxyType, proxyHost, proxyPort) {
			logger.Infof("proxy:add", "proxy '%s %s:%d' added successfully", proxyType, proxyHost, proxyPort)

			pac.NewPac(
				storage.NewStorage(configuration.GetPac().GetPath(), 0755),
				configuration.GetPac().GetName(),
			).Generate(db.ProxyList(), db.DomainList())
		} else {
			logger.Warnf("proxy:add", "failed to add proxy '%s %s:%d'", proxyType, proxyHost, proxyPort)
		}
	},
}
View Source
var ProxyAuthCommand = &cobra.Command{
	Use:   "auth",
	Short: "Proxy Authentication Management",
	Long:  "Command which allows to manage proxy authentication settings",
	Run: func(cmd *cobra.Command, args []string) {
		if err := cmd.Help(); err != nil {
			logger.Fatalf("cmd:proxy", "error while executing help command: %s", err.Error())
		}
	},
}
View Source
var ProxyAuthDisableCommand = &cobra.Command{
	Use:   "disable",
	Short: "Disable the authentication",
	Long:  "Command which allows to disable the authentication for the proxy server",
	Run: func(cmd *cobra.Command, args []string) {
		configuration, err := config.Initialize()
		if err != nil {
			logger.Fatalf("proxy:auth:disable", "failed to initialize configuration: %s", err.Error())
		}

		db := database.NewDatabase(
			storage.NewStorage(configuration.GetDatabase().GetPath(), 0755),
			configuration.GetDatabase().GetName(),
		)

		if err := db.Start(); err != nil {
			logger.Fatalf("proxy:auth:disable", "failed to start database: %s", err.Error())
		}
		defer db.Stop()

		proxyHost := prompt.Text("Proxy Host")

		proxyHostsRaw := db.ProxyFindByHost(proxyHost)

		if len(proxyHostsRaw) == 0 {
			logger.Fatalf("proxy:auth:disable", "no proxy found for host '%s'", proxyHost)
		}

		proxyHosts := make([]string, len(proxyHostsRaw))
		for i, proxy := range proxyHostsRaw {
			proxyHosts[i] = fmt.Sprintf(
				"%s %s:%d",
				proxy.Type,
				proxy.Host,
				proxy.Port,
			)
		}

		disableAuth := prompt.Select("Proxy to disable authentication", "Selected proxy to disable authentication is: ", proxyHosts)

		parts := strings.Split(disableAuth, " ")
		proxyType := parts[0]
		parts = strings.Split(parts[1], ":")
		proxyHost = parts[0]

		proxyPort, err := strconv.Atoi(parts[1])
		if err != nil {
			logger.Fatalf("proxy:auth:disable", "failed to convert port to integer: %s", err.Error())
		}

		ensure := prompt.Confirm("Are you sure you want to disable the authentication for the proxy server?")
		if ensure {
			if db.ProxyDisableAuthentication(proxyType, proxyHost, proxyPort) {
				logger.Infof("proxy:auth:disable", "authentication disabled for proxy server '%s'", disableAuth)
			} else {
				logger.Fatalf("proxy:auth:disable", "failed to disable authentication for proxy server '%s'", disableAuth)
			}
		}
	},
}
View Source
var ProxyAuthEnableCommand = &cobra.Command{
	Use:   "enable",
	Short: "Enable the authentication for the proxy",
	Long:  "Command which allows to enable the authentication for the proxy",
	Run: func(cmd *cobra.Command, args []string) {
		configuration, err := config.Initialize()
		if err != nil {
			logger.Fatalf("proxy:auth:enable", "failed to initialize configuration: %s", err.Error())
		}

		db := database.NewDatabase(
			storage.NewStorage(configuration.GetDatabase().GetPath(), 0755),
			configuration.GetDatabase().GetName(),
		)

		if err := db.Start(); err != nil {
			logger.Fatalf("proxy:auth:enable", "failed to start database: %s", err.Error())
		}
		defer db.Stop()

		proxyHost := prompt.Text("Proxy Host")

		proxyHostsRaw := db.ProxyFindByHost(proxyHost)

		if len(proxyHostsRaw) == 0 {
			logger.Fatalf("proxy:auth:enable", "no proxy found for host '%s'", proxyHost)
		}

		proxyHosts := make([]string, len(proxyHostsRaw))
		for i, proxy := range proxyHostsRaw {
			proxyHosts[i] = fmt.Sprintf(
				"%s %s:%d",
				proxy.Type,
				proxy.Host,
				proxy.Port,
			)
		}

		enableAuth := prompt.Select("Proxy to enable authentication", "Selected proxy to enable authentication is: ", proxyHosts)

		parts := strings.Split(enableAuth, " ")
		proxyType := parts[0]
		parts = strings.Split(parts[1], ":")
		proxyHost = parts[0]

		proxyPort, err := strconv.Atoi(parts[1])
		if err != nil {
			logger.Fatalf("proxy:auth:enable", "failed to convert port to integer: %s", err.Error())
		}

		username := prompt.Text("Username")
		password := prompt.Password("Password")

		if db.ProxyEnableAuthentication(proxyType, proxyHost, proxyPort, username, password) {
			logger.Infof("proxy:auth:enable", "authentication enabled for proxy '%s %s:%d'", proxyType, proxyHost, proxyPort)
		} else {
			logger.Fatalf("proxy:auth:enable", "failed to enable authentication for proxy '%s %s:%d'", proxyType, proxyHost, proxyPort)
		}
	},
}
View Source
var ProxyCommand = &cobra.Command{
	Use:   "proxy",
	Short: "Proxy commands",
	Long:  "Displays commands which are related to proxy servers management",
	Run: func(cmd *cobra.Command, args []string) {
		if err := cmd.Help(); err != nil {
			logger.Fatalf("cmd:proxy", "error while executing help command: %s", err.Error())
		}
	},
}
View Source
var ProxyDeleteCommand = &cobra.Command{
	Use:   "delete",
	Short: "Delete a proxy",
	Long:  "Command which allows to delete a proxy server",
	Run: func(cmd *cobra.Command, args []string) {
		configuration, err := config.Initialize()
		if err != nil {
			logger.Fatalf("proxy:delete", "failed to initialize configuration: %s", err.Error())
		}

		db := database.NewDatabase(
			storage.NewStorage(configuration.GetDatabase().GetPath(), 0755),
			configuration.GetDatabase().GetName(),
		)

		if err := db.Start(); err != nil {
			logger.Fatalf("proxy:delete", "failed to start database: %s", err.Error())
		}
		defer db.Stop()

		proxyHost := prompt.Text("Proxy Host")

		proxyHostsRaw := db.ProxyFindByHost(proxyHost)

		if len(proxyHostsRaw) == 0 {
			logger.Fatalf("proxy:delete", "no proxy found for host '%s'", proxyHost)
		}

		proxyHosts := make([]string, len(proxyHostsRaw))
		for i, proxy := range proxyHostsRaw {
			proxyHosts[i] = fmt.Sprintf(
				"%s %s:%d",
				proxy.Type,
				proxy.Host,
				proxy.Port,
			)
		}

		deleteProxy := prompt.Select("Proxy to delete", "Selected proxy to delete is: ", proxyHosts)

		parts := strings.Split(deleteProxy, " ")
		proxyType := parts[0]
		parts = strings.Split(parts[1], ":")
		proxyHost = parts[0]
		proxyPort, err := strconv.Atoi(parts[1])

		if err != nil {
			logger.Fatalf("proxy:delete", "failed to convert proxy port to integer: %s", err.Error())
		}

		if db.ProxyDelete(proxyType, proxyHost, proxyPort) {
			logger.Infof("proxy:delete", "proxy '%s %s:%d' deleted successfully", proxyType, proxyHost, proxyPort)

			pac.NewPac(
				storage.NewStorage(configuration.GetPac().GetPath(), 0755),
				configuration.GetPac().GetName(),
			).Generate(db.ProxyList(), db.DomainList())
		} else {
			logger.Warnf("proxy:delete", "failed to delete proxy '%s %s:%d'", proxyType, proxyHost, proxyPort)
		}
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL