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
+11.3k Golang : Simple file scaning and remove virus example
+14.3k Golang : Rename directory
+7k Golang : Calculate how many weeks left to go in a given year
+25.4k Golang : How to read integer value from standard input ?
+33.6k Golang : Create x509 certificate, private and public keys
+11.5k Golang : GTK Input dialog box examples
+9.1k Golang : Terminate-stay-resident or daemonize your program?
+26.8k Golang : Find files by name - cross platform example
+9.3k Golang : Read file with ioutil
+23.4k Golang : Fix type interface{} has no field or no methods and type assertions example
+17.3k Golang : [json: cannot unmarshal object into Go value of type]