tgun

package module
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2023 License: BSD-3-Clause Imports: 12 Imported by: 13

README

tgun

a http and tcp client with common options
  • Use Proxy (http, socks4, socks5, tor)
  • Use custom UserAgent (even during redirects)
  • Set headers
  • Use simple authentication
  • Custom timeout
// 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: "MyCrawler/0.1 (https://github.com/user/repo)",
  Headers:   headers,
}

// get bytes
b, err := dialer.GetBytes("https://example.org")

See tgun_test.go for more examples.

c usage

harness tgun in your c application!

first make in plugin directory, creating tgun.a tgun.so tgun.h and an example tgun curl-like application.

#include <tgun.h>

int main(){
    // set user-agent
    easy_ua("libtgun/1.0");
    // set proxy url, or alias 'tor' (9050 or 9150 depending on platform) or 'socks' (127.0.0.1:1080)
    easy_proxy("tor");
    char* b = get_url("http://example.org");

    // if any errors, NULL is returned and an error is waiting
    if (!b) {
      fprintf(stderr, "error: %s\n", tgunerr());    
    } else {
      // normal string, do something with it, then free().
      printf("%s", b);
      free(b);
    }
}

see plugin directory for c usage example

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var DefaultProxy = "socks5://127.0.0.1:1080"

DefaultProxy is used when c.Proxy is "1080" or "socks" or "proxy" or "true" or "1"

View Source
var DefaultTimeout = time.Second * 30

DefaultTimeout is used if c.Timeout is not set

View Source
var DefaultTor = func() string {
	if runtime.GOOS == "windows" {
		return "socks5://127.0.0.1:9150"
	}

	return "socks5://127.0.0.1:9050"
}()

DefaultTor proxy is used when c.Proxy is set to "tor"

View Source
var DefaultUserAgent = fmt.Sprintf("tgun/%s", version)

DefaultUserAgent is used when c.UserAgent is empty

Functions

func Join added in v0.1.5

func Join(s ...string) string

Types

type Client

type Client struct {
	DirectConnect bool          // Set to true to bypass proxy
	Proxy         string        // In the format: socks5://localhost:1080
	UserAgent     string        // In the format: "MyThing/0.1" or "MyThing/0.1 (http://example.org)"
	Timeout       time.Duration // If unset, DefaultTimeout is used.
	AuthUser      string
	AuthPassword  string
	Headers       map[string]string
	// contains filtered or unexported fields
}

Client holds connection options

func (*Client) Dial added in v0.1.6

func (c *Client) Dial(network string, addr string) (net.Conn, error)

Dial an address using c.Proxy if set

func (*Client) DialTCP added in v0.1.6

func (c *Client) DialTCP(addr string) (net.Conn, error)

DialTCP an address using c.Proxy if set

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

Do returns an http response. The request's config is *fortified* with http.Client, proxy, headers, authentication, and user agent.

func (*Client) Get

func (c *Client) Get(url string) (*http.Response, error)

Get connects returns an http response

func (*Client) GetBytes

func (c *Client) GetBytes(url string) ([]byte, error)

GetBytes connects and returns an http response body in the form of bytes

func (Client) HTTPClient

func (c Client) HTTPClient() (*http.Client, error)

HTTPClient returns a http.Client with proxy (but no headers, auth, user-agent)

func (*Client) Join added in v0.1.5

func (c *Client) Join(s ...string) string

func (*Client) Unmarshal added in v0.1.5

func (c *Client) Unmarshal(url string, ptr interface{}) error

Unmarshal JSON (GET, no body)

func (*Client) UnmarshalPost added in v0.1.5

func (c *Client) UnmarshalPost(url string, ptr interface{}, body io.Reader) error

Unmarshal JSON (POST, with body param)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL