github.com/lunarobliq/gophish@v0.8.1-0.20230523153303-93511002234d/gophish.go (about)

     1  package main
     2  
     3  /*
     4  gophish - Open-Source Phishing Framework
     5  
     6  The MIT License (MIT)
     7  
     8  Copyright (c) 2013 Jordan Wright
     9  
    10  Permission is hereby granted, free of charge, to any person obtaining a copy
    11  of this software and associated documentation files (the "Software"), to deal
    12  in the Software without restriction, including without limitation the rights
    13  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    14  copies of the Software, and to permit persons to whom the Software is
    15  furnished to do so, subject to the following conditions:
    16  
    17  The above copyright notice and this permission notice shall be included in
    18  all copies or substantial portions of the Software.
    19  
    20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    21  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    23  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    25  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    26  THE SOFTWARE.
    27  */
    28  import (
    29  	"fmt"
    30  	"io/ioutil"
    31  	"net/http"
    32  	"os"
    33  	"os/signal"
    34  
    35  	"gopkg.in/alecthomas/kingpin.v2"
    36  
    37  	"github.com/gophish/gophish/config"
    38  	"github.com/gophish/gophish/controllers"
    39  	"github.com/gophish/gophish/dialer"
    40  	"github.com/gophish/gophish/imap"
    41  	log "github.com/gophish/gophish/logger"
    42  	"github.com/gophish/gophish/middleware"
    43  	"github.com/gophish/gophish/models"
    44  	"github.com/gophish/gophish/webhook"
    45  )
    46  
    47  const (
    48  	modeAll   string = "all"
    49  	modeAdmin string = "admin"
    50  	modePhish string = "phish"
    51  )
    52  
    53  var (
    54  	configPath    = kingpin.Flag("config", "Location of config.json.").Default("./config.json").String()
    55  	disableMailer = kingpin.Flag("disable-mailer", "Disable the mailer (for use with multi-system deployments)").Bool()
    56  	mode          = kingpin.Flag("mode", fmt.Sprintf("Run the binary in one of the modes (%s, %s or %s)", modeAll, modeAdmin, modePhish)).
    57  			Default("all").Enum(modeAll, modeAdmin, modePhish)
    58  )
    59  
    60  func main() {
    61  	// Load the version
    62  
    63  	version, err := ioutil.ReadFile("./VERSION")
    64  	if err != nil {
    65  		log.Fatal(err)
    66  	}
    67  	kingpin.Version(string(version))
    68  
    69  	// Parse the CLI flags and load the config
    70  	kingpin.CommandLine.HelpFlag.Short('h')
    71  	kingpin.Parse()
    72  
    73  	// Load the config
    74  	conf, err := config.LoadConfig(*configPath)
    75  	// Just warn if a contact address hasn't been configured
    76  	if err != nil {
    77  		log.Fatal(err)
    78  	}
    79  	if conf.ContactAddress == "" {
    80  		log.Warnf("No contact address has been configured.")
    81  		log.Warnf("Please consider adding a contact_address entry in your config.json")
    82  	}
    83  	config.Version = string(version)
    84  
    85  	// Configure our various upstream clients to make sure that we restrict
    86  	// outbound connections as needed.
    87  	dialer.SetAllowedHosts(conf.AdminConf.AllowedInternalHosts)
    88  	webhook.SetTransport(&http.Transport{
    89  		DialContext: dialer.Dialer().DialContext,
    90  	})
    91  
    92  	err = log.Setup(conf.Logging)
    93  	if err != nil {
    94  		log.Fatal(err)
    95  	}
    96  
    97  	// Provide the option to disable the built-in mailer
    98  	// Setup the global variables and settings
    99  	err = models.Setup(conf)
   100  	if err != nil {
   101  		log.Fatal(err)
   102  	}
   103  
   104  	// Unlock any maillogs that may have been locked for processing
   105  	// when Gophish was last shutdown.
   106  	err = models.UnlockAllMailLogs()
   107  	if err != nil {
   108  		log.Fatal(err)
   109  	}
   110  
   111  	// Create our servers
   112  	adminOptions := []controllers.AdminServerOption{}
   113  	if *disableMailer {
   114  		adminOptions = append(adminOptions, controllers.WithWorker(nil))
   115  	}
   116  	adminConfig := conf.AdminConf
   117  	adminServer := controllers.NewAdminServer(adminConfig, adminOptions...)
   118  	middleware.Store.Options.Secure = adminConfig.UseTLS
   119  
   120  	phishConfig := conf.PhishConf
   121  	phishServer := controllers.NewPhishingServer(phishConfig)
   122  
   123  	imapMonitor := imap.NewMonitor()
   124  	if *mode == "admin" || *mode == "all" {
   125  		go adminServer.Start()
   126  		go imapMonitor.Start()
   127  	}
   128  	if *mode == "phish" || *mode == "all" {
   129  		go phishServer.Start()
   130  	}
   131  
   132  	// Handle graceful shutdown
   133  	c := make(chan os.Signal, 1)
   134  	signal.Notify(c, os.Interrupt)
   135  	<-c
   136  	log.Info("CTRL+C Received... Gracefully shutting down servers")
   137  	if *mode == modeAdmin || *mode == modeAll {
   138  		adminServer.Shutdown()
   139  		imapMonitor.Shutdown()
   140  	}
   141  	if *mode == modePhish || *mode == modeAll {
   142  		phishServer.Shutdown()
   143  	}
   144  
   145  }