nat1

package module
v0.0.0-...-bd39d09 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2024 License: MIT Imports: 19 Imported by: 0

README

nat1

Expose your local service to Internet in NAT1 network.

  • Maintain the mapped address automatically.
  • Bind the mapped address to SVCB record.
  • Support both TCP and UDP.

Single mapping

Install the command line tool:

go install github.com/koho/nat1/cmd/nat1@latest
Zero config

A NAT entry is automatically created on gateway.

Note: The gateway needs to have UPnP enabled.

# export local port 3389
nat1 tcp 3389
Manual config

Create a public address mapping only. You need to manually configure the local address mapping on gateway (See Port forwarding).

# random local port
nat1 tcp
# fixed local port
nat1 tcp -l :5000

Multiple mappings

Bind the mapped address to DNS SVCB record. Currently only dnspod is supported.

Install the command line tool:

go install github.com/koho/nat1/cmd/nat1s@latest

An example config file:

{
  "dns": "xxx.dnspod.net:53",
  "dnspod": {
    "secret_id": "",
    "secret_key": ""
  },
  "service": [
    {
      "domain": "svc1.example.com",
      "local": "0.0.0.0:50000",
      "alpn": ["wg"],
      "network": "udp"  
    },
    {
      "domain": "svc2.example.com",
      "local": "0.0.0.0:50001",
      "alpn": ["h2"],
      "hint": true,
      "network": "tcp"
    }
  ]
}

Run the service with the following command:

nat1s config.json
Port forwarding

For each service, add corresponding forwarding rules to the router.

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 50001 -j DNAT --to-destination 192.168.1.55:443
Lookup domain

Your can find your mapped address using dig or https://www.nslookup.io/svcb-lookup/.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Service

type Service struct {
	*pb.Service
	// contains filtered or unexported fields
}

func NewService

func NewService(provider ns.NS, service *pb.Service, dnsServer string, ip string, port uint16) *Service

func (*Service) CompareAndUpdate

func (s *Service) CompareAndUpdate() error

func (*Service) Update

func (s *Service) Update(newIP string, newPort uint16) error

type StunClient

type StunClient interface {
	io.Closer
	AwaitConnection(ctx context.Context) error
	MapAddress(ctx context.Context) (lAddr, rAddr netip.AddrPort, err error)
}

func NewStunUDPClient

func NewStunUDPClient(localAddr string, server string) (StunClient, error)

type StunTCPClient

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

func NewStunTCPClient

func NewStunTCPClient(localAddr string, server string, keepaliveUrl string) (*StunTCPClient, error)

func (*StunTCPClient) AwaitConnection

func (c *StunTCPClient) AwaitConnection(ctx context.Context) error

func (*StunTCPClient) Close

func (c *StunTCPClient) Close() error

func (*StunTCPClient) MapAddress

func (c *StunTCPClient) MapAddress(ctx context.Context) (lAddr, rAddr netip.AddrPort, err error)

type StunUDPClient

type StunUDPClient struct {
	*net.UDPConn
	// contains filtered or unexported fields
}

func (*StunUDPClient) AwaitConnection

func (c *StunUDPClient) AwaitConnection(ctx context.Context) error

func (*StunUDPClient) Close

func (c *StunUDPClient) Close() error

func (*StunUDPClient) MapAddress

func (c *StunUDPClient) MapAddress(ctx context.Context) (lAddr, rAddr netip.AddrPort, err error)

Directories

Path Synopsis
cmd
ns

Jump to

Keyboard shortcuts

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