nathole

package
v0.0.0-...-1656c47 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2024 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EasyNAT = "EasyNAT"
	HardNAT = "HardNAT"

	BehaviorNoChange    = "BehaviorNoChange"
	BehaviorIPChanged   = "BehaviorIPChanged"
	BehaviorPortChanged = "BehaviorPortChanged"
	BehaviorBothChanged = "BehaviorBothChanged"
)

Variables

View Source
var (
	// mode 0: simple detect mode, usually for both EasyNAT or HardNAT & EasyNAT(Public Network)
	// a. receiver sends detect message with low TTL
	// b. sender sends normal detect message to receiver
	// c. receiver receives detect message and sends back a message to sender
	//
	// mode 1: For HardNAT & EasyNAT, send detect messages to multiple guessed ports.
	// Usually applicable to scenarios where port changes are regular.
	// Most of the steps are the same as mode 0, but EasyNAT is fixed as the receiver and will send detect messages
	// with low TTL to multiple guessed ports of the sender.
	//
	// mode 2: For HardNAT & EasyNAT, ports changes are not regular.
	// a. HardNAT machine will listen on multiple ports and send detect messages with low TTL to EasyNAT machine
	// b. EasyNAT machine will send detect messages to random ports of HardNAT machine.
	//
	// mode 3: For HardNAT & HardNAT, both changes in the ports are regular.
	// Most of the steps are the same as mode 1, but the sender also needs to send detect messages to multiple guessed
	// ports of the receiver.
	//
	// mode 4: For HardNAT & HardNAT, one of the changes in the ports is regular.
	// Regular port changes are usually on the sender side.
	// a. Receiver listens on multiple ports and sends detect messages with low TTL to the sender's guessed range ports.
	// b. Sender sends detect messages to random ports of the receiver.
	SupportedModes = []int{DetectMode0, DetectMode1, DetectMode2, DetectMode3, DetectMode4}
	SupportedRoles = []string{DetectRoleSender, DetectRoleReceiver}

	DetectMode0        = 0
	DetectMode1        = 1
	DetectMode2        = 2
	DetectMode3        = 3
	DetectMode4        = 4
	DetectRoleSender   = "sender"
	DetectRoleReceiver = "receiver"
)
View Source
var NatHoleTimeout int64 = 10

NatHoleTimeout seconds.

Functions

func ClassifyFeatureCount

func ClassifyFeatureCount(features []*NatFeature) (int, int, int)

func DecodeMessageInto

func DecodeMessageInto(data, key []byte, m msg.Message) error

func Discover

func Discover(stunServers []string, localAddr string) ([]string, net.Addr, error)

If the localAddr is empty, it will listen on a random port.

func EncodeMessage

func EncodeMessage(m msg.Message, key []byte) ([]byte, error)

func ExchangeInfo

func ExchangeInfo(
	ctx context.Context, transporter transport.MessageTransporter,
	laneKey string, m msg.Message, timeout time.Duration,
) (*msg.NatHoleResp, error)

ExchangeInfo is used to exchange information between client and visitor. 1. Send input message to server by msgTransporter. 2. Server will gather information from client and visitor and analyze it. Then send back a NatHoleResp message to them to tell them how to do next. 3. Receive NatHoleResp message from server.

func ListAllLocalIPs

func ListAllLocalIPs() ([]net.IP, error)

func ListLocalIPsForNatHole

func ListLocalIPsForNatHole(maxItems int) ([]string, error)

func MakeHole

func MakeHole(ctx context.Context, listenConn *net.UDPConn, m *msg.NatHoleResp, key []byte) (*net.UDPConn, *net.UDPAddr, error)

MakeHole is used to make a NAT hole between client and visitor.

func NewTransactionID

func NewTransactionID() string

func PreCheck

func PreCheck(
	ctx context.Context, transporter transport.MessageTransporter,
	proxyName string, timeout time.Duration,
) error

PreCheck is used to check if the proxy is ready for penetration. Call this function before calling Prepare to avoid unnecessary preparation work.

Types

type Analyzer

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

func NewAnalyzer

func NewAnalyzer(dataReserveDuration time.Duration) *Analyzer

func (*Analyzer) Clean

func (a *Analyzer) Clean() (int, int)

func (*Analyzer) GetRecommandBehaviors

func (a *Analyzer) GetRecommandBehaviors(key string, c, v *NatFeature) (mode, index int, _ RecommandBehavior, _ RecommandBehavior)

func (*Analyzer) ReportSuccess

func (a *Analyzer) ReportSuccess(key string, mode, index int)

type BehaviorScore

type BehaviorScore struct {
	Mode  int
	Index int
	// between -10 and 10
	Score int
}

type ChangedAddress

type ChangedAddress struct {
	IP   net.IP
	Port int
}

func (*ChangedAddress) GetFrom

func (s *ChangedAddress) GetFrom(m *stun.Message) error

func (*ChangedAddress) String

func (s *ChangedAddress) String() string

type ClientCfg

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

type Controller

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

func NewController

func NewController(analysisDataReserveDuration time.Duration) (*Controller, error)

func (*Controller) CleanWorker

func (c *Controller) CleanWorker(ctx context.Context)

func (*Controller) CloseClient

func (c *Controller) CloseClient(name string)

func (*Controller) GenNatHoleResponse

func (c *Controller) GenNatHoleResponse(transactionID string, session *Session, errInfo string) *msg.NatHoleResp

func (*Controller) GenSid

func (c *Controller) GenSid() string

func (*Controller) HandleClient

func (c *Controller) HandleClient(m *msg.NatHoleClient, transporter transport.MessageTransporter)

func (*Controller) HandleReport

func (c *Controller) HandleReport(m *msg.NatHoleReport)

func (*Controller) HandleVisitor

func (c *Controller) HandleVisitor(m *msg.NatHoleVisitor, transporter transport.MessageTransporter, visitorUser string)

func (*Controller) ListenClient

func (c *Controller) ListenClient(name string, sk string, allowUsers []string) (chan string, error)

type MakeHoleRecords

type MakeHoleRecords struct {
	LastUpdateTime time.Time
	// contains filtered or unexported fields
}

func NewMakeHoleRecords

func NewMakeHoleRecords(c, v *NatFeature) *MakeHoleRecords

func (*MakeHoleRecords) Recommand

func (mhr *MakeHoleRecords) Recommand() (mode, index int)

func (*MakeHoleRecords) ReportSuccess

func (mhr *MakeHoleRecords) ReportSuccess(mode int, index int)

type Message

type Message struct {
	Body []byte
	Addr string
}

type NatFeature

type NatFeature struct {
	NatType            string
	Behavior           string
	PortsDifference    int
	RegularPortsChange bool
	PublicNetwork      bool
}

func ClassifyNATFeature

func ClassifyNATFeature(addresses []string, localIPs []string) (*NatFeature, error)

type PrepareResult

type PrepareResult struct {
	Addrs         []string
	AssistedAddrs []string
	ListenConn    *net.UDPConn
	NatType       string
	Behavior      string
}

func Prepare

func Prepare(stunServers []string) (*PrepareResult, error)

Prepare is used to do some preparation work before penetration.

type RecommandBehavior

type RecommandBehavior struct {
	Role              string
	TTL               int
	SendDelayMs       int
	PortsRangeNumber  int
	PortsRandomNumber int
	ListenRandomPorts int
}

type Session

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

Jump to

Keyboard shortcuts

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