You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

75 lines
2.1 KiB

package main
import (
"log"
"sync"
"time"
"github.com/prologic/go-gopher"
"github.com/spf13/viper"
)
const SECRETSHOP_VERSION = "1.0.1"
func main() {
viper.SetConfigName("config")
viper.AddConfigPath("/etc/secretshop/")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("FATAL: Fatal error reading config file: %v \n", err)
}
viper.SetConfigType("yaml")
viper.SetDefault("port", "1965")
viper.SetDefault("full_logging", false)
//Load configs
logging := viper.GetBool("full_logging")
if !logging {
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
} else {
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
log.SetPrefix(time.Now().Format("2006-01-02 15:04:05") + " secretshop ")
}
active_capsules := viper.GetStringSlice("active_capsules")
active_holes := viper.GetStringSlice("active_holes")
port := viper.GetString("port")
capsule_list := make([]GeminiConfig, len(active_capsules))
hole_list := make([]GopherConfig, len(active_holes))
for i, c := range active_capsules {
viper.UnmarshalKey(c, &(capsule_list[i]))
log.Printf("INFO: Loading capsule %v %v", i, capsule_list[i].Hostname)
}
for i, h := range active_holes {
viper.UnmarshalKey(h, &(hole_list[i]))
log.Printf("INFO: Loading hole %v %v", i, hole_list[i].Hostname)
}
if len(capsule_list) < 1 && len(hole_list) < 1 {
log.Println("INFO: No capsules or gopherholes loaded. Shutting down.")
return
}
log.Printf("INFO: %v capsules loaded, %v gopherholes loaded", len(capsule_list), len(hole_list))
// Intialize servers
wg := new(sync.WaitGroup)
wg.Add(1 + len(hole_list))
go func(c interface{}) {
log.Fatal(ListenAndServeTLS(port, c.([]GeminiConfig)))
wg.Done()
}(capsule_list)
for _, h := range hole_list {
go func(h interface{}) {
hole := h.(GopherConfig)
gopher.Handle("/", index(gopher.Dir(hole.RootDir)))
server := &gopher.Server{Addr: "0.0.0.0:" + hole.Port, Hostname: hole.Hostname, Handler: nil}
log.Fatal(server.ListenAndServe())
wg.Done()
}(h)
}
log.Println("INFO: Loaded SecretShop " + SECRETSHOP_VERSION)
log.Println("INFO: Ho ho! You found me!")
wg.Wait()
}