Exemple #1
0
func (m *Armor) RunWithRPC(g *gin.Engine, rpcInit func(*grpc.Server)) {
	runOn := fmt.Sprintf("%s:%s", m.Config.GetString("interface"), m.Config.GetString("port"))
	banner := `

      A R M O R
    .---.___.---.
    |     |     |   Running .....: PRODUCT_NAME
    |_____|_____|   Host/Iface ..: HOST/INTERFACE
      |___|___|
      |___|___|
     `
	// XXX replace with tmpl
	banner = strings.Replace(
		strings.Replace(
			strings.Replace(banner, "HOST", m.Config.Hostname, -1), "PRODUCT_NAME", m.Config.Product, -1),
		"INTERFACE", runOn, -1)

	log.Print(banner)
	log.Printf("-> Environment: %v", m.Config.Environment)
	log.Printf("-> Product: %v", m.Config.Product)
	log.Printf("-> Host/Interface: %v/%v", m.Config.Hostname, runOn)
	log.Printf("-> Config: %v", m.Config.All())

	if rpcInit != nil {
		rpc := newRPC(m.Config)
		rpc.Run(rpcInit)
	}
	g.Run(runOn)
}
Exemple #2
0
func main() {
	flag.Parse()
	if *logToFile {
		logWriteTo, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
		if err != nil {
			log.Printf("error opening to log file: %v", err)
		}
		if logWriteTo != nil {
			log.SetOutput(logWriteTo)
		}
	}
	gin.SetMode(*ginMode)
	var route *gin.Engine
	if *useGinLogger {
		route = gin.Default()
	} else {
		route = gin.New()
		route.Use(logger)
	}

	route.GET("/", func(ctx *gin.Context) {
		ctx.JSON(200, gin.H{"message": "Its an entry point :)"})
	})

	route.GET("/token", func(ctx *gin.Context) {
		tokenString := auth.GetToken(secretpassword)
		ctx.JSON(200, gin.H{"token": tokenString})
	})
	route.GET("/dbaccessor", func(ctx *gin.Context) {
		ctx.JSON(200, getFakeDbData())
	})
	route.Use(auth.Auth(secretpassword))
	route.POST("/auth", func(ctx *gin.Context) {
		ak := ctx.Request.FormValue("authkey")
		if ak == "" {
			ctx.JSON(401, "No auth key")
		} else if !auth.VerifyAuthKey(ak) {
			ctx.JSON(401, "Wrong key")
		} else {
			ctx.Redirect(http.StatusFound, "/user")
		}
	})
	route.GET("/user", func(ctx *gin.Context) {
		key := ctx.MustGet("authKey")
		udetails := dbAccessor(key.(string))
		ctx.JSON(200, gin.H{"user": udetails})
	})
	route.GET("/user/:id", func(ctx *gin.Context) {
		id := ctx.Params.ByName("id")
		ctx.JSON(200, find(id))
	})

	if err := route.Run(fmt.Sprintf(":%d", *port)); err != nil {
		logFatal("Http not running: ", err)
	} else {
		logPrintln("shutting down")
	}
}
Exemple #3
0
func main() {
	var engine *gin.Engine = gin.New()
	engine.Use(gin.Logger())
	engine.Use(gin.Recovery())
	engine.GET("/:name", func(c *gin.Context) {
		c.String(http.StatusOK, "Hello %s\n", c.Param("name"))
	})
	engine.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Index\n")
	})
	engine.Run(":8080")
}
Exemple #4
0
func main() {
	var err error
	var cp string
	var initial bool
	var r *gin.Engine

	tbox, _ := rice.FindBox("templates")
	abox, _ := rice.FindBox("assets")

	flag.StringVarP(&cp, "conf", "c", "conf.yml", "Local path to configuration file.")
	flag.BoolVarP(&initial, "initial", "i", false, "Run the initial setup of the server.")
	flag.Parse()

	if err = conf.Load(cp, !initial); err != nil || initial {
		setup.Run()
	}
	if err = database.Initialize(); err != nil {
		log.Fatal(err)
	}
	defer database.DB.Close()
	if err = models.Initialize(); err != nil {
		log.Fatal(err)
	}
	go monitoring.Monit()
	if r, err = router.Setup(tbox, abox); err != nil {
		log.Fatal(err)
	}

	logger.Info("server", "Started goploader server on port", conf.C.Port)
	if conf.C.ServeHTTPS {
		if err = http.ListenAndServeTLS(fmt.Sprintf(":%d", conf.C.Port), conf.C.SSLCert, conf.C.SSLPrivKey, r); err != nil {
			logger.Err("server", "Fatal error", err)
		}
	} else {
		if err = r.Run(fmt.Sprintf(":%d", conf.C.Port)); err != nil {
			logger.Err("server", "Fatal error", err)
		}
	}
}
Exemple #5
0
// Application entry point
func main() {

	fmt.Printf("\n%s (%s) %s\n\n", APP_TITLE, APP_NAME, APP_VERSION)

	// Setup the logging infrastructure
	initialiseLogging()

	// Setup some default values
	opt = new(Options)
	opt.ConfigFile = "./" + APP_NAME + ".config"
	opt.CsvField = -1
	opt.CsvDelimiter = ","
	opt.RemoveQuotes = false
	opt.Format = FORMAT_ALL // Default to all output

	goptions.ParseAndFail(opt)

	// Increment the CSV field to make it easier for the user, since our arrays are 0 based
	if opt.CsvField > -1 {
		opt.CsvField -= 1
	}

	// Validate the mode value
	switch opt.Mode {
	case MODE_SERVER:
		// Load the applications configuration such as ports and IP
		config = loadConfig(opt.ConfigFile, true)
	case MODE_FILE:
		if len(opt.InputFile) == 0 {
			logger.Fatal("Input file path must be supplied when in file mode")
		}

		if len(opt.OutputFile) == 0 {
			logger.Fatal("Output file path must be supplied when in file mode")
		}

		// Load the applications configuration such as ports and IP
		config = loadConfig(opt.ConfigFile, false)
	default:
		logger.Fatal("Invalid mode value (m): %v", opt.Mode)
	}

	// Validate the format value
	switch opt.Format {
	case FORMAT_UNIDENTIFIED:
	case FORMAT_IDENTIFIED:
	case FORMAT_ALL:
	default:
		logger.Fatal("Invalid format value (f): %v", opt.Format)
	}

	// Lets make sure that the users input file actually exists
	if _, err := os.Stat(opt.DataFile); os.IsNotExist(err) {
		logger.Fatal("Data file does not exist")
	}

	processDataFile(opt.DataFile)

	// Start the web API interface if the user wants it running
	if opt.Mode == "s" {
		logger.Info("HTTP API server running: " + config.ApiIp + ":" + fmt.Sprintf("%d", config.ApiPort))
		go func() {
			var r *gin.Engine
			if config.ShowRequests == true {
				r = gin.Default()
			} else {
				gin.SetMode(gin.ReleaseMode)
				r = gin.New()

				r.Use(gin.Recovery())
			}

			r.GET("/single/:hash/", lookupSingleHash)
			r.POST("/bulk", lookupMultipleHashes)
			r.Run(config.ApiIp + ":" + fmt.Sprintf("%d", config.ApiPort))
		}()

		var wg sync.WaitGroup
		wg.Add(1)
		wg.Wait()
	} else {
		processInputFile()
	}
}
Exemple #6
0
func startServer(r *gin.Engine, wg *sync.WaitGroup) {
	defer wg.Done()

	listen := httpListenString()
	r.Run(listen)
}