first configuration

This commit is contained in:
Jake Hillion 2020-10-23 20:07:15 +01:00
parent 61c12079ec
commit bbc32822cb
8 changed files with 132 additions and 14 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
config.ini
# Created by https://www.toptal.com/developers/gitignore/api/intellij+all,go
# Edit at https://www.toptal.com/developers/gitignore?templates=intellij+all,go

27
config/config.go Normal file
View File

@ -0,0 +1,27 @@
package config
import "github.com/go-playground/validator/v10"
var v = validator.New()
type Configuration struct {
Host Host
Peers []Peer `validate:"dive"`
}
type Host struct {
PrivateKey string `validate:"required"`
Methods []string `validate:"min=1,dive,oneof=TCP"`
}
type Peer struct {
PublicKey string `validate:"required"`
Method string `validate:"oneof=TCP"`
RemoteHost string `validate:"required,fqdn|ip"`
RemotePort uint `validate:"required,max=65535"`
}
func (c Configuration) Validate() error {
return v.Struct(c)
}

42
config/loader.go Normal file
View File

@ -0,0 +1,42 @@
package config
import (
"gopkg.in/ini.v1"
)
func LoadConfig(path string) (c Configuration, err error) {
var file *ini.File
file, err = ini.LoadSources(ini.LoadOptions{
AllowShadows: true,
AllowNonUniqueSections: true,
}, path)
if err != nil {
return
}
if s := file.Section("Host"); s != nil {
err = s.MapTo(&c.Host)
if err != nil {
return
}
}
var sections []*ini.Section
sections, err = file.SectionsByName("Peer")
for _, s := range sections {
if s == nil {
continue
}
p := Peer{}
err = s.MapTo(&p)
if err != nil {
return
}
c.Peers = append(c.Peers, p)
}
err = c.Validate()
return
}

6
go.mod
View File

@ -2,4 +2,8 @@ module mbpl3p
go 1.15
require github.com/pkg/taptun v0.0.0-20160424131934-bbbd335672ab
require (
github.com/go-playground/validator/v10 v10.4.1
github.com/pkg/taptun v0.0.0-20160424131934-bbbd335672ab
gopkg.in/ini.v1 v1.62.0
)

43
go.sum
View File

@ -1,2 +1,45 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/pkg/taptun v0.0.0-20160424131934-bbbd335672ab h1:dAXDRtXYxj4sTR5WeRuTFJGH18QMT6AUpUgRwedI6es=
github.com/pkg/taptun v0.0.0-20160424131934-bbbd335672ab/go.mod h1:N5a/Ll2ZNk5wjiLNW9LIiNtO9RNYcaYmcXSYKMYrlDg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -2,14 +2,14 @@ package main
import (
"fmt"
"mbpl3p/proxy"
"mbpl3p/config"
)
func main() {
sourceSink, err := proxy.NewTun("nc%d", 1500)
c, err := config.LoadConfig("config.ini")
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", c)
}

View File

@ -23,11 +23,11 @@ type MacVerifier interface {
}
type Source interface {
Acquire() (Packet, error)
Source() (Packet, error)
}
type Sink interface {
Release(packet Packet) error
Sink(packet Packet) error
}
type Proxy struct {

View File

@ -1,16 +1,17 @@
package proxy
package tun
import (
"github.com/pkg/taptun"
"io"
"mbpl3p/proxy"
)
type TunSourceSink struct {
type SourceSink struct {
tun *taptun.Tun
bufferSize int
}
func NewTun(namingScheme string, bufferSize int) (ss TunSourceSink, err error) {
func NewTun(namingScheme string, bufferSize int) (ss SourceSink, err error) {
ss.tun, err = taptun.NewTun(namingScheme)
if err != nil {
return
@ -20,22 +21,22 @@ func NewTun(namingScheme string, bufferSize int) (ss TunSourceSink, err error) {
return
}
func (t *TunSourceSink) Acquire() (Packet, error) {
func (t *SourceSink) Source() (proxy.Packet, error) {
buf := make([]byte, t.bufferSize)
read, err := t.tun.Read(buf)
if err != nil {
return Packet{}, err
return proxy.Packet{}, err
}
if read == 0 {
return Packet{}, io.EOF
return proxy.Packet{}, io.EOF
}
return NewPacket(buf[:read]), nil
return proxy.NewPacket(buf[:read]), nil
}
func (t *TunSourceSink) Release(packet Packet) error {
func (t *SourceSink) Sink(packet proxy.Packet) error {
_, err := t.tun.Write(packet.Raw())
return err
}