<commit_before>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: <NAME> <EMAIL> [:ulfr]
package main

import (
	"flag"
	"fmt"
	"net/http"
	"os"
	"runtime"
	"strings"

	"github.com/gorilla/context"
	"github.com/gorilla/mux"
	"github.com/jvehent/cljs"
	"mig.ninja/mig"
)

var ctx Context

func main() {
	var err error
	cpus := runtime.NumCPU()
	runtime.GOMAXPROCS(cpus)

	// command line options
	var config = flag.String("c", "/etc/mig/api.cfg", "Load configuration from file")
	var debug = flag.Bool("d", false, "Debug mode: run in foreground, log to stdout.")
	var showversion = flag.Bool("V", false, "Show build version and exit")
	flag.Parse()

	if *showversion {
		fmt.Println(mig.Version)
		os.Exit(0)
	}

	// The context initialization takes care of parsing the configuration,
	// and creating connections to database, syslog, ...
	fmt.Fprintf(os.Stderr, "Initializing API context...")
	ctx, err = Init(*config, *debug) //ctx is a global variable
	if err != nil {
		fmt.Printf("\nFATAL: %v\n", err)
		os.Exit(9)
	}
	fmt.Fprintf(os.Stderr, "OK\n")
	ctx.Channels.Log <- mig.Log{Desc: "Context initialization done"}

	// Goroutine that handles events, such as logs and panics,
	// and decides what to do with them
	go func() {
		for event := range ctx.Channels.Log {
			stop, err := mig.ProcessLog(ctx.Logging, event)
			if err != nil {
				panic("Unable to process logs")
			}
			// if ProcessLog says we should stop
			if stop {
				panic("Logger routine asked to stop")
			}
		}
	}()
	ctx.Channels.Log <- mig.Log{Desc: "Logger routine started"}

	// register routes
	r := mux.NewRouter()
	s := r.PathPrefix(ctx.Server.BaseRoute).Subrouter()
	// unauthenticated endpoints
	s.HandleFunc("/heartbeat", getHeartbeat).Methods("GET")
	s.HandleFunc("/ip", getIP).Methods("GET")
	// Loader manifest endpoints
	s.HandleFunc("/manifest", authenticate(getManifest)).Methods("GET")
	s.HandleFunc("/manifest/sign/", authenticate(signManifest)).Methods("POST")
	s.HandleFunc("/manifest/status/", authenticate(statusManifest)).Methods("POST")
	s.HandleFunc("/manifest/agent/", getAgentManifest).Methods("POST")
	s.HandleFunc("/manifest/fetch/", getManifestFile).Methods("POST")
	// all other resources require authentication
	s.HandleFunc("/", authenticate(getHome)).Methods("GET")
	s.HandleFunc("/search", authenticate(search)).Methods("GET")
	s.HandleFunc("/action", authenticate(getAction)).Methods("GET")
	s.HandleFunc("/action/create/", authenticate(describeCreateAction)).Methods("GET")
	s.HandleFunc("/action/create/", authenticate(createAction)).Methods("POST")
	s.HandleFunc("/command", authenticate(getCommand)).Methods("GET")
	s.HandleFunc("/agent", authenticate(getAgent)).Methods("GET")
	s.HandleFunc("/investigator", authenticate(getInvestigator)).Methods("GET")
	s.HandleFunc("/investigator/create/", authenticate(describeCreateInvestigator)).Methods("GET")
	s.HandleFunc("/investigator/create/", authenticate(createInvestigator)).Methods("POST")
	s.HandleFunc("/investigator/update/", authenticate(describeUpdateInvestigator)).Methods("GET")
	s.HandleFunc("/investigator/update/", authenticate(updateInvestigator)).Methods("POST")
	s.HandleFunc("/dashboard", authenticate(getDashboard)).Methods("GET")

	ctx.Channels.Log <- mig.Log{Desc: "Starting HTTP handler"}
<commit_msg>[minor] move auth manifest endpoints under correct section in function<commit_after>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: <NAME> <EMAIL> [:ulfr]
package main

import (
	"flag"
	"fmt"
	"net/http"
	"os"
	"runtime"
	"strings"

	"github.com/gorilla/context"
	"github.com/gorilla/mux"
	"github.com/jvehent/cljs"
	"mig.ninja/mig"
)

var ctx Context

func main() {
	var err error
	cpus := runtime.NumCPU()
	runtime.GOMAXPROCS(cpus)

	// command line options
	var config = flag.String("c", "/etc/mig/api.cfg", "Load configuration from file")
	var debug = flag.Bool("d", false, "Debug mode: run in foreground, log to stdout.")
	var showversion = flag.Bool("V", false, "Show build version and exit")
	flag.Parse()

	if *showversion {
		fmt.Println(mig.Version)
		os.Exit(0)
	}

	// The context initialization takes care of parsing the configuration,
	// and creating connections to database, syslog, ...
	fmt.Fprintf(os.Stderr, "Initializing API context...")
	ctx, err = Init(*config, *debug) //ctx is a global variable
	if err != nil {
		fmt.Printf("\nFATAL: %v\n", err)
		os.Exit(9)
	}
	fmt.Fprintf(os.Stderr, "OK\n")
	ctx.Channels.Log <- mig.Log{Desc: "Context initialization done"}

	// Goroutine that handles events, such as logs and panics,
	// and decides what to do with them
	go func() {
		for event := range ctx.Channels.Log {
			stop, err := mig.ProcessLog(ctx.Logging, event)
			if err != nil {
				panic("Unable to process logs")
			}
			// if ProcessLog says we should stop
			if stop {
				panic("Logger routine asked to stop")
			}
		}
	}()
	ctx.Channels.Log <- mig.Log{Desc: "Logger routine started"}

	// register routes
	r := mux.NewRouter()
	s := r.PathPrefix(ctx.Server.BaseRoute).Subrouter()
	// unauthenticated endpoints
	s.HandleFunc("/heartbeat", getHeartbeat).Methods("GET")
	s.HandleFunc("/ip", getIP).Methods("GET")
	// Loader manifest endpoints
	s.HandleFunc("/manifest/agent/", getAgentManifest).Methods("POST")
	s.HandleFunc("/manifest/fetch/", getManifestFile).Methods("POST")
	// all other resources require authentication
	s.HandleFunc("/", authenticate(getHome)).Methods("GET")
	s.HandleFunc("/manifest", authenticate(getManifest)).Methods("GET")
	s.HandleFunc("/manifest/sign/", authenticate(signManifest)).Methods("POST")
	s.HandleFunc("/manifest/status/", authenticate(statusManifest)).Methods("POST")
	s.HandleFunc("/search", authenticate(search)).Methods("GET")
	s.HandleFunc("/action", authenticate(getAction)).Methods("GET")
	s.HandleFunc("/action/create/", authenticate(describeCreateAction)).Methods("GET")
	s.HandleFunc("/action/create/", authenticate(createAction)).Methods("POST")
	s.HandleFunc("/command", authenticate(getCommand)).Methods("GET")
	s.HandleFunc("/agent", authenticate(getAgent)).Methods("GET")
	s.HandleFunc("/investigator", authenticate(getInvestigator)).Methods("GET")
	s.HandleFunc("/investigator/create/", authenticate(describeCreateInvestigator)).Methods("GET")
	s.HandleFunc("/investigator/create/", authenticate(createInvestigator)).Methods("POST")
	s.HandleFunc("/investigator/update/", authenticate(describeUpdateInvestigator)).Methods("GET")
	s.HandleFunc("/investigator/update/", authenticate(updateInvestigator)).Methods("POST")
	s.HandleFunc("/dashboard", authenticate(getDashboard)).Methods("GET")

	ctx.Channels.Log <- mig.Log{Desc: "Starting HTTP handler"}

{'fraction_non_alphanumeric': 0.16544592847910383, 'fraction_numerical': 0.0017233950883239983, 'mean_word_length': 4.842281879194631, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5236424', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2220, 'n_words': 517}