Add capability to run the binary in a mode (#1817)

These commit includes changes to start the server as one of admin (also
IMAP) or phish server. Before this change the servers used to run in
monolith this change will decouple the two core component i.e. admin
and phish server so that they can be run independently.

This will help where admin and phish server are required to run
saperately e.g. phish server runs in a DMZ.

The available modes are `admin`, `phish` and `all`. Running the binary
in the `admin` mode will start the admin and IMAP server, while running
the binary in the `phish` mode will start the phish server. `all` mode,
which is also the default mode will start admin, IMAP and phish servers.
e.g. `go run gophish.go --mode admin`
pull/1557/merge
Prasoon Dwivedi 2020-04-27 09:21:39 +05:30 committed by GitHub
parent a0e8c4a369
commit 26e82cb2e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 7 deletions

View File

@ -26,6 +26,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
*/ */
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/signal" "os/signal"
@ -40,9 +41,17 @@ import (
"github.com/gophish/gophish/models" "github.com/gophish/gophish/models"
) )
const (
modeAll string = "all"
modeAdmin string = "admin"
modePhish string = "phish"
)
var ( var (
configPath = kingpin.Flag("config", "Location of config.json.").Default("./config.json").String() configPath = kingpin.Flag("config", "Location of config.json.").Default("./config.json").String()
disableMailer = kingpin.Flag("disable-mailer", "Disable the mailer (for use with multi-system deployments)").Bool() disableMailer = kingpin.Flag("disable-mailer", "Disable the mailer (for use with multi-system deployments)").Bool()
mode = kingpin.Flag("mode", fmt.Sprintf("Run the binary in one of the modes (%s, %s or %s)", modeAll, modeAdmin, modePhish)).
Default("all").Enum(modeAll, modeAdmin, modePhish)
) )
func main() { func main() {
@ -102,18 +111,25 @@ func main() {
phishServer := controllers.NewPhishingServer(phishConfig) phishServer := controllers.NewPhishingServer(phishConfig)
imapMonitor := imap.NewMonitor() imapMonitor := imap.NewMonitor()
if *mode == "admin" || *mode == "all" {
go adminServer.Start() go adminServer.Start()
go phishServer.Start() go imapMonitor.Start()
go imapMonitor.Start() }
if *mode == "phish" || *mode == "all" {
go phishServer.Start()
}
// Handle graceful shutdown // Handle graceful shutdown
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt) signal.Notify(c, os.Interrupt)
<-c <-c
log.Info("CTRL+C Received... Gracefully shutting down servers") log.Info("CTRL+C Received... Gracefully shutting down servers")
adminServer.Shutdown() if *mode == modeAdmin || *mode == modeAll {
phishServer.Shutdown() adminServer.Shutdown()
imapMonitor.Shutdown() imapMonitor.Shutdown()
}
if *mode == modePhish || *mode == modeAll {
phishServer.Shutdown()
}
} }