github.com/edermi/gophish_mods@v0.7.0/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  	"compress/gzip"
    30  	"context"
    31  	"io/ioutil"
    32  	"net/http"
    33  	"os"
    34  	"sync"
    35  
    36  	"gopkg.in/alecthomas/kingpin.v2"
    37  
    38  	"github.com/NYTimes/gziphandler"
    39  	"github.com/gophish/gophish/auth"
    40  	"github.com/gophish/gophish/config"
    41  	"github.com/gophish/gophish/controllers"
    42  	log "github.com/gophish/gophish/logger"
    43  	"github.com/gophish/gophish/mailer"
    44  	"github.com/gophish/gophish/models"
    45  	"github.com/gophish/gophish/util"
    46  	"github.com/gorilla/handlers"
    47  )
    48  
    49  var (
    50  	configPath    = kingpin.Flag("config", "Location of config.json.").Default("./config.json").String()
    51  	disableMailer = kingpin.Flag("disable-mailer", "Disable the mailer (for use with multi-system deployments)").Bool()
    52  )
    53  
    54  func main() {
    55  	// Load the version
    56  
    57  	version, err := ioutil.ReadFile("./VERSION")
    58  	if err != nil {
    59  		log.Fatal(err)
    60  	}
    61  	kingpin.Version(string(version))
    62  
    63  	// Parse the CLI flags and load the config
    64  	kingpin.CommandLine.HelpFlag.Short('h')
    65  	kingpin.Parse()
    66  
    67  	// Load the config
    68  	config.LoadConfig(*configPath)
    69  	config.Version = string(version)
    70  
    71  	ctx, cancel := context.WithCancel(context.Background())
    72  	defer cancel()
    73  
    74  	// Provide the option to disable the built-in mailer
    75  	if !*disableMailer {
    76  		go mailer.Mailer.Start(ctx)
    77  	}
    78  	// Setup the global variables and settings
    79  	err = models.Setup()
    80  	if err != nil {
    81  		log.Fatal(err)
    82  	}
    83  	// Unlock any maillogs that may have been locked for processing
    84  	// when Gophish was last shutdown.
    85  	err = models.UnlockAllMailLogs()
    86  	if err != nil {
    87  		log.Fatal(err)
    88  	}
    89  	wg := &sync.WaitGroup{}
    90  	wg.Add(1)
    91  	// Start the web servers
    92  	go func() {
    93  		defer wg.Done()
    94  		gzipWrapper, _ := gziphandler.NewGzipLevelHandler(gzip.BestCompression)
    95  		adminHandler := gzipWrapper(controllers.CreateAdminRouter())
    96  		auth.Store.Options.Secure = config.Conf.AdminConf.UseTLS
    97  		if config.Conf.AdminConf.UseTLS { // use TLS for Admin web server if available
    98  			err := util.CheckAndCreateSSL(config.Conf.AdminConf.CertPath, config.Conf.AdminConf.KeyPath)
    99  			if err != nil {
   100  				log.Fatal(err)
   101  			}
   102  			log.Infof("Starting admin server at https://%s", config.Conf.AdminConf.ListenURL)
   103  			log.Info(http.ListenAndServeTLS(config.Conf.AdminConf.ListenURL, config.Conf.AdminConf.CertPath, config.Conf.AdminConf.KeyPath,
   104  				handlers.CombinedLoggingHandler(log.Writer(), adminHandler)))
   105  		} else {
   106  			log.Infof("Starting admin server at http://%s", config.Conf.AdminConf.ListenURL)
   107  			log.Info(http.ListenAndServe(config.Conf.AdminConf.ListenURL, handlers.CombinedLoggingHandler(os.Stdout, adminHandler)))
   108  		}
   109  	}()
   110  	wg.Add(1)
   111  	go func() {
   112  		defer wg.Done()
   113  		phishHandler := gziphandler.GzipHandler(controllers.CreatePhishingRouter())
   114  		if config.Conf.PhishConf.UseTLS { // use TLS for Phish web server if available
   115  			log.Infof("Starting phishing server at https://%s", config.Conf.PhishConf.ListenURL)
   116  			log.Info(http.ListenAndServeTLS(config.Conf.PhishConf.ListenURL, config.Conf.PhishConf.CertPath, config.Conf.PhishConf.KeyPath,
   117  				handlers.CombinedLoggingHandler(log.Writer(), phishHandler)))
   118  		} else {
   119  			log.Infof("Starting phishing server at http://%s", config.Conf.PhishConf.ListenURL)
   120  			log.Fatal(http.ListenAndServe(config.Conf.PhishConf.ListenURL, handlers.CombinedLoggingHandler(os.Stdout, phishHandler)))
   121  		}
   122  	}()
   123  	wg.Wait()
   124  }