Skip to content

Commit

Permalink
add license, more tests, godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
aerth committed Dec 5, 2017
1 parent 8514c3e commit 85a18d6
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 11 deletions.
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2017 aerth. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of aerth nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,23 @@

* Use Proxy (http, socks4, socks5, tor)
* Use Custom UserAgent (even during redirects)
* Set headers

```
// set headers if necessary
headers := map[string]string{
"API_KEY": "12345"
"API_SECRET": "12345"
}
// set user agent and proxy in the initialization
dialer := tgun.Client{
Proxy: "socks5://localhost:1080",
UserAgent: "CBaser/0.1 (https://github.com/aerth/cbaser)",
Headers: headers,
}
// get bytes
b, err := dialer.GetBytes("https://example.org")
```
9 changes: 9 additions & 0 deletions tgun.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2017 The tgun Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// package tgun provides a TCP/http(s) client with common options
package tgun

Expand All @@ -6,6 +10,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"os"
"sync"
"time"

Expand All @@ -29,6 +34,7 @@ type Client struct {
mu sync.RWMutex
}

// Get returns an http response
func (c *Client) Get(url string) (*http.Response, error) {
if err := c.refresh(); err != nil {
return nil, err
Expand All @@ -46,6 +52,7 @@ func (c *Client) Get(url string) (*http.Response, error) {
return c.httpClient.Do(req)
}

// Get returns an http response body in the form of bytes
func (c *Client) GetBytes(url string) ([]byte, error) {
resp, err := c.Get(url)
if err != nil {
Expand All @@ -65,6 +72,7 @@ func getDialer(proxyurl string) (proxy.Dialer, error) {
return proxy.Direct, nil
}
if proxyurl == "tor" {
fmt.Fprintln(os.Stderr, "Using default tor address:", defaultTor)
proxyurl = defaultTor
}

Expand Down Expand Up @@ -110,6 +118,7 @@ func (c *Client) refresh() error {
Jar: nil,
}

// create redirect policy
redirectPolicyFunc := func(req *http.Request, reqs []*http.Request) error {
req.Header.Set("User-Agent", c.UserAgent)
return nil
Expand Down
68 changes: 57 additions & 11 deletions tgun_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Copyright 2017 The tgun Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tgun

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
Expand All @@ -15,6 +20,8 @@ import (
// To complete all tests: TESTALL=1 go test -v
// The test suite first tests User Agent, then checks Tor and SOCKS connections

const skiptest = "SKIPPED. Set TESTALL in your environment to run this test."

func TestUserAgent(t *testing.T) {
dialer := &Client{
Proxy: "",
Expand All @@ -29,7 +36,7 @@ func TestUserAgent(t *testing.T) {
t.Fail()
}
i++
if i < 100 {
if i < 10 {
http.Redirect(w, r, fmt.Sprintf("/?test-forwarding-%d", i), http.StatusFound)
}
}
Expand All @@ -48,14 +55,14 @@ func TestEmptyUserAgent(t *testing.T) {
}
var i = 0
handler := func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Your User Agent is: %q\n", r.UserAgent())
fmt.Println(r.URL.String())
if r.UserAgent() != defaultUserAgent {
fmt.Println("It should be:", defaultUserAgent)
t.Fail()
fmt.Printf("Your User Agent is: %q\n", r.UserAgent())
fmt.Println(r.URL.String())
t.FailNow()
}
i++
if i < 100 {
if i < 10 {
http.Redirect(w, r, fmt.Sprintf("/?test-forwarding-%d", i), http.StatusFound)
}
}
Expand All @@ -67,13 +74,52 @@ func TestEmptyUserAgent(t *testing.T) {
}
}

func TestHeaders(t *testing.T) {
dialer := &Client{
Proxy: "", // direct
UserAgent: "", // defaultUserAgent
Headers: map[string]string{
"MyHeader1": "123", // note capitalization of key funk
"MyHeader2": "456",
},
}

b, err := dialer.GetBytes("https://httpbin.org/headers")
if err != nil {
fmt.Println(err)
t.FailNow()
return
}

reply := struct {
Headers map[string]string `json:"headers"`
}{}
err = json.Unmarshal(b, &reply)
if err != nil {
fmt.Println("ERROR")
fmt.Println(string(b))
fmt.Println(err)
t.FailNow()
return
}

if reply.Headers["Myheader1"] != "123" { // Note capitalization difference in reply funk
fmt.Printf("Expected MyHeader1 to be 123, got %q\n", reply.Headers["MyHeader1"])
t.Fail()
}
if reply.Headers["Myheader2"] != "456" {
fmt.Printf("Expected MyHeader2 to be 456, got %q\n", reply.Headers["MyHeader2"])
t.Fail()
}
}

func TestTor(t *testing.T) {
if os.Getenv("TESTALL") == "" {
fmt.Println("Skipping test")
fmt.Println(skiptest)
return
}
dialer := &Client{
Proxy: "socks5://127.0.0.1:9050",
Proxy: "tor",
UserAgent: "Testing/1.2",
}

Expand All @@ -85,9 +131,9 @@ func TestTor(t *testing.T) {
t.FailNow()
}
}

if strings.Contains(string(b), "Congratulations. This browser is configured to use Tor.") {
fmt.Println("Congratulations. This browser is configured to use Tor.")
torline := "Congratulations. This browser is configured to use Tor."
if strings.Contains(string(b), torline) {
fmt.Println(torline)
return
}

Expand All @@ -97,7 +143,7 @@ func TestTor(t *testing.T) {

func TestSOCKS(t *testing.T) {
if os.Getenv("TESTALL") == "" {
fmt.Println("Skipping test")
fmt.Println(skiptest)
return
}
dialer := &Client{
Expand Down

0 comments on commit 85a18d6

Please sign in to comment.