func indexHandler(w http.ResponseWriter, r *http.Request) { defer func() { if r := recover(); r != nil { fmt.Println("Control Panel has encountered a panic in IndexHandler.\n", r) } }() TemplateMutex.Lock() defer TemplateMutex.Unlock() if false == checkControlPanelPassword(w, r) { return } //templates.ParseGlob(FILES_PATH + "templates/index/*.html") files.CustomParseGlob(templates, "templates/index/*.html") if len(GitBuild) == 0 { GitBuild = "Unknown (Must install with script)" } err := templates.ExecuteTemplate(w, "indexPage", GitBuild) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
func handleSearchResult(content *SearchedStruct, w http.ResponseWriter) { // Functions able to be used within the html funcMap := template.FuncMap{ "truncate": func(s string) string { bytes := []byte(s) hash := sha256.Sum256(bytes) str := fmt.Sprintf(" - Bytes: %d <br /> - Hash: %x", len(bytes), hash) return str }, "AddressFACorrect": func(s string) string { hash, err := primitives.HexToHash(s) if err != nil { return "There has been an error converting the address" } prefix := []byte{0x5f, 0xb1} addr := hash.Bytes() addr = append(prefix, addr[:]...) oneSha := sha256.Sum256(addr) twoSha := sha256.Sum256(oneSha[:]) addr = append(addr, twoSha[:4]...) str := base58.Encode(addr) return str }, "AddressECCorrect": func(s string) string { hash, err := primitives.HexToHash(s) if err != nil { return "There has been an error converting the address" } prefix := []byte{0x59, 0x2a} addr := hash.Bytes() addr = append(prefix, addr[:]...) oneSha := sha256.Sum256(addr) twoSha := sha256.Sum256(oneSha[:]) addr = append(addr, twoSha[:4]...) str := base58.Encode(addr) return str }, "TransactionAmountCorrect": func(u uint64) string { s := fmt.Sprintf("%d", u) f, err := strconv.ParseFloat(s, 64) if err != nil { return s } f = f / 1e8 return fmt.Sprintf("%.8f", f) }, } TemplateMutex.Lock() templates.Funcs(funcMap) files.CustomParseGlob(templates, "templates/searchresults/*.html") files.CustomParseFile(templates, "templates/searchresults/type/"+content.Type+".html") TemplateMutex.Unlock() var err error _ = err switch content.Type { case "entry": entry := getEntry(content.Input) if entry == nil { break } TemplateMutex.Lock() err = templates.ExecuteTemplate(w, content.Type, entry) TemplateMutex.Unlock() return case "chainhead": arr := getAllChainEntries(content.Input) if arr == nil { break } arr[0].Content = struct { Head interface{} Length int }{arr[0].Content, len(arr) - 1} TemplateMutex.Lock() err = templates.ExecuteTemplate(w, content.Type, arr) TemplateMutex.Unlock() return case "eblock": eblk := getEblock(content.Input) if eblk == nil { break } TemplateMutex.Lock() err = templates.ExecuteTemplate(w, content.Type, eblk) TemplateMutex.Unlock() return case "dblock": dblk := getDblock(content.Input) if dblk == nil { break } TemplateMutex.Lock() err = templates.ExecuteTemplate(w, content.Type, dblk) TemplateMutex.Unlock() return case "ablock": ablk := getAblock(content.Input) if ablk == nil { break } TemplateMutex.Lock() err = templates.ExecuteTemplate(w, content.Type, ablk) TemplateMutex.Unlock() return case "fblock": fblk := getFblock(content.Input) if fblk == nil { break } TemplateMutex.Lock() err = templates.ExecuteTemplate(w, content.Type, fblk) TemplateMutex.Unlock() return case "ecblock": ecblock := getECblock(content.Input) if ecblock == nil { break } TemplateMutex.Lock() err = templates.ExecuteTemplate(w, content.Type, ecblock) TemplateMutex.Unlock() return case "entryack": entryAck := getEntryAck(content.Input) if entryAck == nil { break } TemplateMutex.Lock() err = templates.ExecuteTemplate(w, content.Type, entryAck) TemplateMutex.Unlock() return case "factoidack": factoidAck := getFactoidAck(content.Input) if factoidAck == nil { break } TemplateMutex.Lock() err = templates.ExecuteTemplate(w, content.Type, factoidAck) TemplateMutex.Unlock() return case "facttransaction": transaction := getFactTransaction(content.Input) if transaction == nil { break } TemplateMutex.Lock() err = templates.ExecuteTemplate(w, content.Type, transaction) TemplateMutex.Unlock() return case "ectransaction": transaction := getEcTransaction(content.Input) if transaction == nil { break } TemplateMutex.Lock() err = templates.ExecuteTemplate(w, content.Type, transaction) TemplateMutex.Unlock() return case "EC": hash := base58.Decode(content.Input) if len(hash) < 34 { break } var fixed [32]byte copy(fixed[:], hash[2:34]) bal := fmt.Sprintf("%d", StatePointer.FactoidState.GetECBalance(fixed)) TemplateMutex.Lock() templates.ExecuteTemplate(w, content.Type, struct { Balance string Address string }{bal, content.Input}) TemplateMutex.Unlock() return case "FA": hash := base58.Decode(content.Input) if len(hash) < 34 { break } var fixed [32]byte copy(fixed[:], hash[2:34]) bal := fmt.Sprintf("%.8f", float64(StatePointer.FactoidState.GetFactoidBalance(fixed))/1e8) TemplateMutex.Lock() templates.ExecuteTemplate(w, content.Type, struct { Balance string Address string }{bal, content.Input}) TemplateMutex.Unlock() return } TemplateMutex.Lock() files.CustomParseFile(templates, "templates/searchresults/type/notfound.html") templates.ExecuteTemplate(w, "notfound", content.Input) TemplateMutex.Unlock() }
// Main function. This intiates appropriate variables and starts the control panel serving func ServeControlPanel(displayStateChannel chan state.DisplayState, statePointer *state.State, connections chan interface{}, controller *p2p.Controller, gitBuild string) { defer func() { if r := recover(); r != nil { // The following recover string indicates an overwrite of existing http.ListenAndServe goroutine if r != "http: multiple registrations for /" { fmt.Println("Control Panel has encountered a panic in ServeControlPanel.\n", r) } } }() StatePointer = statePointer StatePointer.ControlPanelDataRequest = true // Request initial State // Wait for initial State select { case DisplayState = <-displayStateChannel: } DisplayStateMutex.RLock() controlPanelSetting := DisplayState.ControlPanelSetting port := DisplayState.ControlPanelPort DisplayStateMutex.RUnlock() if controlPanelSetting == 0 { // 0 = Disabled fmt.Println("Control Panel has been disabled withing the config file and will not be served. This is recommended for any public server, if you wish to renable it, check your config file.") return } go DisplayStateDrain(displayStateChannel) GitBuild = gitBuild portStr := ":" + strconv.Itoa(port) Controller = controller TemplateMutex.Lock() templates = files.CustomParseGlob(nil, "templates/general/*.html") templates = template.Must(templates, nil) TemplateMutex.Unlock() // Updated Globals. A seperate GoRoutine updates these, we just initialize RecentTransactions = new(LastDirectoryBlockTransactions) AllConnections = NewConnectionsMap() // Mux for static files mux = http.NewServeMux() mux.Handle("/", files.StaticServer) go doEvery(10*time.Second, getRecentTransactions) go manageConnections(connections) http.HandleFunc("/", static(indexHandler)) http.HandleFunc("/search", searchHandler) http.HandleFunc("/post", postHandler) http.HandleFunc("/factomd", factomdHandler) http.HandleFunc("/factomdBatch", factomdBatchHandler) tlsIsEnabled, tlsPrivate, tlsPublic := StatePointer.GetTlsInfo() if tlsIsEnabled { waitfortls: for { // lets wait for both the tls cert and key to be created. if they are not created, wait for the RPC API process to create the files. // it is in a different goroutine, so just wait until it is done. it happens in wsapi.Start with genCertPair() if _, err := os.Stat(tlsPublic); err == nil { if _, err := os.Stat(tlsPrivate); err == nil { break waitfortls } } time.Sleep(100 * time.Millisecond) } fmt.Println("Starting encrypted Control Panel on https://localhost" + portStr + "/ Please note the HTTPS in the browser.") http.ListenAndServeTLS(portStr, tlsPublic, tlsPrivate, nil) } else { fmt.Println("Starting Control Panel on http://localhost" + portStr + "/") http.ListenAndServe(portStr, nil) } }