wpn

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2023 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIPBlocked          = errors.New("IP is blocked")
	ErrNetworkUnreachable = errors.New("Network unreachable") // network is not support
	ErrHostUnreachable    = errors.New("Host unreachable")
)
View Source
var DefaultTransport http.RoundTripper = &http.Transport{
	Proxy: nil,
	DialContext: (&net.Dialer{
		Timeout:   10 * time.Second,
		KeepAlive: 20 * time.Second,
	}).DialContext,
	ForceAttemptHTTP2:     true,
	MaxIdleConns:          30,
	IdleConnTimeout:       30 * time.Second,
	TLSHandshakeTimeout:   6 * time.Second,
	ExpectContinueTimeout: 1 * time.Second,
}
View Source
var (
	ErrIllegalStatus = errors.New("Illegal status error")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	Logger logger.Logger

	// wpn.Client.Transport will override wpn.Client.WebsocketOpts.HTTPClient.Transport if that is nil
	// Default value is wpn.DefaultTransport
	Transport     http.RoundTripper
	WebsocketOpts *websocket.DialOptions
	IdleTimeout   time.Duration
	MaxIdleConn   int
	// contains filtered or unexported fields
}

func NewClient

func NewClient(server string) (c *Client)

func (*Client) Dial

func (c *Client) Dial(protocol string, target string) (rw net.Conn, err error)

func (*Client) DialContext

func (c *Client) DialContext(ctx context.Context, protocol string, target string) (rw net.Conn, err error)

func (*Client) Ping

func (c *Client) Ping() (t time.Duration, err error)

func (*Client) Shutdown

func (c *Client) Shutdown()

type Conn

type Conn struct {
	PingInterval time.Duration
	Dialer       Dialer

	// If the function is not `nil`, and it returned false, the incoming connection will be blocked
	OnCheckIncoming func(protocol string, target string) bool
	// contains filtered or unexported fields
}

func WrapConn

func WrapConn(ctx context.Context, ws *websocket.Conn) (c *Conn)

func (*Conn) Close

func (c *Conn) Close() (err error)

return the latest error cause the connection closed

or the close handshake error (maybe nil) if there are no error during the connection is open

func (*Conn) Closed

func (c *Conn) Closed() bool

func (*Conn) Dial

func (c *Conn) Dial(protocol string, target string) (pipe *ConnPipe, err error)

`DialContext` will only success if `status` is `StatusIdle` or `StatusLocked` `Conn.Handle` must be called concurrently with `DialContext`

func (*Conn) DialContext

func (c *Conn) DialContext(ctx context.Context, protocol string, target string) (pipe *ConnPipe, err error)

`DialContext` will only success if `status` is `StatusIdle` or `StatusLocked` `Conn.Handle` must be called concurrently with `DialContext`

func (*Conn) Handle

func (c *Conn) Handle() (err error)

will automatically close connection if an error occurred

func (*Conn) Lock

func (c *Conn) Lock() (ok bool)

Lock the connection for later use on clientside only

func (*Conn) Ping

func (c *Conn) Ping() (t time.Duration, err error)

`Ping` will ping the remote point, return the time used and possible error If an error occured during ping, the connection will be automaticly closed The connection can ping with any status

func (*Conn) Status

func (c *Conn) Status() ConnStatus

func (*Conn) Unlock

func (c *Conn) Unlock() (ok bool)

Unlock will unlock the connection if it's locked and return true, or it will return false

type ConnPipe

type ConnPipe struct {
	net.Conn
	// contains filtered or unexported fields
}

func (*ConnPipe) AfterClose

func (p *ConnPipe) AfterClose() <-chan struct{}

func (*ConnPipe) Close

func (p *ConnPipe) Close() error

func (*ConnPipe) LocalAddr

func (p *ConnPipe) LocalAddr() net.Addr

func (*ConnPipe) RemoteAddr

func (p *ConnPipe) RemoteAddr() net.Addr

type ConnStatus

type ConnStatus byte
const (
	// The connection is idle
	StatusIdle ConnStatus = 0x00
	// The connection is used for packet based internet, such as IP, UDP
	StatusPacket ConnStatus = 0x01
	// The connection is used for stream based internet, such as TCP
	StatusStream ConnStatus = 0x02

	// The connection is locked for later use
	StatusLocked ConnStatus = 0xfe
	// The connection is closed
	StatusClosed ConnStatus = 0xff
)

