-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4a54b8e
Showing
20 changed files
with
593 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
assets/** filter=lfs diff=lfs merge=lfs -text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
build | ||
dist |
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.