gpac

package module
v0.0.0-...-b56d652 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2021 License: MIT Imports: 15 Imported by: 4

README

gpac

PkgGoDev

This package provides a pure Go pac parser based on otto

Example usage

package main

import (
	"fmt"

	"github.com/darren/gpac"
)

var scripts = `
  function FindProxyForURL(url, host) {
    if (isPlainHostName(host)) return DIRECT;
    else return "PROXY 127.0.0.1:8080; PROXY 127.0.0.1:8081; DIRECT";
  }
`

func main() {
	pac, _ := gpac.New(scripts)

	r, _ := pac.FindProxyForURL("http://www.example.com/")
	fmt.Println(r) // returns PROXY 127.0.0.1:8080; PROXY 127.0.0.1:8081; DIRECT

	// Get issues request via a list of proxies and returns at the first request that succeeds
	resp, _ := pac.Get("http://www.example.com/")
	fmt.Println(resp.Status)
}

Simple wrapper for curl and wget

There's a simple tool that wraps curl and wget for pac file support.

Install

go get  github.com/darren/gpac/gpacw

Usage

gpacw wpad.dat curl -v http://example.com
gpacw http://wpad/wpad.dat wget -O /dev/null http://example.com

note url should be the last argument of the command or it will fail.

Documentation

Overview

Example
package main

import (
	"fmt"
	"io/ioutil"
	"os"

	"github.com/darren/gpac"
)

func main() {
	pacf, _ := os.Open("testdata/wpad.dat")
	defer pacf.Close()

	data, _ := ioutil.ReadAll(pacf)
	pac, _ := gpac.New(string(data))

	r, _ := pac.FindProxyForURL("http://www.example.com/")

	fmt.Println(r)
}
Output:

PROXY 4.5.6.7:8080; PROXY 7.8.9.10:8080

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

type Parser struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Parser the parsed pac instance

func From

func From(dst string) (*Parser, error)

From load pac from file or url

func FromFile

func FromFile(filename string) (*Parser, error)

FromFile load pac from file

func FromURL

func FromURL(urlstr string) (*Parser, error)

FromURL load pac from url

func New

func New(text string) (*Parser, error)

New create a parser from text content

func (*Parser) Do

func (p *Parser) Do(req *http.Request) (*http.Response, error)

Do sends an HTTP request via a list of proxies found it returns first HTTP response that succeeds

func (*Parser) FindProxy

func (p *Parser) FindProxy(urlstr string) ([]*Proxy, error)

FindProxy find the proxy in pac and return a list of Proxy

func (*Parser) FindProxyForURL

func (p *Parser) FindProxyForURL(urlstr string) (string, error)

FindProxyForURL finding proxy for url returns string like: PROXY 4.5.6.7:8080; PROXY 7.8.9.10:8080; DIRECT

func (*Parser) Get

func (p *Parser) Get(urlstr string) (*http.Response, error)

Get issues a GET to the specified URL via the proxy list found it stops at the first proxy that succeeds

func (*Parser) Source

func (p *Parser) Source() string

Source returns the original javascript snippet of the pac

type Proxy

type Proxy struct {
	Type     string // Proxy type: PROXY HTTP HTTPS SOCKS DIRECT etc.
	Address  string // Proxy address
	Username string // Proxy username
	Password string // Proxy password
	// contains filtered or unexported fields
}

Proxy is proxy type defined in pac file like PROXY 127.0.0.1:8080 SOCKS 127.0.0.1:1080

func ParseProxy

func ParseProxy(pstr string) []*Proxy

ParseProxy parses proxy string returned by FindProxyForURL and returns a slice of proxies

func (*Proxy) Client

func (p *Proxy) Client() *http.Client

Client returns an http.Client ready for use with this proxy

func (*Proxy) Dialer

func (p *Proxy) Dialer() func(ctx context.Context, network, addr string) (net.Conn, error)

Dialer returns a Dial function that will connect to remote address

func (*Proxy) Do

func (p *Proxy) Do(req *http.Request) (*http.Response, error)

Do sends an HTTP request via the proxy and returns an HTTP response

func (*Proxy) Get

func (p *Proxy) Get(urlstr string) (*http.Response, error)

Get issues a GET to the specified URL via the proxy

func (*Proxy) Init

func (p *Proxy) Init()

func (*Proxy) IsDirect

func (p *Proxy) IsDirect() bool

IsDirect tests whether it is using direct connection

func (*Proxy) IsSOCKS

func (p *Proxy) IsSOCKS() bool

IsSOCKS test whether it is a socks proxy

func (*Proxy) Proxy

func (p *Proxy) Proxy() func(*http.Request) (*url.URL, error)

Proxy returns Proxy function that is ready use for http.Transport

func (*Proxy) String

func (p *Proxy) String() string

func (*Proxy) Transport

func (p *Proxy) Transport() *http.Transport

Transport get the http.RoundTripper

func (*Proxy) URL

func (p *Proxy) URL() string

URL returns a url representation for the proxy for curl -x

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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