type Dialer

type Dialer func(ctx context.Context, protocol string, target string) (net.Conn, error)

type IPRule

type IPRule struct {
	// An IP prefix, such as `192.168.0.1/24` or `::1/128`
	Prefix netip.Prefix
	// whether allow or not allow the prefix
	Not bool
}

func ParseIPRule

func ParseIPRule(text string) (r *IPRule, err error)

func (IPRule) Contains

func (r IPRule) Contains(ip netip.Addr) (ok bool)

Check if an IP match the rule. it will unmap the IP that passed in. for example, `127.0.0.1/8` contains `::ffff::127.1.2.3` note that it won't unmap the prefix, so `::ffff::127.0.0.1/104` doesn't contains `127.0.0.1`

func (*IPRule) MarshalText

func (r *IPRule) MarshalText() (buf []byte, err error)

func (*IPRule) UnmarshalText

func (r *IPRule) UnmarshalText(buf []byte) (err error)

Parse an IP rule from text Empty bytes is not ok. It will return io.EOF if the text's length is zero

type IPRuleSet

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

IPRuleSet is a list of IPRule.

func NewIPRuleSet

func NewIPRuleSet() (s *IPRuleSet)

Create an empty rule set.

func NewIPRuleSetFrom

func NewIPRuleSetFrom(rules []IPRule) (s *IPRuleSet)

Create a rule set from a slice of rules. It's the invoker's responsibility to ensure the rules are only exists once in the slice.

func ReadIPRuleSet

func ReadIPRuleSet(r io.Reader) (s *IPRuleSet, err error)

Read rule set from a text reader. `IPRuleSet.UnmarshalText` will be called

func (*IPRuleSet) Add

func (s *IPRuleSet) Add(r IPRule) (ok bool)

Add a rule to the rule set. If the rule is already exists in the rule set, the method will do nothing and return false. Otherwise, the new rule will be append to the end, and return true. Rule conflict won't be detect

func (*IPRuleSet) Contains

func (s *IPRuleSet) Contains(ip netip.Addr) (ok bool)

Check if an address is in the rule set or not. It will always return false if *IPRuleSet is nil.

Multiple include rules (Not == false) use `or (||)` operation. Multiple exclude rules (Not == true) use `and (&&)` operation. Exclude rules will always have the highest priority.

func (*IPRuleSet) MarshalText

func (s *IPRuleSet) MarshalText() (_ []byte, err error)

func (*IPRuleSet) Remove

func (s *IPRuleSet) Remove(r IPRule) (ok bool)

Remove a rule from the rule set

func (*IPRuleSet) Rules

func (s *IPRuleSet) Rules() []IPRule

Return the IP rule slice of the rule set. Operation on the slice it returns is not recommend and it's undefined. It will always return a nil slice if *IPRuleSet is nil.

func (*IPRuleSet) UnmarshalText

func (s *IPRuleSet) UnmarshalText(buf []byte) error

Parse a IPRuleSet file. each line will trim spaces before parse. empty line or line starts with hash sign ('#') will be ignored. During unmarshalling, the error lines will be skipped,

the errors will be returned after wrapped with `RuleSetError` and `errors.Join`.

type RemoteDialError

type RemoteDialError struct {
	Message string
}

func (*RemoteDialError) Error

func (e *RemoteDialError) Error() string

type RuleSetError

type RuleSetError struct {
	Line int
	Err  error
}

func (*RuleSetError) Error

func (e *RuleSetError) Error() string

func (*RuleSetError) Unwrap

func (e *RuleSetError) Unwrap() error

type Server

type Server struct {
	Logger logger.Logger

	WebsocketOpts *websocket.AcceptOptions
	Resolver      *net.Resolver
	Dialer        Dialer

	Blocks *IPRuleSet
}

func NewServer

func NewServer() (s *Server)

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type StatusError

type StatusError struct {
	Status ConnStatus
}

func (*StatusError) Error

func (e *StatusError) Error() string

Directories

Path Synopsis
client
This package provide a basic socks5 server
This package provide a basic socks5 server

Jump to

Keyboard shortcuts

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