ipmux

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2024 License: MIT Imports: 9 Imported by: 0

README

IPMux

pipeline codecov Go Report Card Go Reference

GoLang Library for Multiplexing HTTP Clients based on Source IP

IPMux is an open-source GoLang library that provides a simple and efficient way to multiplex HTTP clients based on source IP addresses. This library is designed to handle scenarios where you need to make HTTP requests from specific network interfaces based on the source IP.

This library could be useful to bypass rate limit errors which operate on source IPs.

Features

  • Multiplex HTTP clients based on source IP addresses.
  • Customizable options for creating HTTP clients.
  • Easy-to-use interface for seamless integration.
  • Enabling DNS cache with a refresh interval.

Getting Started

Installation
go get github.com/optimus-hft/go-ipmux
Usage
package main

import (
	"fmt"
	"io"
	"time"

	"github.com/optimus-hft/go-ipmux"
)

func main() {
	// both ips should be attached to your device for IPMux to operate properly
	ips := []string{"192.168.0.1", "192.168.0.2"}
	ipMux, err := ipmux.New(ips, ipmux.WithTimeout(10*time.Second))
	if err != nil {
		fmt.Println(err)
		return
	}

	client := ipMux.Client()
	response, err := client.Get("https://google.com")
	if err != nil {
		panic(err)
	}
	defer response.Body.Close()
	fmt.Println(io.ReadAll(response.Body))
}

API Documentation

func New(ips []string, options ...Option) (*IPMux, error)
  • ips: List of IP addresses to create HTTP clients for.
  • options: Optional parameters to customize client creation. Returns a new IPMux instance.
func (i *IPMux) Client() *http.Client

Returns an HTTP client associated with one of the IPs provided in the New function. Returns http.DefaultClient if no clients are available.

func (i *IPMux) Clients() []*http.Client

Returns a list of all clients created for the list of IPs given in New. Returns a list containing one client (http.DefaultClient) when there are no available clients.

Options
WithBaseClient(baseClient *http.Client) Option

Change the base client used to create clients for IPMux.

WithTimeout(timeout time.Duration) Option

Set timeout on both client and dialer of the clients.

WithKeepAlive(keepalive time.Duration) Option

Set keepalive on dialer of the clients.

WithBaseTransport(transport *http.Transport) Option

Change the base transport (http.RoundTripper) used to create clients for IPMux.

WithDialer(dialer *net.Dialer) Option

Change the base dialer used to create clients for IPMux.

WithDNSCache(refreshInterval time.Duration) Option

Enable the DNS cache feature with a refresh interval.

WithContext(ctx context.Context) Option

Change the default context to control background goroutine like DNS cache refresh. The default context is context.Background().

Contributing

Pull requests and bug reports are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidIP = errors.New("invalid source ip, not found in device network interfaces")

Functions

This section is empty.

Types

type IPMux

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

func New

func New(ips []string, options ...Option) (*IPMux, error)

New is the constructor of IPMux. It creates a http.Client for each of the ips given. If there are any errors for one of the ips, the client will not be created for that ip but the other clients will be created. you can customize the created clients with Option functions.

func (*IPMux) Client

func (i *IPMux) Client() *http.Client

Client returns one of the clients that is associated with one of the IPs given in New. the function is safe to use without error handling of the constructor. it returns http.DefaultClient when there are no available clients.

func (*IPMux) ClientWithCounter added in v0.3.0

func (i *IPMux) ClientWithCounter(counter uint64) *http.Client

ClientWithCounter returns one of the clients that is associated with one of the IPs given in New. the function is safe to use without error handling of the constructor. it returns http.DefaultClient when there are no available clients.

func (*IPMux) Clients

func (i *IPMux) Clients() []*http.Client

Clients returns a list of all clients created for the list of ips given in New. the function is safe to use without error handling of the constructor. it returns a list containing ob client (http.DefaultClient) when there are no available clients.

func (*IPMux) Stop added in v0.2.0

func (i *IPMux) Stop()

Stop stops the ipMux.

type Option

type Option func(base *clientBaseOpts)

func WithBaseClient

func WithBaseClient(baseClient *http.Client) Option

WithBaseClient is used to change the base client that is used to create the clients for IPMux.

func WithBaseTransport

func WithBaseTransport(transport *http.Transport) Option

WithBaseTransport is used to change the base transport (http.RoundTripper) that is used to create the clients for IPMux.

func WithContext added in v0.2.0

func WithContext(ctx context.Context) Option

WithContext is used to change the context that is used to detect when to stop background goroutines like refreshing dns cache.

func WithDNSCache added in v0.2.0

func WithDNSCache(refreshInterval time.Duration) Option

WithDNSCache is used to enable dns cache for the clients.

func WithDialer

func WithDialer(dialer *net.Dialer) Option

WithDialer is used to change the base dialer that is used to create the clients for IPMux.

func WithKeepAlive

func WithKeepAlive(keepalive time.Duration) Option

WithKeepAlive is used to set keepalive on dialer of the clients.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout is used to set timeout on both client and dialer of the clients.

Jump to

Keyboard shortcuts

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