Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
crawl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ale
crawl
Commits
37c649a8
Commit
37c649a8
authored
4 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Allow setting DNS overrides using the --resolve option
parent
cf35cce6
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
client.go
+40
-3
40 additions, 3 deletions
client.go
cmd/crawl/crawl.go
+32
-14
32 additions, 14 deletions
cmd/crawl/crawl.go
with
72 additions
and
17 deletions
client.go
+
40
−
3
View file @
37c649a8
package
crawl
import
(
"context"
"crypto/tls"
"net"
"net/http"
"net/http/cookiejar"
"time"
...
...
@@ -9,14 +11,19 @@ import (
var
defaultClientTimeout
=
60
*
time
.
Second
// DefaultClient
returns a
http.Client suitable for crawling:
does not
// follow redirects, accepts invalid TLS certificates, sets a
// DefaultClient
points at a shared
http.Client suitable for crawling:
//
does not
follow redirects, accepts invalid TLS certificates, sets a
// reasonable timeout for requests.
var
DefaultClient
*
http
.
Client
func
init
()
{
DefaultClient
=
NewHTTPClient
()
}
// NewHTTPClient returns an http.Client suitable for crawling.
func
NewHTTPClient
()
*
http
.
Client
{
jar
,
_
:=
cookiejar
.
New
(
nil
)
// nolint
DefaultClient
=
&
http
.
Client
{
return
&
http
.
Client
{
Timeout
:
defaultClientTimeout
,
Transport
:
&
http
.
Transport
{
TLSClientConfig
:
&
tls
.
Config
{
...
...
@@ -29,3 +36,33 @@ func init() {
Jar
:
jar
,
}
}
// NewHTTPClientWithDNSOverride returns an http.Client suitable for
// crawling, with some additional DNS overrides.
func
NewHTTPClientWithDNSOverride
(
dnsMap
map
[
string
]
string
)
*
http
.
Client
{
jar
,
_
:=
cookiejar
.
New
(
nil
)
// nolint
dialer
:=
new
(
net
.
Dialer
)
transport
:=
&
http
.
Transport
{
DialContext
:
func
(
ctx
context
.
Context
,
network
,
addr
string
)
(
net
.
Conn
,
error
)
{
host
,
port
,
err
:=
net
.
SplitHostPort
(
addr
)
if
err
!=
nil
{
return
nil
,
err
}
if
override
,
ok
:=
dnsMap
[
host
];
ok
{
addr
=
net
.
JoinHostPort
(
override
,
port
)
}
return
dialer
.
DialContext
(
ctx
,
network
,
addr
)
},
TLSClientConfig
:
&
tls
.
Config
{
InsecureSkipVerify
:
true
,
// nolint
},
}
return
&
http
.
Client
{
Timeout
:
defaultClientTimeout
,
Transport
:
transport
,
CheckRedirect
:
func
(
req
*
http
.
Request
,
via
[]
*
http
.
Request
)
error
{
return
http
.
ErrUseLastResponse
},
Jar
:
jar
,
}
}
This diff is collapsed.
Click to expand it.
cmd/crawl/crawl.go
+
32
−
14
View file @
37c649a8
...
...
@@ -5,6 +5,7 @@ package main
import
(
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"io"
...
...
@@ -38,12 +39,27 @@ var (
warcFileSizeMB
=
flag
.
Int
(
"output-max-size"
,
100
,
"maximum output WARC file size (in MB) when using patterns"
)
cpuprofile
=
flag
.
String
(
"cpuprofile"
,
""
,
"create cpu profile"
)
dnsMap
=
dnsMapFlag
(
make
(
map
[
string
]
string
))
excludes
[]
*
regexp
.
Regexp
httpClient
*
http
.
Client
)
func
init
()
{
flag
.
Var
(
&
excludesFlag
{},
"exclude"
,
"exclude regex URL patterns"
)
flag
.
Var
(
&
excludesFileFlag
{},
"exclude-from-file"
,
"load exclude regex URL patterns from a file"
)
flag
.
Var
(
dnsMap
,
"resolve"
,
"set DNS overrides (in hostname=addr format)"
)
stats
=
&
crawlStats
{
states
:
make
(
map
[
int
]
int
),
start
:
time
.
Now
(),
}
go
func
()
{
for
range
time
.
Tick
(
10
*
time
.
Second
)
{
stats
.
Dump
()
}
}()
}
type
excludesFlag
struct
{}
...
...
@@ -82,6 +98,19 @@ func (f *excludesFileFlag) Set(s string) error {
return
nil
}
type
dnsMapFlag
map
[
string
]
string
func
(
f
dnsMapFlag
)
String
()
string
{
return
""
}
func
(
f
dnsMapFlag
)
Set
(
s
string
)
error
{
parts
:=
strings
.
Split
(
s
,
"="
)
if
len
(
parts
)
!=
2
{
return
errors
.
New
(
"value not in host=addr format"
)
}
f
[
parts
[
0
]]
=
parts
[
1
]
return
nil
}
func
extractLinks
(
p
crawl
.
Publisher
,
u
string
,
depth
int
,
resp
*
http
.
Response
,
_
error
)
error
{
links
,
err
:=
analysis
.
GetLinks
(
resp
)
if
err
!=
nil
{
...
...
@@ -217,26 +246,13 @@ func (c *crawlStats) Dump() {
var
stats
*
crawlStats
func
fetch
(
urlstr
string
)
(
*
http
.
Response
,
error
)
{
resp
,
err
:=
crawl
.
Default
Client
.
Get
(
urlstr
)
resp
,
err
:=
http
Client
.
Get
(
urlstr
)
if
err
==
nil
{
stats
.
Update
(
resp
)
}
return
resp
,
err
}
func
init
()
{
stats
=
&
crawlStats
{
states
:
make
(
map
[
int
]
int
),
start
:
time
.
Now
(),
}
go
func
()
{
for
range
time
.
Tick
(
10
*
time
.
Second
)
{
stats
.
Dump
()
}
}()
}
type
byteCounter
struct
{
io
.
ReadCloser
}
...
...
@@ -298,6 +314,8 @@ func main() {
log
.
Fatal
(
err
)
}
httpClient
=
crawl
.
NewHTTPClientWithDNSOverride
(
dnsMap
)
crawler
,
err
:=
crawl
.
NewCrawler
(
*
dbPath
,
seeds
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment