Creating a Windows graphical configuration tool for rinetd with AutoHotkey

in #development7 years ago

rinetd is the internet "redirection server." It's a very old but good tool that was targeted for Linux but there is a Windows port as well. It's used for forwarding or redirecting TCP traffic.

rinetd is a command line program that can be downloaded from https://www.boutell.com/rinetd/.

To launch it, you have to write down all the rules into a configuration file, say rinetd.conf and then run rinetd -c rinetd.conf to start the process. Making changes to the configuration file would require editing the file and relaunching the process.

To create a graphical configuration tool, we will use AutoHotkey, which as the website says is The ultimate automation scripting language for Windows. More information and download at autohotkey.com.

Here is what we will create:

The tool manages the configuration file and running the rinetd. On startup, it loads the configuration file and starts the process (shows up running in a command prompt window). Pressing Exit will stop the process and close the tool. Changes are made by editing the text area and pressing Save, which will save the config file and relaunch the process. Closing the window will hide rinetd and the tool, which can be restored by the tray icon.

Here's the script, place in the same folder as rinetd:

#SingleInstance force

Menu, Tray, Add, Restore, Restore
Menu, Tray, Icon, shell32.dll, 19
Menu, Tray, default, Restore
Menu, Tray, Click, 2

Gui, Add, Edit, vMainEdit WantTab W500 R25

Gui, Add, Button, x20, `    Save    `
Gui, Add, Button, x+60, `    Exit    `

GoSub FileRead
GoSub CloseRinetd
GoSub RunRinetd
Sleep, 100

Gui, Show,, rinetd

return


Restore:
    GoSub ShowRinetd
    Gui +LastFound
    WinShow
    WinRestore
return


ButtonShow:
ShowRinetd:
    WinShow, ahk_exe rinetd.exe
return


ButtonHide:
HideRinetd:
    WinHide, ahk_exe rinetd.exe
return


FileRead:
    FileRead, MainEdit, rinetd.conf
    if ErrorLevel
    {
        MsgBox Could not open "rinetd.conf".
        return
    }
    GuiControl,, MainEdit, %MainEdit%
return


ButtonSave:
FileSave:
    GoSub CloseRinetd
    Sleep, 100
    IfExist rinetd.conf
    {
        FileDelete rinetd.conf
        if ErrorLevel
        {
            MsgBox The attempt to overwrite "rinetd.conf" failed.
            return
        }
    }
    GuiControlGet, MainEdit
    FileAppend, %MainEdit%, rinetd.conf
    GoSub RunRinetd
return


GuiSize:
    if (A_EventInfo = 1)
    {
        WinHide
        GoSub HideRinetd
        return
    }
return


RunRinetd:
    Run, rinetd -c rinetd.conf
return


CloseRinetd:
    Process, Close, rinetd.exe
return


GuiClose:
    WinHide
    GoSub HideRinetd
return


ButtonExit:
    GoSub CloseRinetd
    ExitApp

 

Follow me! @bitcoiner