Skip to content

Commit

Permalink
initial draft
Browse files Browse the repository at this point in the history
  • Loading branch information
reddec committed Apr 7, 2020
0 parents commit 4a54b8e
Show file tree
Hide file tree
Showing 20 changed files with 593 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assets/** filter=lfs diff=lfs merge=lfs -text
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
build
dist
3 changes: 3 additions & 0 deletions assets/windows/tap-win64/OemWin2k.inf
Git LFS file not shown
3 changes: 3 additions & 0 deletions assets/windows/tap-win64/tap0902.cat
Git LFS file not shown
3 changes: 3 additions & 0 deletions assets/windows/tap-win64/tap0902.sys
Git LFS file not shown
3 changes: 3 additions & 0 deletions assets/windows/tap-win64/tapinstall.exe
Git LFS file not shown
3 changes: 3 additions & 0 deletions assets/windows/tincd.exe
Git LFS file not shown
1 change: 1 addition & 0 deletions cmd/tinc-web-boot/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module tinc-web-boot

go 1.13

require github.com/phayes/permbits v0.0.0-20190612203442-39d7c581d2ee // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/phayes/permbits v0.0.0-20190612203442-39d7c581d2ee h1:P6U24L02WMfj9ymZTxl7CxS73JC99x3ukk+DBkgQGQs=
github.com/phayes/permbits v0.0.0-20190612203442-39d7c581d2ee/go.mod h1:3uODdxMgOaPYeWU7RzZLxVtJHZ/x1f/iHkBZuKJDzuY=
21 changes: 21 additions & 0 deletions network/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package network

type Config struct {
Name string
Port uint16
Interface string
ConnectTo []string
}

type Address struct {
Host string
Port uint16
}

type Node struct {
Name string
Subnet string
Port uint16
Address []Address
PublicKey string
}
26 changes: 26 additions & 0 deletions network/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package network

import "testing"

func Test_parse(t *testing.T) {
node := Node{
Name: "TEST",
Subnet: "1.2.3.4/32",
Address: []Address{{Host: "127.0.0.1", Port: 321}, {Host: "127.0.0.1", Port: 1223}},
PublicKey: "---\nXXX\n---",
}
text, err := node.MarshalText()
if err != nil {
t.Error(err)
return
}
t.Log(string(text))

var cp Node
err = cp.UnmarshalText(text)
if err != nil {
t.Error(err)
return
}
t.Logf("%+v", cp)
}
170 changes: 170 additions & 0 deletions network/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package network

import (
"encoding"
"fmt"
"sort"
"strconv"
"strings"
)

func (cfg *Config) MarshalText() (text []byte, err error) {
var params = map[string][]interface{}{
"Name": {cfg.Name},
"Port": {fmt.Sprint(cfg.Port)},
"Interface": {cfg.Interface},
}
for _, con := range cfg.ConnectTo {
params["ConnectTo"] = append(params["ConnectTo"], con)
}
return makeContent(params, "")
}

func (cfg *Config) UnmarshalText(text []byte) error {
params, _ := parseContent(string(text))
cfg.ConnectTo = params["ConnectTo"]
cfg.Interface = params.First("Interface", "")
cfg.Name = params.First("Name", "")
cfg.Port = params.FirstUint16("Port")
return nil
}

func (a *Address) MarshalText() (text []byte, err error) {
out := a.Host
if a.Port != 0 {
out += " " + strconv.FormatUint(uint64(a.Port), 10)
}
return []byte(out), nil
}

func (a *Address) UnmarshalText(text []byte) error {
vals := strings.Split(string(text), " ")
a.Host = vals[0]
if len(vals) == 1 {
return nil
}
p, err := strconv.ParseUint(vals[1], 10, 16)
if err != nil {
return err
}
a.Port = uint16(p)
return nil
}

func (n *Node) MarshalText() (text []byte, err error) {
params := map[string][]interface{}{
"Name": {n.Name},
"Subnet": {n.Subnet},
"Port": {fmt.Sprint(n.Port)},
}
for i := range n.Address {
params["Address"] = append(params["Address"], &n.Address[i])
}

return makeContent(params, n.PublicKey)
}

func (n *Node) UnmarshalText(data []byte) error {
params, tail := parseContent(string(data))
n.Name = first(params["Name"], "")
n.Subnet = first(params["Subnet"], "")
n.PublicKey = tail
n.Address = nil
for _, addr := range params["Address"] {
var a Address
err := a.UnmarshalText([]byte(addr))
if err != nil {
return err
}
n.Address = append(n.Address, a)
}
n.Port = params.FirstUint16("Port")
return nil
}

func first(vals []string, def string) string {
if len(vals) > 0 {
return vals[0]
}
return def
}

type multiMap map[string][]string

func (mm *multiMap) First(name, def string) string {
if mm == nil {
return def
}
v := (*mm)[name]
if len(v) == 0 {
return def
}
return v[0]
}

func (mm *multiMap) FirstUint16(name string) uint16 {
v := mm.First(name, "0")
x, _ := strconv.ParseUint(v, 10, 16)
return uint16(x)
}

func parseContent(content string) (params multiMap, tail string) {
var offset = 0
params = make(map[string][]string)
for i, r := range []rune(content) {
if r != '\n' {
continue
}
line := strings.TrimSpace(content[offset:i])

if len(line) == 0 || line[0] == '#' {
continue
}
kv := strings.SplitN(line, "=", 2)
if len(kv) != 2 {
break
}
offset = i + 1
key := strings.TrimSpace(kv[0])
value := strings.TrimSpace(kv[1])
params[key] = append(params[key], value)
}
tail = content[offset:]
return
}

func makeContent(params map[string][]interface{}, tail string) ([]byte, error) {
out := &strings.Builder{}
var keys = make([]string, len(params))
for k := range params {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := params[k]
for _, item := range v {
if item == nil {
continue
}
var val []byte
if coder, ok := item.(encoding.TextMarshaler); ok {
x, err := coder.MarshalText()
if err != nil {
return nil, err
}
val = x
} else {
val = []byte(fmt.Sprint(item))
}
if len(val) == 0 {
continue
}
out.WriteString(k)
out.WriteString(" = ")
out.Write(val)
out.WriteRune('\n')
}
}
out.WriteString(tail)
return []byte(out.String()), nil
}
Loading

0 comments on commit 4a54b8e

Please sign in to comment.