pacman

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2022 License: MIT Imports: 21 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() {
	url := "http://www.example.com/"
	pac, err := pacman.New(scripts)
	if err != nil {
		log.Fatal(err)
	}

	r, err := pac.FindProxyForURL(url) // returns PROXY 127.0.0.1:8080; PROXY 127.0.0.1:8081; DIRECT
	if err != nil {
		log.Fatal(err)
	}

	proxies, err := pacman.ParseProxy(r)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Found proxies for %s:\n", url)

	for _, proxy := range proxies {
		fmt.Println(proxy.String())
	}
}

Documentation

Overview

Example (FindProxyForURL)

FindProxyForURL example.

package main

import (
	"fmt"
	"io"
	"os"

	"github.com/saucelabs/pacman"
)

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

	data, _ := io.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

This section is empty.

Variables

This section is empty.

Functions

func EqualLocalhost added in v0.0.10

func EqualLocalhost(uri1, uri2 *url.URL) bool

Compares if two given URIs are "localhost".

func IsLocalhost added in v0.0.10

func IsLocalhost(uri *url.URL) bool

Checks if the given URI is "localhost".

Types

type Parser

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

Parser definition.

func New

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

New is able to load PAC from many sources: - Direct: `textOrURI` is the PAC content - Remote: `textOrURI` is an HTTP/HTTPS URI - File: `textOrURI` points to a file:

Notes:

  • Optionally, credentials for each/any proxy specified in the PAC content can be set (`proxiesURIs`) using standard URI format. These credentials will be automatically set when `FindProxy` is called.
  • URI is: scheme://credential@host/path` where:
  • `credential` is `username:password`, and is optional
  • `host` is `hostname:port`, and is optional.

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(uri string) ([]Proxy, error)

FindProxy for the given `url`, returning a list of `Proxies`.

Note: If the returned proxies requires credentials, and it was set when the Parser was created (`proxiesURIs`), it will be automatically added to the `Proxy`.

func (*Parser) FindProxyForURL

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

FindProxyForURL for the given `url`, returning as string, example: "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 ProxiesCredentials added in v0.0.5

type ProxiesCredentials map[string]*credential.BasicAuth

type Proxy

type Proxy struct {
	// contains filtered or unexported fields
}

Proxy definition.

func ParseProxy

func ParseProxy(pstr string) ([]Proxy, error)

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

func (*Proxy) GetAddress added in v0.0.5

func (p *Proxy) GetAddress() string

GetAddress returns the original address parsed from PAC content. If type is `DIRECT`, returns empty.

func (*Proxy) GetMode added in v0.0.5

func (p *Proxy) GetMode() mode.Mode

GetType returns the mode of the proxy.

func (*Proxy) GetURI added in v0.0.5

func (p *Proxy) GetURI() *url.URL

GetURI returns the proxy URI. If proxy mode is `DIRECT`, returns `nil`.

func (*Proxy) String

func (p *Proxy) String() string

String is the Stringer interface implementation. Returns mode is `DIRECT`, otherwise returns `{MODE} {URI}`.

Directories

Path Synopsis
internal
credential
Package credential provides different types of credentials for authentication.
Package credential provides different types of credentials for authentication.
validation
Package validation provides a singleton which allows validation of data across the application.
Package validation provides a singleton which allows validation of data across the application.
pkg

Jump to

Keyboard shortcuts

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