mr2

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

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

Go to latest
Published: Jul 16, 2021 License: GPL-3.0 Imports: 15 Imported by: 0

README

mr2

中文

Donate Slack

mr2 can help you expose local server to external network. Support both TCP/UDP, of course support HTTP. Keep it simple, stupid.

❤️ A project by txthinking.com

Install via nami
$ nami install github.com/txthinking/mr2
Install via brew (macOS only)
$ brew install mr2
Usage
NAME:
   mr2 - Expose local TCP and UDP server to external network

USAGE:
   mr2 [global options] command [command options] [arguments...]

VERSION:
   20210401

COMMANDS:
   server       Run as server mode
   client       Run as client mode
   httpsserver  Run as https server mode
   httpsclient  Run as https client mode
   help, h      Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --help, -h     show help (default: false)
   --version, -v  print the version (default: false)

server and client

On remote server. Note that the firewall opens TCP and UDP on all relevant ports

$ mr2 server -l :9999 -p password

More parameters: $ mr2 server -h

On local. Assume your remote mr2 server is 1.2.3.4:9999, your local server is 127.0.0.1:8080, want the remote server to open port 8888

$ mr2 client -s 1.2.3.4:9999 -p password --serverPort 8888 -c 127.0.0.1:8080

More parameters: $ mr2 client -h

Then access 1.2.3.4:8888 equals to access 127.0.0.1:8080

Example of server and client

Expose local HTTP server
$ mr2 client -s 1.2.3.4:9999 -p password --serverPort 8888 -c 127.0.0.1:8080

Then access 1.2.3.4:8888 equals to access 127.0.0.1:8080

Expose local SSH
$ mr2 client -s 1.2.3.4:9999 -p password --serverPort 8888 -c 127.0.0.1:22

Then access 1.2.3.4:8888 equals to access 127.0.0.1:22

$ ssh -oPort=8888 yourlocaluser@1.2.3.4
Expose local DNS server
$ mr2 client -s 1.2.3.4:9999 -p password --serverPort 8888 -c 127.0.0.1:53

Then access 1.2.3.4:8888 equals to access 127.0.0.1:53

$ dig github.com @1.2.3.4 -p 8888
Expose local directory via HTTP
$ mr2 client -s 1.2.3.4:9999 -p password --serverPort 8888 --clientDirectory /path/to/www --clientPort 8080

Then access 1.2.3.4:8888 equals to access 127.0.0.1:8080, web root is /path/to/www

Expose local brook server
$ brook server -l :8080 -p password # or wsserver
$ mr2 client -s 1.2.3.4:9999 -p password --serverPort 8888 -c 127.0.0.1:8080

Then access 1.2.3.4:8888 equals to access 127.0.0.1:8080, used to create a brook server or wsserver in a server even if there is no public IP

Expose any TCP/UDP service
...

httpsserver and httpsclient

On remote server. Assume your domain is domain.com, cert of *.domain.com is ./domain_com_cert.pem and ./domain_com_cert_key.pem, want https listen on 443. Note that the firewall opens TCP on all relevant ports

$ mr2 httpsserver -l :9999 -p password --domain domain.com --cert ./domain_com_cert.pem --certKey ./domain_com_cert_key.pem --tlsPort 443

More parameters: $ mr2 httpsserver -h

On local. Assume your remote mr2 httpsserver is 1.2.3.4:9999, your local HTTP 1.1 server is 127.0.0.1:8080, want the remote server to open subdomain hey

$ mr2 httpsclient -s 1.2.3.4:9999 -p password --serverSubdomain hey -c 127.0.0.1:8080

More parameters: $ mr2 httpsclient -h

Then access https://hey.domain.com:443 equals to access http://127.0.0.1:8080

About UDP

In some cases of multi-layer NAT, UDP may fail. I passed the test when I connected directly to the Wi-Fi provided by the ISP.

License

