Compare commits

..

No commits in common. 'master' and 'v0.2.1' have entirely different histories.

  1. 1
      .gitignore
  2. 2
      Makefile
  3. 75
      README.md
  4. 17
      src/config/config.go
  5. 161
      src/dashboard/dashboard.go
  6. 223
      src/dashboard/res/index.html
  7. 11783
      src/dashboard/res/static/bootstrap.css
  8. 1
      src/dashboard/res/static/bootstrap.css.map
  9. 0
      src/dashboard/res/static/stylesheet.css
  10. 227
      src/main.go
  11. 54
      src/queue/visitqueue.go
  12. 90
      src/web/audio.go
  13. 45
      src/web/documents.go
  14. 90
      src/web/extentions.go
  15. 90
      src/web/images.go
  16. 6
      src/web/job.go
  17. 15
      src/web/requests.go
  18. 86
      src/web/text.go
  19. 90
      src/web/videos.go
  20. 25
      src/worker/pool.go
  21. 274
      src/worker/worker.go

1
.gitignore vendored

@ -8,4 +8,3 @@ wecr
release/ release/
scraped/ scraped/
extracted_data.txt extracted_data.txt
visit_queue.tmp

2
Makefile

@ -20,7 +20,7 @@ WINDIR64:=$(WINDIR)_x64
DARWINDIR64:=$(DARWINDIR)_x64 DARWINDIR64:=$(DARWINDIR)_x64
all: all: clean
cd $(SRCDIR) && go build && mv $(EXE) .. cd $(SRCDIR) && go build && mv $(EXE) ..
test: all test: all

75
README.md

