pacman

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2021 License: MIT Imports: 16 Imported by: 0

README

pacman

This package provides a pure Go pac parser based on goja

Usage

Get package
go get github.com/saucelabs/pacman
Import package
package main

import (
 "fmt"
 "log"

 "github.com/saucelabs/pacman"
)

// Example of PAC.
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, err := pacman.New(scripts)
	if err != nil {
		log.Fatall(err)
	}

 	r, err := pac.FindProxyForURL("http://www.example.com/")
	 if err != nil {
		log.Fatall(err)
	}

 	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, err := pac.Get("http://www.example.com/")
	 if err != nil {
		log.Fatall(err)
	}

 	fmt.Println(resp.Status)
}

Documentation

Overview

Example
package main

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

	"github.com/saucelabs/pacman"
)

func main() {
	pacf, _ := os.Open("resources/data.pac")
	defer pacf.Close()

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

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

	fmt.Println(r)

}
Output:

PROXY http://4.5.6.7:8080; PROXY https://4.5.6.7:8081; PROXY socks://4.5.6.7:8082; PROXY socks5://4.5.6.7:8083; PROXY quic://4.5.6.7:8084; PROXY 4.5.6.7:8085

Index

Examples

Constants

View Source
const (
	NonDirectTypeAddsLen = 2
	SOCKS5Number         = 5
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

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

Parser definition.

func New

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

New is able to load PAC from many sources. - Directly: `textOrURI` is the PAC content - Remotely: `textOrURI` is a HTTP/HTTPS URI - File: `textOrURI` points to a file (requires `.pac` extension).

func (*Parser) Content added in v0.0.3

func (p *Parser) Content() string

Content returns the PAC content.

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) Source added in v0.0.2

func (p *Parser) Source() string

Source of the PAC content.

type Proxy

type Proxy struct {
	Address  string
	Password string
	Type     string
	Username string
}

Proxy definition.

func ParseProxy

func ParseProxy(pstr string) []Proxy

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

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) String

func (p *Proxy) String() string

func (*Proxy) URL

func (p *Proxy) URL() string

URL returns a url representation.

Jump to

Keyboard shortcuts

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