Compare commits

..

No commits in common. "f32ddcf13a34bb52d7e2a5d08fa8bc1ab4102915" and "26e404f6bc5ec5e6b013469f2c37702b61a20f2b" have entirely different histories.

9 changed files with 65 additions and 141 deletions

View file

@ -9,7 +9,8 @@ import (
)
var (
funcs = template.FuncMap{
debugMode bool = true
funcs = template.FuncMap{
"sub": func(a, b int) int {
return a - b
},

View file

@ -2,6 +2,7 @@ package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
@ -17,7 +18,7 @@ func initConfig() error {
return createConfig()
}
printInfo("Configuration file already exists.")
fmt.Println("Configuration file already exists.")
config = loadConfig()
return nil
}
@ -25,12 +26,12 @@ func initConfig() error {
func createConfig() error {
reader := bufio.NewReader(os.Stdin)
printMessage("Configuration file not found.")
printMessage("Do you want to use default values? (yes/no): ")
fmt.Println("Configuration file not found.")
fmt.Print("Do you want to use default values? (yes/no): ")
useDefaults, _ := reader.ReadString('\n')
if strings.TrimSpace(useDefaults) != "yes" {
printMessage("Enter port (default 5000): ")
fmt.Print("Enter port (default 5000): ")
portStr, _ := reader.ReadString('\n')
if portStr != "\n" {
port, err := strconv.Atoi(strings.TrimSpace(portStr))
@ -41,7 +42,7 @@ func createConfig() error {
}
}
printMessage("Enter your domain address (default localhost): ")
fmt.Print("Enter your domain address (default localhost): ")
domain, _ := reader.ReadString('\n')
if domain != "\n" {
config.Domain = strings.TrimSpace(domain)
@ -52,13 +53,10 @@ func createConfig() error {
if config.AuthCode == "" {
config.AuthCode = generateStrongRandomString(64)
printMessage("Generated connection code: %s\n", config.AuthCode)
fmt.Printf("Generated connection code: %s\n", config.AuthCode)
}
config.NodesEnabled = len(config.Peers) > 0
config.CrawlerEnabled = true
config.WebsiteEnabled = true
config.LogLevel = 1
saveConfig(config)
return nil
@ -75,20 +73,17 @@ func saveConfig(config Config) {
sec.Key("Peers").SetValue(peers)
sec.Key("Domain").SetValue(config.Domain)
sec.Key("NodesEnabled").SetValue(strconv.FormatBool(config.NodesEnabled))
sec.Key("CrawlerEnabled").SetValue(strconv.FormatBool(config.CrawlerEnabled))
sec.Key("WebsiteEnabled").SetValue(strconv.FormatBool(config.WebsiteEnabled))
sec.Key("LogLevel").SetValue(strconv.Itoa(config.LogLevel))
err := cfg.SaveTo(configFilePath)
if err != nil {
printErr("Error writing to config file: %v", err)
fmt.Println("Error writing to config file:", err)
}
}
func loadConfig() Config {
cfg, err := ini.Load(configFilePath)
if err != nil {
printErr("Error opening config file: %v", err)
log.Fatalf("Error opening config file: %v", err)
}
port, err := cfg.Section("").Key("Port").Int()
@ -115,31 +110,13 @@ func loadConfig() Config {
nodesEnabled = len(peers) > 0 // Enable nodes if peers are configured
}
crawlerEnabled, err := cfg.Section("").Key("CrawlerEnabled").Bool()
if err != nil { // Default to true if not found
crawlerEnabled = true
}
websiteEnabled, err := cfg.Section("").Key("WebsiteEnabled").Bool()
if err != nil { // Default to true if not found
websiteEnabled = true
}
logLevel, err := cfg.Section("").Key("LogLevel").Int()
if err != nil || logLevel < 0 || logLevel > 4 { // Default to 1 if not found or out of range
logLevel = 1
}
config = Config{
Port: port,
AuthCode: cfg.Section("").Key("AuthCode").String(),
PeerID: cfg.Section("").Key("PeerID").String(),
Peers: peers,
Domain: domain,
NodesEnabled: nodesEnabled,
CrawlerEnabled: crawlerEnabled,
WebsiteEnabled: websiteEnabled,
LogLevel: logLevel,
Port: port,
AuthCode: cfg.Section("").Key("AuthCode").String(),
PeerID: cfg.Section("").Key("PeerID").String(),
Peers: peers,
Domain: domain,
NodesEnabled: nodesEnabled,
}
return config
@ -150,7 +127,7 @@ var configLock sync.RWMutex
func startFileWatcher() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
printErr("%v", err)
log.Fatal(err)
}
go func() {
@ -162,7 +139,7 @@ func startFileWatcher() {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
printInfo("Modified file: %v", event.Name)
log.Println("Modified file:", event.Name)
configLock.Lock()
config = loadConfig()
configLock.Unlock()
@ -171,7 +148,7 @@ func startFileWatcher() {
if !ok {
return
}
printWarn("Error watching configuration file: %v", err)
log.Println("Error watching configuration file:", err)
}
}
}()

35
init.go
View file

@ -1,30 +1,25 @@
package main
import (
"fmt"
"log"
"time"
)
type Config struct {
Port int
AuthCode string
PeerID string
Peers []string
Domain string
NodesEnabled bool
CrawlerEnabled bool
WebsiteEnabled bool
LogLevel int
Port int
AuthCode string
PeerID string
Peers []string
Domain string
NodesEnabled bool
}
var defaultConfig = Config{
Port: 5000,
Domain: "localhost",
Peers: []string{},
AuthCode: generateStrongRandomString(64),
NodesEnabled: true,
CrawlerEnabled: true,
WebsiteEnabled: true,
LogLevel: 1,
Port: 5000,
Domain: "localhost",
Peers: []string{},
AuthCode: generateStrongRandomString(64),
}
const configFilePath = "config.ini"
@ -34,7 +29,7 @@ var config Config
func main() {
err := initConfig()
if err != nil {
printErr("Error during initialization:")
fmt.Println("Error during initialization:", err)
return
}
@ -44,14 +39,14 @@ func main() {
if config.AuthCode == "" {
config.AuthCode = generateStrongRandomString(64)
printInfo("Generated connection code: %s\n", config.AuthCode)
fmt.Printf("Generated connection code: %s\n", config.AuthCode)
saveConfig(config)
}
// Generate Host ID
hostID, nodeErr := generateHostID()
if nodeErr != nil {
printErr("Failed to generate host ID: %v", nodeErr)
log.Fatalf("Failed to generate host ID: %v", nodeErr)
}
config.PeerID = hostID

24
node.go
View file

@ -8,13 +8,16 @@ import (
"io/ioutil"
"log"
"net/http"
"sync"
"time"
)
var (
authCode string
peers []string
hostID string
authCode string
peers []string
authMutex sync.Mutex
authenticated = make(map[string]bool)
hostID string
)
type Message struct {
@ -24,6 +27,13 @@ type Message struct {
VisitedNodes []string `json:"visitedNodes"`
}
type CrawlerConfig struct {
ID string
Host string
Port int
AuthCode string
}
func loadNodeConfig() {
config := loadConfig()
authCode = config.AuthCode
@ -94,7 +104,7 @@ func handleNodeRequest(w http.ResponseWriter, r *http.Request) {
}
defer r.Body.Close()
printDebug("Received message: %+v\n", msg)
log.Printf("Received message: %+v\n", msg)
w.Write([]byte("Message received"))
interpretMessage(msg)
@ -123,9 +133,9 @@ func startNodeClient() {
func interpretMessage(msg Message) {
switch msg.Type {
case "test":
printDebug("Received test message: %v", msg.Content)
fmt.Println("Received test message:", msg.Content)
case "update":
printDebug("Received update message: %v", msg.Content)
fmt.Println("Received update message:", msg.Content)
go update()
case "heartbeat":
handleHeartbeat(msg.Content)
@ -152,6 +162,6 @@ func interpretMessage(msg Message) {
case "file-results":
handleFileResultsMessage(msg)
default:
printWarn("Received unknown message type: %v", msg.Type)
fmt.Println("Received unknown message type:", msg.Type)
}
}

View file

@ -36,7 +36,7 @@ func generateOpenSearchXML(config Config) {
file, err := os.Create("static/opensearch.xml")
if err != nil {
printErr("Error creating OpenSearch file: %v", err)
fmt.Println("Error creating OpenSearch file:", err)
return
}
defer file.Close()
@ -44,9 +44,9 @@ func generateOpenSearchXML(config Config) {
enc := xml.NewEncoder(file)
enc.Indent(" ", " ")
if err := enc.Encode(opensearch); err != nil {
printErr("Error encoding OpenSearch XML: %v", err)
fmt.Println("Error encoding OpenSearch XML:", err)
return
}
printInfo("OpenSearch description file generated successfully.")
fmt.Println("OpenSearch description file generated successfully.")
}

View file

@ -1,50 +0,0 @@
package main
import (
"fmt"
"time"
)
// printDebug logs debug-level messages when LogLevel is set to 4.
func printDebug(format string, args ...interface{}) {
if config.LogLevel >= 4 {
logMessage("DEBUG", format, args...)
}
}
// printInfo logs info-level messages when LogLevel is set to 3 or higher.
func printInfo(format string, args ...interface{}) {
if config.LogLevel >= 3 {
logMessage("INFO", format, args...)
}
}
// printWarn logs warning-level messages when LogLevel is set to 2 or higher.
func printWarn(format string, args ...interface{}) {
if config.LogLevel >= 2 {
logMessage("WARN", format, args...)
}
}
// printErr logs error-level messages regardless of LogLevel.
func printErr(format string, args ...interface{}) {
if config.LogLevel >= 1 {
logMessage("ERROR", format, args...)
}
}
// printMessage logs messages without a specific log level (e.g., general output).
func printMessage(format string, args ...interface{}) {
logMessage("", format, args...)
}
// logMessage handles the actual logging logic without using the default logger's timestamp.
func logMessage(level string, format string, args ...interface{}) {
timestamp := time.Now().Format("2006-01-02|15:04:05")
message := fmt.Sprintf(format, args...)
if level != "" {
fmt.Printf("[%s|%s] %s\n", timestamp, level, message)
} else {
fmt.Printf("[%s] %s\n", timestamp, message)
}
}

View file

@ -483,16 +483,6 @@ hr {
font-size: 16px;
}
.video_title a {
color: var(--link);
text-decoration: none;
}
.video_title a:hover {
color: var(--link);
text-decoration: underline;
}
.video-results-margin {
margin-bottom: 0px !important;
}
@ -1690,7 +1680,6 @@ body, h1, p, a, input, button {
margin-top: 0px;
top: 0px;
left: 0px;
background-color: var(--search-bg);
}
.mobile-none {

View file

@ -51,15 +51,12 @@
<div>
<div class="video__results">
<div class="video__img__results">
<a href="{{ .Href }}">
<img src="{{ .Image }}">
<a href="{{ .Href }}"> <img src="{{ .Image }}">
<div class="duration">{{ .Duration }}</div>
</a>
</img></a>
</div>
<div class="results video-results-margin">
<h3 class="video_title">
<a href="{{ .Href }}">{{ .Title }}</a>
</h3>
<h3 class="video_title" href="{{ .Href }}">{{ .Title }}</h3></a>
<p class="stats">{{ .Views }} <span class="pipe">|</span> {{ .Date }}</p>
<p class="publish__info">YouTube <span class="pipe">|</span> {{ .Creator }}</p>
</div>

View file

@ -2,6 +2,7 @@ package main
import (
"fmt"
"log"
"net/http"
"net/url"
"strings"
@ -52,7 +53,9 @@ func PerformGoogleTextSearch(query, safe, lang string, page int) ([]TextSearchRe
duration := time.Since(startTime) // Calculate the duration
if len(results) == 0 {
printDebug("No results found from Google Search")
if debugMode {
log.Println("No results found from Google")
}
}
return results, duration, nil
@ -80,7 +83,9 @@ func parseResults(doc *goquery.Document) []TextSearchResult {
link := s.Find("a")
href, exists := link.Attr("href")
if !exists {
printDebug("No href attribute found for result %d\n", i)
if debugMode {
log.Printf("No href attribute found for result %d\n", i)
}
return
}