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
+21.5k Golang : Use TLS version 1.2 and enforce server security configuration over client
+4.9k Golang : Issue HTTP commands to server and port example
+31.1k Golang : How to convert(cast) string to IP address?
+18.5k Golang : Padding data for encryption and un-padding data for decryption
+12.4k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+11.9k Golang : md5 hash of a string
+47.2k Golang : Convert int to byte array([]byte)
+27.8k Golang : Connect to database (MySQL/MariaDB) server
+4.8k Golang : Check if a word is countable or not
+15.6k Golang : Read a file line by line
+6.9k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+10.2k Golang : Allow Cross-Origin Resource Sharing request