go-blocklist/main.go
Martin Donnelly 111bb88d18 Init
2024-09-02 14:08:28 +01:00

124 lines
2.8 KiB
Go

package main
import (
"fmt"
"io"
"net/http"
"path/filepath"
"github.com/gofiber/fiber/v2"
cp "github.com/otiai10/copy"
"github.com/robfig/cron/v3"
"log"
"os"
)
func main() {
if err := os.Mkdir("./public", 0750); err != nil && !os.IsExist(err) {
log.Fatal(err)
}
app := fiber.New(fiber.Config{})
port := os.Getenv("PORT")
if port == "" {
port = "8989"
}
c := cron.New()
cronsetting := os.Getenv("CRONSETTING")
if cronsetting == "" {
cronsetting = "0 1 * * *" // At 01:00, every day
}
c.AddFunc(cronsetting, func() {
update()
})
c.Start()
doupdate := os.Getenv("UPDATE")
if doupdate != "" {
update()
}
app.Static("/", "./public")
log.Fatalln(app.Listen(fmt.Sprintf(":%v", port)))
}
func update() {
log.Println("Update")
if err := os.Mkdir("./downloads", 0750); err != nil && !os.IsExist(err) {
log.Fatal(err)
}
log.Println(filepath.Base("https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt"))
urls := []string{"youtube", "abuse", "ads", "crypto", "drugs", "facebook", "fraud", "gambling", "malware", "phishing", "piracy", "porn", "ransomware", "redirect", "scam", "torrent", "tracking"}
for uc := range urls {
log.Println("Updating", urls[uc])
url := fmt.Sprintf("https://blocklistproject.github.io/Lists/%s.txt", urls[uc])
path := fmt.Sprintf("./downloads/%s.txt", urls[uc])
log.Println("Downloading", url)
dlerr := DownloadFile(path, url)
if dlerr != nil {
log.Println("Error downloading", dlerr)
}
}
otherList := []string{"https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt", "https://s3.amazonaws.com/lists.disconnect.me/simple_malvertising.txt", "https://adaway.org/hosts.txt", "https://v.firebog.net/hosts/static/w3kbl.txt", "https://raw.githubusercontent.com/kboghdady/youTube_ads_4_pi-hole/master/youtubelist.txt", "https://block.energized.pro/unified/formats/domains.txt", "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn/hosts"}
for oc := range otherList {
log.Println("Updating", otherList[oc])
url := otherList[oc]
path := fmt.Sprintf("./downloads/%s", filepath.Base(url))
log.Println("Downloading", url)
dlerr := DownloadFile(path, url)
if dlerr != nil {
log.Println("Error downloading", dlerr)
}
}
copyerror := cp.Copy("./downloads", "./public")
if copyerror != nil {
log.Println("Error copying files", copyerror)
}
}
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
log.Println("Sattus", resp.Status)
if resp.Status == "200 OK" {
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
return err
}