grass

package module
v0.0.0-...-9d436b2 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2020 License: MIT Imports: 21 Imported by: 0

README

Grass

Grass is a Peer-to-Peer VPN.

Usage

Clone this repo, run go build main.go in the bin directory.

Sample config:

{
    "Id": 114514, // some random number
    "Key": "some_key111", // pre-shared key
    "InterfaceName": "tun233",
    "MTU": 5800,
    "IPv4": "10.56.0.2", // ipv4 for this client, ipv6 support will be added soon
    "IPv4Gateway": "10.56.0.1", // ipv4 gateway, in fact useless
    "IPv4Mask": "255.255.255.0",
    "IPv4DNS": "8.8.8.8,8.8.4.4", // seems only useful on windows
    "Listen": [
        {
            "LocalAddr": "0.0.0.0",
            "NetworkAddr": "192.168.254.1", // your ip on external network
            "Port": 14514
        },
        {
            "LocalAddr": "[::]",
            "NetworkAddr": "[fe80:114:514:1919:810::233]",
            "Port": 14515
        }
    ],
    "BootstrapNodes": ["11.4.5.14:1919"],
    "CheckClientsInterval": 1,
    "PingInterval": 20,
    "CheckConnInterval": 5,
    "Debug": false,
    "ConnectNewPeer": true, // if false, the client will not discover new clients
    "StartTun": true // if false, the client will not start tunnel interface
}

Documentation

Index

Constants

View Source
const (
	FarPingRequest uint32 = 0
	FarPingReply   uint32 = 1
	FarPingCircuit uint32 = 2
)
View Source
const (
	GrassPacketIPv4    uint8 = 0
	GrassPacketPing    uint8 = 1
	GrassPacketInfo    uint8 = 2
	GrassPacketPong    uint8 = 3
	GrassPacketClients uint8 = 4
	GrassPacketFarPing uint8 = 5
	GrassPacketExpired uint8 = 255
)
View Source
const MAX_PEER_CONNS = 8
View Source
const PacketMagicNumber uint8 = 114514 % 256

Variables

This section is empty.

Functions

func EncodePacketClients

func EncodePacketClients(pkt PacketClients) []byte

func EncodePacketFarPing

func EncodePacketFarPing(pkt PacketFarPing) []byte

func EncodePacketIPv4

func EncodePacketIPv4(pkt PacketIPv4) []byte

func IPToUint32

func IPToUint32(ip net.IP) uint32

func ReadPacket

func ReadPacket(pkt *Packet, conn *Conn, client *Client) error

func Uint32ToIP

func Uint32ToIP(intIP uint32) net.IP

func WritePacket

func WritePacket(pkt *Packet, conn *Conn, client *Client) error

func WriteRawPacket

func WriteRawPacket(typ uint8, content []byte, conn *Conn, client *Client) error

Types

type Client

type Client struct {
	Id                 uint32
	IPv4               uint32
	Key                []byte
	Peers              map[uint32]*Peer
	PeerMutex          *sync.RWMutex
	OClients           OtherClients
	LastCheckClients   OtherClientsInfo
	Tun                *network.TunHandler
	Config             ClientConfig
	ConnInfo           string
	KnownConnInfo      map[string]uint32
	KnownConnInfoMutex *sync.Mutex
	// contains filtered or unexported fields
}

func (*Client) CheckClients

func (client *Client) CheckClients()

func (*Client) CheckConn

func (client *Client) CheckConn()

func (*Client) HandleConn

func (client *Client) HandleConn(rawConn net.Conn, connInfo string) error

func (*Client) HandleFinalIPv4

func (client *Client) HandleFinalIPv4(pkt PacketIPv4) error

func (*Client) HandlePacketFarPing

func (client *Client) HandlePacketFarPing(pkt PacketFarPing) error

func (*Client) HandlePacketIPv4

func (client *Client) HandlePacketIPv4(pkt PacketIPv4) error

func (*Client) HandleTunIPv4

func (client *Client) HandleTunIPv4(data []byte, pkt *packet.IPv4) error

func (*Client) Start

func (client *Client) Start() error

func (*Client) Stop

func (client *Client) Stop()

type ClientConfig

type ClientConfig struct {
	Id                   uint32
	Key                  []byte
	InterfaceName        string
	MTU                  int
	IPv4                 uint32
	IPv4Gateway          uint32
	IPv4Mask             uint32
	IPv4DNS              []uint32
	Listen               []ListenInfo
	BootstrapNodes       []string
	CheckClientsInterval int
	PingInterval         int
	CheckConnInterval    int
	Debug                bool
	ConnectNewPeer       bool
	StartTun             bool
}

func UnmarshalConfig

func UnmarshalConfig(s []byte) (ClientConfig, error)

type Config

type Config struct {
	Id                   uint32
	Key                  string
	InterfaceName        string
	MTU                  int
	IPv4                 string
	IPv4Gateway          string
	IPv4Mask             string
	IPv4DNS              string
	Listen               []ListenInfo
	BootstrapNodes       []string
	CheckClientsInterval int
	PingInterval         int
	CheckConnInterval    int
	Debug                bool
	ConnectNewPeer       bool
	StartTun             bool
}

type Conn

type Conn struct {
	Conn    net.Conn
	Reader  *bufio.Reader
	Writer  Writer
	Latency []uint32

	WriteCh      chan QueuedPacket
	WriterStopCh chan bool
	// contains filtered or unexported fields
}

type LatencyPair

type LatencyPair struct {
	Id      uint32
	Latency uint32
}

