Skip to content

Commit

Permalink
Merge pull request #606 from iawia002/proxy
Browse files Browse the repository at this point in the history
request: respect HTTP proxy environment variables and drop support for setting proxy by options
  • Loading branch information
iawia002 authored Feb 4, 2020
2 parents a8db701 + a8f6508 commit 6a859b8
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 44 deletions.
16 changes: 3 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,23 +318,17 @@ $ annie -c cookies.txt https://www.bilibili.com/video/av20203945
```

### Proxy
#### HTTP proxy

An HTTP proxy can be specified with the `-x` option:
You can set the HTTP/SOCKS5 proxy using environment variables:

```console
$ annie -x http://127.0.0.1:7777 -i https://www.youtube.com/watch?v=Gnbch2osEeo
$ HTTP_PROXY="http://127.0.0.1:1087/" annie -i https://www.youtube.com/watch?v=Gnbch2osEeo
```

#### SOCKS5 proxy

A SOCKS5 proxy can be specified with the `-s` option:

```console
$ annie -s 127.0.0.1:1080 -i https://www.youtube.com/watch?v=Gnbch2osEeo
$ HTTP_PROXY="socks5://127.0.0.1:1080/" annie -i https://www.youtube.com/watch?v=Gnbch2osEeo
```


### Multi-Thread

Use `-n` option to set the number of download threads(default is 10, only works for multiple-parts video).
Expand Down Expand Up @@ -519,10 +513,6 @@ $ annie -j https://www.bilibili.com/video/av20203945
#### Network:

```
-s string
SOCKS5 proxy
-x string
HTTP proxy
-retry int
How many times to retry when the download failed (default 10)
```
Expand Down
4 changes: 0 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ var (
Playlist bool
// Refer use specified Referrer
Refer string
// Proxy HTTP proxy
Proxy string
// Socks5Proxy SOCKS5 proxy
Socks5Proxy string
// Stream select specified stream to download
Stream string
// OutputPath output file path
Expand Down
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ func init() {
flag.StringVar(&config.Cookie, "c", "", "Cookie")
flag.BoolVar(&config.Playlist, "p", false, "Download playlist")
flag.StringVar(&config.Refer, "r", "", "Use specified Referrer")
flag.StringVar(&config.Proxy, "x", "", "HTTP proxy")
flag.StringVar(&config.Socks5Proxy, "s", "", "SOCKS5 proxy")
flag.StringVar(&config.Stream, "f", "", "Select specific stream to download")
flag.StringVar(&config.OutputPath, "o", "", "Specify the output path")
flag.StringVar(&config.OutputName, "O", "", "Specify the output file name")
Expand Down
26 changes: 1 addition & 25 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
netURL "net/url"
"strconv"
"strings"
"time"

cookiemonster "github.com/MercuryEngineering/CookieMonster"
"github.com/fatih/color"
"github.com/kr/pretty"
"golang.org/x/net/proxy"

"github.com/iawia002/annie/config"
)
Expand All @@ -28,32 +25,11 @@ func Request(
method, url string, body io.Reader, headers map[string]string,
) (*http.Response, error) {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableCompression: true,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
if config.Proxy != "" {
var httpProxy, err = netURL.Parse(config.Proxy)
if err != nil {
return nil, err
}
transport.Proxy = http.ProxyURL(httpProxy)
}
if config.Socks5Proxy != "" {
dialer, err := proxy.SOCKS5(
"tcp",
config.Socks5Proxy,
nil,
&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
},
)
if err != nil {
return nil, err
}
transport.Dial = dialer.Dial
}
client := &http.Client{
Transport: transport,
Timeout: 15 * time.Minute,
Expand Down

0 comments on commit 6a859b8

Please sign in to comment.