@ -1,91 +1,38 @@
# Wecr - versatile WEb CRawler # Wecr - simple web crawler
## Overview ## Overview
A simple HTML web spider with no dependencies. It is possible to search for pages with a text on them or for the text itself, extract images, video, audio and save pages that satisfy the criteria along the way. Just a simple HTML web spider with minimal dependencies. It is possible to search for pages with a text on them or for the text itself, extract images and save pages that satisfy the criteria along the way.
## Configuration Overview ## Configuration
The flow of work fully depends on the configuration file. By default `conf.json` is used as a configuration file, but the name can be changed via `-conf` flag. The default configuration is embedded in the program so on the first launch or by simply deleting the file, a new `conf.json` will be created in the working directory unless the `-wdir` (working directory) flag is set to some other value, in which case it has a bigger importance. To see all available flags run `wecr -h`. The flow of work fully depends on the configuration file. By default `conf.json` is used as a configuration file, but the name can be changed via `-conf` flag. The default configuration is embedded in the program so on the first launch or by simply deleting the file, a new `conf.json` will be created in the same directory as the executable itself unless the `-wDir` (working directory) flag is set to some other value. To see al available flags run `wecr -h`.
The configuration is split into different branches like `requests` (how requests are made, ie: request timeout, wait time, user agent), `logging` (use logs, output to a file), `save` (output file|directory, save pages or not) or `search` (use regexp, query string) each of which contain tweakable parameters. There are global ones as well such as `workers` (working threads that make requests in parallel) and `depth` (literally, how deep the recursive search should go). The names are simple and self-explanatory so no attribute-by-attribute explanation needed for most of them. The configuration is split into different branches like `requests` (how requests are made, ie: request timeout, wait time, user agent), `logging` (use logs, output to a file), `save` (output file|directory, save pages or not) or `search` (use regexp, query string) each of which contain tweakable parameters. There are global ones as well such as `workers` (working threads that make requests in parallel) and `depth` (literally, how deep the recursive search should go). The names are simple and self-explanatory so no attribute-by-attribute explanation needed for most of them.
The parsing starts from `initial_pages` and goes deeper while ignoring the pages on domains that are in `blacklisted_domains` or are NOT in `allowed_domains`. If all initial pages are happen to be on blacklisted domains or are not in the allowed list - the program will get stuck. It is important to note that `*_domains` should be specified with an existing scheme (ie: https://en.wikipedia.org). Subdomains and ports **matter**: `https://unbewohnte.su:3000/` and `https://unbewohnte.su/` are **different**. The parsing starts from `initial_pages` and goes deeper while ignoring the pages on domains that are in `blacklisted_domains` or are NOT in `allowed_domains`. If all initial pages are happen to be on blacklisted domains or are not in the allowed list - the program will get stuck. It is important to note that `*_domains` should be specified with an existing scheme (ie: https://en.wikipedia.org). Subdomains and ports **matter**: `https://unbewohnte.su:3000/` and `https://unbewohnte.su/` are **different**.
Previous versions stored the entire visit queue in memory, resulting in gigabytes of memory usage but as of `v0.2.4` it is possible to offload the queue to the persistent storage via `in_memory_visit_queue` option (`false` by default).
You can change search `query` at **runtime** via web dashboard if `launch_dashboard` is set to `true`
### Search query ### Search query
There are some special `query` values to control the flow of work: There are some special `query` values:
- `email` - tells wecr to scrape email addresses and output to `output_file` - `email` - tells wecr to scrape email addresses and output to `output_file`
- `images` - find all images on pages and output to the corresponding directory in `output_dir` (**IMPORTANT**: set `content_fetch_timeout_ms` to `0` so the images (and other content below) load fully) - `images` - find all images on pages and output to the corresponding directory in `output_dir` (**IMPORTANT**: set `content_fetch_timeout_ms` to `0` so the images (and other content below) load fully)
- `videos` - find and fetch files that look like videos - `videos` - find and fetch files that look like videos
- `audio` - find and fetch files that look like audio - `audio` - find and fetch files that look like audio
- `documents` - find and fetch files that look like a document - `everything` - find and fetch images, audio and video
- `everything` - find and fetch images, audio, video, documents and email addresses
- `archive` - no text to be searched, save every visited page
When `is_regexp` is enabled, the `query` is treated as a regexp string (in Go "flavor") and pages will be scanned for matches that satisfy it.
### Data Output When `is_regexp` is enabled, the `query` is treated as a regexp string and pages will be scanned for matches that satisfy it.
If the query is not something of special value, all text matches will be outputted to `found_text.json` file as separate continuous JSON objects in `output_dir`; if `save_pages` is set to `true` and|or `query` is set to `images`, `videos`, `audio`, etc. - the additional contents will be also put in the corresponding directories inside `output_dir`, which is neatly created in the working directory or, if `-wdir` flag is set - there. If `output_dir` is happened to be empty - contents will be outputted directly to the working directory. ### Output
The output almost certainly contains some duplicates and is not easy to work with programmatically, so you can use `-extractData` with the output JSON file argument (like `found_text.json`, which is the default output file name for simple text searches) to extract the actual data, filter out the duplicates and put each entry on its new line in a new text file. By default, if the query is not something of special values all the matches and other data will be outputted to `output.json` file as separate continuous JSON objects, but if `save_pages` is set to `true` and|or `query` is set to `images`, `videos`, `audio`, etc. - the additional contents will be put in the corresponding directories inside `output_dir`, which is neatly created by the executable's side.
## Build ## Build
If you're on *nix - it's as easy as `make`. If you're on *nix - it's as easy as `make`.
Otherwise - `go build` in the `src` directory to build `wecr`. No dependencies. Otherwise - `go build` in the `src` directory to build `wecr`.
## Examples
See [a page on my website](https://unbewohnte.su/wecr) for some basic examples.
Dump of a basic configuration:
```json
{
"search": {
"is_regexp": true,
"query": "(sequence to search)|(other sequence)"
},
"requests": {
"request_wait_timeout_ms": 2500,
"request_pause_ms": 100,
"content_fetch_timeout_ms": 0,
"user_agent": ""
},
"depth": 90,
"workers": 30,
"initial_pages": [
"https://en.wikipedia.org/wiki/Main_Page"
],
"allowed_domains": [
"https://en.wikipedia.org/"
],
"blacklisted_domains": [
""
],
"in_memory_visit_queue": false,
"web_dashboard": {
"launch_dashboard": true,
"port": 13370
},
"save": {
"output_dir": "scraped",
"save_pages": false
},
"logging": {
"output_logs": true,
"logs_file": "logs.log"
}
}
```
## License ## License
wecr is distributed under AGPLv3 license AGPLv3

17
src/config/config.go

@ -29,9 +29,7 @@ const (
QueryVideos string = "videos" QueryVideos string = "videos"
QueryAudio string = "audio" QueryAudio string = "audio"
QueryEmail string = "email" QueryEmail string = "email"
QueryDocuments string = "documents"
QueryEverything string = "everything" QueryEverything string = "everything"
QueryArchive string = "archive"
) )
const ( const (
@ -39,7 +37,6 @@ const (
SaveImagesDir string = "images" SaveImagesDir string = "images"
SaveVideosDir string = "videos" SaveVideosDir string = "videos"
SaveAudioDir string = "audio" SaveAudioDir string = "audio"
SaveDocumentsDir string = "documents"
) )
type Search struct { type Search struct {
@ -49,6 +46,7 @@ type Search struct {
type Save struct { type Save struct {
OutputDir string `json:"output_dir"` OutputDir string `json:"output_dir"`
OutputFile string `json:"output_file"`
SavePages bool `json:"save_pages"` SavePages bool `json:"save_pages"`
} }
@ -64,11 +62,6 @@ type Logging struct {
LogsFile string `json:"logs_file"` LogsFile string `json:"logs_file"`
} }
type WebDashboard struct {
UseDashboard bool `json:"launch_dashboard"`
Port uint16 `json:"port"`
}
// Configuration file structure // Configuration file structure
type Conf struct { type Conf struct {
Search Search `json:"search"` Search Search `json:"search"`
@ -78,8 +71,6 @@ type Conf struct {
InitialPages []string `json:"initial_pages"` InitialPages []string `json:"initial_pages"`
AllowedDomains []string `json:"allowed_domains"` AllowedDomains []string `json:"allowed_domains"`
BlacklistedDomains []string `json:"blacklisted_domains"` BlacklistedDomains []string `json:"blacklisted_domains"`
InMemoryVisitQueue bool `json:"in_memory_visit_queue"`
Dashboard WebDashboard `json:"web_dashboard"`
Save Save `json:"save"` Save Save `json:"save"`
Logging Logging `json:"logging"` Logging Logging `json:"logging"`
} }
@ -94,6 +85,7 @@ func Default() *Conf {
Save: Save{ Save: Save{
OutputDir: "scraped", OutputDir: "scraped",
SavePages: false, SavePages: false,
OutputFile: "scraped.json",
}, },
Requests: Requests{ Requests: Requests{
UserAgent: "", UserAgent: "",
@ -106,11 +98,6 @@ func Default() *Conf {
Workers: 20, Workers: 20,
AllowedDomains: []string{""}, AllowedDomains: []string{""},
BlacklistedDomains: []string{""}, BlacklistedDomains: []string{""},
InMemoryVisitQueue: false,
Dashboard: WebDashboard{
UseDashboard: true,
Port: 13370,
},
Logging: Logging{ Logging: Logging{
OutputLogs: true, OutputLogs: true,
LogsFile: "logs.log", LogsFile: "logs.log",

161
src/dashboard/dashboard.go

@ -1,161 +0,0 @@
/*
Wecr - crawl the web for data
Copyright (C) 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package dashboard
import (
"embed"
"encoding/json"
"fmt"
"html/template"
"io"
"io/fs"
"net/http"
"unbewohnte/wecr/config"
"unbewohnte/wecr/logger"
"unbewohnte/wecr/worker"
)
type Dashboard struct {
Server *http.Server
}
//go:embed res
var resFS embed.FS
type PageData struct {
Conf config.Conf
Stats worker.Statistics
}
type PoolStop struct {
Stop bool `json:"stop"`
}
func NewDashboard(port uint16, webConf *config.Conf, pool *worker.Pool) *Dashboard {
mux := http.NewServeMux()
res, err := fs.Sub(resFS, "res")
if err != nil {
logger.Error("Failed to Sub embedded dashboard FS: %s", err)
return nil
}
mux.Handle("/static/", http.FileServer(http.FS(res)))
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
template, err := template.ParseFS(res, "*.html")
if err != nil {
logger.Error("Failed to parse embedded dashboard FS: %s", err)
return
}
template.ExecuteTemplate(w, "index.html", nil)
})
mux.HandleFunc("/stop", func(w http.ResponseWriter, req *http.Request) {
var stop PoolStop
requestBody, err := io.ReadAll(req.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
logger.Error("Failed to read stop|resume signal from dashboard request: %s", err)
return
}
defer req.Body.Close()
err = json.Unmarshal(requestBody, &stop)
if err != nil {
http.Error(w, "Failed to unmarshal stop|resume signal", http.StatusInternalServerError)
logger.Error("Failed to unmarshal stop|resume signal from dashboard UI: %s", err)
return
}
if stop.Stop {
// stop worker pool
pool.Stop()
logger.Info("Stopped worker pool via request from dashboard")
} else {
// resume work
pool.Work()
logger.Info("Resumed work via request from dashboard")
}
})
mux.HandleFunc("/stats", func(w http.ResponseWriter, req *http.Request) {
jsonStats, err := json.MarshalIndent(pool.Stats, "", " ")
if err != nil {
http.Error(w, "Failed to marshal statistics", http.StatusInternalServerError)
logger.Error("Failed to marshal stats to send to the dashboard: %s", err)
return
}
w.Header().Add("Content-type", "application/json")
w.Write(jsonStats)
})
mux.HandleFunc("/conf", func(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPost:
var newConfig config.Conf
defer req.Body.Close()
newConfigData, err := io.ReadAll(req.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
logger.Error("Failed to read new configuration from dashboard request: %s", err)
return
}
err = json.Unmarshal(newConfigData, &newConfig)
if err != nil {
http.Error(w, "Failed to unmarshal new configuration", http.StatusInternalServerError)
logger.Error("Failed to unmarshal new configuration from dashboard UI: %s", err)
return
}
// DO NOT blindly replace global configuration. Manually check and replace values
webConf.Search.IsRegexp = newConfig.Search.IsRegexp
if len(newConfig.Search.Query) != 0 {
webConf.Search.Query = newConfig.Search.Query
}
webConf.Logging.OutputLogs = newConfig.Logging.OutputLogs
default:
jsonConf, err := json.MarshalIndent(webConf, "", " ")
if err != nil {
http.Error(w, "Failed to marshal configuration", http.StatusInternalServerError)
logger.Error("Failed to marshal current configuration to send to the dashboard UI: %s", err)
return
}
w.Header().Add("Content-type", "application/json")
w.Write(jsonConf)
}
})
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: mux,
}
return &Dashboard{
Server: server,
}
}
func (board *Dashboard) Launch() error {
return board.Server.ListenAndServe()
}

223
src/dashboard/res/index.html

@ -1,223 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Wecr dashboard</title>
<!-- <link rel="icon" href="/static/icon.png"> -->
<link rel="stylesheet" href="/static/bootstrap.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body class="d-flex flex-column h-100">
<div class="container">
<header class="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom">
<a href="/" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-dark text-decoration-none">
<svg class="bi me-2" width="40" height="32">
<use xlink:href="#bootstrap"></use>
</svg>
<strong class="fs-4">Wecr</strong>
</a>
<ul class="nav nav-pills">
<li class="nav-item"><a href="/stats" class="nav-link">Stats</a></li>
<li class="nav-item"><a href="/conf" class="nav-link">Config</a></li>
</ul>
</header>
</div>
<div class="container">
<h1>Dashboard</h1>
<div style="height: 3rem;"></div>
<div class="container">
<h2>Statistics</h2>
<div id="statistics">
<ol class="list-group list-group-numbered">
<li class="list-group-item d-flex justify-content-between align-items-start">
<div class="ms-2 me-auto">
<div class="fw-bold">Pages visited</div>
</div>
<span class="badge bg-primary rounded-pill" id="pages_visited">0</span>
</li>
<li class="list-group-item d-flex justify-content-between align-items-start">
<div class="ms-2 me-auto">
<div class="fw-bold">Matches found</div>
</div>
<span class="badge bg-primary rounded-pill" id="matches_found">0</span>
</li>
<li class="list-group-item d-flex justify-content-between align-items-start">
<div class="ms-2 me-auto">
<div class="fw-bold">Pages saved</div>
</div>
<span class="badge bg-primary rounded-pill" id="pages_saved">0</span>
</li>
<li class="list-group-item d-flex justify-content-between align-items-start">
<div class="ms-2 me-auto">
<div class="fw-bold">Start time</div>
</div>
<span class="badge bg-primary rounded-pill" id="start_time_unix">0</span>
</li>
<li class="list-group-item d-flex justify-content-between align-items-start">
<div class="ms-2 me-auto">
<div class="fw-bold">Stopped</div>
</div>
<span class="badge bg-primary rounded-pill" id="stopped">false</span>
</li>
</ol>
</div>
<button class="btn btn-primary" id="btn_stop">Stop</button>
<button class="btn btn-primary" id="btn_resume" disabled>Resume</button>
</div>
<div style="height: 3rem;"></div>
<div class="container">
<h2>Configuration</h2>
<div>
<b>Make runtime changes to configuration</b>
<table class="table table-borderless">
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<tr>
<th>Query</th>
<th>
<input type="text" id="conf_query">
</th>
</tr>
<tr>
<th>Is regexp</th>
<th>
<input type="text" id="conf_is_regexp">
</th>
</tr>
</table>
<button class="btn btn-primary" id="config_apply_button">
Apply
</button>
</div>
<div style="height: 3rem;"></div>
<pre id="conf_output"></pre>
</div>
</div>
</body>
<script>
window.onload = function () {
let confOutput = document.getElementById("conf_output");
let pagesVisitedOut = document.getElementById("pages_visited");
let matchesFoundOut = document.getElementById("matches_found");
let pagesSavedOut = document.getElementById("pages_saved");
let startTimeOut = document.getElementById("start_time_unix");
let stoppedOut = document.getElementById("stopped");
let applyConfButton = document.getElementById("config_apply_button");
let confQuery = document.getElementById("conf_query");
let confIsRegexp = document.getElementById("conf_is_regexp");
let buttonStop = document.getElementById("btn_stop");
let buttonResume = document.getElementById("btn_resume");
buttonStop.addEventListener("click", (event) => {
buttonStop.disabled = true;
buttonResume.disabled = false;
// stop worker pool
let signal = {
"stop": true,
};
fetch("/stop", {
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify(signal),
});
});
buttonResume.addEventListener("click", (event) => {
buttonResume.disabled = true;
buttonStop.disabled = false;
// resume worker pool's work
let signal = {
"stop": false,
};
fetch("/stop", {
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify(signal),
});
});
applyConfButton.addEventListener("click", (event) => {
let query = String(confQuery.value);
if (confIsRegexp.value === "0") {
isRegexp = false;
} else if (confIsRegexp.value === "1") {
isRegexp = true;
};
if (confIsRegexp.value === "false") {
isRegexp = false;
} else if (confIsRegexp.value === "true") {
isRegexp = true;
};
let newConf = {
"search": {
"is_regexp": isRegexp,
"query": query,
},
};
fetch("/conf", {
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify(newConf),
});
});
const interval = setInterval(function () {
// update statistics
fetch("/stats")
.then((response) => response.json())
.then((statistics) => {
pagesVisitedOut.innerText = statistics.pages_visited;
matchesFoundOut.innerText = statistics.matches_found;
pagesSavedOut.innerText = statistics.pages_saved;
startTimeOut.innerText = new Date(1000 * statistics.start_time_unix);
stoppedOut.innerText = statistics.stopped;
});
// update config
fetch("/conf")
.then((response) => response.text())
.then((config) => {
// "print" whole configuration
confOutput.innerText = config;
// update values in the change table if they're empty
let confJSON = JSON.parse(config);
if (confQuery.value == "") {
confQuery.value = confJSON.search.query;
}
if (confIsRegexp.value == "") {
confIsRegexp.value = confJSON.search.is_regexp;
}
});
}, 650);
}();
</script>
</html>

11783
src/dashboard/res/static/bootstrap.css vendored

File diff suppressed because it is too large Load Diff

1
src/dashboard/res/static/bootstrap.css.map

File diff suppressed because one or more lines are too long

0
src/dashboard/res/static/stylesheet.css

227
src/main.go

@ -19,6 +19,7 @@
package main package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -28,25 +29,20 @@ import (
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"time" "time"
"unbewohnte/wecr/config" "unbewohnte/wecr/config"
"unbewohnte/wecr/dashboard"
"unbewohnte/wecr/logger" "unbewohnte/wecr/logger"
"unbewohnte/wecr/queue"
"unbewohnte/wecr/utilities" "unbewohnte/wecr/utilities"
"unbewohnte/wecr/web" "unbewohnte/wecr/web"
"unbewohnte/wecr/worker" "unbewohnte/wecr/worker"
) )
const version = "v0.3.5" const version = "v0.2.1"
const ( const (
configFilename string = "conf.json" defaultConfigFile string = "conf.json"
prettifiedTextOutputFilename string = "extracted_data.txt" defaultOutputFile string = "output.json"
visitQueueFilename string = "visit_queue.tmp" defaultPrettifiedOutputFile string = "extracted_data.txt"
textOutputFilename string = "found_text.json"
emailsOutputFilename string = "found_emails.json"
) )
var ( var (
@ -61,25 +57,32 @@ var (
) )
configFile = flag.String( configFile = flag.String(
"conf", configFilename, "conf", defaultConfigFile,
"Configuration file name to create|look for", "Configuration file name to create|look for",
) )
outputFile = flag.String(
"out", defaultOutputFile,
"Output file name to output information into",
)
extractDataFilename = flag.String( extractDataFilename = flag.String(
"extractData", "", "extractData", "",
"Specify previously outputted JSON file and extract data from it, put each entry nicely on a new line in a new file, exit afterwards", "Set filename for output JSON file and extract data from it, put each entry nicely on a new line in a new file, then exit",
) )
workingDirectory string workingDirectory string
configFilePath string configFilePath string
outputFilePath string
) )
func init() { func init() {
// set log output // set log output
logger.SetOutput(os.Stdout) logger.SetOutput(os.Stdout)
// make default http logger silent // and work around random log prints by /x/net library
log.SetOutput(io.Discard) log.SetOutput(io.Discard)
log.SetFlags(0)
// parse and process flags // parse and process flags
flag.Parse() flag.Parse()
@ -107,12 +110,12 @@ func init() {
if *wDir != "" { if *wDir != "" {
workingDirectory = *wDir workingDirectory = *wDir
} else { } else {
wdir, err := os.Getwd() exePath, err := os.Executable()
if err != nil { if err != nil {
logger.Error("Failed to determine working directory path: %s", err) logger.Error("Failed to determine executable's path: %s", err)
return return
} }
workingDirectory = wdir workingDirectory = filepath.Dir(exePath)
} }
logger.Info("Working in \"%s\"", workingDirectory) logger.Info("Working in \"%s\"", workingDirectory)
@ -120,17 +123,20 @@ func init() {
// extract data if needed // extract data if needed
if strings.TrimSpace(*extractDataFilename) != "" { if strings.TrimSpace(*extractDataFilename) != "" {
logger.Info("Extracting data from %s...", *extractDataFilename) logger.Info("Extracting data from %s...", *extractDataFilename)
err := utilities.ExtractDataFromOutput(*extractDataFilename, prettifiedTextOutputFilename, "\n", false) err := utilities.ExtractDataFromOutput(*extractDataFilename, defaultPrettifiedOutputFile, "\n", false)
if err != nil { if err != nil {
logger.Error("Failed to extract data from %s: %s", *extractDataFilename, err) logger.Error("Failed to extract data from %s: %s", *extractDataFilename, err)
os.Exit(1) os.Exit(1)
} }
logger.Info("Outputted \"%s\"", prettifiedTextOutputFilename) logger.Info("Outputted \"%s\"", defaultPrettifiedOutputFile)
os.Exit(0) os.Exit(0)
} }
// global path to configuration file // global path to configuration file
configFilePath = filepath.Join(workingDirectory, *configFile) configFilePath = filepath.Join(workingDirectory, *configFile)
// global path to output file
outputFilePath = filepath.Join(workingDirectory, *outputFile)
} }
func main() { func main() {
@ -156,6 +162,30 @@ func main() {
} }
logger.Info("Successfully opened configuration file") logger.Info("Successfully opened configuration file")
// create logs if needed
if conf.Logging.OutputLogs {
if conf.Logging.LogsFile != "" {
// output logs to a file
logFile, err := os.Create(filepath.Join(workingDirectory, conf.Logging.LogsFile))
if err != nil {
logger.Error("Failed to create logs file: %s", err)
return
}
defer logFile.Close()
logger.Info("Outputting logs to %s", conf.Logging.LogsFile)
logger.SetOutput(logFile)
} else {
// output logs to stdout
logger.Info("Outputting logs to stdout")
logger.SetOutput(os.Stdout)
}
} else {
// no logging needed
logger.Info("No further logs will be outputted")
logger.SetOutput(nil)
}
// sanitize and correct inputs // sanitize and correct inputs
if len(conf.InitialPages) == 0 { if len(conf.InitialPages) == 0 {
logger.Error("No initial page URLs have been set") logger.Error("No initial page URLs have been set")
@ -229,7 +259,7 @@ func main() {
logger.Warning("User agent is not set. Forced to \"%s\"", conf.Requests.UserAgent) logger.Warning("User agent is not set. Forced to \"%s\"", conf.Requests.UserAgent)
} }
// create output directory and corresponding specialized ones, text output files // create output directories and corresponding specialized ones
if !filepath.IsAbs(conf.Save.OutputDir) { if !filepath.IsAbs(conf.Save.OutputDir) {
conf.Save.OutputDir = filepath.Join(workingDirectory, conf.Save.OutputDir) conf.Save.OutputDir = filepath.Join(workingDirectory, conf.Save.OutputDir)
} }
@ -263,26 +293,6 @@ func main() {
return return
} }
err = os.MkdirAll(filepath.Join(conf.Save.OutputDir, config.SaveDocumentsDir), os.ModePerm)
if err != nil {
logger.Error("Failed to create output directory for documents: %s", err)
return
}
textOutputFile, err := os.Create(filepath.Join(conf.Save.OutputDir, textOutputFilename))
if err != nil {
logger.Error("Failed to create text output file: %s", err)
return
}
defer textOutputFile.Close()
emailsOutputFile, err := os.Create(filepath.Join(conf.Save.OutputDir, emailsOutputFilename))
if err != nil {
logger.Error("Failed to create email addresses output file: %s", err)
return
}
defer emailsOutputFile.Close()
switch conf.Search.Query { switch conf.Search.Query {
case config.QueryEmail: case config.QueryEmail:
logger.Info("Looking for email addresses") logger.Info("Looking for email addresses")
@ -292,17 +302,8 @@ func main() {
logger.Info("Looking for videos (%+s)", web.VideoExtentions) logger.Info("Looking for videos (%+s)", web.VideoExtentions)
case config.QueryAudio: case config.QueryAudio:
logger.Info("Looking for audio (%+s)", web.AudioExtentions) logger.Info("Looking for audio (%+s)", web.AudioExtentions)
case config.QueryDocuments:
logger.Info("Looking for documents (%+s)", web.DocumentExtentions)
case config.QueryArchive:
logger.Info("Archiving every visited page")
case config.QueryEverything: case config.QueryEverything:
logger.Info("Looking for email addresses, images, videos, audio and various documents (%+s - %+s - %+s - %+s)", logger.Info("Looking for email addresses, images, videos and audio (%+s - %+s - %+s)", web.ImageExtentions, web.VideoExtentions, web.AudioExtentions)
web.ImageExtentions,
web.VideoExtentions,
web.AudioExtentions,
web.DocumentExtentions,
)
default: default:
if conf.Search.IsRegexp { if conf.Search.IsRegexp {
logger.Info("Looking for RegExp matches (%s)", conf.Search.Query) logger.Info("Looking for RegExp matches (%s)", conf.Search.Query)
@ -311,97 +312,49 @@ func main() {
} }
} }
// create visit queue file if not turned off // create output file
var visitQueueFile *os.File = nil outputFile, err := os.Create(outputFilePath)
if !conf.InMemoryVisitQueue {
var err error
visitQueueFile, err = os.Create(filepath.Join(workingDirectory, visitQueueFilename))
if err != nil { if err != nil {
logger.Error("Could not create visit queue temporary file: %s", err) logger.Error("Failed to create output file: %s", err)
return return
} }
defer func() { defer outputFile.Close()
visitQueueFile.Close()
os.Remove(filepath.Join(workingDirectory, visitQueueFilename)) // prepare channels
}() jobs := make(chan web.Job, conf.Workers*5)
} results := make(chan web.Result, conf.Workers*5)
// create initial jobs // create initial jobs
initialJobs := make(chan web.Job, conf.Workers*5)
if !conf.InMemoryVisitQueue {
for _, initialPage := range conf.InitialPages { for _, initialPage := range conf.InitialPages {
var newJob web.Job = web.Job{ jobs <- web.Job{
URL: initialPage, URL: initialPage,
Search: conf.Search, Search: conf.Search,
Depth: conf.Depth, Depth: conf.Depth,
} }
err = queue.InsertNewJob(visitQueueFile, newJob)
if err != nil {
logger.Error("Failed to encode an initial job to the visit queue: %s", err)
continue
}
} }
visitQueueFile.Seek(0, io.SeekStart)
} else {
for _, initialPage := range conf.InitialPages {
initialJobs <- web.Job{
URL: initialPage,
Search: conf.Search,
Depth: conf.Depth,
}
}
}
// Prepare global statistics variable
statistics := worker.Statistics{}
// form a worker pool // form a worker pool
workerPool := worker.NewWorkerPool(initialJobs, conf.Workers, &worker.WorkerConf{ workerPool := worker.NewWorkerPool(jobs, results, conf.Workers, worker.WorkerConf{
Search: &conf.Search, Requests: conf.Requests,
Requests: &conf.Requests, Save: conf.Save,
Save: &conf.Save,
BlacklistedDomains: conf.BlacklistedDomains, BlacklistedDomains: conf.BlacklistedDomains,
AllowedDomains: conf.AllowedDomains, AllowedDomains: conf.AllowedDomains,
VisitQueue: worker.VisitQueue{ })
VisitQueue: visitQueueFile,
Lock: &sync.Mutex{},
},
EmailsOutput: emailsOutputFile,
TextOutput: textOutputFile,
}, &statistics)
logger.Info("Created a worker pool with %d workers", conf.Workers) logger.Info("Created a worker pool with %d workers", conf.Workers)
// open dashboard if needed // set up graceful shutdown
var board *dashboard.Dashboard = nil sig := make(chan os.Signal, 1)
if conf.Dashboard.UseDashboard { signal.Notify(sig, os.Interrupt)
board = dashboard.NewDashboard(conf.Dashboard.Port, conf, workerPool) go func() {
go board.Launch() <-sig
logger.Info("Launched dashboard at http://localhost:%d", conf.Dashboard.Port) logger.Info("Received interrupt signal. Exiting...")
}
// create and redirect logs if needed // stop workers
if conf.Logging.OutputLogs { workerPool.Stop()
if conf.Logging.LogsFile != "" {
// output logs to a file
logFile, err := os.Create(filepath.Join(workingDirectory, conf.Logging.LogsFile))
if err != nil {
logger.Error("Failed to create logs file: %s", err)
return
}
defer logFile.Close()
logger.Info("Outputting logs to %s", conf.Logging.LogsFile) // close results channel
logger.SetOutput(logFile) close(results)
} else { }()
// output logs to stdout
logger.Info("Outputting logs to stdout")
logger.SetOutput(os.Stdout)
}
} else {
// no logging needed
logger.Info("No further logs will be outputted")
logger.SetOutput(nil)
}
// launch concurrent scraping ! // launch concurrent scraping !
workerPool.Work() workerPool.Work()
@ -410,30 +363,36 @@ func main() {
// if logs are not used or are printed to the file - output a nice statistics message on the screen // if logs are not used or are printed to the file - output a nice statistics message on the screen
if !conf.Logging.OutputLogs || (conf.Logging.OutputLogs && conf.Logging.LogsFile != "") { if !conf.Logging.OutputLogs || (conf.Logging.OutputLogs && conf.Logging.LogsFile != "") {
go func() { go func() {
var lastPagesVisited uint64 = 0
fmt.Printf("\n") fmt.Printf("\n")
for { for {
time.Sleep(time.Second) time.Sleep(time.Second)
timeSince := time.Since(time.Unix(int64(statistics.StartTimeUnix), 0)).Round(time.Second) timeSince := time.Since(workerPool.Stats.StartTime).Round(time.Second)
fmt.Fprintf(os.Stdout, "\r[%s] %d pages visited; %d pages saved; %d matches (%d pages/sec)", fmt.Fprintf(os.Stdout, "\r[%s] %d pages visited; %d pages saved; %d matches (%d pages/sec)",
timeSince.String(), timeSince.String(),
statistics.PagesVisited, workerPool.Stats.PagesVisited,
statistics.PagesSaved, workerPool.Stats.PagesSaved,
statistics.MatchesFound, workerPool.Stats.MatchesFound,
statistics.PagesVisited-lastPagesVisited, workerPool.Stats.PagesVisited/uint64(timeSince.Seconds()),
) )
lastPagesVisited = statistics.PagesVisited
} }
}() }()
} }
// set up graceful shutdown // get text results and write them to the output file (files are handled by each worker separately)
sig := make(chan os.Signal, 1) for {
signal.Notify(sig, os.Interrupt) result, ok := <-results
<-sig if !ok {
logger.Info("Received interrupt signal. Exiting...") break
}
// stop workers // each entry in output file is a self-standing JSON object
workerPool.Stop() entryBytes, err := json.MarshalIndent(result, " ", "\t")
if err != nil {
continue
}
outputFile.Write(entryBytes)
outputFile.Write([]byte("\n"))
}
} }

54
src/queue/visitqueue.go

@ -1,54 +0,0 @@
package queue
import (
"encoding/json"
"io"
"os"
"unbewohnte/wecr/web"
)
func PopLastJob(queue *os.File) (*web.Job, error) {
stats, err := queue.Stat()
if err != nil {
return nil, err
}
if stats.Size() == 0 {
return nil, nil
}
// find the last job in the queue
var job web.Job
var offset int64 = -1
for {
currentOffset, err := queue.Seek(offset, io.SeekEnd)
if err != nil {
return nil, err
}
decoder := json.NewDecoder(queue)
err = decoder.Decode(&job)
if err != nil || job.URL == "" || job.Search.Query == "" {
offset -= 1
continue
}
queue.Truncate(currentOffset)
return &job, nil
}
}
func InsertNewJob(queue *os.File, newJob web.Job) error {
_, err := queue.Seek(0, io.SeekEnd)
if err != nil {
return err
}
encoder := json.NewEncoder(queue)
err = encoder.Encode(&newJob)
if err != nil {
return err
}
return nil
}

90
src/web/audio.go

@ -20,25 +20,99 @@ package web
import ( import (
"net/url" "net/url"
"strings"
) )
func HasAudioExtention(url string) bool {
for _, extention := range AudioExtentions {
if strings.HasSuffix(url, extention) {
return true
}
}
return false
}
// Tries to find audio URLs on the page // Tries to find audio URLs on the page
func FindPageAudio(pageBody []byte, from url.URL) []url.URL { func FindPageAudio(pageBody []byte, from *url.URL) []string {
var urls []url.URL var urls []string
// for every element that has "src" attribute // for every element that has "src" attribute
for _, link := range FindPageSrcLinks(pageBody, from) { for _, match := range tagSrcRegexp.FindAllString(string(pageBody), -1) {
if HasAudioExtention(link.EscapedPath()) { var linkStartIndex int
urls = append(urls, link) var linkEndIndex int
linkStartIndex = strings.Index(match, "\"")
if linkStartIndex == -1 {
linkStartIndex = strings.Index(match, "'")
if linkStartIndex == -1 {
continue
}
linkEndIndex = strings.LastIndex(match, "'")
if linkEndIndex == -1 {
continue
}
} else {
linkEndIndex = strings.LastIndex(match, "\"")
if linkEndIndex == -1 {
continue
}
}
if linkEndIndex <= linkStartIndex+1 {
continue
}
link, err := url.Parse(match[linkStartIndex+1 : linkEndIndex])
if err != nil {
continue
}
linkResolved := ResolveLink(link, from.Host)
if HasAudioExtention(linkResolved) {
urls = append(urls, linkResolved)
} }
} }
// for every "a" element as well // for every "a" element as well
for _, link := range FindPageLinks(pageBody, from) { for _, match := range tagHrefRegexp.FindAllString(string(pageBody), -1) {
if HasAudioExtention(link.EscapedPath()) { var linkStartIndex int
urls = append(urls, link) var linkEndIndex int
linkStartIndex = strings.Index(match, "\"")
if linkStartIndex == -1 {
linkStartIndex = strings.Index(match, "'")
if linkStartIndex == -1 {
continue
}
linkEndIndex = strings.LastIndex(match, "'")
if linkEndIndex == -1 {
continue
}
} else {
linkEndIndex = strings.LastIndex(match, "\"")
if linkEndIndex == -1 {
continue
}
}
if linkEndIndex <= linkStartIndex+1 {
continue
}
link, err := url.Parse(match[linkStartIndex+1 : linkEndIndex])
if err != nil {
continue
}
linkResolved := ResolveLink(link, from.Host)
if HasAudioExtention(linkResolved) {
urls = append(urls, linkResolved)
} }
} }
// return discovered mutual video urls
return urls return urls
} }

45
src/web/documents.go

@ -1,45 +0,0 @@
/*
Wecr - crawl the web for data
Copyright (C) 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package web
import (
"net/url"
)
// Tries to find docs' URLs on the page
func FindPageDocuments(pageBody []byte, from url.URL) []url.URL {
var urls []url.URL
// for every element that has "src" attribute
for _, link := range FindPageSrcLinks(pageBody, from) {
if HasDocumentExtention(link.EscapedPath()) {
urls = append(urls, link)
}
}
// for every "a" element as well
for _, link := range FindPageLinks(pageBody, from) {
if HasDocumentExtention(link.EscapedPath()) {
urls = append(urls, link)
}
}
// return discovered doc urls
return urls
}

90
src/web/extentions.go

@ -18,8 +18,6 @@
package web package web
import "strings"
var AudioExtentions = []string{ var AudioExtentions = []string{
".3gp", ".3gp",
".aa", ".aa",
@ -84,91 +82,3 @@ var VideoExtentions = []string{
".vob", ".vob",
".ogv", ".ogv",
} }
var DocumentExtentions = []string{
".pdf",
".doc",
".docx",
".epub",
".fb2",
".pub",
".ppt",
".pptx",
".txt",
".tex",
".odt",
".bib",
".ps",
".dwg",
".lyx",
".key",
".ott",
".odf",
".odc",
".ppg",
".xlc",
".latex",
".c",
".cpp",
".sh",
".go",
".java",
".cs",
".rs",
".lua",
".php",
".py",
".pl",
".lua",
".kt",
".rb",
".asm",
".rar",
".tar",
".db",
".7z",
".zip",
".gbr",
".tex",
".ttf",
".ttc",
".woff",
".otf",
".exif",
}
func HasImageExtention(urlPath string) bool {
for _, extention := range ImageExtentions {
if strings.HasSuffix(urlPath, extention) {
return true
}
}
return false
}
func HasDocumentExtention(urlPath string) bool {
for _, extention := range DocumentExtentions {
if strings.HasSuffix(urlPath, extention) {
return true
}
}
return false
}
func HasVideoExtention(urlPath string) bool {
for _, extention := range VideoExtentions {
if strings.HasSuffix(urlPath, extention) {
return true
}
}
return false
}
func HasAudioExtention(urlPath string) bool {
for _, extention := range AudioExtentions {
if strings.HasSuffix(urlPath, extention) {
return true
}
}
return false
}

90
src/web/images.go

@ -20,25 +20,99 @@ package web
import ( import (
"net/url" "net/url"
"strings"
) )
func HasImageExtention(url string) bool {
for _, extention := range ImageExtentions {
if strings.HasSuffix(url, extention) {
return true
}
}
return false
}
// Tries to find images' URLs on the page // Tries to find images' URLs on the page
func FindPageImages(pageBody []byte, from url.URL) []url.URL { func FindPageImages(pageBody []byte, from *url.URL) []string {
var urls []url.URL var urls []string
// for every element that has "src" attribute // for every element that has "src" attribute
for _, link := range FindPageSrcLinks(pageBody, from) { for _, match := range tagSrcRegexp.FindAllString(string(pageBody), -1) {
if HasImageExtention(link.EscapedPath()) { var linkStartIndex int
urls = append(urls, link) var linkEndIndex int
linkStartIndex = strings.Index(match, "\"")
if linkStartIndex == -1 {
linkStartIndex = strings.Index(match, "'")
if linkStartIndex == -1 {
continue
}
linkEndIndex = strings.LastIndex(match, "'")
if linkEndIndex == -1 {
continue
}
} else {
linkEndIndex = strings.LastIndex(match, "\"")
if linkEndIndex == -1 {
continue
}
}
if linkEndIndex <= linkStartIndex+1 {
continue
}
link, err := url.Parse(match)
if err != nil {
continue
}
linkResolved := ResolveLink(link, from.Host)
if HasImageExtention(linkResolved) {
urls = append(urls, linkResolved)
} }
} }
// for every "a" element as well // for every "a" element as well
for _, link := range FindPageLinks(pageBody, from) { for _, match := range tagHrefRegexp.FindAllString(string(pageBody), -1) {
if HasImageExtention(link.EscapedPath()) { var linkStartIndex int
urls = append(urls, link) var linkEndIndex int
linkStartIndex = strings.Index(match, "\"")
if linkStartIndex == -1 {
linkStartIndex = strings.Index(match, "'")
if linkStartIndex == -1 {
continue
}
linkEndIndex = strings.LastIndex(match, "'")
if linkEndIndex == -1 {
continue
}
} else {
linkEndIndex = strings.LastIndex(match, "\"")
if linkEndIndex == -1 {
continue
}
}
if linkEndIndex <= linkStartIndex+1 {
continue
}
link, err := url.Parse(match[linkStartIndex+1 : linkEndIndex])
if err != nil {
continue
}
linkResolved := ResolveLink(link, from.Host)
if HasImageExtention(linkResolved) {
urls = append(urls, linkResolved)
} }
} }
// return discovered mutual image urls from <img> and <a> tags
return urls return urls
} }

6
src/web/job.go

@ -22,7 +22,7 @@ import "unbewohnte/wecr/config"
// Job to pass around workers // Job to pass around workers
type Job struct { type Job struct {
URL string `json:"u"` URL string
Search config.Search `json:"s"` Search config.Search
Depth uint `json:"d"` Depth uint
} }

15
src/web/requests.go

@ -27,8 +27,10 @@ import (
// Get page data coming from url with optional user agent and timeout // Get page data coming from url with optional user agent and timeout
func GetPage(url string, userAgent string, timeOutMs uint64) ([]byte, error) { func GetPage(url string, userAgent string, timeOutMs uint64) ([]byte, error) {
http.DefaultClient.CloseIdleConnections() // client := &http.Client{}
http.DefaultClient.Timeout = time.Duration(timeOutMs * uint64(time.Millisecond)) // client.CheckRedirect = http.DefaultClient.CheckRedirect
// client.Transport = http.DefaultClient.Transport
// client.Timeout = time.Duration(timeOutMs)
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
@ -36,18 +38,19 @@ func GetPage(url string, userAgent string, timeOutMs uint64) ([]byte, error) {
} }
req.Header.Set("User-Agent", userAgent) req.Header.Set("User-Agent", userAgent)
// response, err := client.Do(req)
response, err := http.DefaultClient.Do(req) response, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer response.Body.Close() defer response.Body.Close()
pageData, err := io.ReadAll(response.Body) responseBody, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return pageData, nil return responseBody, nil
} }
// Fetch file from url and save to file at filePath // Fetch file from url and save to file at filePath
@ -62,13 +65,11 @@ func FetchFile(url string, userAgent string, timeOutMs uint64, filePath string)
return err return err
} }
req.Header.Set("User-Agent", userAgent) req.Header.Set("User-Agent", userAgent)
req.Close = true
response, err := client.Do(req) response, err := client.Do(req)
if err != nil { if err != nil {
return nil return nil
} }
response.Close = true
defer response.Body.Close() defer response.Body.Close()
file, err := os.Create(filePath) file, err := os.Create(filePath)
@ -79,7 +80,5 @@ func FetchFile(url string, userAgent string, timeOutMs uint64, filePath string)
_, _ = io.Copy(file, response.Body) _, _ = io.Copy(file, response.Body)
client.CloseIdleConnections()
return nil return nil
} }

86
src/web/text.go

@ -36,28 +36,28 @@ var tagSrcRegexp *regexp.Regexp = regexp.MustCompile(`(?i)(src)[\s]*=[\s]*("|')(
var emailRegexp *regexp.Regexp = regexp.MustCompile(`[A-Za-z0-9._%+\-!%&?~^#$]+@[A-Za-z0-9.\-]+\.[a-zA-Z]{2,4}`) var emailRegexp *regexp.Regexp = regexp.MustCompile(`[A-Za-z0-9._%+\-!%&?~^#$]+@[A-Za-z0-9.\-]+\.[a-zA-Z]{2,4}`)
// Fix relative link and construct an absolute one. Does nothing if the URL already looks alright // var emailRegexp *regexp.Regexp = regexp.MustCompile("[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*")
func ResolveLink(link url.URL, fromHost string) url.URL {
var resolvedURL url.URL = link
if !resolvedURL.IsAbs() { // Fix relative link and construct an absolute one. Does nothing if the URL already looks alright
if resolvedURL.Scheme == "" { func ResolveLink(url *url.URL, fromHost string) string {
if !url.IsAbs() {
if url.Scheme == "" {
// add scheme // add scheme
resolvedURL.Scheme = "https" url.Scheme = "http"
} }
if resolvedURL.Host == "" { if url.Host == "" {
// add host // add host
resolvedURL.Host = fromHost url.Host = fromHost
} }
} }
return resolvedURL return url.String()
} }
// Find all links on page that are specified in href attribute. Do not resolve links. Return URLs as they are on the page // Find all links on page that are specified in <a> tag
func FindPageLinksDontResolve(pageBody []byte) []url.URL { func FindPageLinks(pageBody []byte, from *url.URL) []string {
var urls []url.URL var urls []string
for _, match := range tagHrefRegexp.FindAllString(string(pageBody), -1) { for _, match := range tagHrefRegexp.FindAllString(string(pageBody), -1) {
var linkStartIndex int var linkStartIndex int
@ -90,69 +90,9 @@ func FindPageLinksDontResolve(pageBody []byte) []url.URL {
continue continue
} }
urls = append(urls, *link) urls = append(urls, ResolveLink(link, from.Host))
}
return urls
}
// Find all links on page that are specified in href attribute
func FindPageLinks(pageBody []byte, from url.URL) []url.URL {
urls := FindPageLinksDontResolve(pageBody)
for index := 0; index < len(urls); index++ {
urls[index] = ResolveLink(urls[index], from.Host)
}
return urls
}
// Find all links on page that are specified in "src" attribute. Do not resolve ULRs, return them as they are on page
func FindPageSrcLinksDontResolve(pageBody []byte) []url.URL {
var urls []url.URL
for _, match := range tagSrcRegexp.FindAllString(string(pageBody), -1) {
var linkStartIndex int
var linkEndIndex int
linkStartIndex = strings.Index(match, "\"")
if linkStartIndex == -1 {
linkStartIndex = strings.Index(match, "'")
if linkStartIndex == -1 {
continue
} }
linkEndIndex = strings.LastIndex(match, "'")
if linkEndIndex == -1 {
continue
}
} else {
linkEndIndex = strings.LastIndex(match, "\"")
if linkEndIndex == -1 {
continue
}
}
if linkEndIndex <= linkStartIndex+1 {
continue
}
link, err := url.Parse(match[linkStartIndex+1 : linkEndIndex])
if err != nil {
continue
}
urls = append(urls, *link)
}
return urls
}
// Find all links on page that are specified in "src" attribute
func FindPageSrcLinks(pageBody []byte, from url.URL) []url.URL {
urls := FindPageSrcLinksDontResolve(pageBody)
for index := 0; index < len(urls); index++ {
urls[index] = ResolveLink(urls[index], from.Host)
}
return urls return urls
} }

90
src/web/videos.go

@ -20,25 +20,99 @@ package web
import ( import (
"net/url" "net/url"
"strings"
) )
func HasVideoExtention(url string) bool {
for _, extention := range VideoExtentions {
if strings.HasSuffix(url, extention) {
return true
}
}
return false
}
// Tries to find videos' URLs on the page // Tries to find videos' URLs on the page
func FindPageVideos(pageBody []byte, from url.URL) []url.URL { func FindPageVideos(pageBody []byte, from *url.URL) []string {
var urls []url.URL var urls []string
// for every element that has "src" attribute // for every element that has "src" attribute
for _, link := range FindPageSrcLinks(pageBody, from) { for _, match := range tagSrcRegexp.FindAllString(string(pageBody), -1) {
if HasVideoExtention(link.EscapedPath()) { var linkStartIndex int
urls = append(urls, link) var linkEndIndex int
linkStartIndex = strings.Index(match, "\"")
if linkStartIndex == -1 {
linkStartIndex = strings.Index(match, "'")
if linkStartIndex == -1 {
continue
}
linkEndIndex = strings.LastIndex(match, "'")
if linkEndIndex == -1 {
continue
}
} else {
linkEndIndex = strings.LastIndex(match, "\"")
if linkEndIndex == -1 {
continue
}
}
if linkEndIndex <= linkStartIndex+1 {
continue
}
link, err := url.Parse(match[linkStartIndex+1 : linkEndIndex])
if err != nil {
continue
}
linkResolved := ResolveLink(link, from.Host)
if HasVideoExtention(linkResolved) {
urls = append(urls, linkResolved)
} }
} }
// for every "a" element as well // for every "a" element as well
for _, link := range FindPageLinks(pageBody, from) { for _, match := range tagHrefRegexp.FindAllString(string(pageBody), -1) {
if HasVideoExtention(link.EscapedPath()) { var linkStartIndex int
urls = append(urls, link) var linkEndIndex int
linkStartIndex = strings.Index(match, "\"")
if linkStartIndex == -1 {
linkStartIndex = strings.Index(match, "'")
if linkStartIndex == -1 {
continue
}
linkEndIndex = strings.LastIndex(match, "'")
if linkEndIndex == -1 {
continue
}
} else {
linkEndIndex = strings.LastIndex(match, "\"")
if linkEndIndex == -1 {
continue
}
}
if linkEndIndex <= linkStartIndex+1 {
continue
}
link, err := url.Parse(match[linkStartIndex+1 : linkEndIndex])
if err != nil {
continue
}
linkResolved := ResolveLink(link, from.Host)
if HasVideoExtention(linkResolved) {
urls = append(urls, linkResolved)
} }
} }
// return discovered mutual video urls
return urls return urls
} }

25
src/worker/pool.go

@ -32,11 +32,10 @@ type visited struct {
// Whole worker pool's statistics // Whole worker pool's statistics
type Statistics struct { type Statistics struct {
PagesVisited uint64 `json:"pages_visited"` PagesVisited uint64
MatchesFound uint64 `json:"matches_found"` MatchesFound uint64
PagesSaved uint64 `json:"pages_saved"` PagesSaved uint64
StartTimeUnix uint64 `json:"start_time_unix"` StartTime time.Time
Stopped bool `json:"stopped"`
} }
// Web-Worker pool // Web-Worker pool
@ -44,11 +43,11 @@ type Pool struct {
workersCount uint workersCount uint
workers []*Worker workers []*Worker
visited visited visited visited
Stats *Statistics Stats Statistics
} }
// Create a new worker pool // Create a new worker pool
func NewWorkerPool(initialJobs chan web.Job, workerCount uint, workerConf *WorkerConf, stats *Statistics) *Pool { func NewWorkerPool(jobs chan web.Job, results chan web.Result, workerCount uint, workerConf WorkerConf) *Pool {
var newPool Pool = Pool{ var newPool Pool = Pool{
workersCount: workerCount, workersCount: workerCount,
workers: nil, workers: nil,
@ -56,12 +55,16 @@ func NewWorkerPool(initialJobs chan web.Job, workerCount uint, workerConf *Worke
URLs: nil, URLs: nil,
Lock: sync.Mutex{}, Lock: sync.Mutex{},
}, },
Stats: stats, Stats: Statistics{
StartTime: time.Time{},
PagesVisited: 0,
MatchesFound: 0,
},
} }
var i uint var i uint
for i = 0; i < workerCount; i++ { for i = 0; i < workerCount; i++ {
newWorker := NewWorker(initialJobs, workerConf, &newPool.visited, newPool.Stats) newWorker := NewWorker(jobs, results, workerConf, &newPool.visited, &newPool.Stats)
newPool.workers = append(newPool.workers, &newWorker) newPool.workers = append(newPool.workers, &newWorker)
} }
@ -70,8 +73,7 @@ func NewWorkerPool(initialJobs chan web.Job, workerCount uint, workerConf *Worke
// Notify all workers in pool to start scraping // Notify all workers in pool to start scraping
func (p *Pool) Work() { func (p *Pool) Work() {
p.Stats.StartTimeUnix = uint64(time.Now().Unix()) p.Stats.StartTime = time.Now()
p.Stats.Stopped = false
for _, worker := range p.workers { for _, worker := range p.workers {
worker.Stopped = false worker.Stopped = false
@ -81,7 +83,6 @@ func (p *Pool) Work() {
// Notify all workers in pool to stop scraping // Notify all workers in pool to stop scraping
func (p *Pool) Stop() { func (p *Pool) Stop() {
p.Stats.Stopped = true
for _, worker := range p.workers { for _, worker := range p.workers {
worker.Stopped = true worker.Stopped = true
} }

274
src/worker/worker.go

@ -19,54 +19,41 @@
package worker package worker
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"io"
"net/url" "net/url"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"sync"
"time" "time"
"unbewohnte/wecr/config" "unbewohnte/wecr/config"
"unbewohnte/wecr/logger" "unbewohnte/wecr/logger"
"unbewohnte/wecr/queue"
"unbewohnte/wecr/web" "unbewohnte/wecr/web"
) )
type VisitQueue struct {
VisitQueue *os.File
Lock *sync.Mutex
}
// Worker configuration // Worker configuration
type WorkerConf struct { type WorkerConf struct {
Search *config.Search Requests config.Requests
Requests *config.Requests Save config.Save
Save *config.Save
BlacklistedDomains []string BlacklistedDomains []string
AllowedDomains []string AllowedDomains []string
VisitQueue VisitQueue
TextOutput io.Writer
EmailsOutput io.Writer
} }
// Web worker // Web worker
type Worker struct { type Worker struct {
Jobs chan web.Job Jobs chan web.Job
Conf *WorkerConf Results chan web.Result
Conf WorkerConf
visited *visited visited *visited
stats *Statistics stats *Statistics
Stopped bool Stopped bool
} }
// Create a new worker // Create a new worker
func NewWorker(jobs chan web.Job, conf *WorkerConf, visited *visited, stats *Statistics) Worker { func NewWorker(jobs chan web.Job, results chan web.Result, conf WorkerConf, visited *visited, stats *Statistics) Worker {
return Worker{ return Worker{
Jobs: jobs, Jobs: jobs,
Results: results,
Conf: conf, Conf: conf,
visited: visited, visited: visited,
stats: stats, stats: stats,
@ -74,8 +61,8 @@ func NewWorker(jobs chan web.Job, conf *WorkerConf, visited *visited, stats *Sta
} }
} }
func (w *Worker) saveContent(links []url.URL, pageURL *url.URL) { func (w *Worker) saveContent(links []string, pageURL *url.URL) {
var alreadyProcessedUrls []url.URL var alreadyProcessedUrls []string
for count, link := range links { for count, link := range links {
// check if this URL has been processed already // check if this URL has been processed already
var skip bool = false var skip bool = false
@ -93,29 +80,27 @@ func (w *Worker) saveContent(links []url.URL, pageURL *url.URL) {
} }
alreadyProcessedUrls = append(alreadyProcessedUrls, link) alreadyProcessedUrls = append(alreadyProcessedUrls, link)
var fileName string = fmt.Sprintf("%s_%d_%s", pageURL.Host, count, path.Base(link.Path)) var fileName string = fmt.Sprintf("%s_%d_%s", pageURL.Host, count, path.Base(link))
var filePath string var filePath string
if web.HasImageExtention(link.Path) { if web.HasImageExtention(link) {
filePath = filepath.Join(w.Conf.Save.OutputDir, config.SaveImagesDir, fileName) filePath = filepath.Join(w.Conf.Save.OutputDir, config.SaveImagesDir, fileName)
} else if web.HasVideoExtention(link.Path) { } else if web.HasVideoExtention(link) {
filePath = filepath.Join(w.Conf.Save.OutputDir, config.SaveVideosDir, fileName) filePath = filepath.Join(w.Conf.Save.OutputDir, config.SaveVideosDir, fileName)
} else if web.HasAudioExtention(link.Path) { } else if web.HasAudioExtention(link) {
filePath = filepath.Join(w.Conf.Save.OutputDir, config.SaveAudioDir, fileName) filePath = filepath.Join(w.Conf.Save.OutputDir, config.SaveAudioDir, fileName)
} else if web.HasDocumentExtention(link.Path) {
filePath = filepath.Join(w.Conf.Save.OutputDir, config.SaveDocumentsDir, fileName)
} else { } else {
filePath = filepath.Join(w.Conf.Save.OutputDir, fileName) filePath = filepath.Join(w.Conf.Save.OutputDir, fileName)
} }
err := web.FetchFile( err := web.FetchFile(
link.String(), link,
w.Conf.Requests.UserAgent, w.Conf.Requests.UserAgent,
w.Conf.Requests.ContentFetchTimeoutMs, w.Conf.Requests.ContentFetchTimeoutMs,
filePath, filePath,
) )
if err != nil { if err != nil {
logger.Error("Failed to fetch file located at %s: %s", link.String(), err) logger.Error("Failed to fetch file at %s: %s", link, err)
return return
} }
@ -124,115 +109,22 @@ func (w *Worker) saveContent(links []url.URL, pageURL *url.URL) {
} }
} }
// Save page to the disk with a corresponding name; Download any src files, stylesheets and JS along the way // Save page to the disk with a corresponding name
func (w *Worker) savePage(baseURL url.URL, pageData []byte) { func (w *Worker) savePage(baseURL *url.URL, pageData []byte) {
var findPageFileContentURLs func([]byte) []url.URL = func(pageBody []byte) []url.URL { if w.Conf.Save.SavePages && w.Conf.Save.OutputDir != "" {
var urls []url.URL var pageName string = fmt.Sprintf("%s_%s.html", baseURL.Host, path.Base(baseURL.String()))
pageFile, err := os.Create(filepath.Join(w.Conf.Save.OutputDir, config.SavePagesDir, pageName))
for _, link := range web.FindPageLinksDontResolve(pageData) {
if strings.Contains(link.Path, ".css") ||
strings.Contains(link.Path, ".scss") ||
strings.Contains(link.Path, ".js") ||
strings.Contains(link.Path, ".mjs") {
urls = append(urls, link)
}
}
urls = append(urls, web.FindPageSrcLinksDontResolve(pageBody)...)
return urls
}
var cleanLink func(url.URL, url.URL) url.URL = func(link url.URL, from url.URL) url.URL {
resolvedLink := web.ResolveLink(link, from.Host)
cleanLink, err := url.Parse(resolvedLink.Scheme + "://" + resolvedLink.Host + resolvedLink.Path)
if err != nil {
return resolvedLink
}
return *cleanLink
}
// Create directory with all file content on the page
var pageFilesDirectoryName string = fmt.Sprintf(
"%s_%s_files",
baseURL.Host,
strings.ReplaceAll(baseURL.Path, "/", "_"),
)
err := os.MkdirAll(filepath.Join(w.Conf.Save.OutputDir, config.SavePagesDir, pageFilesDirectoryName), os.ModePerm)
if err != nil { if err != nil {
logger.Error("Failed to create directory to store file contents of %s: %s", baseURL.String(), err) logger.Error("Failed to create page of \"%s\": %s", baseURL.String(), err)
return return
} }
defer pageFile.Close()
// Save files on page pageFile.Write(pageData)
srcLinks := findPageFileContentURLs(pageData)
for _, srcLink := range srcLinks {
web.FetchFile(srcLink.String(),
w.Conf.Requests.UserAgent,
w.Conf.Requests.ContentFetchTimeoutMs,
filepath.Join(
w.Conf.Save.OutputDir,
config.SavePagesDir,
pageFilesDirectoryName,
path.Base(srcLink.String()),
),
)
}
// Redirect old content URLs to local files
for _, srcLink := range srcLinks {
cleanLink := cleanLink(srcLink, baseURL)
pageData = bytes.ReplaceAll(
pageData,
[]byte(srcLink.String()),
[]byte("./"+filepath.Join(pageFilesDirectoryName, path.Base(cleanLink.String()))),
)
}
// Create page output file
pageName := fmt.Sprintf(
"%s_%s.html",
baseURL.Host,
strings.ReplaceAll(baseURL.Path, "/", "_"),
)
outfile, err := os.Create(filepath.Join(
filepath.Join(w.Conf.Save.OutputDir, config.SavePagesDir),
pageName,
))
if err != nil {
fmt.Printf("Failed to create output file: %s\n", err)
}
defer outfile.Close()
outfile.Write(pageData)
logger.Info("Saved \"%s\"", pageName) logger.Info("Saved \"%s\"", pageName)
w.stats.PagesSaved++ w.stats.PagesSaved++
}
const (
textTypeMatch = iota
textTypeEmail = iota
)
// Save text result to an appropriate file
func (w *Worker) saveResult(result web.Result, textType int) {
// write result to the output file
var output io.Writer
switch textType {
case textTypeEmail:
output = w.Conf.EmailsOutput
default:
output = w.Conf.TextOutput
}
// each entry in output file is a self-standing JSON object
entryBytes, err := json.MarshalIndent(result, " ", "\t")
if err != nil {
return
} }
output.Write(entryBytes)
output.Write([]byte("\n"))
} }
// Launch scraping process on this worker // Launch scraping process on this worker
@ -241,27 +133,7 @@ func (w *Worker) Work() {
return return
} }
for { for job := range w.Jobs {
var job web.Job
if w.Conf.VisitQueue.VisitQueue != nil {
w.Conf.VisitQueue.Lock.Lock()
newJob, err := queue.PopLastJob(w.Conf.VisitQueue.VisitQueue)
if err != nil {
logger.Error("Failed to get a new job from visit queue: %s", err)
w.Conf.VisitQueue.Lock.Unlock()
continue
}
if newJob == nil {
w.Conf.VisitQueue.Lock.Unlock()
continue
}
job = *newJob
w.Conf.VisitQueue.Lock.Unlock()
} else {
job = <-w.Jobs
}
// check if the worker has been stopped // check if the worker has been stopped
if w.Stopped { if w.Stopped {
// stop working // stop working
@ -333,85 +205,52 @@ func (w *Worker) Work() {
} }
// find links // find links
pageLinks := web.FindPageLinks(pageData, *pageURL) pageLinks := web.FindPageLinks(pageData, pageURL)
go func() { go func() {
if job.Depth > 1 { if job.Depth > 1 {
// decrement depth and add new jobs // decrement depth and add new jobs to the channel
job.Depth-- job.Depth--
if w.Conf.VisitQueue.VisitQueue != nil {
// add to the visit queue
w.Conf.VisitQueue.Lock.Lock()
for _, link := range pageLinks {
if link.String() != job.URL {
err = queue.InsertNewJob(w.Conf.VisitQueue.VisitQueue, web.Job{
URL: link.String(),
Search: *w.Conf.Search,
Depth: job.Depth,
})
if err != nil {
logger.Error("Failed to encode a new job to a visit queue: %s", err)
continue
}
}
}
w.Conf.VisitQueue.Lock.Unlock()
} else {
// add to the in-memory channel
for _, link := range pageLinks { for _, link := range pageLinks {
if link.String() != job.URL { if link != job.URL {
w.Jobs <- web.Job{ w.Jobs <- web.Job{
URL: link.String(), URL: link,
Search: *w.Conf.Search, Search: job.Search,
Depth: job.Depth, Depth: job.Depth,
} }
} }
} }
} }
}
pageLinks = nil
}() }()
// process and output result // process and output result
var savePage bool = false var savePage bool = false
switch job.Search.Query { switch job.Search.Query {
case config.QueryArchive:
savePage = true
case config.QueryImages: case config.QueryImages:
// find image URLs, output images to the file while not saving already outputted ones // find image URLs, output images to the file while not saving already outputted ones
imageLinks := web.FindPageImages(pageData, *pageURL) imageLinks := web.FindPageImages(pageData, pageURL)
if len(imageLinks) > 0 {
w.saveContent(imageLinks, pageURL) w.saveContent(imageLinks, pageURL)
if len(imageLinks) > 0 {
savePage = true savePage = true
} }
case config.QueryVideos: case config.QueryVideos:
// search for videos // search for videos
// find video URLs, output videos to the files while not saving already outputted ones // find video URLs, output videos to the files while not saving already outputted ones
videoLinks := web.FindPageVideos(pageData, *pageURL) videoLinks := web.FindPageVideos(pageData, pageURL)
if len(videoLinks) > 0 {
w.saveContent(videoLinks, pageURL) w.saveContent(videoLinks, pageURL)
if len(videoLinks) > 0 {
savePage = true savePage = true
} }
case config.QueryAudio: case config.QueryAudio:
// search for audio // search for audio
// find audio URLs, output audio to the file while not saving already outputted ones // find audio URLs, output audio to the file while not saving already outputted ones
audioLinks := web.FindPageAudio(pageData, *pageURL) audioLinks := web.FindPageAudio(pageData, pageURL)
if len(audioLinks) > 0 {
w.saveContent(audioLinks, pageURL) w.saveContent(audioLinks, pageURL)
savePage = true if len(audioLinks) > 0 {
}
case config.QueryDocuments:
// search for various documents
// find documents URLs, output docs to the file while not saving already outputted ones
docsLinks := web.FindPageDocuments(pageData, *pageURL)
if len(docsLinks) > 0 {
w.saveContent(docsLinks, pageURL)
savePage = true savePage = true
} }
@ -419,11 +258,11 @@ func (w *Worker) Work() {
// search for email // search for email
emailAddresses := web.FindPageEmailsWithCheck(pageData) emailAddresses := web.FindPageEmailsWithCheck(pageData)
if len(emailAddresses) > 0 { if len(emailAddresses) > 0 {
w.saveResult(web.Result{ w.Results <- web.Result{
PageURL: job.URL, PageURL: job.URL,
Search: job.Search, Search: job.Search,
Data: emailAddresses, Data: emailAddresses,
}, textTypeEmail) }
w.stats.MatchesFound += uint64(len(emailAddresses)) w.stats.MatchesFound += uint64(len(emailAddresses))
savePage = true savePage = true
} }
@ -432,29 +271,28 @@ func (w *Worker) Work() {
// search for everything // search for everything
// files // files
var contentLinks []url.URL var contentLinks []string
contentLinks = append(contentLinks, web.FindPageImages(pageData, *pageURL)...) contentLinks = append(contentLinks, web.FindPageImages(pageData, pageURL)...)
contentLinks = append(contentLinks, web.FindPageAudio(pageData, *pageURL)...) contentLinks = append(contentLinks, web.FindPageAudio(pageData, pageURL)...)
contentLinks = append(contentLinks, web.FindPageVideos(pageData, *pageURL)...) contentLinks = append(contentLinks, web.FindPageVideos(pageData, pageURL)...)
contentLinks = append(contentLinks, web.FindPageDocuments(pageData, *pageURL)...)
w.saveContent(contentLinks, pageURL) w.saveContent(contentLinks, pageURL)
if len(contentLinks) > 0 {
savePage = true
}
// email // email
emailAddresses := web.FindPageEmailsWithCheck(pageData) emailAddresses := web.FindPageEmailsWithCheck(pageData)
if len(emailAddresses) > 0 { if len(emailAddresses) > 0 {
w.saveResult(web.Result{ w.Results <- web.Result{
PageURL: job.URL, PageURL: job.URL,
Search: job.Search, Search: job.Search,
Data: emailAddresses, Data: emailAddresses,
}, textTypeEmail) }
w.stats.MatchesFound += uint64(len(emailAddresses)) w.stats.MatchesFound += uint64(len(emailAddresses))
savePage = true savePage = true
} }
if len(contentLinks) > 0 || len(emailAddresses) > 0 {
savePage = true
}
default: default:
// text search // text search
switch job.Search.IsRegexp { switch job.Search.IsRegexp {
@ -468,36 +306,36 @@ func (w *Worker) Work() {
matches := web.FindPageRegexp(re, pageData) matches := web.FindPageRegexp(re, pageData)
if len(matches) > 0 { if len(matches) > 0 {
w.saveResult(web.Result{ w.Results <- web.Result{
PageURL: job.URL, PageURL: job.URL,
Search: job.Search, Search: job.Search,
Data: matches, Data: matches,
}, textTypeMatch) }
logger.Info("Found matches: %+v", matches) logger.Info("Found matches: %+v", matches)
w.stats.MatchesFound += uint64(len(matches)) w.stats.MatchesFound += uint64(len(matches))
savePage = true savePage = true
} }
case false: case false:
// just text // just text
if web.IsTextOnPage(job.Search.Query, true, pageData) { if web.IsTextOnPage(job.Search.Query, true, pageData) {
w.saveResult(web.Result{ w.Results <- web.Result{
PageURL: job.URL, PageURL: job.URL,
Search: job.Search, Search: job.Search,
Data: []string{job.Search.Query}, Data: nil,
}, textTypeMatch) }
logger.Info("Found \"%s\" on page", job.Search.Query) logger.Info("Found \"%s\" on page", job.Search.Query)
w.stats.MatchesFound++ w.stats.MatchesFound++
savePage = true savePage = true
} }
} }
} }
// save page // save page
if savePage && w.Conf.Save.SavePages { if savePage {
w.savePage(*pageURL, pageData) w.savePage(pageURL, pageData)
} }
pageData = nil
pageURL = nil
// sleep before the next request // sleep before the next request
time.Sleep(time.Duration(w.Conf.Requests.RequestPauseMs * uint64(time.Millisecond))) time.Sleep(time.Duration(w.Conf.Requests.RequestPauseMs * uint64(time.Millisecond)))

Loading…
Cancel
Save