Licensed under The GPLv3 License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLengthMr2 = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowMr2   = fmt.Errorf("proto: integer overflow")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	Server       string
	ServerPort   int64
	ServerDomain string
	ClientServer string
	TCPDeadline  int64
	TCPTimeout   int64
	UDPDeadline  int64
	UDPConn      *net.UDPConn
	Ckv          *encrypt.KV
	TCPClient    *TCPClient
	UDPClient    *UDPClient
}

Client .

func NewClient

func NewClient(server, password string, serverPort int64, serverDomain, clientServer string, tcpTimeout, tcpDeadline, udpDeadline int64) *Client

NewClient .

func (*Client) ListenAndServe

func (c *Client) ListenAndServe() error

Run .

func (*Client) Shutdown

func (c *Client) Shutdown() error

Shutdown server.

type PingPong

type PingPong struct {
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*PingPong) Descriptor

func (*PingPong) Descriptor() ([]byte, []int)

func (*PingPong) Marshal

func (m *PingPong) Marshal() (dAtA []byte, err error)

func (*PingPong) MarshalTo

func (m *PingPong) MarshalTo(dAtA []byte) (int, error)

func (*PingPong) ProtoMessage

func (*PingPong) ProtoMessage()

func (*PingPong) Reset

func (m *PingPong) Reset()

func (*PingPong) Size

func (m *PingPong) Size() (n int)

func (*PingPong) String

func (m *PingPong) String() string

func (*PingPong) Unmarshal

func (m *PingPong) Unmarshal(dAtA []byte) error

func (*PingPong) XXX_DiscardUnknown

func (m *PingPong) XXX_DiscardUnknown()

func (*PingPong) XXX_Marshal

func (m *PingPong) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*PingPong) XXX_Merge

func (dst *PingPong) XXX_Merge(src proto.Message)

func (*PingPong) XXX_Size

func (m *PingPong) XXX_Size() int

func (*PingPong) XXX_Unmarshal

func (m *PingPong) XXX_Unmarshal(b []byte) error

type Server

type Server struct {
	TCPAddr   *net.TCPAddr
	UDPAddr   *net.UDPAddr
	TCPListen *net.TCPListener
	UDPConn   *net.UDPConn
	Cache     *cache.Cache
	Ckv       *encrypt.KV
	PortCkv   map[int64]*encrypt.KV
}

Server .

func NewServer

func NewServer(addr, password string, portPassword []string) (*Server, error)

NewServer .

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe .

func (*Server) RunTCPServer

func (s *Server) RunTCPServer() error

RunTCPServer

func (*Server) RunUDPServer

func (s *Server) RunUDPServer() error

RunUDPServer

func (*Server) Shutdown

func (s *Server) Shutdown() error

Shutdown server.

func (*Server) TCPHandle

func (s *Server) TCPHandle(c *net.TCPConn) error

TCPHandle

func (*Server) UDPHandle

func (s *Server) UDPHandle(addr *net.UDPAddr, b []byte) error

UDPHandle

type TCPClient

type TCPClient struct {
	Client  *Client
	TCPConn *net.TCPConn
	Cache   *cache.Cache
	Done    chan byte
	Data    chan []byte
	Error   chan error
}

TCPClient .

func NewTCPClient

func NewTCPClient(c *Client) (*TCPClient, error)

NewTCPClient .

func (*TCPClient) Ping

func (c *TCPClient) Ping()

Ping server .

func (*TCPClient) Read

func (c *TCPClient) Read()

Read data from server.

func (*TCPClient) Run

func (c *TCPClient) Run() error

Run .

func (*TCPClient) Stop

func (c *TCPClient) Stop()

Stop .

type TCPHello

