Golang : How to stop user from directly running an executable file?
There are times when you need to prevent the user from directly executing an executable file, especially if the executable/binary file is exposed as an icon that the user can see and click while using a graphical user interface(GUI) application launcher such as those found in Windows, KDE, GNOME and Mac OS X.
Wait?! Why do you want to prevent the user from launching your application directly?
The reason is that you want to the user to launch your application updater (a program to update your program!) first before launching the actual program. For example, after installing your program, your user will have an icon of your program appear on the desktop ... you want the user to click on that instead of the actual main program.
The icon is linked to the another executable file ... i.e the file is actually a program where it will first check if your current program has expired and need to update to the latest version.
First, let's create a program that refuses to execute directly. We will use the Golang-QT binding at https://github.com/therecipe/qt for creating this program so that the Graphical User Interface(GUI) can work under Linux/Unix/Windows.
Please follow up the setup instruction at https://github.com/therecipe/qt to setup Qt.
prevent.go
package main
import (
"fmt"
"github.com/therecipe/qt/widgets"
"os"
)
var (
mainApp *widgets.QApplication
imageFileName string
)
func main() {
mainApp = widgets.NewQApplication(len(os.Args), os.Args)
if (len(os.Args) <= 1) || os.Args[1] != "password" {
widgets.QMessageBox_Critical(nil, "OK", "This application was unable to start properly. Please run MyProgram.exe instead. Click OK to close.", widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
// need to handle those user that try to launch the program from command line
fmt.Println("This application was unable to start properly. Please run MyProgram.exe")
} else {
fmt.Println("This application was able to start properly.")
widgets.QMessageBox_Information(nil, "OK", "This application was able to start properly. Click OK to close.", widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
}
mainApp.Quit()
}
This program will not be able to launch directly by the user.
but if supplied with the right password and number of argument...the program stills can be launched.
What we want is to create another program that will launch the main application. The following program that will execute the first program, but we omitted out the update part...well, simply because this is just a tutorial.
This is the program that you want your user to click on.
Here you go!
launchprevent.go
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
)
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage : %s <main application> \n", os.Args[0])
os.Exit(0)
}
mainApp := os.Args[1]
/* this is where you want to check for update such as connecting to your
server to query the latest version or bug fixes, etc */
var cmd *exec.Cmd
// For Linux/Mac, we have to pad "./" before the main application executable filename
// For Windows, usually executable has .exe extension
if runtime.GOOS != "windows" {
cmd = exec.Command("./"+mainApp, "password")
} else {
cmd = exec.Command(mainApp, "password")
}
err := cmd.Start()
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
fmt.Println("Launched main application and the process ID is : ", cmd.Process.Pid)
os.Exit(0)
}
Launcher/updater program successfully launched the main application:
While you are here. Do check out the tutorial on how to force your Golang program to run with root permission.
References:
https://www.socketloop.com/tutorials/golang-daemonizing-a-simple-web-server-process-example
http://doc.qt.io/qt-4.8/qmessagebox.html
https://www.socketloop.com/tutorials/golang-qt-image-viewer-example
See also : Golang : Force your program to run with root permissions
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.1k Gogland : Datasource explorer
+11.7k Golang : Simple client-server HMAC authentication without SSL example
+5.7k Javascript : Get operating system and browser information
+11.1k Golang : Concurrency and goroutine example
+12.9k Golang : Generate Code128 barcode
+11k Golang : How to flush a channel before the end of program?
+4.3k Mac OSX : Get disk partitions' size, type and name
+5.9k Golang : Scan forex opportunities by Bollinger bands
+12.1k Golang : Arithmetic operation with numerical slices or arrays example
+10.7k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+8.8k Golang : How to check if a string with spaces in between is numeric?
+13.1k Golang : Strings comparison