type ListenInfo

type ListenInfo struct {
	LocalAddr   string
	NetworkAddr string
	Port        uint16
}

type OtherClient

type OtherClient struct {
	Id            uint32
	IPv4          uint32
	Latency       map[uint32]uint32 // connId->latency
	LatencyAdd    map[uint32]uint32
	LatencySorted []LatencyPair
	LatencyMutex  *sync.RWMutex
	ConnInfo      []string
	ConnInfoStr   string
}

func (*OtherClient) GetLatency

func (oc *OtherClient) GetLatency() uint32

func (*OtherClient) SortLatency

func (oc *OtherClient) SortLatency()

type OtherClientInfo

type OtherClientInfo struct {
	ConnInfo string
	IPv4     uint32
	Latency  uint32
}

type OtherClients

type OtherClients struct {
	C             map[uint32]*OtherClient
	M             *sync.RWMutex
	IPv4Map       map[uint32]uint32
	BannedClients *TmpKeySet
}

func (*OtherClients) ClearClients

func (c *OtherClients) ClearClients(viaId uint32)

func (*OtherClients) IPv4ToId

func (c *OtherClients) IPv4ToId(ipv4 uint32) uint32

func (*OtherClients) Init

func (c *OtherClients) Init(checkInterval int)

func (*OtherClients) UpdateClient

func (c *OtherClients) UpdateClient(id, ipv4, viaId, latency, latencyAdd uint32)

func (*OtherClients) UpdateClientConnInfo

func (c *OtherClients) UpdateClientConnInfo(id uint32, connInfo string)

func (*OtherClients) UpdateClients

func (c *OtherClients) UpdateClients(pkt PacketClients, viaId, addLatency uint32)

func (*OtherClients) UpdateLatencyAdd

func (c *OtherClients) UpdateLatencyAdd(viaId, addLatency uint32)

type OtherClientsInfo

type OtherClientsInfo map[uint32]OtherClientInfo

type Packet

type Packet struct {
	TmpKey    uint32
	Timestamp uint16
	Type      uint8
	CRC32     uint32
	Length    uint32
	Content   []byte
}

type PacketClients

type PacketClients struct {
	Count    uint64
	Ids      []uint32
	ConnInfo PacketClientsStringArr
	IPv4     PacketClientsUint32Arr
	Latency  PacketClientsUint32Arr
}

func DecodePacketClients

func DecodePacketClients(buf []byte) (PacketClients, error)

func GenDiffPacketClients

func GenDiffPacketClients(cur, old OtherClientsInfo) (PacketClients, bool)

type PacketClientsOkTypeArr

type PacketClientsOkTypeArr interface {
	SetOk(int)
	GetOk(int) bool
}

type PacketClientsString

type PacketClientsString struct {
	Val string
	Ok  bool
}

type PacketClientsStringArr

type PacketClientsStringArr []PacketClientsString

func (PacketClientsStringArr) GetOk

func (a PacketClientsStringArr) GetOk(n int) bool

func (PacketClientsStringArr) SetOk

func (a PacketClientsStringArr) SetOk(n int)

type PacketClientsUint32

type PacketClientsUint32 struct {
	Val uint32
	Ok  bool
}

type PacketClientsUint32Arr

type PacketClientsUint32Arr []PacketClientsUint32

func (PacketClientsUint32Arr) GetOk

func (a PacketClientsUint32Arr) GetOk(n int) bool

func (PacketClientsUint32Arr) SetOk

func (a PacketClientsUint32Arr) SetOk(n int)

type PacketFarPing

type PacketFarPing struct {
	SrcNode      uint32
	DstNode      uint32
	Status       uint32
	HistoryNodes []uint32
}

func DecodePacketFarPing

func DecodePacketFarPing(buf []byte) (PacketFarPing, error)

type PacketIPv4

type PacketIPv4 struct {
	TargetNode   uint32
	TTL          uint8
	HistoryNodes []uint32
	Data         []byte
	IPPacket     *packet.IPv4
}

func DecodePacketIPv4

func DecodePacketIPv4(buf []byte) (PacketIPv4, error)

type Peer

type Peer struct {
	Id              uint32
	Conns           []*Conn
	ConnInfo        []string
	ConnsMutex      *sync.RWMutex
	LastSendClients OtherClientsInfo
}

func (*Peer) AvgLatency

func (peer *Peer) AvgLatency() uint32

func (*Peer) SendPacket

func (peer *Peer) SendPacket(tp uint8, data []byte, client *Client) error

func (*Peer) SendPacketV2

func (peer *Peer) SendPacketV2(tp uint8, data []byte, client *Client, maxQlen int) bool

type QueuedPacket

type QueuedPacket struct {
	Type uint8
	Data []byte
}

type TmpKey

type TmpKey struct {
	Key  uint32
	Time uint
}

type TmpKeySet

type TmpKeySet struct {
	Keys   []TmpKey
	Map    map[uint32]struct{}
	Mutex  sync.Mutex
	Expire uint
}

func NewTmpKeySet

func NewTmpKeySet(expire uint) *TmpKeySet

func (*TmpKeySet) AddKey

func (s *TmpKeySet) AddKey(key uint32)

func (*TmpKeySet) HasKey

func (s *TmpKeySet) HasKey(key uint32) bool

type Writer

type Writer struct {
	W *bufio.Writer
	M *sync.Mutex
}

func NewWriter

func NewWriter(w io.Writer) Writer

Directories

Path Synopsis
tun

Jump to

Keyboard shortcuts

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