type TCPHello struct {
	Port                 int64    `protobuf:"varint,1,opt,name=Port,proto3" json:"Port,omitempty"`
	TCPTimeout           int64    `protobuf:"varint,2,opt,name=TCPTimeout,proto3" json:"TCPTimeout,omitempty"`
	TCPDeadline          int64    `protobuf:"varint,3,opt,name=TCPDeadline,proto3" json:"TCPDeadline,omitempty"`
	Key                  string   `protobuf:"bytes,4,opt,name=Key,proto3" json:"Key,omitempty"`
	Domain               string   `protobuf:"bytes,5,opt,name=Domain,proto3" json:"Domain,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*TCPHello) Descriptor

func (*TCPHello) Descriptor() ([]byte, []int)

func (*TCPHello) GetDomain

func (m *TCPHello) GetDomain() string

func (*TCPHello) GetKey

func (m *TCPHello) GetKey() string

func (*TCPHello) GetPort

func (m *TCPHello) GetPort() int64

func (*TCPHello) GetTCPDeadline

func (m *TCPHello) GetTCPDeadline() int64

func (*TCPHello) GetTCPTimeout

func (m *TCPHello) GetTCPTimeout() int64

func (*TCPHello) Marshal

func (m *TCPHello) Marshal() (dAtA []byte, err error)

func (*TCPHello) MarshalTo

func (m *TCPHello) MarshalTo(dAtA []byte) (int, error)

func (*TCPHello) ProtoMessage

func (*TCPHello) ProtoMessage()

func (*TCPHello) Reset

func (m *TCPHello) Reset()

func (*TCPHello) Size

func (m *TCPHello) Size() (n int)

func (*TCPHello) String

func (m *TCPHello) String() string

func (*TCPHello) Unmarshal

func (m *TCPHello) Unmarshal(dAtA []byte) error

func (*TCPHello) XXX_DiscardUnknown

func (m *TCPHello) XXX_DiscardUnknown()

func (*TCPHello) XXX_Marshal

func (m *TCPHello) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*TCPHello) XXX_Merge

func (dst *TCPHello) XXX_Merge(src proto.Message)

func (*TCPHello) XXX_Size

func (m *TCPHello) XXX_Size() int

func (*TCPHello) XXX_Unmarshal

func (m *TCPHello) XXX_Unmarshal(b []byte) error

type TCPPacket

type TCPPacket struct {
	Address              string   `protobuf:"bytes,1,opt,name=Address,proto3" json:"Address,omitempty"`
	Data                 []byte   `protobuf:"bytes,2,opt,name=Data,proto3" json:"Data,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*TCPPacket) Descriptor

func (*TCPPacket) Descriptor() ([]byte, []int)

func (*TCPPacket) GetAddress

func (m *TCPPacket) GetAddress() string

func (*TCPPacket) GetData

func (m *TCPPacket) GetData() []byte

func (*TCPPacket) Marshal

func (m *TCPPacket) Marshal() (dAtA []byte, err error)

func (*TCPPacket) MarshalTo

func (m *TCPPacket) MarshalTo(dAtA []byte) (int, error)

func (*TCPPacket) ProtoMessage

func (*TCPPacket) ProtoMessage()

func (*TCPPacket) Reset

func (m *TCPPacket) Reset()

func (*TCPPacket) Size

func (m *TCPPacket) Size() (n int)

func (*TCPPacket) String

func (m *TCPPacket) String() string

func (*TCPPacket) Unmarshal

func (m *TCPPacket) Unmarshal(dAtA []byte) error

func (*TCPPacket) XXX_DiscardUnknown

func (m *TCPPacket) XXX_DiscardUnknown()

func (*TCPPacket) XXX_Marshal

func (m *TCPPacket) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*TCPPacket) XXX_Merge

func (dst *TCPPacket) XXX_Merge(src proto.Message)

func (*TCPPacket) XXX_Size

func (m *TCPPacket) XXX_Size() int

func (*TCPPacket) XXX_Unmarshal

func (m *TCPPacket) XXX_Unmarshal(b []byte) error

type TCPServer

type TCPServer struct {
	Server      *Server
	Cache       *cache.Cache
	TCPConn     *net.TCPConn
	TCPListen   *net.TCPListener
	TCPTimeout  int64
	TCPDeadline int64
	Done        chan byte
	Data        chan []byte
	Error       chan error
}

TCPServer .

func NewTCPServer

func NewTCPServer(s *Server, c *net.TCPConn) (*TCPServer, error)

