https://github.com/keltia/proxy
Authenticated proxy module.
https://github.com/keltia/proxy
authentication golang http-proxy
Last synced: 5 months ago
JSON representation
Authenticated proxy module.
- Host: GitHub
- URL: https://github.com/keltia/proxy
- Owner: keltia
- License: other
- Created: 2018-05-15T16:09:53.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-03-11T14:55:51.000Z (over 7 years ago)
- Last Synced: 2024-11-17T12:15:51.663Z (over 1 year ago)
- Topics: authentication, golang, http-proxy
- Language: Go
- Size: 24.4 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
netrc
=====
[](https://github.com/keltia/proxy/releases)
[](https://github.com/keltia/proxy/issues)
[](https://golang.org/dl/)
[](https://travis-ci.org/keltia/proxy)
[](http://godoc.org/github.com/keltia/proxy)
[](https://semver.org/spec/v2.0.0.html)
[](https://opensource.org/licenses/BSD-2-Clause)
[](https://goreportcard.com/report/github.com/keltia/proxy)
Go library to load and prepare proxy authentication by parsing the `netrc` file as defined in `ftp(1)`. Its purpose is now mainly to have a standard way of specifying the credentials for proxy authentication.
## Requirements
* Go >= 1.10
## Installation
This is a pure library, there is no associated command (like I do in some of my other packages such as [RIPE Atlas](https://github.com/keltia/ripe-atlas/) or [Cryptcheck](https://github.com/keltia/cryptcheck/)).
Installation is like many Go libraries with a simple
go get github.com/keltia/proxy
`Proxy` also has `vgo` support & metadata (see the articles on [vgo](https://research.swtch.com/vgo-intro)). It respects the [Semantic Versioning](https://research.swtch.com/vgo-import) principle with tagged releases.
## API Usage
The API is very simple in `net/http`. You have to create a custom transport and look for credentials.
The main work is done by `SetupAuthProxy()` which looks at the standard file `.netrc` file. This file was defined a long time by the `ftp(1)` command to store FTP sites' credentials. We (ab)use it with a special **site** called "proxy".
The goal is to avoid polluting (and leaking) your credentials in the environment variable.
import "github.com/keltia/proxy"
authstr, err := proxy.SetupProxyAuth()
This looks for proxy credentials and store that internally. If you need the credentials later, you can still call `GetAuth()`:
authstr := proxy.GetAuth()
`autstr` is suitable for inclusing in a `Proxy-Authorization` standard HTTP header like this (this only support Basic Authentication):
req.Header.Add("Proxy-Authorization", authstr)
To create the tailored HTTP `Transport`, you can use `SetupTransport()`.
req, transport := proxy.SetupTransport(URL)
URL is there to trigger the search for the various proxy definitions (through the environment variables like `HTTP_PROXY` or other means that are supported by `net/http`.
There are also two functions dealing with logging, log levels and stuff:
proxy.SetLevel(N) // 0 (default), 1 (verbose), 2 (debug)
proxy.SetLog(logger) // logger is a *log.Logger object
By default, nothing is logged but if you set to 1 or more, the default is to log to Stderr in a fairly classic way.
## `netrc` file
On UNIX systems like FreeBSD, macOS or Linux, the `.netrc`file is located in the user's home directory (aka `$HOME`). On Windows, I have decided to emulate this by looking for a `netrc` file (no ".") located in the AFAIK traditional location, designed by the `%LOCALAPPDATA%` variable.
Format:
machine HOST username USER password PASS
in our case, `HOST` **must** be **`proxy`** or **`default`**.
If there is no `netrc` file or no `proxy` entry with credentials, the HTTP proxy can still be used but without authentication.
## License
This is under the 2-Clause BSD license, see `LICENSE.md`.
## History
I originally wrote this code for the [erc-cimbl](https://github.com/keltia/erc-cimbl/) project and have re-used it enough time to think about putting it into its own module.
## Contributing
Please see CONTRIBUTING.md for some simple rules.