NewTCPServer. Some cases return nil, nil

func (*TCPServer) Accept

func (s *TCPServer) Accept()

Accept consumer.

func (*TCPServer) ListenAndServe

func (s *TCPServer) ListenAndServe() error

ListenAndServe .

func (*TCPServer) Read

func (s *TCPServer) Read()

Read data from client.

func (*TCPServer) Shutdown

func (s *TCPServer) Shutdown()

Shutdown .

type UDPClient

type UDPClient struct {
	Client  *Client
	UDPConn *net.UDPConn
	Cache   *cache.Cache
	Done    chan byte
	Data    chan []byte
	Error   chan error
}

UDPClient .

func NewUDPClient

func NewUDPClient(c *Client) (*UDPClient, error)

NewUDPClient .

func (*UDPClient) Ping

func (c *UDPClient) Ping()

Ping server .

func (*UDPClient) Read

func (c *UDPClient) Read()

Read data from server.

func (*UDPClient) Run

func (c *UDPClient) Run() error

Run .

func (*UDPClient) Stop

func (c *UDPClient) Stop()

Stop .

type UDPPacket

type UDPPacket struct {
	Port                 int64    `protobuf:"varint,1,opt,name=Port,proto3" json:"Port,omitempty"`
	Address              string   `protobuf:"bytes,2,opt,name=Address,proto3" json:"Address,omitempty"`
	Data                 []byte   `protobuf:"bytes,3,opt,name=Data,proto3" json:"Data,omitempty"`
	Key                  string   `protobuf:"bytes,4,opt,name=Key,proto3" json:"Key,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*UDPPacket) Descriptor

func (*UDPPacket) Descriptor() ([]byte, []int)

func (*UDPPacket) GetAddress

func (m *UDPPacket) GetAddress() string

func (*UDPPacket) GetData

func (m *UDPPacket) GetData() []byte

func (*UDPPacket) GetKey

func (m *UDPPacket) GetKey() string

func (*UDPPacket) GetPort

func (m *UDPPacket) GetPort() int64

func (*UDPPacket) Marshal

func (m *UDPPacket) Marshal() (dAtA []byte, err error)

func (*UDPPacket) MarshalTo

func (m *UDPPacket) MarshalTo(dAtA []byte) (int, error)

func (*UDPPacket) ProtoMessage

func (*UDPPacket) ProtoMessage()

func (*UDPPacket) Reset

func (m *UDPPacket) Reset()

func (*UDPPacket) Size

func (m *UDPPacket) Size() (n int)

func (*UDPPacket) String

func (m *UDPPacket) String() string

func (*UDPPacket) Unmarshal

func (m *UDPPacket) Unmarshal(dAtA []byte) error

func (*UDPPacket) XXX_DiscardUnknown

func (m *UDPPacket) XXX_DiscardUnknown()

func (*UDPPacket) XXX_Marshal

func (m *UDPPacket) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*UDPPacket) XXX_Merge

func (dst *UDPPacket) XXX_Merge(src proto.Message)

func (*UDPPacket) XXX_Size

func (m *UDPPacket) XXX_Size() int

func (*UDPPacket) XXX_Unmarshal

func (m *UDPPacket) XXX_Unmarshal(b []byte) error

type UDPServer

type UDPServer struct {
	Server  *Server
	UDPConn *net.UDPConn
	Addr    *net.UDPAddr
}

UDPServer .

func NewUDPServer

func NewUDPServer(s *Server, p *UDPPacket, addr *net.UDPAddr) (*UDPServer, error)

NewUDPServer .

func (*UDPServer) HandlePacket

func (s *UDPServer) HandlePacket(p *UDPPacket) error

HandlePacket sends data to consumer.

func (*UDPServer) ListenAndServe

func (s *UDPServer) ListenAndServe() error

ListenAndServe .

func (*UDPServer) Shutdown

func (s *UDPServer) Shutdown()

Shutdown .

Directories

Path Synopsis
cli
mr2

Jump to

Keyboard shortcuts

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